servicemix-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From gno...@apache.org
Subject svn commit: r681537 - in /servicemix/components/shared-libraries/trunk/servicemix-common/src/main/java/org/apache/servicemix/common/osgi: EndpointExporter.java EndpointTracker.java EndpointWrapper.java EndpointWrapperImpl.java
Date Thu, 31 Jul 2008 23:04:03 GMT
Author: gnodet
Date: Thu Jul 31 16:04:02 2008
New Revision: 681537

URL: http://svn.apache.org/viewvc?rev=681537&view=rev
Log:
Use a fake ServiceUnit when deploying an endpoint in OSGi so that we are able to reuse the
classloader used by spring when creating the spring context

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

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=681537&r1=681536&r2=681537&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
Thu Jul 31 16:04:02 2008
@@ -51,7 +51,7 @@
             eps = this.applicationContext.getBeansOfType(Endpoint.class).values();
         }
         for (Endpoint ep : eps) {
-            EndpointWrapper wrapper = new EndpointWrapperImpl(ep);
+            EndpointWrapper wrapper = new EndpointWrapperImpl(ep, applicationContext.getClassLoader());
             Dictionary props = new Properties();
             bundleContext.registerService(EndpointWrapper.class.getName(), wrapper, props);
         }

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=681537&r1=681536&r2=681537&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
Thu Jul 31 16:04:02 2008
@@ -19,8 +19,12 @@
 import java.util.Map;
 import java.util.concurrent.ConcurrentHashMap;
 
+import javax.jbi.management.DeploymentException;
+
 import org.apache.servicemix.common.Endpoint;
 import org.apache.servicemix.common.DefaultComponent;
+import org.apache.servicemix.common.ServiceUnit;
+import org.apache.servicemix.id.IdGenerator;
 import org.apache.commons.logging.Log;
 import org.apache.commons.logging.LogFactory;
 
@@ -29,7 +33,7 @@
     private static final Log LOGGER = LogFactory.getLog(EndpointTracker.class);
 
     protected DefaultComponent component;
-    protected Map<EndpointWrapper, Endpoint> endpoints = new ConcurrentHashMap<EndpointWrapper,
Endpoint>();
+    protected Map<EndpointWrapper, OsgiServiceUnit> endpoints = new ConcurrentHashMap<EndpointWrapper,
OsgiServiceUnit>();
 
     public DefaultComponent getComponent() {
         return component;
@@ -48,8 +52,19 @@
             if (LOGGER.isDebugEnabled()) {
     	        LOGGER.debug("[" + component.getComponentName() + "] Endpoint recognized");
             }
-            endpoints.put(wrapper, endpoint);
-            component.addEndpoint(endpoint);
+            OsgiServiceUnit su = new OsgiServiceUnit(component, endpoint, wrapper.getClassLoader());
+            endpoints.put(wrapper, su);
+            component.getRegistry().registerServiceUnit(su);
+            if (component.isStarted()) {
+                ClassLoader cl = Thread.currentThread().getContextClassLoader();
+                try {
+                    Thread.currentThread().setContextClassLoader(su.getConfigurationClassLoader());
+                    su.start();
+                } finally {
+                    Thread.currentThread().setContextClassLoader(cl);
+                }
+            }
+
         }
     }
 
@@ -58,12 +73,47 @@
             LOGGER.debug("[" + component.getComponentName() + "] Endpoint unregistered with
properties: " + properties);
         }
         // Do not access the wrapper using wrapper.getEndpoint(), has the osgi context may
already be shut down
-        Endpoint endpoint = endpoints.remove(wrapper);
-        if (endpoint != null && component.isKnownEndpoint(endpoint)) {
+        OsgiServiceUnit su = endpoints.remove(wrapper);
+        if (su != null && component.isKnownEndpoint(su.getEndpoint())) {
             if (LOGGER.isDebugEnabled()) {
     	        LOGGER.debug("[" + component.getComponentName() + "] Endpoint recognized");
             }
-            component.removeEndpoint(endpoint);
+            try {
+                ClassLoader cl = Thread.currentThread().getContextClassLoader();
+                try {
+                    Thread.currentThread().setContextClassLoader(su.getConfigurationClassLoader());
+                    su.stop();
+                } finally {
+                    Thread.currentThread().setContextClassLoader(cl);
+                }
+            } finally {
+                component.getRegistry().unregisterServiceUnit(su);
+            }
+        }
+    }
+
+    public static class OsgiServiceUnit extends ServiceUnit {
+        private static final IdGenerator idGenerator = new IdGenerator();
+        private final Endpoint endpoint;
+        private final ClassLoader classLoader;
+        public OsgiServiceUnit(DefaultComponent component, Endpoint endpoint, ClassLoader
classLoader) throws DeploymentException {
+            this.component = component;
+            this.name = idGenerator.generateSanitizedId();
+            this.endpoint = endpoint;
+            this.classLoader = classLoader;
+            this.endpoint.setServiceUnit(this);
+            this.endpoint.validate();
+            addEndpoint(this.endpoint);
+        }
+        public Endpoint getEndpoint() {
+            return endpoint;
+        }
+        public ClassLoader  getConfigurationClassLoader() {
+            if (classLoader != null) {
+                return classLoader;
+            } else {
+                return super.getConfigurationClassLoader();
+            }
         }
     }
 

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=681537&r1=681536&r2=681537&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
Thu Jul 31 16:04:02 2008
@@ -29,4 +29,6 @@
 
     Endpoint getEndpoint();
 
+    ClassLoader getClassLoader();
+
 }

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=681537&r1=681536&r2=681537&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
Thu Jul 31 16:04:02 2008
@@ -21,12 +21,19 @@
 public class EndpointWrapperImpl implements EndpointWrapper {
 
     private final Endpoint endpoint;
+    private final ClassLoader classLoader;
 
-    public EndpointWrapperImpl(Endpoint endpoint) {
+    public EndpointWrapperImpl(Endpoint endpoint, ClassLoader classLoader) {
         this.endpoint = endpoint;
+        this.classLoader = classLoader;
     }
 
     public Endpoint getEndpoint() {
         return endpoint;
     }
+
+    public ClassLoader getClassLoader() {
+        return classLoader;
+    }
+
 }



Mime
View raw message