You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@servicemix.apache.org by gn...@apache.org on 2009/03/16 23:29:40 UTC

svn commit: r755027 - in /servicemix/components/shared-libraries/trunk/servicemix-common/src/main/java/org/apache/servicemix: common/osgi/ jbi/deployer/

Author: gnodet
Date: Mon Mar 16 22:29:39 2009
New Revision: 755027

URL: http://svn.apache.org/viewvc?rev=755027&view=rev
Log:
SMX4NMR-130: Lifecycle issues when using OSGi package service assemblies

Modified:
    servicemix/components/shared-libraries/trunk/servicemix-common/src/main/java/org/apache/servicemix/common/osgi/EndpointExporter.java
    servicemix/components/shared-libraries/trunk/servicemix-common/src/main/java/org/apache/servicemix/common/osgi/EndpointTracker.java
    servicemix/components/shared-libraries/trunk/servicemix-common/src/main/java/org/apache/servicemix/common/osgi/EndpointWrapper.java
    servicemix/components/shared-libraries/trunk/servicemix-common/src/main/java/org/apache/servicemix/common/osgi/EndpointWrapperImpl.java
    servicemix/components/shared-libraries/trunk/servicemix-common/src/main/java/org/apache/servicemix/jbi/deployer/DeployedAssembly.java

Modified: servicemix/components/shared-libraries/trunk/servicemix-common/src/main/java/org/apache/servicemix/common/osgi/EndpointExporter.java
URL: http://svn.apache.org/viewvc/servicemix/components/shared-libraries/trunk/servicemix-common/src/main/java/org/apache/servicemix/common/osgi/EndpointExporter.java?rev=755027&r1=755026&r2=755027&view=diff
==============================================================================
--- servicemix/components/shared-libraries/trunk/servicemix-common/src/main/java/org/apache/servicemix/common/osgi/EndpointExporter.java (original)
+++ servicemix/components/shared-libraries/trunk/servicemix-common/src/main/java/org/apache/servicemix/common/osgi/EndpointExporter.java Mon Mar 16 22:29:39 2009
@@ -22,11 +22,15 @@
 import java.util.Map;
 import java.util.HashMap;
 import java.util.ArrayList;
-
-import javax.jbi.management.DeploymentException;
+import java.util.Set;
+import java.util.HashSet;
+import java.util.Timer;
+import java.util.TimerTask;
 
 import org.apache.servicemix.common.Endpoint;
 import org.apache.servicemix.jbi.deployer.DeployedAssembly;
+import org.apache.commons.logging.Log;
+import org.apache.commons.logging.LogFactory;
 import org.osgi.framework.BundleContext;
 import org.osgi.framework.ServiceRegistration;
 import org.springframework.osgi.context.BundleContextAware;
@@ -37,11 +41,17 @@
 
 public class EndpointExporter implements BundleContextAware, ApplicationContextAware, InitializingBean, DisposableBean, DeployedAssembly {
 
+    private static final Log LOG = LogFactory.getLog(EndpointExporter.class);
+
     private BundleContext bundleContext;
     private ApplicationContext applicationContext;
     private Collection<Endpoint> endpoints;
+    private Set<Endpoint> deployed;
     private String assemblyName;
     private Collection<ServiceRegistration> endpointRegistrations;
+    private ServiceRegistration assemblyRegistration;
+    private Timer timer;
+    private boolean scheduled;
 
     public void setBundleContext(BundleContext bundleContext) {
         this.bundleContext = bundleContext;
@@ -67,16 +77,6 @@
         return assemblyName;
     }
 
-    public void deploy() {
-        endpointRegistrations = new ArrayList<ServiceRegistration>();
-        for (Endpoint ep : getEndpoints()) {
-            EndpointWrapper wrapper = new EndpointWrapperImpl(ep, applicationContext.getClassLoader());
-            Dictionary props = new Properties();
-            ServiceRegistration reg = bundleContext.registerService(EndpointWrapper.class.getName(), wrapper, props);
-            endpointRegistrations.add(reg);
-        }
-    }
-
     public Map<String, String> getServiceUnits() {
         if (endpointRegistrations == null) {
             throw new IllegalStateException("Service assembly has not been deployed");
@@ -84,23 +84,93 @@
         Map<String, String> sus = new HashMap<String, String>();
         for (Endpoint ep : getEndpoints()) {
             if (ep.getServiceUnit() == null) {
-                throw new IllegalStateException("Endpoint has not been initialized.  Check that the component is started.");
+                // This should not happen, as we only register the SA after all endpoints have been deployed
+                throw new IllegalStateException("Endpoint has not been initialized.  Check that the component is installed.");
             }
             sus.put(ep.getServiceUnit().getName(), ep.getServiceUnit().getComponent().getComponentName());
         }
         return sus;
     }
 
-    public void afterPropertiesSet() throws Exception {
-        this.assemblyName = bundleContext.getBundle().getSymbolicName();
-        bundleContext.registerService(DeployedAssembly.class.getName(), this, new Properties());
-    }
-
-    public void destroy() throws Exception {
+    public void undeploy(boolean restart) {
         if (endpointRegistrations != null) {
             for (ServiceRegistration reg : endpointRegistrations) {
                 reg.unregister();
             }
+            endpointRegistrations = null;
+        }
+        if (assemblyRegistration != null) {
+            assemblyRegistration.unregister();
+            assemblyRegistration = null;
+        }
+        if (restart) {
+            deploy();
         }
     }
+
+    public void deploy() {
+        this.assemblyName = bundleContext.getBundle().getSymbolicName();
+        endpointRegistrations = new ArrayList<ServiceRegistration>();
+        deployed = new HashSet<Endpoint>();
+        for (final Endpoint ep : getEndpoints()) {
+            EndpointWrapper wrapper = new EndpointWrapperImpl(ep, applicationContext.getClassLoader()) {
+                public void setDeployed() {
+                    checkAndRegisterSA(ep);
+                }
+            };
+            Dictionary props = new Properties();
+            ServiceRegistration reg = bundleContext.registerService(EndpointWrapper.class.getName(), wrapper, props);
+            endpointRegistrations.add(reg);
+        }
+        if (assemblyRegistration == null) {
+            LOG.info("Waiting for all endpoints to be deployed before registering service assembly");
+        }
+    }
+
+    protected void checkAndRegisterSA(Endpoint ep) {
+        if (ep != null) {
+            deployed.add(ep);
+        }
+        Collection<Endpoint> endpoints = getEndpoints();
+        if (deployed.size() == endpoints.size()) {
+            boolean initialized = true;
+            for (Endpoint e : endpoints) {
+                if (e.getServiceUnit().getComponent().getComponentContext() == null) {
+                    initialized = false;
+                    break;
+                }
+            }
+            if (!initialized) {
+                if (timer == null) {
+                    timer = new Timer();
+                    LOG.info("All endpoints have been deployed but waiting for components initialization");
+                }
+                synchronized (this) {
+                    if (!scheduled) {
+                        timer.schedule(new TimerTask() {
+                            public void run() {
+                                checkAndRegisterSA(null);
+                            }
+                        }, 500);
+                        scheduled = true;
+                    }
+                }
+            } else {
+                if (timer != null) {
+                    timer.cancel();
+                    timer = null;
+                }
+                LOG.info("All endpoints have been deployed and components initialized. Registering service assembly.");
+                assemblyRegistration = bundleContext.registerService(DeployedAssembly.class.getName(), this, new Properties());
+            }
+        }
+    }
+
+    public void afterPropertiesSet() {
+        deploy();
+    }
+
+    public void destroy() throws Exception {
+    }
+
 }

Modified: servicemix/components/shared-libraries/trunk/servicemix-common/src/main/java/org/apache/servicemix/common/osgi/EndpointTracker.java
URL: http://svn.apache.org/viewvc/servicemix/components/shared-libraries/trunk/servicemix-common/src/main/java/org/apache/servicemix/common/osgi/EndpointTracker.java?rev=755027&r1=755026&r2=755027&view=diff
==============================================================================
--- servicemix/components/shared-libraries/trunk/servicemix-common/src/main/java/org/apache/servicemix/common/osgi/EndpointTracker.java (original)
+++ servicemix/components/shared-libraries/trunk/servicemix-common/src/main/java/org/apache/servicemix/common/osgi/EndpointTracker.java Mon Mar 16 22:29:39 2009
@@ -37,7 +37,6 @@
     private static final Log LOGGER = LogFactory.getLog(EndpointTracker.class);
 
     protected DefaultComponent component;
-    protected Map<EndpointWrapper, OsgiServiceUnit> endpoints = new ConcurrentHashMap<EndpointWrapper, OsgiServiceUnit>();
 
     public DefaultComponent getComponent() {
         return component;
@@ -57,8 +56,8 @@
     	        LOGGER.debug("[" + component.getComponentName() + "] Endpoint recognized");
             }
             OsgiServiceUnit su = new OsgiServiceUnit(component, endpoint, wrapper.getClassLoader());
-            endpoints.put(wrapper, su);
             component.getRegistry().registerServiceUnit(su);
+            wrapper.setDeployed();
         }
     }
 

Modified: servicemix/components/shared-libraries/trunk/servicemix-common/src/main/java/org/apache/servicemix/common/osgi/EndpointWrapper.java
URL: http://svn.apache.org/viewvc/servicemix/components/shared-libraries/trunk/servicemix-common/src/main/java/org/apache/servicemix/common/osgi/EndpointWrapper.java?rev=755027&r1=755026&r2=755027&view=diff
==============================================================================
--- servicemix/components/shared-libraries/trunk/servicemix-common/src/main/java/org/apache/servicemix/common/osgi/EndpointWrapper.java (original)
+++ servicemix/components/shared-libraries/trunk/servicemix-common/src/main/java/org/apache/servicemix/common/osgi/EndpointWrapper.java Mon Mar 16 22:29:39 2009
@@ -31,4 +31,6 @@
 
     ClassLoader getClassLoader();
 
+    void setDeployed();
+
 }

Modified: servicemix/components/shared-libraries/trunk/servicemix-common/src/main/java/org/apache/servicemix/common/osgi/EndpointWrapperImpl.java
URL: http://svn.apache.org/viewvc/servicemix/components/shared-libraries/trunk/servicemix-common/src/main/java/org/apache/servicemix/common/osgi/EndpointWrapperImpl.java?rev=755027&r1=755026&r2=755027&view=diff
==============================================================================
--- servicemix/components/shared-libraries/trunk/servicemix-common/src/main/java/org/apache/servicemix/common/osgi/EndpointWrapperImpl.java (original)
+++ servicemix/components/shared-libraries/trunk/servicemix-common/src/main/java/org/apache/servicemix/common/osgi/EndpointWrapperImpl.java Mon Mar 16 22:29:39 2009
@@ -18,7 +18,7 @@
 
 import org.apache.servicemix.common.Endpoint;
 
-public class EndpointWrapperImpl implements EndpointWrapper {
+public abstract class EndpointWrapperImpl implements EndpointWrapper {
 
     private final Endpoint endpoint;
     private final ClassLoader classLoader;

Modified: servicemix/components/shared-libraries/trunk/servicemix-common/src/main/java/org/apache/servicemix/jbi/deployer/DeployedAssembly.java
URL: http://svn.apache.org/viewvc/servicemix/components/shared-libraries/trunk/servicemix-common/src/main/java/org/apache/servicemix/jbi/deployer/DeployedAssembly.java?rev=755027&r1=755026&r2=755027&view=diff
==============================================================================
--- servicemix/components/shared-libraries/trunk/servicemix-common/src/main/java/org/apache/servicemix/jbi/deployer/DeployedAssembly.java (original)
+++ servicemix/components/shared-libraries/trunk/servicemix-common/src/main/java/org/apache/servicemix/jbi/deployer/DeployedAssembly.java Mon Mar 16 22:29:39 2009
@@ -31,8 +31,8 @@
 
     String getName();
 
-    void deploy();
-
     Map<String, String> getServiceUnits();
 
+    void undeploy(boolean restart);
+
 }