From 695099482aa984eeb4f499f9de570328f1487b9d Mon Sep 17 00:00:00 2001 From: ramkumar Date: Wed, 22 Jul 2009 12:08:32 +0000 Subject: Fixes for spring module classloader changes ref: TUSCANY-3069 in 1.x git-svn-id: http://svn.us.apache.org/repos/asf/tuscany@796684 13f79535-47bb-0310-9956-ffa450edef68 --- .../spring/runtime/context/SpringContextTie.java | 81 +++++++++++----------- .../runtime/context/SpringImplementationStub.java | 14 +++- .../implementation/spring/SpringBeanElement.java | 11 +++ .../spring/SpringImplementation.java | 11 ++- .../introspect/SpringXMLComponentTypeLoader.java | 59 ++++++++++++++-- .../spring/invocation/SpringContextStub.java | 6 +- .../spring/invocation/SpringImplementationTie.java | 7 +- 7 files changed, 137 insertions(+), 52 deletions(-) (limited to 'java/sca/modules') diff --git a/java/sca/modules/implementation-spring-sca/src/main/java/org/apache/tuscany/sca/implementation/spring/runtime/context/SpringContextTie.java b/java/sca/modules/implementation-spring-sca/src/main/java/org/apache/tuscany/sca/implementation/spring/runtime/context/SpringContextTie.java index 3c36b04ba8..18517755d5 100644 --- a/java/sca/modules/implementation-spring-sca/src/main/java/org/apache/tuscany/sca/implementation/spring/runtime/context/SpringContextTie.java +++ b/java/sca/modules/implementation-spring-sca/src/main/java/org/apache/tuscany/sca/implementation/spring/runtime/context/SpringContextTie.java @@ -77,54 +77,51 @@ public class SpringContextTie { /** * Include BeanPostProcessor to deal with SCA Annotations in Spring Bean */ -// private Object createApplicationContext(SCAParentApplicationContext scaParentContext) { - private AbstractApplicationContext createApplicationContext(SCAParentApplicationContext scaParentContext, URL resource) { XmlBeanFactory beanFactory = new XmlBeanFactory(new UrlResource(resource)); + beanFactory.setBeanClassLoader(implementation.getClassLoader()); AbstractApplicationContext appContext = null; for (String bean : beanFactory.getBeanDefinitionNames()) { - String beanClassName = (beanFactory.getType(bean)).getName(); - if (beanClassName.indexOf(".ClassPathXmlApplicationContext") != -1 || - beanClassName.indexOf(".FileSystemXmlApplicationContext") != -1) - { - BeanDefinition beanDef = beanFactory.getBeanDefinition(bean); - String[] listValues = null; - List conArgs = - beanDef.getConstructorArgumentValues().getGenericArgumentValues(); - for (ConstructorArgumentValues.ValueHolder conArg : conArgs) { - if (conArg.getValue() instanceof TypedStringValue) { - TypedStringValue value = (TypedStringValue) conArg.getValue(); - if (value.getValue().indexOf(".xml") != -1) - listValues = new String[]{value.getValue()}; - } - if (conArg.getValue() instanceof ManagedList) { - Iterator itml = ((ManagedList)conArg.getValue()).iterator(); - StringBuffer values = new StringBuffer(); - while (itml.hasNext()) { - TypedStringValue next = (TypedStringValue)itml.next(); - if (next.getValue().indexOf(".xml") != -1) { - values.append(next.getValue()); - values.append("~"); - } - } - listValues = (values.toString()).split("~"); - } - } - - if (beanClassName.indexOf(".ClassPathXmlApplicationContext") != -1) { - appContext = new ClassPathXmlApplicationContext(listValues, false, scaParentContext); - appContext.refresh(); // TODO why is this needed here now? - includeAnnotationProcessors(appContext.getBeanFactory()); - return appContext; - } else { - appContext = new FileSystemXmlApplicationContext(listValues, false, scaParentContext); - appContext.refresh(); // TODO why is this needed here now? - includeAnnotationProcessors(appContext.getBeanFactory()); - return appContext; + String beanClassName = (beanFactory.getType(bean)).getName(); + if (beanClassName.indexOf(".ClassPathXmlApplicationContext") != -1 || + beanClassName.indexOf(".FileSystemXmlApplicationContext") != -1) + { + BeanDefinition beanDef = beanFactory.getBeanDefinition(bean); + String[] configLocations = null; + List conArgs = + beanDef.getConstructorArgumentValues().getGenericArgumentValues(); + for (ConstructorArgumentValues.ValueHolder conArg : conArgs) { + if (conArg.getValue() instanceof TypedStringValue) { + TypedStringValue value = (TypedStringValue) conArg.getValue(); + if (value.getValue().indexOf(".xml") != -1) + configLocations = new String[]{value.getValue()}; + } + if (conArg.getValue() instanceof ManagedList) { + Iterator itml = ((ManagedList)conArg.getValue()).iterator(); + StringBuffer values = new StringBuffer(); + while (itml.hasNext()) { + TypedStringValue next = (TypedStringValue)itml.next(); + if (next.getValue().indexOf(".xml") != -1) { + values.append(implementation.getClassLoader().getResource(next.getValue()).toString()); + values.append("~"); + } } - } + configLocations = (values.toString()).split("~"); + } + } + + if (beanClassName.indexOf(".ClassPathXmlApplicationContext") != -1) { + appContext = new ClassPathXmlApplicationContext(configLocations, true, scaParentContext); + includeAnnotationProcessors(appContext.getBeanFactory()); + return appContext; + } else { + appContext = new FileSystemXmlApplicationContext(configLocations, true, scaParentContext); + includeAnnotationProcessors(appContext.getBeanFactory()); + return appContext; + } + } } // use the generic application context as default @@ -165,4 +162,4 @@ public class SpringContextTie { beanFactory.addBeanPostProcessor(constructorProcessor); } -} +} \ No newline at end of file diff --git a/java/sca/modules/implementation-spring-sca/src/main/java/org/apache/tuscany/sca/implementation/spring/runtime/context/SpringImplementationStub.java b/java/sca/modules/implementation-spring-sca/src/main/java/org/apache/tuscany/sca/implementation/spring/runtime/context/SpringImplementationStub.java index abf9d55e86..af39869522 100644 --- a/java/sca/modules/implementation-spring-sca/src/main/java/org/apache/tuscany/sca/implementation/spring/runtime/context/SpringImplementationStub.java +++ b/java/sca/modules/implementation-spring-sca/src/main/java/org/apache/tuscany/sca/implementation/spring/runtime/context/SpringImplementationStub.java @@ -39,6 +39,7 @@ public class SpringImplementationStub { Method getComponentName; Method getComponentTie; Method getPropertyValueTie; + Method getClassLoader; public SpringImplementationStub(Object tie) { this.tie = tie; @@ -49,6 +50,7 @@ public class SpringImplementationStub { getComponentName = tieClass.getMethod("getComponentName"); getComponentTie = tieClass.getMethod("getComponentTie"); getPropertyValueTie = tieClass.getMethod("getPropertyValueTie"); + getClassLoader = tieClass.getMethod("getClassLoader"); } catch (Exception e) { throw new RuntimeException(e); } @@ -114,4 +116,14 @@ public class SpringImplementationStub { throw new RuntimeException(e); } } -} + + public ClassLoader getClassLoader() { + try { + + return (ClassLoader) getClassLoader.invoke(tie); + + } catch (Exception e) { + throw new RuntimeException(e); + } + } +} \ No newline at end of file diff --git a/java/sca/modules/implementation-spring/src/main/java/org/apache/tuscany/sca/implementation/spring/SpringBeanElement.java b/java/sca/modules/implementation-spring/src/main/java/org/apache/tuscany/sca/implementation/spring/SpringBeanElement.java index f098e2d78e..60af3e2ad5 100644 --- a/java/sca/modules/implementation-spring/src/main/java/org/apache/tuscany/sca/implementation/spring/SpringBeanElement.java +++ b/java/sca/modules/implementation-spring/src/main/java/org/apache/tuscany/sca/implementation/spring/SpringBeanElement.java @@ -33,6 +33,9 @@ public class SpringBeanElement { private String id; private String className; private boolean innerBean = false; + private boolean abstractBean = false; + private boolean refParentBean = false; + private boolean reffactoryBean = false; private List properties = new ArrayList(); private List constructorargs = new ArrayList(); @@ -72,5 +75,13 @@ public class SpringBeanElement { public void setInnerBean(boolean innerBean) { this.innerBean = innerBean; } + + public boolean isAbstractBean() { + return abstractBean; + } + + public void setAbstractBean(boolean abstractBean) { + this.abstractBean = abstractBean; + } } // end class SpringBeanElement diff --git a/java/sca/modules/implementation-spring/src/main/java/org/apache/tuscany/sca/implementation/spring/SpringImplementation.java b/java/sca/modules/implementation-spring/src/main/java/org/apache/tuscany/sca/implementation/spring/SpringImplementation.java index 0df04457fb..9a8fac6a61 100644 --- a/java/sca/modules/implementation-spring/src/main/java/org/apache/tuscany/sca/implementation/spring/SpringImplementation.java +++ b/java/sca/modules/implementation-spring/src/main/java/org/apache/tuscany/sca/implementation/spring/SpringImplementation.java @@ -55,6 +55,7 @@ public class SpringImplementation extends ImplementationImpl implements Implemen private Hashtable> propertyMap; // List of unresolved bean property references private Hashtable unresolvedBeanRef; + private ClassLoader classLoader; public SpringImplementation() { super(TYPE); @@ -217,4 +218,12 @@ public class SpringImplementation extends ImplementationImpl implements Implemen } return newProperty; } -} + + public ClassLoader getClassLoader() { + return classLoader; + } + + public void setClassLoader(ClassLoader classLoader) { + this.classLoader = classLoader; + } +} \ No newline at end of file diff --git a/java/sca/modules/implementation-spring/src/main/java/org/apache/tuscany/sca/implementation/spring/introspect/SpringXMLComponentTypeLoader.java b/java/sca/modules/implementation-spring/src/main/java/org/apache/tuscany/sca/implementation/spring/introspect/SpringXMLComponentTypeLoader.java index efc4b309e0..720a56e0ad 100644 --- a/java/sca/modules/implementation-spring/src/main/java/org/apache/tuscany/sca/implementation/spring/introspect/SpringXMLComponentTypeLoader.java +++ b/java/sca/modules/implementation-spring/src/main/java/org/apache/tuscany/sca/implementation/spring/introspect/SpringXMLComponentTypeLoader.java @@ -27,7 +27,11 @@ import java.io.IOException; import java.net.MalformedURLException; import java.net.URI; import java.net.URL; +import java.util.Arrays; import java.util.ArrayList; +import java.util.Collection; +import java.util.Collections; +import java.util.Enumeration; import java.util.Iterator; import java.util.List; import java.util.Map; @@ -77,7 +81,6 @@ import org.apache.tuscany.sca.policy.PolicyFactory; * Introspects a Spring XML application-context configuration file to create * component type information. * - * * @version $Rev$ $Date$ */ public class SpringXMLComponentTypeLoader { @@ -158,6 +161,8 @@ public class SpringXMLComponentTypeLoader { try { resource = resolveLocation(resolver, contextPath); resource = getApplicationContextResource(resource); + + implementation.setClassLoader(new ContextClassLoader(resolver)); implementation.setResource(resource); // The URI is used to uniquely identify the Implementation implementation.setURI(resource.toString()); @@ -687,12 +692,13 @@ public class SpringXMLComponentTypeLoader { } } } + // Look for the default applicaiton-context.xml file, when MANIFEST.MF is absent. je = jf.getJarEntry("META-INF" + "/" + "spring" + "/" + SpringImplementationConstants.APPLICATION_CONTEXT); if (je != null) { - return new URL("jar:" + locationFile.toURI().toURL() + "!/" + SpringImplementationConstants.APPLICATION_CONTEXT); + return new URL("jar:" + locationFile.toURI().toURL() + "!/" + + "META-INF" + "/" + "spring" + "/" + SpringImplementationConstants.APPLICATION_CONTEXT); } } catch (IOException e) { - // bad archive // TODO: create a more appropriate exception type throw new ContributionReadException("SpringXMLLoader getApplicationContextResource: " + " IO exception reading context file.", e); @@ -706,8 +712,8 @@ public class SpringXMLComponentTypeLoader { // Deal with the directory inside a jar file, in case the contribution itself is a JAR file. try { if (locationFile.getPath().indexOf(".jar") > 0) { - String jarEntry = url.getPath().substring(6, url.getPath().indexOf("!")); - JarFile jf = new JarFile(jarEntry); + String jarPath = url.getPath().substring(6, url.getPath().indexOf("!")); + JarFile jf = new JarFile(jarPath); JarEntry je = jf.getJarEntry(url.getPath().substring(url.getPath().indexOf("!/")+2) + "/" + "META-INF" + "/" + "MANIFEST.MF"); if (je != null) { @@ -721,6 +727,13 @@ public class SpringXMLComponentTypeLoader { } } } + // Look for the default applicaiton-context.xml file, when MANIFEST.MF is absent. + je = jf.getJarEntry(url.getPath().substring(url.getPath().indexOf("!/")+2) + "/" + + "META-INF" + "/" + "spring" + "/" + SpringImplementationConstants.APPLICATION_CONTEXT); + if (je != null) { + return new URL("jar:" + url.getPath() + "/" + + "META-INF" + "/" + "spring" + "/" + SpringImplementationConstants.APPLICATION_CONTEXT); + } } } catch (IOException e) { throw new ContributionReadException("Error reading manifest " + manifestFile); @@ -778,4 +791,38 @@ public class SpringXMLComponentTypeLoader { return reference; } -} // end class SpringXMLComponentTypeLoader + + private class ContextClassLoader extends ClassLoader { + public ContextClassLoader(ModelResolver resolver) { + super(); + this.resolver = resolver; + } + + private ModelResolver resolver; + + @Override + protected Class findClass(String name) throws ClassNotFoundException { + return SpringXMLComponentTypeLoader.this.resolveClass(resolver, name); + } + + @Override + protected URL findResource(String name) { + try { + return resolveLocation(resolver, name); + } catch (Exception e) { + return null; + } + } + + @Override + protected Enumeration findResources(String name) throws IOException { + URL url = findResource(name); + if (url != null) { + return Collections.enumeration(Arrays.asList(url)); + } else { + Collection urls = Collections.emptyList(); + return Collections.enumeration(urls); + } + } + } +} // end class SpringXMLComponentTypeLoader \ No newline at end of file diff --git a/java/sca/modules/implementation-spring/src/main/java/org/apache/tuscany/sca/implementation/spring/invocation/SpringContextStub.java b/java/sca/modules/implementation-spring/src/main/java/org/apache/tuscany/sca/implementation/spring/invocation/SpringContextStub.java index 6a7eb2e024..5ca8c128f4 100644 --- a/java/sca/modules/implementation-spring/src/main/java/org/apache/tuscany/sca/implementation/spring/invocation/SpringContextStub.java +++ b/java/sca/modules/implementation-spring/src/main/java/org/apache/tuscany/sca/implementation/spring/invocation/SpringContextStub.java @@ -85,6 +85,7 @@ public class SpringContextStub { } catch (IllegalAccessException e) { throw new RuntimeException(e); } catch (InvocationTargetException e) { + e.printStackTrace(); throw new RuntimeException(e); } } @@ -97,6 +98,7 @@ public class SpringContextStub { } catch (IllegalAccessException e) { throw new RuntimeException(e); } catch (InvocationTargetException e) { + e.printStackTrace(); throw new RuntimeException(e); } } @@ -109,6 +111,7 @@ public class SpringContextStub { } catch (IllegalAccessException e) { throw new RuntimeException(e); } catch (InvocationTargetException e) { + e.printStackTrace(); throw new RuntimeException(e); } } @@ -123,8 +126,9 @@ public class SpringContextStub { } catch (IllegalAccessException e) { throw new RuntimeException(e); } catch (InvocationTargetException e) { + e.printStackTrace(); throw new RuntimeException(e); } } -} +} \ No newline at end of file diff --git a/java/sca/modules/implementation-spring/src/main/java/org/apache/tuscany/sca/implementation/spring/invocation/SpringImplementationTie.java b/java/sca/modules/implementation-spring/src/main/java/org/apache/tuscany/sca/implementation/spring/invocation/SpringImplementationTie.java index 590b278e4c..6ff573f4be 100644 --- a/java/sca/modules/implementation-spring/src/main/java/org/apache/tuscany/sca/implementation/spring/invocation/SpringImplementationTie.java +++ b/java/sca/modules/implementation-spring/src/main/java/org/apache/tuscany/sca/implementation/spring/invocation/SpringImplementationTie.java @@ -140,4 +140,9 @@ public class SpringImplementationTie { public Object getPropertyValueTie() { return new PropertyValueTie(component, propertyFactory); } -} + + public ClassLoader getClassLoader() { + return implementation.getClassLoader(); + } + +} \ No newline at end of file -- cgit v1.2.3