Author: [email protected]
Date: Mon Jan 30 09:48:08 2012
New Revision: 2018

Log:
AMDATU-507 Replaced synchronization by singlethreaded executor

Modified:
   
trunk/amdatu-core/fileinstall-autoconf/src/main/java/org/amdatu/core/fileinstall/tenantconf/AutoConfArtifactInstaller.java

Modified: 
trunk/amdatu-core/fileinstall-autoconf/src/main/java/org/amdatu/core/fileinstall/tenantconf/AutoConfArtifactInstaller.java
==============================================================================
--- 
trunk/amdatu-core/fileinstall-autoconf/src/main/java/org/amdatu/core/fileinstall/tenantconf/AutoConfArtifactInstaller.java
  (original)
+++ 
trunk/amdatu-core/fileinstall-autoconf/src/main/java/org/amdatu/core/fileinstall/tenantconf/AutoConfArtifactInstaller.java
  Mon Jan 30 09:48:08 2012
@@ -18,8 +18,11 @@
 import java.io.BufferedInputStream;
 import java.io.File;
 import java.io.FileInputStream;
+import java.io.FileNotFoundException;
 import java.io.IOException;
 import java.io.InputStream;
+import java.util.concurrent.ExecutorService;
+import java.util.concurrent.Executors;
 
 import org.apache.felix.fileinstall.ArtifactInstaller;
 import org.apache.felix.metatype.MetaData;
@@ -28,33 +31,44 @@
 import org.osgi.service.deploymentadmin.DeploymentPackage;
 import org.osgi.service.deploymentadmin.spi.DeploymentSession;
 import org.osgi.service.deploymentadmin.spi.ResourceProcessor;
+import org.osgi.service.deploymentadmin.spi.ResourceProcessorException;
 import org.osgi.service.log.LogService;
 
 /**
- * Apache Felix FileInstall extension that hands of metatype configuration 
artifacts
- * to a autoconf {@link ResourceProcessor}.
+ * {@code AutoConfArtifactInstaller} is an extension to Apache Felix 
FileInstall that hands of
+ * metatype configuration artifacts to an autoconf {@link ResourceProcessor}.
+ * 
+ * This implementation uses a single threaded executor to provide a non 
blocking implementation
+ * that synchronously handles tasks with order guarantee.
  * 
  * @author <a href="mailto:[email protected]";>Amdatu Project 
Team</a>
  */
 public final class AutoConfArtifactInstaller implements ArtifactInstaller {
 
-    private final static DeploymentSession DUMMY_SESSION = new 
DeploymentSession() {
-
-        public DeploymentPackage getTargetDeploymentPackage() {
-            return null;
-        }
+    // injected by Apache Felix DependencyManager
+    private volatile ResourceProcessor m_autoConfProcessor;
+    private volatile LogService m_logService;
 
-        public DeploymentPackage getSourceDeploymentPackage() {
-            return null;
-        }
+    private ExecutorService m_executorService;
 
-        public File getDataFile(Bundle bundle) {
-            return null;
-        }
-    };
+    /**
+     * Apache Felix DependencyManager Component lifecycle method.
+     * 
+     * @throws Exception unexpected error
+     */
+    public void start() throws Exception {
+        m_executorService = Executors.newSingleThreadExecutor();
+    }
 
-    private volatile ResourceProcessor m_autoConfProcessor;
-    private volatile LogService m_logService;
+    /**
+     * Apache Felix DependencyManager Component lifecycle method.
+     * 
+     * @throws Exception unexpected error
+     */
+    public void stop() throws Exception {
+        m_executorService.shutdown();
+        m_executorService = null;
+    }
 
     /**
      * @see 
org.apache.felix.fileinstall.ArtifactListener#canHandle(java.io.File)
@@ -63,7 +77,6 @@
         if (!(artifact.getName().endsWith(".xml"))) {
             return false;
         }
-
         InputStream stream = null;
         MetaData metaData = null;
         try {
@@ -85,7 +98,6 @@
                 }
             }
         }
-
         return metaData != null;
     }
 
@@ -93,62 +105,94 @@
      * @see 
org.apache.felix.fileinstall.ArtifactInstaller#install(java.io.File)
      */
     public void install(File artifact) throws Exception {
-        m_logService.log(LogService.LOG_DEBUG,
-            "Installing metatype configuration file " + artifact.getName());
-
-        InputStream stream = null;
-        try {
-            stream = new BufferedInputStream(new FileInputStream(artifact));
-            synchronized (m_autoConfProcessor) {
-                m_autoConfProcessor.begin(DUMMY_SESSION);
-                m_autoConfProcessor.process(artifact.getName(), stream);
-                m_autoConfProcessor.prepare();
-                m_autoConfProcessor.commit();
-            }
-        }
-        finally {
-            if (stream != null) {
-                stream.close();
-            }
-        }
+        m_executorService.submit(new ProcessorTransaction(Task.INSTALL, 
artifact));
     }
 
     /**
      * @see org.apache.felix.fileinstall.ArtifactInstaller#update(java.io.File)
      */
     public void update(File artifact) throws Exception {
-        m_logService.log(LogService.LOG_DEBUG,
-            "Updating metatype configuration file " + artifact.getName());
-
-        InputStream stream = null;
-        try {
-            stream = new BufferedInputStream(new FileInputStream(artifact));
-            synchronized (m_autoConfProcessor) {
-                m_autoConfProcessor.begin(DUMMY_SESSION);
-                m_autoConfProcessor.process(artifact.getName(), stream);
-                m_autoConfProcessor.prepare();
-                m_autoConfProcessor.commit();
-            }
-        }
-        finally {
-            if (stream != null) {
-                stream.close();
-            }
-        }
+        m_executorService.submit(new ProcessorTransaction(Task.UPDATE, 
artifact));
     }
 
     /**
      * @see 
org.apache.felix.fileinstall.ArtifactInstaller#uninstall(java.io.File)
      */
     public void uninstall(File artifact) throws Exception {
-        m_logService.log(LogService.LOG_DEBUG,
-            "Uninstalling metatype configuration file " + artifact.getName());
+        m_executorService.submit(new ProcessorTransaction(Task.UNINSTALL, 
artifact));
+    }
 
-        synchronized (m_autoConfProcessor) {
-            m_autoConfProcessor.begin(DUMMY_SESSION);
-            m_autoConfProcessor.dropped(artifact.getName());
-            m_autoConfProcessor.prepare();
-            m_autoConfProcessor.commit();
+    enum Task {
+        INSTALL,
+            UPDATE,
+            UNINSTALL
+    }
+
+    // Dummy DeploymentSession
+    class ProcessorSession implements DeploymentSession {
+
+        public DeploymentPackage getTargetDeploymentPackage() {
+            return null;
+        }
+
+        public DeploymentPackage getSourceDeploymentPackage() {
+            return null;
+        }
+
+        public File getDataFile(Bundle bundle) {
+            return null;
+        }
+    }
+
+    // Task implementation
+    class ProcessorTransaction implements Runnable {
+
+        private final Task m_task;
+        private final File m_artifact;
+
+        public ProcessorTransaction(Task task, File artifact) {
+            m_task = task;
+            m_artifact = artifact;
+        }
+
+        public void run() {
+            m_logService.log(LogService.LOG_DEBUG,
+                "Processing metatype configuration file " + 
m_artifact.getName() + "(" + m_task.name() + ")");
+
+            InputStream stream = null;
+            try {
+                m_autoConfProcessor.begin(new ProcessorSession());
+                if (m_task == Task.UNINSTALL) {
+                    m_autoConfProcessor.dropped(m_artifact.getName());
+                }
+                else {
+                    stream = new BufferedInputStream(new 
FileInputStream(m_artifact));
+                    m_autoConfProcessor.process(m_artifact.getName(), stream);
+                }
+                m_autoConfProcessor.prepare();
+                m_autoConfProcessor.commit();
+            }
+            catch (FileNotFoundException e) {
+                m_logService.log(LogService.LOG_DEBUG,
+                    "Processing metatype configuration file " + 
m_artifact.getName() + "failed", e);
+            }
+            catch (ResourceProcessorException e) {
+                // Thrown by process or dropped
+                m_autoConfProcessor.rollback();
+                m_logService.log(LogService.LOG_DEBUG,
+                    "Processing metatype configuration file " + 
m_artifact.getName() + "failed", e);
+            }
+            finally {
+                if (stream != null) {
+                    try {
+                        stream.close();
+                    }
+                    catch (IOException e) {
+                        m_logService.log(LogService.LOG_DEBUG,
+                            "Processing metatype configuration file " + 
m_artifact.getName() + "failed", e);
+                    }
+                }
+            }
         }
     }
 }
_______________________________________________
Amdatu-commits mailing list
[email protected]
http://lists.amdatu.org/mailman/listinfo/amdatu-commits

Reply via email to