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/10/31 16:34:00 UTC

svn commit: r469525 - in /incubator/servicemix/trunk/servicemix-common/src: main/java/org/apache/servicemix/common/xbean/ test/java/org/apache/servicemix/common/xbean/ test/resources/xbean-cp/ test/resources/xbean-cp/lib/ test/resources/xbean-inline/ t...

Author: gnodet
Date: Tue Oct 31 07:33:59 2006
New Revision: 469525

URL: http://svn.apache.org/viewvc?view=rev&rev=469525
Log:
SM-673: Simplify classloader definition for xbean based SU

Added:
    incubator/servicemix/trunk/servicemix-common/src/main/java/org/apache/servicemix/common/xbean/ClassLoaderXmlPreprocessor.java
    incubator/servicemix/trunk/servicemix-common/src/test/resources/xbean-cp/
    incubator/servicemix/trunk/servicemix-common/src/test/resources/xbean-cp/classpath.xml
    incubator/servicemix/trunk/servicemix-common/src/test/resources/xbean-cp/lib/
    incubator/servicemix/trunk/servicemix-common/src/test/resources/xbean-cp/lib/classpath.zip   (with props)
    incubator/servicemix/trunk/servicemix-common/src/test/resources/xbean-cp/xbean.xml
    incubator/servicemix/trunk/servicemix-common/src/test/resources/xbean-inline/
    incubator/servicemix/trunk/servicemix-common/src/test/resources/xbean-inline/lib/
    incubator/servicemix/trunk/servicemix-common/src/test/resources/xbean-inline/lib/classpath.zip   (with props)
    incubator/servicemix/trunk/servicemix-common/src/test/resources/xbean-inline/xbean.xml
    incubator/servicemix/trunk/servicemix-common/src/test/resources/xbean-lib/
    incubator/servicemix/trunk/servicemix-common/src/test/resources/xbean-lib/lib/
    incubator/servicemix/trunk/servicemix-common/src/test/resources/xbean-lib/lib/classpath.zip   (with props)
    incubator/servicemix/trunk/servicemix-common/src/test/resources/xbean-lib/xbean.xml
Modified:
    incubator/servicemix/trunk/servicemix-common/src/main/java/org/apache/servicemix/common/xbean/AbstractXBeanDeployer.java
    incubator/servicemix/trunk/servicemix-common/src/test/java/org/apache/servicemix/common/xbean/XBeanDeployerTest.java

Modified: incubator/servicemix/trunk/servicemix-common/src/main/java/org/apache/servicemix/common/xbean/AbstractXBeanDeployer.java
URL: http://svn.apache.org/viewvc/incubator/servicemix/trunk/servicemix-common/src/main/java/org/apache/servicemix/common/xbean/AbstractXBeanDeployer.java?view=diff&rev=469525&r1=469524&r2=469525
==============================================================================
--- 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 Oct 31 07:33:59 2006
@@ -24,7 +24,6 @@
 import org.apache.xbean.kernel.KernelFactory;
 import org.apache.xbean.kernel.ServiceName;
 import org.apache.xbean.server.repository.FileSystemRepository;
-import org.apache.xbean.server.spring.configuration.ClassLoaderXmlPreprocessor;
 import org.apache.xbean.server.spring.loader.SpringLoader;
 import org.springframework.beans.factory.config.PropertyPlaceholderConfigurer;
 import org.springframework.core.io.FileSystemResource;

Added: incubator/servicemix/trunk/servicemix-common/src/main/java/org/apache/servicemix/common/xbean/ClassLoaderXmlPreprocessor.java
URL: http://svn.apache.org/viewvc/incubator/servicemix/trunk/servicemix-common/src/main/java/org/apache/servicemix/common/xbean/ClassLoaderXmlPreprocessor.java?view=auto&rev=469525
==============================================================================
--- 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 Oct 31 07:33:59 2006
@@ -0,0 +1,118 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You 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.io.File;
+import java.io.FilenameFilter;
+import java.net.URL;
+
+import javax.xml.parsers.DocumentBuilder;
+
+import org.apache.servicemix.jbi.jaxp.SourceTransformer;
+import org.apache.xbean.classloader.JarFileClassLoader;
+import org.apache.xbean.server.repository.FileSystemRepository;
+import org.apache.xbean.server.repository.Repository;
+import org.apache.xbean.server.spring.loader.SpringLoader;
+import org.apache.xbean.spring.context.SpringApplicationContext;
+import org.apache.xbean.spring.context.SpringXmlPreprocessor;
+import org.springframework.beans.factory.BeanDefinitionStoreException;
+import org.springframework.beans.factory.xml.XmlBeanDefinitionReader;
+import org.w3c.dom.Document;
+import org.w3c.dom.NodeList;
+
+/**
+ * An advanced xml preprocessor that will create a default classloader for the SU if none
+ * is configured.
+ * 
+ * @author gnodet
+ */
+public class ClassLoaderXmlPreprocessor implements SpringXmlPreprocessor {
+
+    public static final String CLASSPATH_XML = "classpath.xml";
+    public static final String LIB_DIR = "/lib";
+    
+    private final FileSystemRepository repository;
+    private final SpringXmlPreprocessor preprocessor;
+    
+    public ClassLoaderXmlPreprocessor(Repository repository) {
+        if (repository instanceof FileSystemRepository == false) {
+            throw new IllegalArgumentException("repository must be a FileSystemRepository");
+        }
+        this.repository = (FileSystemRepository) repository;
+        this.preprocessor = new org.apache.xbean.server.spring.configuration.ClassLoaderXmlPreprocessor(repository);
+    }
+
+    public void preprocess(SpringApplicationContext applicationContext, XmlBeanDefinitionReader reader, Document document) {
+        // determine the classLoader
+        NodeList classpathElements = document.getDocumentElement().getElementsByTagName("classpath");
+        if (classpathElements.getLength() == 0) {
+            // Check if a classpath.xml file exists in the root of the SU
+            URL url = repository.getResource(CLASSPATH_XML);
+            if (url != null) {
+                try {
+                    DocumentBuilder builder = new SourceTransformer().createDocumentBuilder();
+                    Document doc = builder.parse(url.toString());
+                    preprocessor.preprocess(applicationContext, reader, doc);
+                } catch (Exception e) {
+                    throw new BeanDefinitionStoreException("Unable to load classpath.xml file", e);
+                }
+            } else {
+                try {
+                    File root = repository.getRoot();
+                    File[] jars = new File(root, LIB_DIR).listFiles(new FilenameFilter() {
+                        public boolean accept(File dir, String name) {
+                            name = name.toLowerCase();
+                            return name.endsWith(".jar") || name.endsWith(".zip");
+                        }
+                    });
+                    URL[] urls = new URL[jars != null ? jars.length + 1 : 1];
+                    urls[0] = root.toURL();
+                    if (jars != null) {
+                        for (int i = 0; i < jars.length; i++) {
+                            urls[i+1] = jars[i].toURL();
+                        }
+                    }
+                    ClassLoader parentLoader = getClassLoader(applicationContext);
+                    ClassLoader classLoader = new JarFileClassLoader(
+                                                         applicationContext.getDisplayName(), 
+                                                         urls, 
+                                                         parentLoader);
+                    // assign the class loader to the xml reader and the application context
+                    reader.setBeanClassLoader(classLoader);
+                    applicationContext.setClassLoader(classLoader);
+                    Thread.currentThread().setContextClassLoader(classLoader);
+                } catch (Exception e) {
+                    throw new BeanDefinitionStoreException("Unable to create default classloader for SU", e);
+                }
+            }
+        } else {
+            preprocessor.preprocess(applicationContext, reader, document);
+        }
+    }
+
+    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/test/java/org/apache/servicemix/common/xbean/XBeanDeployerTest.java
URL: http://svn.apache.org/viewvc/incubator/servicemix/trunk/servicemix-common/src/test/java/org/apache/servicemix/common/xbean/XBeanDeployerTest.java?view=diff&rev=469525&r1=469524&r2=469525
==============================================================================
--- incubator/servicemix/trunk/servicemix-common/src/test/java/org/apache/servicemix/common/xbean/XBeanDeployerTest.java (original)
+++ incubator/servicemix/trunk/servicemix-common/src/test/java/org/apache/servicemix/common/xbean/XBeanDeployerTest.java Tue Oct 31 07:33:59 2006
@@ -21,6 +21,7 @@
 
 import org.apache.servicemix.common.BaseComponent;
 import org.apache.servicemix.common.ServiceUnit;
+import org.apache.xbean.classloader.JarFileClassLoader;
 
 import junit.framework.TestCase;
 
@@ -41,6 +42,39 @@
         assertEquals(1, su.getEndpoints().size());
         XBeanEndpoint ep = (XBeanEndpoint) su.getEndpoints().iterator().next();
         assertEquals("value", ep.getProp());
+    }
+    
+    public void testDeployWithClasspathXml() throws Exception {
+        MyXBeanDeployer deployer = new MyXBeanDeployer(new BaseComponent() { });
+        ServiceUnit su = deployer.deploy("xbean-cp", getServiceUnitPath("xbean-cp"));
+        assertNotNull(su);
+        ClassLoader cl = su.getConfigurationClassLoader();
+        assertNotNull(cl);
+        assertTrue(cl instanceof JarFileClassLoader);
+        assertEquals(2, ((JarFileClassLoader) cl).getURLs().length);
+        assertNotNull(cl.getResource("test.xml"));
+    }
+    
+    public void testDeployWithInlineClasspath() throws Exception {
+        MyXBeanDeployer deployer = new MyXBeanDeployer(new BaseComponent() { });
+        ServiceUnit su = deployer.deploy("xbean-inline", getServiceUnitPath("xbean-inline"));
+        assertNotNull(su);
+        ClassLoader cl = su.getConfigurationClassLoader();
+        assertNotNull(cl);
+        assertTrue(cl instanceof JarFileClassLoader);
+        assertEquals(2, ((JarFileClassLoader) cl).getURLs().length);
+        assertNotNull(cl.getResource("test.xml"));
+    }
+    
+    public void testDeployWithDefaultClasspath() throws Exception {
+        MyXBeanDeployer deployer = new MyXBeanDeployer(new BaseComponent() { });
+        ServiceUnit su = deployer.deploy("xbean-lib", getServiceUnitPath("xbean-lib"));
+        assertNotNull(su);
+        ClassLoader cl = su.getConfigurationClassLoader();
+        assertNotNull(cl);
+        assertTrue(cl instanceof JarFileClassLoader);
+        assertEquals(2, ((JarFileClassLoader) cl).getURLs().length);
+        assertNotNull(cl.getResource("test.xml"));
     }
     
     public static class MyXBeanDeployer extends AbstractXBeanDeployer {

Added: incubator/servicemix/trunk/servicemix-common/src/test/resources/xbean-cp/classpath.xml
URL: http://svn.apache.org/viewvc/incubator/servicemix/trunk/servicemix-common/src/test/resources/xbean-cp/classpath.xml?view=auto&rev=469525
==============================================================================
--- incubator/servicemix/trunk/servicemix-common/src/test/resources/xbean-cp/classpath.xml (added)
+++ incubator/servicemix/trunk/servicemix-common/src/test/resources/xbean-cp/classpath.xml Tue Oct 31 07:33:59 2006
@@ -0,0 +1,27 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<!--
+
+    Licensed to the Apache Software Foundation (ASF) under one or more
+    contributor license agreements.  See the NOTICE file distributed with
+    this work for additional information regarding copyright ownership.
+    The ASF licenses this file to You 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.
+
+-->
+<beans>
+
+  <classpath>
+    <location>.</location>
+    <location>lib/classpath.zip</location>
+  </classpath>
+
+</beans>

Added: incubator/servicemix/trunk/servicemix-common/src/test/resources/xbean-cp/lib/classpath.zip
URL: http://svn.apache.org/viewvc/incubator/servicemix/trunk/servicemix-common/src/test/resources/xbean-cp/lib/classpath.zip?view=auto&rev=469525
==============================================================================
Binary file - no diff available.

Propchange: incubator/servicemix/trunk/servicemix-common/src/test/resources/xbean-cp/lib/classpath.zip
------------------------------------------------------------------------------
    svn:mime-type = application/octet-stream

Added: incubator/servicemix/trunk/servicemix-common/src/test/resources/xbean-cp/xbean.xml
URL: http://svn.apache.org/viewvc/incubator/servicemix/trunk/servicemix-common/src/test/resources/xbean-cp/xbean.xml?view=auto&rev=469525
==============================================================================
--- incubator/servicemix/trunk/servicemix-common/src/test/resources/xbean-cp/xbean.xml (added)
+++ incubator/servicemix/trunk/servicemix-common/src/test/resources/xbean-cp/xbean.xml Tue Oct 31 07:33:59 2006
@@ -0,0 +1,28 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<!--
+
+    Licensed to the Apache Software Foundation (ASF) under one or more
+    contributor license agreements.  See the NOTICE file distributed with
+    this work for additional information regarding copyright ownership.
+    The ASF licenses this file to You 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.
+
+-->
+<beans>
+
+  <bean class="org.apache.servicemix.common.xbean.XBeanEndpoint">
+    <property name="service" value="service"/>
+    <property name="endpoint" value="endpoint" />
+    <property name="prop" value="${prop}" />
+  </bean>
+
+</beans>

Added: incubator/servicemix/trunk/servicemix-common/src/test/resources/xbean-inline/lib/classpath.zip
URL: http://svn.apache.org/viewvc/incubator/servicemix/trunk/servicemix-common/src/test/resources/xbean-inline/lib/classpath.zip?view=auto&rev=469525
==============================================================================
Binary file - no diff available.

Propchange: incubator/servicemix/trunk/servicemix-common/src/test/resources/xbean-inline/lib/classpath.zip
------------------------------------------------------------------------------
    svn:mime-type = application/octet-stream

Added: incubator/servicemix/trunk/servicemix-common/src/test/resources/xbean-inline/xbean.xml
URL: http://svn.apache.org/viewvc/incubator/servicemix/trunk/servicemix-common/src/test/resources/xbean-inline/xbean.xml?view=auto&rev=469525
==============================================================================
--- incubator/servicemix/trunk/servicemix-common/src/test/resources/xbean-inline/xbean.xml (added)
+++ incubator/servicemix/trunk/servicemix-common/src/test/resources/xbean-inline/xbean.xml Tue Oct 31 07:33:59 2006
@@ -0,0 +1,33 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<!--
+
+    Licensed to the Apache Software Foundation (ASF) under one or more
+    contributor license agreements.  See the NOTICE file distributed with
+    this work for additional information regarding copyright ownership.
+    The ASF licenses this file to You 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.
+
+-->
+<beans>
+
+  <classpath>
+    <location>.</location>
+    <location>lib/classpath.zip</location>
+  </classpath>
+  
+  <bean class="org.apache.servicemix.common.xbean.XBeanEndpoint">
+    <property name="service" value="service"/>
+    <property name="endpoint" value="endpoint" />
+    <property name="prop" value="${prop}" />
+  </bean>
+
+</beans>

Added: incubator/servicemix/trunk/servicemix-common/src/test/resources/xbean-lib/lib/classpath.zip
URL: http://svn.apache.org/viewvc/incubator/servicemix/trunk/servicemix-common/src/test/resources/xbean-lib/lib/classpath.zip?view=auto&rev=469525
==============================================================================
Binary file - no diff available.

Propchange: incubator/servicemix/trunk/servicemix-common/src/test/resources/xbean-lib/lib/classpath.zip
------------------------------------------------------------------------------
    svn:mime-type = application/octet-stream

Added: incubator/servicemix/trunk/servicemix-common/src/test/resources/xbean-lib/xbean.xml
URL: http://svn.apache.org/viewvc/incubator/servicemix/trunk/servicemix-common/src/test/resources/xbean-lib/xbean.xml?view=auto&rev=469525
==============================================================================
--- incubator/servicemix/trunk/servicemix-common/src/test/resources/xbean-lib/xbean.xml (added)
+++ incubator/servicemix/trunk/servicemix-common/src/test/resources/xbean-lib/xbean.xml Tue Oct 31 07:33:59 2006
@@ -0,0 +1,28 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<!--
+
+    Licensed to the Apache Software Foundation (ASF) under one or more
+    contributor license agreements.  See the NOTICE file distributed with
+    this work for additional information regarding copyright ownership.
+    The ASF licenses this file to You 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.
+
+-->
+<beans>
+
+  <bean class="org.apache.servicemix.common.xbean.XBeanEndpoint">
+    <property name="service" value="service"/>
+    <property name="endpoint" value="endpoint" />
+    <property name="prop" value="${prop}" />
+  </bean>
+
+</beans>