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 2006/01/31 19:17:31 UTC

svn commit: r373881 - in /incubator/servicemix/trunk: servicemix-common/src/main/java/org/apache/servicemix/common/xbean/ servicemix-jsr181/src/main/java/org/apache/servicemix/jsr181/

Author: gnodet
Date: Tue Jan 31 10:17:21 2006
New Revision: 373881

URL: http://svn.apache.org/viewcvs?rev=373881&view=rev
Log:
Fix classloading problems on servicemix-common based xbean deployment.
The ClassLoaderXmlPreprocessor.java fix is in xbean-2.2-SNAPSHOT and should be removed once SM upgraded.

Added:
    incubator/servicemix/trunk/servicemix-common/src/main/java/org/apache/servicemix/common/xbean/ClassLoaderXmlPreprocessor.java
Modified:
    incubator/servicemix/trunk/servicemix-common/src/main/java/org/apache/servicemix/common/xbean/AbstractXBeanDeployer.java
    incubator/servicemix/trunk/servicemix-common/src/main/java/org/apache/servicemix/common/xbean/XBeanServiceUnit.java
    incubator/servicemix/trunk/servicemix-jsr181/src/main/java/org/apache/servicemix/jsr181/Jsr181Endpoint.java
    incubator/servicemix/trunk/servicemix-jsr181/src/main/java/org/apache/servicemix/jsr181/Jsr181SpringComponent.java

Modified: incubator/servicemix/trunk/servicemix-common/src/main/java/org/apache/servicemix/common/xbean/AbstractXBeanDeployer.java
URL: http://svn.apache.org/viewcvs/incubator/servicemix/trunk/servicemix-common/src/main/java/org/apache/servicemix/common/xbean/AbstractXBeanDeployer.java?rev=373881&r1=373880&r2=373881&view=diff
==============================================================================
--- incubator/servicemix/trunk/servicemix-common/src/main/java/org/apache/servicemix/common/xbean/AbstractXBeanDeployer.java (original)
+++ incubator/servicemix/trunk/servicemix-common/src/main/java/org/apache/servicemix/common/xbean/AbstractXBeanDeployer.java Tue Jan 31 10:17:21 2006
@@ -24,7 +24,6 @@
 import org.xbean.kernel.KernelFactory;
 import org.xbean.kernel.ServiceName;
 import org.xbean.server.repository.FileSystemRepository;
-import org.xbean.server.spring.configuration.ClassLoaderXmlPreprocessor;
 import org.xbean.server.spring.loader.SpringLoader;
 
 import javax.jbi.management.DeploymentException;
@@ -68,6 +67,7 @@
             su.setName(serviceUnitName);
             su.setRootPath(serviceUnitRootPath);
             // Load configuration
+            Thread.currentThread().setContextClassLoader(getClass().getClassLoader());
             FileSystemRepository repository = new FileSystemRepository(new File(serviceUnitRootPath));
             ClassLoaderXmlPreprocessor classLoaderXmlPreprocessor = new ClassLoaderXmlPreprocessor(repository);
             List xmlPreprocessors = Collections.singletonList(classLoaderXmlPreprocessor);
@@ -77,6 +77,7 @@
             springLoader.setXmlPreprocessors(xmlPreprocessors);
             ServiceName configurationName = springLoader.load(getXBeanFile());
             kernel.startService(configurationName);
+            su.setConfiguration(configurationName);
             // Retrieve endpoints
             List services = getServices(kernel);
             if (services == null || services.size() == 0) {

Added: incubator/servicemix/trunk/servicemix-common/src/main/java/org/apache/servicemix/common/xbean/ClassLoaderXmlPreprocessor.java
URL: http://svn.apache.org/viewcvs/incubator/servicemix/trunk/servicemix-common/src/main/java/org/apache/servicemix/common/xbean/ClassLoaderXmlPreprocessor.java?rev=373881&view=auto
==============================================================================
--- incubator/servicemix/trunk/servicemix-common/src/main/java/org/apache/servicemix/common/xbean/ClassLoaderXmlPreprocessor.java (added)
+++ incubator/servicemix/trunk/servicemix-common/src/main/java/org/apache/servicemix/common/xbean/ClassLoaderXmlPreprocessor.java Tue Jan 31 10:17:21 2006
@@ -0,0 +1,118 @@
+/**
+*
+* Copyright 2005 the original author or authors.
+*
+*  Licensed under the Apache License, Version 2.0 (the "License");
+*  you may not use this file except in compliance with the License.
+*  You may obtain a copy of the License at
+*
+*     http://www.apache.org/licenses/LICENSE-2.0
+*
+*  Unless required by applicable law or agreed to in writing, software
+*  distributed under the License is distributed on an "AS IS" BASIS,
+*  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+*  See the License for the specific language governing permissions and
+*  limitations under the License.
+*/
+package org.apache.servicemix.common.xbean;
+
+import java.net.URL;
+import java.util.ArrayList;
+import java.util.List;
+import java.util.ListIterator;
+
+import org.xbean.server.classloader.MultiParentClassLoader;
+import org.xbean.server.repository.Repository;
+import org.xbean.server.spring.loader.SpringLoader;
+import org.xbean.spring.context.SpringXmlPreprocessor;
+import org.xbean.spring.context.SpringApplicationContext;
+import org.springframework.beans.FatalBeanException;
+import org.springframework.beans.factory.xml.XmlBeanDefinitionReader;
+import org.w3c.dom.Document;
+import org.w3c.dom.Element;
+import org.w3c.dom.NodeList;
+import org.w3c.dom.Text;
+
+/**
+* ClassLoaderXmlPreprocessor extracts a ClassLoader definition from the xml document, builds a class loader, assigns
+* the class loader to the application context and xml reader, and removes the classpath element from document.
+*
+* @org.xbean.XBean namespace="http://xbean.org/schemas/server" element="class-loader-xml-preprocessor"
+*     description="Extracts a ClassLoader definition from the xml document."
+*
+* @author Dain Sundstrom
+* @version $Id$
+* @since 2.0
+*/
+public class ClassLoaderXmlPreprocessor implements SpringXmlPreprocessor {
+   private final Repository repository;
+
+   /**
+    * Creates a ClassLoaderXmlPreprocessor that uses the specified repository to resolve the class path locations.
+    * @param repository the repository used to resolve the class path locations
+    */
+   public ClassLoaderXmlPreprocessor(Repository repository) {
+       this.repository = repository;
+   }
+
+   /**
+    * Extracts a ClassLoader definition from the xml document, builds a class loader, assigns
+    * the class loader to the application context and xml reader, and removes the classpath element from document.
+    *
+    * @param applicationContext the application context on which the class loader will be set
+    * @param reader the xml reader on which the class loader will be set
+    * @param document the xml document to inspect
+    */
+   public void preprocess(SpringApplicationContext applicationContext, XmlBeanDefinitionReader reader, Document document) {
+       // determine the classLoader
+       ClassLoader classLoader;
+       NodeList classpathElements = document.getDocumentElement().getElementsByTagName("classpath");
+       if (classpathElements.getLength() < 1) {
+           classLoader = getClass().getClassLoader();
+       } else if (classpathElements.getLength() > 1) {
+           throw new FatalBeanException("Expected only classpath element but found " + classpathElements.getLength());
+       } else {
+           Element classpathElement = (Element) classpathElements.item(0);
+
+           // build the classpath
+           List classpath = new ArrayList();
+           NodeList locations = classpathElement.getElementsByTagName("location");
+           for (int i = 0; i < locations.getLength(); i++) {
+               Element locationElement = (Element) locations.item(i);
+
+               String location = ((Text) locationElement.getFirstChild()).getData().trim();
+               classpath.add(location);
+           }
+
+           // convert the paths to URLS
+           URL[] urls = new URL[classpath.size()];
+           for (ListIterator iterator = classpath.listIterator(); iterator.hasNext();) {
+               String location = (String) iterator.next();
+               urls[iterator.previousIndex()] = repository.getResource(location);
+           }
+
+           // create the classloader
+           ClassLoader parentLoader = getClassLoader(applicationContext);
+           classLoader = new MultiParentClassLoader(applicationContext.getDisplayName(), urls, parentLoader);
+
+           // remove the classpath element so Spring doesn't get confused
+           document.getDocumentElement().removeChild(classpathElement);
+       }
+
+       // assign the class loader to the xml reader and the application context
+       reader.setBeanClassLoader(classLoader);
+       applicationContext.setClassLoader(classLoader);
+       Thread.currentThread().setContextClassLoader(classLoader);
+   }
+
+   private static ClassLoader getClassLoader(SpringApplicationContext applicationContext) {
+       ClassLoader classLoader = applicationContext.getClassLoader();
+       if (classLoader == null) {
+           classLoader = Thread.currentThread().getContextClassLoader();
+       }
+       if (classLoader == null) {
+           classLoader = SpringLoader.class.getClassLoader();
+       }
+       return classLoader;
+   }
+}

Modified: incubator/servicemix/trunk/servicemix-common/src/main/java/org/apache/servicemix/common/xbean/XBeanServiceUnit.java
URL: http://svn.apache.org/viewcvs/incubator/servicemix/trunk/servicemix-common/src/main/java/org/apache/servicemix/common/xbean/XBeanServiceUnit.java?rev=373881&r1=373880&r2=373881&view=diff
==============================================================================
--- incubator/servicemix/trunk/servicemix-common/src/main/java/org/apache/servicemix/common/xbean/XBeanServiceUnit.java (original)
+++ incubator/servicemix/trunk/servicemix-common/src/main/java/org/apache/servicemix/common/xbean/XBeanServiceUnit.java Tue Jan 31 10:17:21 2006
@@ -17,12 +17,16 @@
 
 import org.apache.servicemix.common.ServiceUnit;
 import org.xbean.kernel.Kernel;
+import org.xbean.kernel.ServiceName;
+import org.xbean.kernel.ServiceNotFoundException;
+import org.xbean.server.spring.configuration.SpringConfigurationServiceFactory;
 
 import javax.jbi.JBIException;
 
 public class XBeanServiceUnit extends ServiceUnit {
 
     private Kernel kernel;
+    private ServiceName configuration;
 
     /**
      * @return Returns the kernel.
@@ -38,12 +42,32 @@
         this.kernel = kernel;
     }
 
+    public ServiceName getConfiguration() {
+        return configuration;
+    }
+
+    public void setConfiguration(ServiceName configuration) {
+        this.configuration = configuration;
+    }
+    
     /* (non-Javadoc)
      * @see org.apache.servicemix.common.ServiceUnit#shutDown()
      */
     public void shutDown() throws JBIException {
-        kernel.destroy();
+        if (kernel != null) {
+            kernel.destroy();
+        }
         super.shutDown();
+    }
+    
+    public ClassLoader getConfigurationClassLoader() throws ServiceNotFoundException {
+        if (kernel != null) {
+            Object o = kernel.getServiceFactory(configuration);
+            SpringConfigurationServiceFactory scsf = (SpringConfigurationServiceFactory) o;
+            return scsf.getApplicationContext().getClassLoader();
+        } else {
+            return Thread.currentThread().getContextClassLoader();
+        }
     }
     
 }

Modified: incubator/servicemix/trunk/servicemix-jsr181/src/main/java/org/apache/servicemix/jsr181/Jsr181Endpoint.java
URL: http://svn.apache.org/viewcvs/incubator/servicemix/trunk/servicemix-jsr181/src/main/java/org/apache/servicemix/jsr181/Jsr181Endpoint.java?rev=373881&r1=373880&r2=373881&view=diff
==============================================================================
--- incubator/servicemix/trunk/servicemix-jsr181/src/main/java/org/apache/servicemix/jsr181/Jsr181Endpoint.java (original)
+++ incubator/servicemix/trunk/servicemix-jsr181/src/main/java/org/apache/servicemix/jsr181/Jsr181Endpoint.java Tue Jan 31 10:17:21 2006
@@ -50,6 +50,7 @@
 import org.codehaus.xfire.xmlbeans.XmlBeansTypeRegistry;
 import org.apache.servicemix.common.Endpoint;
 import org.apache.servicemix.common.ExchangeProcessor;
+import org.apache.servicemix.common.xbean.XBeanServiceUnit;
 import org.apache.servicemix.jsr181.xfire.JbiTransport;
 import org.w3c.dom.Document;
 import org.xml.sax.SAXException;
@@ -168,7 +169,8 @@
 
     public void registerService() throws Exception {
         if (pojo == null) {
-            Class cl = Class.forName(pojoClass);
+            ClassLoader classLoader = ((XBeanServiceUnit) getServiceUnit()).getConfigurationClassLoader();
+            Class cl = Class.forName(pojoClass, true, classLoader);
             pojo = cl.newInstance();
         }
         // Determine annotations

Modified: incubator/servicemix/trunk/servicemix-jsr181/src/main/java/org/apache/servicemix/jsr181/Jsr181SpringComponent.java
URL: http://svn.apache.org/viewcvs/incubator/servicemix/trunk/servicemix-jsr181/src/main/java/org/apache/servicemix/jsr181/Jsr181SpringComponent.java?rev=373881&r1=373880&r2=373881&view=diff
==============================================================================
--- incubator/servicemix/trunk/servicemix-jsr181/src/main/java/org/apache/servicemix/jsr181/Jsr181SpringComponent.java (original)
+++ incubator/servicemix/trunk/servicemix-jsr181/src/main/java/org/apache/servicemix/jsr181/Jsr181SpringComponent.java Tue Jan 31 10:17:21 2006
@@ -18,6 +18,7 @@
 import org.apache.servicemix.common.BaseComponent;
 import org.apache.servicemix.common.BaseLifeCycle;
 import org.apache.servicemix.common.ServiceUnit;
+import org.apache.servicemix.common.xbean.XBeanServiceUnit;
 
 /**
  * 
@@ -58,7 +59,7 @@
          */
         protected void doInit() throws Exception {
             super.doInit();
-            su = new ServiceUnit();
+            su = new XBeanServiceUnit();
             su.setComponent(Jsr181SpringComponent.this);
             for (int i = 0; i < endpoints.length; i++) {
                 endpoints[i].setServiceUnit(su);