← Back to team overview

dhis2-devs team mailing list archive

[Branch ~dhis2-devs-core/dhis2/trunk] Rev 12264: Apps, keeping app state in memory list, avoids searching file system

 

------------------------------------------------------------
revno: 12264
committer: Lars Helge Øverland <larshelge@xxxxxxxxx>
branch nick: dhis2
timestamp: Thu 2013-09-26 19:11:40 +0200
message:
  Apps, keeping app state in memory list, avoids searching file system
modified:
  dhis-2/dhis-api/src/main/java/org/hisp/dhis/appmanager/App.java
  dhis-2/dhis-services/dhis-service-core/src/main/java/org/hisp/dhis/appmanager/DefaultAppManagerService.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-2/dhis-api/src/main/java/org/hisp/dhis/appmanager/App.java'
--- dhis-2/dhis-api/src/main/java/org/hisp/dhis/appmanager/App.java	2013-09-26 16:53:50 +0000
+++ dhis-2/dhis-api/src/main/java/org/hisp/dhis/appmanager/App.java	2013-09-26 17:11:40 +0000
@@ -204,7 +204,7 @@
     }
     
     // -------------------------------------------------------------------------
-    // Hashcode & Equals
+    // hashCode, equals, toString
     // -------------------------------------------------------------------------
     
     @Override
@@ -222,6 +222,7 @@
         {
             return false;
         }
+        
         if ( getClass() != obj.getClass() )
         {
             return false;
@@ -236,4 +237,10 @@
         
         return true;
     }
+    
+    @Override
+    public String toString()
+    {
+        return "[" + name + " " + version + "]";
+    }
 }

=== modified file 'dhis-2/dhis-services/dhis-service-core/src/main/java/org/hisp/dhis/appmanager/DefaultAppManagerService.java'
--- dhis-2/dhis-services/dhis-service-core/src/main/java/org/hisp/dhis/appmanager/DefaultAppManagerService.java	2013-09-26 16:53:50 +0000
+++ dhis-2/dhis-services/dhis-service-core/src/main/java/org/hisp/dhis/appmanager/DefaultAppManagerService.java	2013-09-26 17:11:40 +0000
@@ -36,6 +36,8 @@
 import java.util.zip.ZipEntry;
 import java.util.zip.ZipFile;
 
+import javax.annotation.PostConstruct;
+
 import org.apache.ant.compress.taskdefs.Unzip;
 import org.apache.commons.io.FileUtils;
 import org.apache.commons.lang.StringUtils;
@@ -56,9 +58,26 @@
 {
     private static final Log log = LogFactory.getLog( DefaultDataValueService.class );
 
+    /**
+     * In-memory singleton list holding state for apps.
+     */
+    private List<App> apps = new ArrayList<App>();
+
+    @PostConstruct
+    private void init()
+    {
+        reloadAppsInternal();
+        
+        log.info( "Detecting apps: " + apps );
+    }
+        
     @Autowired
     private SystemSettingManager appSettingManager;
 
+    // -------------------------------------------------------------------------
+    // AppManagerService implementation
+    // -------------------------------------------------------------------------
+
     @Override
     public String getAppFolderPath()
     {
@@ -74,42 +93,10 @@
     @Override
     public List<App> getInstalledApps()
     {
-        List<App> appList = new ArrayList<App>();
-        ObjectMapper mapper = new ObjectMapper();
-        mapper.configure( DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false );
-
-        if ( null != getAppFolderPath() )
-        {
-            File appFolderPath = new File( getAppFolderPath() );
-            if ( appFolderPath.isDirectory() )
-            {
-                File[] listFiles = appFolderPath.listFiles();
-                for ( File folder : listFiles )
-                {
-                    if ( folder.isDirectory() )
-                    {
-                        File appManifest = new File( folder, "manifest.webapp" );
-                        if ( appManifest.exists() )
-                        {
-                            try
-                            {
-                                App app = mapper.readValue( appManifest, App.class );
-                                app.setFolderName( folder.getName() );
-                                appList.add( app );
-                            }
-                            catch ( IOException ex )
-                            {
-                                log.error( ex.getLocalizedMessage(), ex );
-                            }
-                        }
-                    }
-                }
-            }
-        }
-
-        return appList;
+        return apps;
     }
     
+    @Override
     public void installApp( File file, String fileName, String rootPath )
         throws IOException
     {
@@ -147,8 +134,13 @@
         }
 
         zip.close();
+        
+        // Reload app state
+        
+        reloadAppsInternal();
     }
-    
+
+    @Override
     public boolean deleteApp( String name )
     {
         for ( App app : getInstalledApps() )
@@ -159,6 +151,11 @@
                 {
                     String folderPath = getAppFolderPath() + File.separator + app.getFolderName();                
                     FileUtils.forceDelete( new File( folderPath ) );
+
+                    // Reload app state
+                    
+                    reloadAppsInternal();
+                    
                     return true;
                 }
                 catch ( IOException ex )
@@ -168,7 +165,7 @@
                 }
             }
         }
-        
+
         return false;
     }
 
@@ -211,4 +208,49 @@
     {
         appSettingManager.saveSystemSetting( KEY_APP_BASE_URL, appBaseUrl );
     }
+
+    // -------------------------------------------------------------------------
+    // Supportive methods
+    // -------------------------------------------------------------------------
+
+    /**
+     * Sets the list of apps with detected apps from the file system.
+     */
+    private void reloadAppsInternal()
+    {
+        List<App> appList = new ArrayList<App>();
+        ObjectMapper mapper = new ObjectMapper();
+        mapper.configure( DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false );
+
+        if ( null != getAppFolderPath() )
+        {
+            File appFolderPath = new File( getAppFolderPath() );
+            if ( appFolderPath.isDirectory() )
+            {
+                File[] listFiles = appFolderPath.listFiles();
+                for ( File folder : listFiles )
+                {
+                    if ( folder.isDirectory() )
+                    {
+                        File appManifest = new File( folder, "manifest.webapp" );
+                        if ( appManifest.exists() )
+                        {
+                            try
+                            {
+                                App app = mapper.readValue( appManifest, App.class );
+                                app.setFolderName( folder.getName() );
+                                appList.add( app );
+                            }
+                            catch ( IOException ex )
+                            {
+                                log.error( ex.getLocalizedMessage(), ex );
+                            }
+                        }
+                    }
+                }
+            }
+        }
+
+        this.apps = appList;
+    }
 }