dhis2-devs team mailing list archive
-
dhis2-devs team
-
Mailing list archive
-
Message #04316
[Branch ~dhis2-devs-core/dhis2/trunk] Rev 1412: Codestyle fix
------------------------------------------------------------
revno: 1412
committer: Lars Helge Oeverland <larshelge@xxxxxxxxx>
branch nick: trunk
timestamp: Wed 2010-02-10 21:45:31 +0100
message:
Codestyle fix
modified:
dhis-live/src/main/java/org/hisp/dhis/TrayApp.java
dhis-live/src/main/java/org/hisp/dhis/WebAppServer.java
--
lp:dhis2
https://code.launchpad.net/~dhis2-devs-core/dhis2/trunk
Your team DHIS 2 developers is subscribed to branch lp:dhis2.
To unsubscribe from this branch go to https://code.launchpad.net/~dhis2-devs-core/dhis2/trunk/+edit-subscription.
=== modified file 'dhis-live/src/main/java/org/hisp/dhis/TrayApp.java'
--- dhis-live/src/main/java/org/hisp/dhis/TrayApp.java 2010-01-08 22:42:23 +0000
+++ dhis-live/src/main/java/org/hisp/dhis/TrayApp.java 2010-02-10 20:45:31 +0000
@@ -39,234 +39,232 @@
/**
* @author Bob Jolliffe
- * @version $Id$
*/
public class TrayApp
- implements LifeCycle.Listener
+ implements LifeCycle.Listener
{
- private static final Log log = LogFactory.getLog( TrayApp.class );
- private static final String CONFIG_DIR = "/conf";
- private static final String STOPPED_ICON = "/icons/stopped.png";
- private static final String STARTING_ICON = "/icons/starting.png";
- private static final String FAILED_ICON ="/icons/failed.png";
- private static final String RUNNING_ICON = "/icons/running.png";
-
- /**
- * Describe variable <code>appServer</code> here.
- *
- */
- protected WebAppServer appServer;
-
- /**
- * Describe variable <code>trayIcon</code> here.
- *
- */
- protected TrayIcon trayIcon;
-
- /**
- * Describe variable <code>installDir</code> here.
- *
- */
- private String installDir;
-
- /**
- * Creates a new <code>TrayApp</code> instance.
- *
- * @exception Exception if an error occurs
- */
- public TrayApp()
- throws Exception
- {
- installDir = getInstallDir();
-
- if (installDir==null) {
- log.info("jar not installed, setting installdir to DHIS2_HOME: "+System.getenv("DHIS2_HOME"));
- installDir = System.getenv("DHIS2_HOME");
+ private static final Log log = LogFactory.getLog( TrayApp.class );
+
+ private static final String CONFIG_DIR = "/conf";
+ private static final String STOPPED_ICON = "/icons/stopped.png";
+ private static final String STARTING_ICON = "/icons/starting.png";
+ private static final String FAILED_ICON = "/icons/failed.png";
+ private static final String RUNNING_ICON = "/icons/running.png";
+
+ private WebAppServer appServer;
+
+ private TrayIcon trayIcon;
+
+ private String installDir;
+
+ // -------------------------------------------------------------------------
+ // Main method
+ // -------------------------------------------------------------------------
+
+ public static void main( String[] args )
+ throws Exception
+ {
+ log.info( "Environment variable DHIS2_HOME: " + System.getenv( "DHIS2_HOME" ) );
+ if ( !SystemTray.isSupported() )
+ {
+ String message = "SystemTray not supported on this platform";
+ JOptionPane.showMessageDialog( (JFrame) null, message );
+ System.exit( 0 );
+ }
+
+ new TrayApp();
}
- System.setProperty("dhis2.home", installDir + CONFIG_DIR);
- System.setProperty("jetty.home", installDir);
-
- System.setProperty("birt.home", installDir + WebAppServer.BIRT_DIR);
- System.setProperty("birt.context.path", WebAppServer.BIRT_CONTEXT_PATH);
-
- SystemTray tray = SystemTray.getSystemTray();
-
- Image image = createImage(STOPPED_ICON, "tray icon");
-
- PopupMenu popup = new PopupMenu();
- MenuItem defaultItem = new MenuItem("Exit");
- popup.add(defaultItem);
-
- trayIcon = new TrayIcon(image,"DHIS 2 Live", popup);
- trayIcon.setImageAutoSize(true);
-
- ActionListener listener = new ActionListener() {
- public void actionPerformed(ActionEvent e) {
- String cmd = e.getActionCommand();
-
- if (cmd.equals("Exit"))
- {
- shutdown();
- }
+ // -------------------------------------------------------------------------
+ // Constructor
+ // -------------------------------------------------------------------------
+
+ public TrayApp()
+ throws Exception
+ {
+ log.info( "Initialising DHIS 2 Live..." );
+
+ installDir = getInstallDir();
+
+ if ( installDir == null )
+ {
+ log.info( "jar not installed, setting installdir to DHIS2_HOME: " + System.getenv( "DHIS2_HOME" ) );
+ installDir = System.getenv( "DHIS2_HOME" );
+ }
+
+ System.setProperty( "dhis2.home", installDir + CONFIG_DIR );
+ System.setProperty( "jetty.home", installDir );
+
+ System.setProperty( "birt.home", installDir + WebAppServer.BIRT_DIR );
+ System.setProperty( "birt.context.path", WebAppServer.BIRT_CONTEXT_PATH );
+
+ SystemTray tray = SystemTray.getSystemTray();
+
+ Image image = createImage( STOPPED_ICON, "tray icon" );
+
+ PopupMenu popup = new PopupMenu();
+ MenuItem defaultItem = new MenuItem( "Exit" );
+ popup.add( defaultItem );
+
+ trayIcon = new TrayIcon( image, "DHIS 2 Live", popup );
+ trayIcon.setImageAutoSize( true );
+
+ ActionListener listener = new ActionListener()
+ {
+ public void actionPerformed( ActionEvent e )
+ {
+ String cmd = e.getActionCommand();
+
+ if ( cmd.equals( "Exit" ) )
+ {
+ shutdown();
+ }
+ };
};
- };
-
- defaultItem.addActionListener(listener);
-
- try {
- tray.add(trayIcon);
- } catch (AWTException ex) {
- log.info("Oops: "+ex.toString());
- }
-
- appServer = new WebAppServer();
- appServer.init(installDir, this);
- try {
- appServer.start();
- } catch (Exception ex) {
- String message = "Web server failed to start: \n"+ex.toString();
- JOptionPane.showMessageDialog((JFrame)null, message);
- }
- }
-
- /**
- * Describe <code>shutdown</code> method here.
- *
- */
- void shutdown()
- {
- log.info("Graceful shutdown...");
- try {
- appServer.stop();
- }
- catch (Exception ex) {
- log.warn("Oops: "+ex.toString());
- }
- log.debug("Exiting...");
- System.exit(0);
- }
-
- /**
- * Describe <code>lifeCycleFailure</code> method here.
- *
- * @param arg0 a <code>LifeCycle</code> value
- * @param arg1 a <code>Throwable</code> value
- */
- public void lifeCycleFailure(LifeCycle arg0, Throwable arg1) {
- log.warn("Lifecycle: server failed");
- trayIcon.setImage(createImage(FAILED_ICON, "Running icon"));
- String message = "Web server failed to start - see logs for details";
- JOptionPane.showMessageDialog((JFrame)null, message);
- shutdown();
- }
-
- /**
- * Describe <code>lifeCycleStarted</code> method here.
- *
- * @param arg0 a <code>LifeCycle</code> value
- */
- public void lifeCycleStarted(LifeCycle arg0) {
- log.info("Lifecycle: server started");
- String url = "http://localhost:" + appServer.getConnectorPort();
- trayIcon.displayMessage("Started","DHIS 2 is running. Your browser will\nbe pointed to " + url + ".",TrayIcon.MessageType.INFO);
- trayIcon.setToolTip("DHIS 2 Server running");
- trayIcon.setImage(createImage(RUNNING_ICON, "Running icon"));
- try {
- Desktop.getDesktop().browse(URI.create(url));
- } catch (Exception ex) {
- log.info("Couldn't open default desktop browse");
- }
- }
-
- /**
- * Describe <code>lifeCycleStarting</code> method here.
- *
- * @param arg0 a <code>LifeCycle</code> value
- */
- public void lifeCycleStarting(LifeCycle arg0) {
- log.info("Lifecycle: server starting");
- trayIcon.displayMessage("Starting","DHIS 2 is starting.\nPlease be patient.",TrayIcon.MessageType.INFO);
- trayIcon.setImage(createImage(STARTING_ICON, "Starting icon"));
- }
-
- /**
- * Describe <code>lifeCycleStopped</code> method here.
- *
- * @param arg0 a <code>LifeCycle</code> value
- */
- public void lifeCycleStopped(LifeCycle arg0) {
- log.info("Lifecycle: server stopped");
- trayIcon.displayMessage("Stopped", "DHIS 2 has stopped.",TrayIcon.MessageType.INFO);
- trayIcon.setImage(createImage(STOPPED_ICON, "Running icon"));
- }
-
- /**
- * Describe <code>lifeCycleStopping</code> method here.
- *
- * @param arg0 a <code>LifeCycle</code> value
- */
- public void lifeCycleStopping(LifeCycle arg0) {
- log.info("Lifecycle: server stopping");
- }
-
- /**
- * Describe <code>createImage</code> method here.
- *
- * @param path a <code>String</code> value
- * @param description a <code>String</code> value
- * @return an <code>Image</code> value
- */
- protected static Image createImage(String path, String description) {
- URL imageURL = TrayApp.class.getResource(path);
-
- if (imageURL == null) {
- log.warn("Resource not found: " + path);
- return null;
- } else {
- return (new ImageIcon(imageURL, description)).getImage();
- }
- }
-
- /**
- * @param args the command line arguments
- */
- public static void main(String[] args)
- throws Exception
- {
- log.info("Environment variable DHIS2_HOME: "+System.getenv("DHIS2_HOME"));
- if(!SystemTray.isSupported()) {
- String message = "SystemTray not supported on this platform";
- JOptionPane.showMessageDialog((JFrame)null, message);
- System.exit(0);
- }
-
- // ok - we're good to go ...
- new TrayApp();
- }
-
- /**
- * The <code>getInstallDir</code> method is a hack to determine the current
- * directory the DHIS 2 Live package is installed in. It does this by finding
- * the file URL of a resource within the executable jar and extracting the
- * installation path from that.
- *
- * @return a <code>String</code> value representing the installation directory
- */
- public static String getInstallDir() {
- // find a resource
- String resourceString = TrayApp.class.getResource("/icons/").toString();
- // we expect to see something of the form:
- // "jar:file:<install_dir>/dhis_xxx.jar!/icons"
- if (!resourceString.startsWith("jar:file:") ) {
- // we're in trouble - its not in a jar file
- return null;
- }
- // find the last "/" just before the "!"
- int endIndex = resourceString.lastIndexOf("/", resourceString.lastIndexOf("!"));
- String result = resourceString.substring(9, endIndex);
- // replace encoded spaces
- result = result.replaceAll("%20"," ");
- return result;
- }
+
+ defaultItem.addActionListener( listener );
+
+ try
+ {
+ tray.add( trayIcon );
+ }
+ catch ( AWTException ex )
+ {
+ log.warn( "Oops: " + ex.toString() );
+ }
+
+ appServer = new WebAppServer();
+ appServer.init( installDir, this );
+
+ try
+ {
+ appServer.start();
+ }
+ catch ( Exception ex )
+ {
+ String message = "Web server failed to start: \n" + ex.toString();
+ JOptionPane.showMessageDialog( (JFrame) null, message );
+ }
+ }
+
+ // -------------------------------------------------------------------------
+ // Listener implementation
+ // -------------------------------------------------------------------------
+
+ public void lifeCycleFailure( LifeCycle arg0, Throwable arg1 )
+ {
+ log.warn( "Lifecycle: server failed" );
+ trayIcon.setImage( createImage( FAILED_ICON, "Running icon" ) );
+ String message = "Web server failed to start - see logs for details";
+ JOptionPane.showMessageDialog( (JFrame) null, message );
+ shutdown();
+ }
+
+ public void lifeCycleStarted( LifeCycle arg0 )
+ {
+ log.info( "Lifecycle: server started" );
+ String url = "http://localhost:" + appServer.getConnectorPort();
+ trayIcon.displayMessage( "Started", "DHIS 2 is running. Your browser will\nbe pointed to " + url + ".",
+ TrayIcon.MessageType.INFO );
+ trayIcon.setToolTip( "DHIS 2 Server running" );
+ trayIcon.setImage( createImage( RUNNING_ICON, "Running icon" ) );
+ try
+ {
+ Desktop.getDesktop().browse( URI.create( url ) );
+ }
+ catch ( Exception ex )
+ {
+ log.warn( "Couldn't open default desktop browse" );
+ }
+ }
+
+ public void lifeCycleStarting( LifeCycle arg0 )
+ {
+ log.info( "Lifecycle: server starting" );
+ trayIcon.displayMessage( "Starting", "DHIS 2 is starting.\nPlease be patient.", TrayIcon.MessageType.INFO );
+ trayIcon.setImage( createImage( STARTING_ICON, "Starting icon" ) );
+ }
+
+ public void lifeCycleStopped( LifeCycle arg0 )
+ {
+ log.info( "Lifecycle: server stopped" );
+ trayIcon.displayMessage( "Stopped", "DHIS 2 has stopped.", TrayIcon.MessageType.INFO );
+ trayIcon.setImage( createImage( STOPPED_ICON, "Running icon" ) );
+ }
+
+ public void lifeCycleStopping( LifeCycle arg0 )
+ {
+ log.info( "Lifecycle: server stopping" );
+ }
+
+ // -------------------------------------------------------------------------
+ // Supportive methods
+ // -------------------------------------------------------------------------
+
+ /**
+ * Shuts down the web application server.
+ */
+ private void shutdown()
+ {
+ log.info( "Graceful shutdown..." );
+ try
+ {
+ appServer.stop();
+ }
+ catch ( Exception ex )
+ {
+ log.warn( "Oops: " + ex.toString() );
+ }
+ log.info( "Exiting..." );
+ System.exit( 0 );
+ }
+
+ /**
+ * Creates an image based on the given path and description.
+ *
+ * @param path the image path.
+ * @param description the image description.
+ * @return an Image.
+ */
+ private static Image createImage( String path, String description )
+ {
+ URL imageURL = TrayApp.class.getResource( path );
+
+ if ( imageURL == null )
+ {
+ log.warn( "Resource not found: " + path );
+ return null;
+ }
+ else
+ {
+ return (new ImageIcon( imageURL, description )).getImage();
+ }
+ }
+
+ /**
+ * The <code>getInstallDir</code> method is a hack to determine the current
+ * directory the DHIS 2 Live package is installed in. It does this by
+ * finding the file URL of a resource within the executable jar and
+ * extracting the installation path from that.
+ *
+ * @return a <code>String</code> value representing the installation directory
+ */
+ private static String getInstallDir()
+ {
+ // find a resource
+ String resourceString = TrayApp.class.getResource( "/icons/" ).toString();
+ // we expect to see something of the form:
+ // "jar:file:<install_dir>/dhis_xxx.jar!/icons"
+ if ( !resourceString.startsWith( "jar:file:" ) )
+ {
+ // we're in trouble - its not in a jar file
+ return null;
+ }
+ // find the last "/" just before the "!"
+ int endIndex = resourceString.lastIndexOf( "/", resourceString.lastIndexOf( "!" ) );
+ String result = resourceString.substring( 9, endIndex );
+ // replace encoded spaces
+ result = result.replaceAll( "%20", " " );
+ return result;
+ }
}
=== modified file 'dhis-live/src/main/java/org/hisp/dhis/WebAppServer.java'
--- dhis-live/src/main/java/org/hisp/dhis/WebAppServer.java 2010-02-05 21:22:23 +0000
+++ dhis-live/src/main/java/org/hisp/dhis/WebAppServer.java 2010-02-10 20:45:31 +0000
@@ -51,89 +51,95 @@
/**
* @author Bob Jolliffe
- * @version $Id$
*/
-public class WebAppServer
+public class WebAppServer
{
- public static final String DHIS_DIR = "/webapps/dhis";
- public static final String BIRT_DIR = "/webapps/birt";
- public static final String BIRT_CONTEXT_PATH = "/birt";
- public static final String JETTY_PORT_CONF = "/conf/jetty.port";
-
- public static final int DEFAULT_JETTY_PORT = 8080;
-
- private static final Log log = LogFactory.getLog( WebAppServer.class );
-
- protected Server server;
- protected Connector connector;
-
- public WebAppServer() {
- server = new Server();
- connector = new SelectChannelConnector();
- }
-
- public void init(String installDir, LifeCycle.Listener serverListener)
- throws Exception
- {
-
- try {
- int portFromConfig = this.getPortFromConfig(installDir + JETTY_PORT_CONF);
- connector.setPort(portFromConfig);
- log.info("Loading DHIS 2 on port: " + portFromConfig );
- } catch (Exception ex) {
- log.info("Couldn't load port number from " + installDir + JETTY_PORT_CONF);
- connector.setPort(DEFAULT_JETTY_PORT);
- log.info("Loading DHIS 2 on port: " + DEFAULT_JETTY_PORT );
- }
-
- server.setConnectors(new Connector[]{connector});
-
- ContextHandlerCollection handlers = new ContextHandlerCollection();
-
- WebAppContext dhisWebApp = new WebAppContext();
- dhisWebApp.setWar(installDir + DHIS_DIR);
- handlers.addHandler(dhisWebApp);
- log.info("Setting DHIS 2 web app context to: "+ installDir + DHIS_DIR);
-
- if ( new File( installDir, BIRT_DIR ).exists() ) {
- WebAppContext birtWebApp = new WebAppContext();
- birtWebApp.setContextPath(BIRT_CONTEXT_PATH);
- birtWebApp.setWar(installDir + BIRT_DIR);
- handlers.addHandler(birtWebApp);
- log.info("Setting BIRT web app context to: "+ installDir + BIRT_DIR);
- }
-
- server.setHandler(handlers);
- server.addLifeCycleListener(serverListener);
- }
-
- public void start()
- throws Exception
- {
- server.start();
- server.join();
- }
-
- public void stop()
- throws Exception
- {
- server.stop();
- }
-
- public int getConnectorPort()
- {
- return connector.getPort();
- }
-
- // read integer value from file
- public int getPortFromConfig(String conf) throws FileNotFoundException, IOException
- {
- Reader r = new BufferedReader(new FileReader(conf));
- char[] cbuf = new char[10];
- r.read(cbuf);
- String numstr = String.copyValueOf(cbuf);
- Integer port = Integer.valueOf(numstr.trim());
- return port.intValue();
- }
-
+ public static final String DHIS_DIR = "/webapps/dhis";
+
+ public static final String BIRT_DIR = "/webapps/birt";
+
+ public static final String BIRT_CONTEXT_PATH = "/birt";
+
+ public static final String JETTY_PORT_CONF = "/conf/jetty.port";
+
+ public static final int DEFAULT_JETTY_PORT = 8080;
+
+ private static final Log log = LogFactory.getLog( WebAppServer.class );
+
+ protected Server server;
+
+ protected Connector connector;
+
+ public WebAppServer()
+ {
+ server = new Server();
+ connector = new SelectChannelConnector();
+ }
+
+ public void init( String installDir, LifeCycle.Listener serverListener )
+ throws Exception
+ {
+ try
+ {
+ int portFromConfig = this.getPortFromConfig( installDir + JETTY_PORT_CONF );
+ connector.setPort( portFromConfig );
+ log.info( "Loading DHIS 2 on port: " + portFromConfig );
+ }
+ catch ( Exception ex )
+ {
+ log.info( "Couldn't load port number from " + installDir + JETTY_PORT_CONF );
+ connector.setPort( DEFAULT_JETTY_PORT );
+ log.info( "Loading DHIS 2 on port: " + DEFAULT_JETTY_PORT );
+ }
+
+ server.setConnectors( new Connector[] { connector } );
+
+ ContextHandlerCollection handlers = new ContextHandlerCollection();
+
+ WebAppContext dhisWebApp = new WebAppContext();
+ dhisWebApp.setWar( installDir + DHIS_DIR );
+ handlers.addHandler( dhisWebApp );
+ log.info( "Setting DHIS 2 web app context to: " + installDir + DHIS_DIR );
+
+ if ( new File( installDir, BIRT_DIR ).exists() )
+ {
+ WebAppContext birtWebApp = new WebAppContext();
+ birtWebApp.setContextPath( BIRT_CONTEXT_PATH );
+ birtWebApp.setWar( installDir + BIRT_DIR );
+ handlers.addHandler( birtWebApp );
+ log.info( "Setting BIRT web app context to: " + installDir + BIRT_DIR );
+ }
+
+ server.setHandler( handlers );
+ server.addLifeCycleListener( serverListener );
+ }
+
+ public void start()
+ throws Exception
+ {
+ server.start();
+ server.join();
+ }
+
+ public void stop()
+ throws Exception
+ {
+ server.stop();
+ }
+
+ public int getConnectorPort()
+ {
+ return connector.getPort();
+ }
+
+ private int getPortFromConfig( String conf )
+ throws FileNotFoundException, IOException
+ {
+ Reader r = new BufferedReader( new FileReader( conf ) );
+ char[] cbuf = new char[10];
+ r.read( cbuf );
+ String numstr = String.copyValueOf( cbuf );
+ Integer port = Integer.valueOf( numstr.trim() );
+ return port.intValue();
+ }
}