FELIX-2776 Fixed a concurrent modification exception, and the order in which individual commands were executed.

git-svn-id: https://svn.apache.org/repos/asf/felix/trunk@1058225 13f79535-47bb-0310-9956-ffa450edef68
diff --git a/deploymentadmin/service/src/main/java/org/apache/felix/deploymentadmin/DeploymentAdminImpl.java b/deploymentadmin/service/src/main/java/org/apache/felix/deploymentadmin/DeploymentAdminImpl.java
index 6003deb..2c30654 100644
--- a/deploymentadmin/service/src/main/java/org/apache/felix/deploymentadmin/DeploymentAdminImpl.java
+++ b/deploymentadmin/service/src/main/java/org/apache/felix/deploymentadmin/DeploymentAdminImpl.java
@@ -162,12 +162,13 @@
             throw new DeploymentException(DeploymentException.CODE_TIMEOUT, "Thread interrupted");
         }
 
-        JarInputStream jarInput = null;
         File tempPackage = null;
-        File tempIndex = null;
-        File tempContents = null;
-
+        StreamDeploymentPackage source = null;
+        boolean succeeded = false;
         try {
+            JarInputStream jarInput = null;
+            File tempIndex = null;
+            File tempContents = null;
             try {
                 File tempDir = m_context.getDataFile(TEMP_DIR);
                 tempDir.mkdirs();
@@ -190,16 +191,9 @@
                 m_log.log(LogService.LOG_ERROR, "Stream does not contain a valid Jar", e);
                 throw new DeploymentException(DeploymentException.CODE_NOT_A_JAR, "Stream does not contain a valid Jar", e);
             }
-        }
-        finally {
-            m_semaphore.release();
-        }
-
-        StreamDeploymentPackage source = new StreamDeploymentPackage(jarInput, m_context);
-        sendStartedEvent(source.getName());
-
-        boolean succeeded = false;
-        try {
+            source = new StreamDeploymentPackage(jarInput, m_context);
+            sendStartedEvent(source.getName());
+            
             AbstractDeploymentPackage target = (AbstractDeploymentPackage) getDeploymentPackage(source.getName());
             boolean newPackage = (target == null);
             if (newPackage) {
@@ -256,8 +250,12 @@
             return fileDeploymentPackage;
         }
         finally {
-        	delete(tempPackage);
-            sendCompleteEvent(source.getName(), succeeded);
+            if (tempPackage != null) {
+                delete(tempPackage);
+            }
+        	if (source != null) {
+        	    sendCompleteEvent(source.getName(), succeeded);
+        	}
             m_semaphore.release();
         }
     }
diff --git a/deploymentadmin/service/src/main/java/org/apache/felix/deploymentadmin/spi/Command.java b/deploymentadmin/service/src/main/java/org/apache/felix/deploymentadmin/spi/Command.java
index eed76cc..3331027 100644
--- a/deploymentadmin/service/src/main/java/org/apache/felix/deploymentadmin/spi/Command.java
+++ b/deploymentadmin/service/src/main/java/org/apache/felix/deploymentadmin/spi/Command.java
@@ -61,8 +61,8 @@
      * Commits all changes the command may have defined when it was executed by calling the <code>execute()</code> method.
      */
     protected void commit() {
-        for (ListIterator i = m_commit.listIterator(m_commit.size()); i.hasPrevious();) {
-            Runnable runnable = (Runnable) i.previous();
+        for (ListIterator i = m_commit.listIterator(); i.hasNext();) {
+            Runnable runnable = (Runnable) i.next();
             runnable.run();
         }
         cleanUp();