← Back to team overview

dhis2-devs team mailing list archive

[Branch ~dhis2-devs-core/dhis2/trunk] Rev 2095: dhis-live: Work in Progress - Added runtime switching of database

 

------------------------------------------------------------
revno: 2095
committer: Saptarshi <sunbiz@xxxxxxxxxxxxx>
branch nick: trunk
timestamp: Sat 2010-09-11 02:41:52 +0200
message:
  dhis-live: Work in Progress - Added runtime switching of database
modified:
  dhis-live/pom.xml
  dhis-live/src/main/java/org/hisp/dhis/LiveMessagingService.java
  dhis-live/src/main/java/org/hisp/dhis/TrayApp.java
  dhis-live/src/main/java/org/hisp/dhis/WebAppServer.java
  dhis-live/src/main/resources/Config.xsd
  dhis-live/src/main/resources/defaultConfig.xml


--
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/pom.xml'
--- dhis-live/pom.xml	2010-09-07 21:23:11 +0000
+++ dhis-live/pom.xml	2010-09-11 00:41:52 +0000
@@ -65,6 +65,7 @@
                 <configuration>
                     <source>1.6</source>
                     <target>1.6</target>
+                    <encoding>${project.build.sourceEncoding}</encoding>
                 </configuration>
             </plugin>
             <!-- Launch4J -->
@@ -89,6 +90,8 @@
                             <downloadUrl>http://java.com/download</downloadUrl>
                             <supportUrl>http://dhis2.org</supportUrl>
                             <customProcName>false</customProcName>
+                            <initialHeapSize>256MB</initialHeapSize>
+                            <maxHeapSize>512MB</maxHeapSize>
                             <stayAlive>false</stayAlive>
                             <manifest></manifest>
                             <icon>${basedir}/util/launch4j/favicon.ico</icon>
@@ -98,6 +101,7 @@
                             </singleInstance>
                             <jre>
                                 <minVersion>1.6.0_14</minVersion>
+                                <opt>-XX:MaxPermSize=128M</opt>
                             </jre>
                             <splash>
                                 <file>${basedir}/util/launch4j/dhis2-splash.bmp</file>
@@ -133,6 +137,7 @@
                             <jre>
                                 <path>jre6</path>
                                 <minVersion>1.6.0_14</minVersion>
+                                <opt>-XX:MaxPermSize=128M</opt>
                             </jre>
                             <splash>
                                 <file>${basedir}/util/launch4j/dhis2-splash.bmp</file>
@@ -145,6 +150,14 @@
 
                 </executions>
             </plugin>
+            <plugin>
+                <groupId>org.apache.maven.plugins</groupId>
+                <artifactId>maven-resources-plugin</artifactId>
+                <version>2.2</version>
+                <configuration>
+                    <encoding>${project.build.sourceEncoding}</encoding>
+                </configuration>
+            </plugin>
         </plugins>
     </build>
 
@@ -195,6 +208,8 @@
         </dependency>
 
     </dependencies>
-
+    <properties>
+        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
+    </properties>
 </project>
 

=== modified file 'dhis-live/src/main/java/org/hisp/dhis/LiveMessagingService.java'
--- dhis-live/src/main/java/org/hisp/dhis/LiveMessagingService.java	2010-09-10 04:26:24 +0000
+++ dhis-live/src/main/java/org/hisp/dhis/LiveMessagingService.java	2010-09-11 00:41:52 +0000
@@ -1,5 +1,5 @@
 /*
-*
+ *
  * Copyright (c) 2004-2010, University of Oslo
  * All rights reserved.
  *
@@ -25,82 +25,76 @@
  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  */
-
 package org.hisp.dhis;
+
+
 import java.util.Locale;
 import java.util.ResourceBundle;
 import org.apache.commons.logging.Log;
 import org.apache.commons.logging.LogFactory;
 
-
-
 /**
  *
  * @author Jason P. Pickering
  */
 public class LiveMessagingService
-
 {
-    
-    private static final Log log = LogFactory.getLog(TrayApp.class);
+
+    private static final Log log = LogFactory.getLog( TrayApp.class );
 
     private static final String defaultLanguage = "en";
-    private static final String defaultCountry =  "GB";
-
-
-
-     private ResourceBundle getMessageBundle()
+
+    private static final String defaultCountry = "GB";
+
+    private ResourceBundle getMessageBundle()
     {
         ResourceBundle messages;
-        String currentLanguage= TrayApp.getInstance().getConfig().getLocaleLanguage();
+        String currentLanguage = TrayApp.getInstance().getConfig().getLocaleLanguage();
         String currentCountry = TrayApp.getInstance().getConfig().getLocaleCountry();
-        Locale currentLocale = new Locale (currentLanguage,currentCountry);
-        log.debug("Current locale set to " + currentLocale.toString());
+        Locale currentLocale = new Locale( currentLanguage, currentCountry );
+        log.debug( "Current locale set to " + currentLocale.toString() );
         try
         {
-            messages = ResourceBundle.getBundle("messages",currentLocale);
-        }
-        catch (Exception e)
-            {
+            messages = ResourceBundle.getBundle( "messages", currentLocale );
+        } catch ( Exception e )
+        {
             //problem loading the desired resource bundle
             //fall back to default
-            log.error("The desired resource bundle could not be loaded.Usig default");
-            currentLocale = new Locale(defaultLanguage, defaultCountry);
-            messages = ResourceBundle.getBundle("messages",currentLocale);
-            }
+            log.error( "The desired resource bundle could not be loaded.Usig default" );
+            currentLocale = new Locale( defaultLanguage, defaultCountry );
+            messages = ResourceBundle.getBundle( "messages", currentLocale );
+        }
 
         return messages;
     }
 
-    public String getString (String messageName) {
+    public String getString( String messageName )
+    {
 
         ResourceBundle messages = getMessageBundle();
 
-        String returnMessage = null ;
+        String returnMessage = null;
         if ( messageName.isEmpty() )
         {
             returnMessage = "messageName not valid";
             return returnMessage;
-        }
-        else
-        {
-
-        if ( messages.containsKey( messageName) )
-        {
-            returnMessage = messages.getString( messageName );
-
-        if (returnMessage == null)
+        } else
+        {
+
+            if ( messages.containsKey( messageName ) )
             {
-                returnMessage = "Message not found";
-            }
-
-            }
-            else
+                returnMessage = messages.getString( messageName );
+
+                if ( returnMessage == null )
+                {
+                    returnMessage = "Message not found";
+                }
+
+            } else
             {
                 returnMessage = "Message key " + messageName + " not found.";
             }
-        return returnMessage;
+            return returnMessage;
+        }
     }
-   }
-
 }

=== modified file 'dhis-live/src/main/java/org/hisp/dhis/TrayApp.java'
--- dhis-live/src/main/java/org/hisp/dhis/TrayApp.java	2010-09-10 04:26:24 +0000
+++ dhis-live/src/main/java/org/hisp/dhis/TrayApp.java	2010-09-11 00:41:52 +0000
@@ -27,17 +27,36 @@
 package org.hisp.dhis;
 
 
-import java.awt.*;
-import java.awt.event.*;
+import java.awt.AWTException;
+import java.awt.Desktop;
+import java.awt.Image;
+import java.awt.Menu;
+import java.awt.MenuItem;
+import java.awt.PopupMenu;
+import java.awt.SystemTray;
+import java.awt.TrayIcon;
+import java.awt.event.ActionEvent;
+import java.awt.event.ActionListener;
+import java.io.File;
+import java.io.FileNotFoundException;
+import java.io.FileOutputStream;
+import java.io.FileReader;
+import java.io.FileWriter;
+import java.io.IOException;
+import java.io.InputStream;
 import java.net.URI;
 import java.net.URL;
-import javax.swing.*;
-import java.io.*;
+import java.util.List;
+import java.util.Properties;
+import javax.swing.ImageIcon;
+import javax.swing.JFrame;
+import javax.swing.JOptionPane;
 import javax.xml.bind.JAXBContext;
 import javax.xml.bind.JAXBElement;
 import javax.xml.bind.JAXBException;
 import javax.xml.bind.Marshaller;
 import javax.xml.bind.Unmarshaller;
+import javax.xml.namespace.QName;
 
 import org.apache.commons.logging.Log;
 import org.apache.commons.logging.LogFactory;
@@ -52,6 +71,8 @@
     implements LifeCycle.Listener
 {
 
+    public static String installDir;
+
     private static final Log log = LogFactory.getLog( TrayApp.class );
 
     private static final String CONFIG_DIR = "/conf";
@@ -72,16 +93,13 @@
 
     private TrayIcon trayIcon;
 
-    private String installDir;
-
-    // the configuration
     private ConfigType config;
 
     private static LiveMessagingService messageService = new LiveMessagingService();
 
     private static TrayApp instance;
 
-// -------------------------------------------------------------------------
+    // -------------------------------------------------------------------------
     // Getters and setters
     // -------------------------------------------------------------------------
     public ConfigType getConfig()
@@ -99,9 +117,6 @@
     // -------------------------------------------------------------------------
     public static void main( String[] args )
     {
-
-
-
         log.info( "Environment variable DHIS2_HOME: " + System.getenv( "DHIS2_HOME" ) );
         if ( !SystemTray.isSupported() )
         {
@@ -164,20 +179,19 @@
             configStream = new java.io.FileInputStream( installDir + CONFIG_FILE_NAME );
         } catch ( FileNotFoundException ex )
         {
-            log.info( "Can't locate external config - falling back to default");
+            log.info( "Can't locate external config - falling back to default" );
             configStream = TrayApp.class.getResourceAsStream( CONFIG_DEFAULT );
         }
 
-        readConfigFromStream(configStream);
+        readConfigFromStream( configStream );
 
-        log.info( "Locale: " + config.getLocaleLanguage() + ":" + config.getLocaleCountry());
+        log.info( "Locale: " + config.getLocaleLanguage() + ":" + config.getLocaleCountry() );
 
         // get the selected database
         ConfigType.DatabaseConnections.Connection conn =
             (ConfigType.DatabaseConnections.Connection) config.getDatabaseConnections().getSelected();
 
-        log.info( "Selected db: "+conn.getName() + "; " + conn.getUserName() + ":" +conn.getPassword() + " "+ conn.getURL());
-        
+        log.info( "Selected db: " + conn.getName() + "; " + conn.getUserName() + ":" + conn.getPassword() + " " + conn.getURL() );
 
         System.setProperty( "dhis2.home", installDir + CONFIG_DIR );
         System.setProperty( "jetty.home", installDir );
@@ -185,22 +199,48 @@
         System.setProperty( "birt.home", installDir + WebAppServer.BIRT_DIR );
         System.setProperty( "birt.context.path", WebAppServer.BIRT_CONTEXT_PATH );
 
+        writeHibernateProperties();
+
         SystemTray tray = SystemTray.getSystemTray();
 
         Image image = createImage( STOPPED_ICON, "tray icon" );
 
         PopupMenu popup = new PopupMenu();
         MenuItem openItem = new MenuItem( messageService.getString( "CMD_OPEN" ) );
-        openItem.setActionCommand("open");
-        MenuItem databaseItem = new MenuItem( messageService.getString( "CMD_DATABASE" ) );
-        databaseItem.setActionCommand("db");
+        openItem.setActionCommand( "open" );
+        Menu databaseMenu = new Menu( messageService.getString( "CMD_DATABASE" ) );
+        List<Connection> dbConns = (List) config.getDatabaseConnections().getConnection();
+        for ( final Connection dbConn : dbConns )
+        {
+            MenuItem connItem = new MenuItem( dbConn.getName() );
+            connItem.addActionListener( new ActionListener()
+            {
+
+                @Override
+                public void actionPerformed( ActionEvent evt )
+                {
+                    config.getDatabaseConnections().setSelected( dbConn );
+                    writeConfigToFile();
+                    writeHibernateProperties();
+                    try
+                    {
+                        appServer.stop();
+                        appServer.start();
+                    } catch ( Exception ex )
+                    {
+                        ex.printStackTrace();
+                    }
+                }
+            } );
+            databaseMenu.add( connItem );
+        }
         MenuItem settingsItem = new MenuItem( messageService.getString( "CMD_SETTINGS" ) );
-        settingsItem.setActionCommand("settings");
+        settingsItem.setActionCommand( "settings" );
         MenuItem exitItem = new MenuItem( messageService.getString( "CMD_EXIT" ) );
-        exitItem.setActionCommand("exit");
+        exitItem.setActionCommand( "exit" );
 
         popup.add( openItem );
-        popup.add( databaseItem );
+        popup.add( databaseMenu );
         popup.add( settingsItem );
         popup.add( exitItem );
 
@@ -210,6 +250,7 @@
         ActionListener listener = new ActionListener()
         {
 
+            @Override
             public void actionPerformed( ActionEvent e )
             {
                 String cmd = e.getActionCommand();
@@ -220,13 +261,12 @@
 
                 } else
                 {
-                    if ( cmd.equals( "exit" )  )
+                    if ( cmd.equals( "exit" ) )
                     {
                         shutdown();
-                    } 
+                    }
                 }
             }
-
         };
 
         openItem.addActionListener( listener );
@@ -236,7 +276,7 @@
         appServer = new WebAppServer();
         try
         {
-            appServer.init( );
+            appServer.init();
         } catch ( Exception e )
         {
             log.fatal( "Application server could not be initialized" );
@@ -253,6 +293,7 @@
     // -------------------------------------------------------------------------
     // Listener implementation
     // -------------------------------------------------------------------------
+    @Override
     public void lifeCycleFailure( LifeCycle arg0, Throwable arg1 )
     {
         log.warn( "Lifecycle: server failed" );
@@ -261,6 +302,7 @@
         shutdown();
     }
 
+    @Override
     public void lifeCycleStarted( LifeCycle arg0 )
     {
         log.info( "Lifecycle: server started" );
@@ -272,6 +314,7 @@
 
     }
 
+    @Override
     public void lifeCycleStarting( LifeCycle arg0 )
     {
         log.info( "Lifecycle: server starting" );
@@ -279,6 +322,7 @@
         trayIcon.setImage( createImage( STARTING_ICON, "Starting icon" ) );
     }
 
+    @Override
     public void lifeCycleStopped( LifeCycle arg0 )
     {
         log.info( "Lifecycle: server stopped" );
@@ -286,6 +330,7 @@
         trayIcon.setImage( createImage( STOPPED_ICON, "Running icon" ) );
     }
 
+    @Override
     public void lifeCycleStopping( LifeCycle arg0 )
     {
         log.info( "Lifecycle: server stopping" );
@@ -466,20 +511,18 @@
         return result;
     }
 
-    private void readConfigFromStream(InputStream configStream)
+    private void readConfigFromStream( InputStream configStream )
     {
         try
         {
             JAXBContext jc = JAXBContext.newInstance( "org.hisp.dhis.config" );
             //Create unmarshaller
             Unmarshaller um = jc.createUnmarshaller();
-            //Unmarshal XML contents of the file myDoc.xml into your Java object instance.
-            JAXBElement<ConfigType> configElement =
-                (JAXBElement<ConfigType>) um.unmarshal( configStream );
-
+            //Unmarshal XML contents of the file config.xml into your Java object instance.
+            JAXBElement<ConfigType> configElement = (JAXBElement<ConfigType>) um.unmarshal( configStream );
             config = configElement.getValue();
 
-        // rather than just logging these errors they should rather bubble a message to the UI
+            // rather than just logging these errors they should rather bubble a message to the UI
         } catch ( JAXBException ex )
         {
             log.error( "Error parsing config file", ex );
@@ -495,9 +538,17 @@
             //Create marshaller
             Marshaller m = jc.createMarshaller();
             //Marshal object into file.
-            m.marshal( config, new FileOutputStream( installDir + CONFIG_FILE_NAME ) );
+            if ( new File( installDir + CONFIG_FILE_NAME ).exists() )
+            {
+                m.marshal( new JAXBElement( new QName( "uri", "local" ), ConfigType.class, config ), new FileOutputStream( installDir + CONFIG_FILE_NAME ) );
+                log.info( "Config Saved at: " + installDir + CONFIG_FILE_NAME );
+            } else
+            {
+                m.marshal( new JAXBElement( new QName( "uri", "local" ), ConfigType.class, config ), new FileOutputStream( CONFIG_DEFAULT ) );
+                log.info( "Config Saved at: " + CONFIG_FILE_NAME );
+            }
 
-        // rather than just logging these errors they should rather bubble a message to the UI
+            // rather than just logging these errors they should rather bubble a message to the UI
         } catch ( FileNotFoundException ex )
         {
             log.error( "Can't find config.xml file", ex );
@@ -506,15 +557,38 @@
             log.error( "Error serializing config to file", ex );
         }
     }
-  /*      private static ResourceBundle messageService
+
+    private void writeHibernateProperties()
     {
-        ResourceBundle rb;
-
-        String currentCountry = TrayApp.getInstance().getConfig().getLocaleCountry();
-        String currentLanguage= TrayApp.getInstance().getConfig().getLocaleLanguage();
-        Locale currentLocale = new Locale (currentCountry,currentLanguage);
-        rb = ResourceBundle.getBundle("messages",currentLocale);
-        return rb;
+        String type = ( (Connection) config.getDatabaseConnections().getSelected() ).getType();
+        String url = ( (Connection) config.getDatabaseConnections().getSelected() ).getURL();
+        String userName = ( (Connection) config.getDatabaseConnections().getSelected() ).getUserName();
+        String password = ( (Connection) config.getDatabaseConnections().getSelected() ).getPassword();
+        Properties props = new Properties();
+        try
+        {
+            props.load( new FileReader( System.getProperty( "dhis2.home" )
+                + "/" + type
+                + ".properties" ) );
+            props.setProperty( "hibernate.connection.url", url );
+            props.setProperty( "hibernate.connection.username", userName );
+            if ( password != null )
+            {
+                props.setProperty( "hibernate.connection.password", password );
+            } else
+            {
+                props.setProperty( "hibernate.connection.password", "" );
+            }
+            props.store( new FileWriter( System.getProperty( "dhis2.home" ) + "/hibernate.properties" ), "DHIS2 Live Created" );
+            log.info( "Hibernate properties written at: " + System.getProperty( "dhis2.home" ) + "/hibernate.properties" );
+        } catch ( FileNotFoundException fnex )
+        {
+            log.error( "Hibernate templates missing: " + fnex.getMessage() );
+            JOptionPane.showMessageDialog( null, "Hibernate Template Files Missing" );
+        } catch ( IOException ioex )
+        {
+            log.error( "Error with Hibernate Properties: " + ioex.getMessage() );
+            JOptionPane.showMessageDialog( null, "Error with Hibernate Properties" );
+        }
     }
-   */
 }

=== modified file 'dhis-live/src/main/java/org/hisp/dhis/WebAppServer.java'
--- dhis-live/src/main/java/org/hisp/dhis/WebAppServer.java	2010-09-07 21:23:11 +0000
+++ dhis-live/src/main/java/org/hisp/dhis/WebAppServer.java	2010-09-11 00:41:52 +0000
@@ -35,7 +35,6 @@
 import java.io.File;
 import org.apache.commons.logging.Log;
 import org.apache.commons.logging.LogFactory;
-import org.mortbay.component.LifeCycle;
 import org.mortbay.jetty.Connector;
 import org.mortbay.jetty.Server;
 import org.mortbay.jetty.handler.ContextHandlerCollection;
@@ -70,10 +69,10 @@
         connector = new SelectChannelConnector();
     }
 
-    public void init(  )
+    public void init()
         throws Exception
     {
-        String installDir = TrayApp.getInstallDir();
+        String installDir = TrayApp.installDir;
         try
         {
             int portFromConfig = this.getPortFromConfig();

=== modified file 'dhis-live/src/main/resources/Config.xsd'
--- dhis-live/src/main/resources/Config.xsd	2010-09-10 04:26:24 +0000
+++ dhis-live/src/main/resources/Config.xsd	2010-09-11 00:41:52 +0000
@@ -20,9 +20,9 @@
                                         <xs:element name="Type"> 
                                             <xs:simpleType>
                                                 <xs:restriction base="xs:string">
-                                                    <xs:enumeration value="H2"/>
-                                                    <xs:enumeration value="Postgresql"/>
-                                                    <xs:enumeration value="Mysql"/>
+                                                    <xs:enumeration value="h2"/>
+                                                    <xs:enumeration value="postgres"/>
+                                                    <xs:enumeration value="mysql"/>
                                                 </xs:restriction>
                                             </xs:simpleType>
                                         </xs:element>

=== modified file 'dhis-live/src/main/resources/defaultConfig.xml'
--- dhis-live/src/main/resources/defaultConfig.xml	2010-09-07 21:23:11 +0000
+++ dhis-live/src/main/resources/defaultConfig.xml	2010-09-11 00:41:52 +0000
@@ -13,20 +13,20 @@
     <DatabaseConnections selected="conn1">
         <Connection ID="conn1">
             <Name>DHIS2 demo database</Name>
-            <Type>H2</Type>
+            <Type>h2</Type>
             <URL>jdbc:h2:./database/demo;AUTO_SERVER=TRUE</URL>
             <UserName>sa</UserName>
         </Connection>
         <Connection ID="conn2">
             <Name>Postgresql example</Name>
-            <Type>Postgresql</Type>
+            <Type>postgres</Type>
             <URL>jdbc:postgresql://localhost/demo</URL>
             <UserName>dhis</UserName>
             <Password>dhis</Password>
         </Connection>
         <Connection ID="conn3">
             <Name>Mysql example</Name>
-            <Type>Postgresql</Type>
+            <Type>mysql</Type>
             <URL>jdbc:mysql://localhost/demo</URL>
             <UserName>dhis</UserName>
             <Password>dhis</Password>