diff options
author | rfeng <rfeng@13f79535-47bb-0310-9956-ffa450edef68> | 2008-08-15 22:07:43 +0000 |
---|---|---|
committer | rfeng <rfeng@13f79535-47bb-0310-9956-ffa450edef68> | 2008-08-15 22:07:43 +0000 |
commit | b260755836736d405f104bfc14111cb9d51f8118 (patch) | |
tree | bb7eb118adeef50761cf6b72bb6b789251d78f88 /java/sca/modules | |
parent | 1c22b82f37a62e40036cd8fdf70918fd7fc93bd0 (diff) |
Port DefaultSCADomain to use SCA Node impl
git-svn-id: http://svn.us.apache.org/repos/asf/tuscany@686391 13f79535-47bb-0310-9956-ffa450edef68
Diffstat (limited to 'java/sca/modules')
22 files changed, 1249 insertions, 632 deletions
diff --git a/java/sca/modules/assembly-xml/src/main/java/org/apache/tuscany/sca/assembly/xml/CompositeDocumentProcessor.java b/java/sca/modules/assembly-xml/src/main/java/org/apache/tuscany/sca/assembly/xml/CompositeDocumentProcessor.java index a2e67dc6c5..eccdbc6be2 100644 --- a/java/sca/modules/assembly-xml/src/main/java/org/apache/tuscany/sca/assembly/xml/CompositeDocumentProcessor.java +++ b/java/sca/modules/assembly-xml/src/main/java/org/apache/tuscany/sca/assembly/xml/CompositeDocumentProcessor.java @@ -36,7 +36,6 @@ import javax.xml.stream.XMLStreamException; import javax.xml.stream.XMLStreamReader; import org.apache.tuscany.sca.assembly.Composite; -import org.apache.tuscany.sca.monitor.Monitor; import org.apache.tuscany.sca.contribution.ModelFactoryExtensionPoint; import org.apache.tuscany.sca.contribution.processor.StAXArtifactProcessor; import org.apache.tuscany.sca.contribution.processor.URLArtifactProcessor; @@ -45,6 +44,7 @@ import org.apache.tuscany.sca.contribution.resolver.ModelResolver; import org.apache.tuscany.sca.contribution.service.ContributionReadException; import org.apache.tuscany.sca.contribution.service.ContributionResolveException; import org.apache.tuscany.sca.definitions.SCADefinitions; +import org.apache.tuscany.sca.monitor.Monitor; import org.apache.tuscany.sca.policy.PolicySet; import org.apache.tuscany.sca.policy.util.PolicyComputationUtils; @@ -86,6 +86,19 @@ public class CompositeDocumentProcessor extends BaseAssemblyProcessor implements public Composite read(URL contributionURL, URI uri, URL url) throws ContributionReadException { InputStream scdlStream = null; try { + URLConnection connection = url.openConnection(); + connection.setUseCaches(false); + scdlStream = connection.getInputStream(); + } catch (IOException e) { + ContributionReadException ce = new ContributionReadException(e); + error("ContributionReadException", url, ce); + throw ce; + } + return read(uri, scdlStream); + } + + public Composite read(URI uri, InputStream scdlStream) throws ContributionReadException { + try { if (scaDefnSink != null ) { fillDomainPolicySets(scaDefnSink); } @@ -96,13 +109,9 @@ public class CompositeDocumentProcessor extends BaseAssemblyProcessor implements try { if ( domainPolicySets != null ) { transformedArtifactContent = - PolicyComputationUtils.addApplicablePolicySets(url, domainPolicySets); + PolicyComputationUtils.addApplicablePolicySets(scdlStream, domainPolicySets); scdlStream = new ByteArrayInputStream(transformedArtifactContent); - } else { - URLConnection connection = url.openConnection(); - connection.setUseCaches(false); - scdlStream = connection.getInputStream(); - } + } } catch ( IOException e ) { ContributionReadException ce = new ContributionReadException(e); error("ContributionReadException", scdlStream, ce); diff --git a/java/sca/modules/domain-manager/pom.xml b/java/sca/modules/domain-manager/pom.xml index af6d35fa40..56366fb177 100644 --- a/java/sca/modules/domain-manager/pom.xml +++ b/java/sca/modules/domain-manager/pom.xml @@ -73,6 +73,12 @@ <dependency> <groupId>org.apache.tuscany.sca</groupId> + <artifactId>tuscany-core</artifactId> + <version>1.4-SNAPSHOT</version> + </dependency> + + <dependency> + <groupId>org.apache.tuscany.sca</groupId> <artifactId>tuscany-node2-impl</artifactId> <version>1.4-SNAPSHOT</version> <scope>runtime</scope> diff --git a/java/sca/modules/extensibility/src/main/java/org/apache/tuscany/sca/extensibility/ClasspathServiceDiscoverer.java b/java/sca/modules/extensibility/src/main/java/org/apache/tuscany/sca/extensibility/ClasspathServiceDiscoverer.java index 8e3bd80da5..ed7260c30c 100644 --- a/java/sca/modules/extensibility/src/main/java/org/apache/tuscany/sca/extensibility/ClasspathServiceDiscoverer.java +++ b/java/sca/modules/extensibility/src/main/java/org/apache/tuscany/sca/extensibility/ClasspathServiceDiscoverer.java @@ -30,6 +30,7 @@ import java.security.AccessController; import java.security.PrivilegedAction; import java.security.PrivilegedActionException; import java.security.PrivilegedExceptionAction; +import java.util.ArrayList; import java.util.Arrays; import java.util.Collections; import java.util.HashMap; @@ -107,12 +108,14 @@ public class ClasspathServiceDiscoverer implements ServiceDiscoverer { } private WeakReference<ClassLoader> classLoaderReference; + private boolean useTCCL = false; private static final Logger logger = Logger.getLogger(ClasspathServiceDiscoverer.class.getName()); public ClasspathServiceDiscoverer() { // ClassLoader classLoader = Thread.currentThread().getContextClassLoader(); ClassLoader classLoader = ClasspathServiceDiscoverer.class.getClassLoader(); this.classLoaderReference = new WeakReference<ClassLoader>(classLoader); + this.useTCCL = true; } public ClasspathServiceDiscoverer(ClassLoader classLoader) { @@ -128,10 +131,33 @@ public class ClasspathServiceDiscoverer implements ServiceDiscoverer { if (url != null) { return Arrays.asList(url); } else { + if (useTCCL) { + // Try thread context classloader + ClassLoader tccl = Thread.currentThread().getContextClassLoader(); + if (tccl != getContextClassLoader()) { + url = tccl.getResource(name); + } + if (url != null) { + return Arrays.asList(url); + } + } return Collections.emptyList(); } } else { - return Collections.list(getContextClassLoader().getResources(name)); + List<URL> urls = Collections.list(getContextClassLoader().getResources(name)); + if (!useTCCL) { + return urls; + } + // Try thread context classloader + ClassLoader tccl = Thread.currentThread().getContextClassLoader(); + if (tccl != getContextClassLoader()) { + urls.addAll(Collections.list(tccl.getResources(name))); + // Remove duplicate entries + Set<URL> urlSet = new HashSet<URL>(urls); + return new ArrayList<URL>(urlSet); + } else { + return urls; + } } } }); @@ -143,9 +169,9 @@ public class ClasspathServiceDiscoverer implements ServiceDiscoverer { public ClassLoader getContextClassLoader() { return classLoaderReference.get(); } - + public <T> T getContext() { - return (T) getContextClassLoader(); + return (T)getContextClassLoader(); } /** diff --git a/java/sca/modules/extensibility/src/main/java/org/apache/tuscany/sca/extensibility/ServiceDiscovery.java b/java/sca/modules/extensibility/src/main/java/org/apache/tuscany/sca/extensibility/ServiceDiscovery.java index 659329a332..2c2016d7e3 100644 --- a/java/sca/modules/extensibility/src/main/java/org/apache/tuscany/sca/extensibility/ServiceDiscovery.java +++ b/java/sca/modules/extensibility/src/main/java/org/apache/tuscany/sca/extensibility/ServiceDiscovery.java @@ -68,20 +68,24 @@ public class ServiceDiscovery { } /** + * @deprecated * Register a ClassLoader with this discovery mechanism. Tuscany extension * ClassLoaders are registered here. * * @param classLoader */ + @Deprecated public synchronized void registerClassLoader(ClassLoader classLoader) { registeredClassLoaders.add(classLoader); } /** + * @deprecated * Unregister a ClassLoader with this discovery mechanism. * * @param classLoader */ + @Deprecated public synchronized void unregisterClassLoader(ClassLoader classLoader) { registeredClassLoaders.remove(classLoader); } @@ -127,9 +131,15 @@ public class ServiceDiscovery { }); if (className != null) { try { - return Class.forName(className, false, Thread.currentThread().getContextClassLoader()); + // Try the classloader for the service interface first + return Class.forName(className, false, serviceInterface.getClassLoader()); } catch (ClassNotFoundException e) { - logger.log(Level.WARNING, e.getMessage(), e); + try { + // Try the thread context classloader + return Class.forName(className, false, Thread.currentThread().getContextClassLoader()); + } catch (ClassNotFoundException ex) { + logger.log(Level.WARNING, ex.getMessage(), ex); + } } } Set<ServiceDeclaration> services = getServiceDiscoverer().discover(serviceInterface.getName(), true); diff --git a/java/sca/modules/host-embedded/pom.xml b/java/sca/modules/host-embedded/pom.xml index 3f5fce333d..4cb523b9a0 100644 --- a/java/sca/modules/host-embedded/pom.xml +++ b/java/sca/modules/host-embedded/pom.xml @@ -29,6 +29,11 @@ <name>Apache Tuscany SCA Embedded Host</name> <dependencies> + <dependency> + <groupId>org.apache.tuscany.sca</groupId> + <artifactId>tuscany-node2-impl</artifactId> + <version>1.4-SNAPSHOT</version> + </dependency> <dependency> <groupId>org.apache.tuscany.sca</groupId> <artifactId>tuscany-extensibility</artifactId> diff --git a/java/sca/modules/host-embedded/src/main/java/org/apache/tuscany/sca/host/embedded/SCADomain.java b/java/sca/modules/host-embedded/src/main/java/org/apache/tuscany/sca/host/embedded/SCADomain.java index 00e6f49f84..a7f148cdda 100644 --- a/java/sca/modules/host-embedded/src/main/java/org/apache/tuscany/sca/host/embedded/SCADomain.java +++ b/java/sca/modules/host-embedded/src/main/java/org/apache/tuscany/sca/host/embedded/SCADomain.java @@ -18,16 +18,9 @@ package org.apache.tuscany.sca.host.embedded; -import java.io.BufferedReader; -import java.io.IOException; -import java.io.InputStream; -import java.io.InputStreamReader; import java.lang.reflect.Constructor; -import java.security.AccessController; -import java.security.PrivilegedAction; -import java.security.PrivilegedActionException; -import java.security.PrivilegedExceptionAction; +import org.apache.tuscany.sca.extensibility.ServiceDiscovery; import org.apache.tuscany.sca.host.embedded.impl.DefaultSCADomain; import org.apache.tuscany.sca.host.embedded.management.ComponentManager; import org.osoa.sca.CallableReference; @@ -158,49 +151,6 @@ public abstract class SCADomain { public abstract <B> ServiceReference<B> getServiceReference(Class<B> businessInterface, String serviceName); /** - * Read the service name from a configuration file - * - * @param classLoader - * @param name The name of the service class - * @return A class name which extends/implements the service class - * @throws IOException - */ - private static String getServiceName(final ClassLoader classLoader, final String name) throws IOException { - InputStream is; - // Allow privileged access to open stream. Requires FilePermission in security policy. - try { - is = AccessController.doPrivileged(new PrivilegedExceptionAction<InputStream>() { - public InputStream run() throws IOException { - return classLoader.getResourceAsStream("META-INF/services/" + name); - } - }); - } catch (PrivilegedActionException e) { - throw (IOException)e.getException(); - } - - if (is == null) { - return null; - } - BufferedReader reader = null; - try { - reader = new BufferedReader(new InputStreamReader(is)); - while (true) { - String line = reader.readLine(); - if (line == null) { - break; - } else if (!line.startsWith("#")) { - return line.trim(); - } - } - } finally { - if (reader != null) { - reader.close(); - } - } - return null; - } - - /** * Returns an SCADomain instance. If the system property * "org.apache.tuscany.sca.host.embedded.SCADomain" is set, its value is used as * the name of the implementation class. Otherwise, if the resource @@ -223,53 +173,45 @@ public abstract class SCADomain { // Determine the runtime and application ClassLoader final ClassLoader runtimeClassLoader = SCADomain.class.getClassLoader(); final ClassLoader applicationClassLoader = Thread.currentThread().getContextClassLoader(); - - // Discover the SCADomain implementation - final String name = SCADomain.class.getName(); - String className = AccessController.doPrivileged(new PrivilegedAction<String>() { - public String run() { - return System.getProperty(name); - } - }); - if (className == null) { - className = getServiceName(runtimeClassLoader, name); - } - - if (className == null) { - + Class<?> implClass = ServiceDiscovery.getInstance().loadFirstServiceClass(SCADomain.class); + + if (implClass == null) { + // Create a default SCA domain implementation domain = - new DefaultSCADomain(runtimeClassLoader, - applicationClassLoader, - domainURI, - contributionLocation, + new DefaultSCADomain(runtimeClassLoader, applicationClassLoader, domainURI, contributionLocation, composites); } else { - + // Create an instance of the discovered SCA domain implementation - Class cls = Class.forName(className, true, runtimeClassLoader); Constructor<?> constructor = null; try { - constructor = cls.getConstructor(ClassLoader.class, ClassLoader.class, - String.class, String.class, String[].class); - } catch (NoSuchMethodException e) {} + constructor = + implClass.getConstructor(ClassLoader.class, + ClassLoader.class, + String.class, + String.class, + String[].class); + } catch (NoSuchMethodException e) { + } if (constructor != null) { - domain = (SCADomain)constructor.newInstance(runtimeClassLoader, - applicationClassLoader, - domainURI, - contributionLocation, - composites); + domain = + (SCADomain)constructor.newInstance(runtimeClassLoader, + applicationClassLoader, + domainURI, + contributionLocation, + composites); } else { - - constructor = cls.getConstructor(ClassLoader.class, String.class); + + constructor = implClass.getConstructor(ClassLoader.class, String.class); domain = (SCADomain)constructor.newInstance(runtimeClassLoader, domainURI); } } - + // FIXME: temporary support for connect() API theDomain = domain; - + return domain; } catch (Exception e) { diff --git a/java/sca/modules/host-embedded/src/main/java/org/apache/tuscany/sca/host/embedded/impl/DefaultSCADomain.java b/java/sca/modules/host-embedded/src/main/java/org/apache/tuscany/sca/host/embedded/impl/DefaultSCADomain.java index 875a139ca2..8316759206 100644 --- a/java/sca/modules/host-embedded/src/main/java/org/apache/tuscany/sca/host/embedded/impl/DefaultSCADomain.java +++ b/java/sca/modules/host-embedded/src/main/java/org/apache/tuscany/sca/host/embedded/impl/DefaultSCADomain.java @@ -19,63 +19,35 @@ package org.apache.tuscany.sca.host.embedded.impl; -import java.io.File; -import java.io.FilenameFilter; -import java.io.IOException; -import java.net.MalformedURLException; -import java.net.URI; -import java.net.URISyntaxException; import java.net.URL; -import java.security.AccessController; -import java.security.PrivilegedAction; -import java.security.PrivilegedActionException; -import java.security.PrivilegedExceptionAction; +import java.net.URLConnection; import java.util.ArrayList; import java.util.HashMap; -import java.util.HashSet; import java.util.List; import java.util.Map; import java.util.Set; import java.util.concurrent.CopyOnWriteArrayList; -import javax.xml.namespace.QName; +import javax.xml.XMLConstants; +import javax.xml.stream.XMLInputFactory; +import javax.xml.stream.XMLStreamReader; -import org.apache.tuscany.sca.assembly.AssemblyFactory; import org.apache.tuscany.sca.assembly.Component; -import org.apache.tuscany.sca.assembly.ComponentService; import org.apache.tuscany.sca.assembly.Composite; -import org.apache.tuscany.sca.assembly.CompositeService; -import org.apache.tuscany.sca.assembly.SCABinding; -import org.apache.tuscany.sca.assembly.SCABindingFactory; -import org.apache.tuscany.sca.assembly.builder.CompositeBuilderException; import org.apache.tuscany.sca.assembly.xml.Constants; -import org.apache.tuscany.sca.contribution.Artifact; -import org.apache.tuscany.sca.contribution.Contribution; -import org.apache.tuscany.sca.contribution.ModelFactoryExtensionPoint; -import org.apache.tuscany.sca.contribution.service.ContributionException; -import org.apache.tuscany.sca.contribution.service.ContributionService; -import org.apache.tuscany.sca.contribution.service.util.FileHelper; -import org.apache.tuscany.sca.core.ExtensionPointRegistry; -import org.apache.tuscany.sca.core.UtilityExtensionPoint; import org.apache.tuscany.sca.core.assembly.ActivationException; import org.apache.tuscany.sca.core.assembly.CompositeActivator; import org.apache.tuscany.sca.core.assembly.RuntimeComponentImpl; -import org.apache.tuscany.sca.core.context.ServiceReferenceImpl; import org.apache.tuscany.sca.host.embedded.SCADomain; import org.apache.tuscany.sca.host.embedded.management.ComponentListener; import org.apache.tuscany.sca.host.embedded.management.ComponentManager; -import org.apache.tuscany.sca.interfacedef.InterfaceContract; -import org.apache.tuscany.sca.interfacedef.java.JavaInterfaceFactory; -import org.apache.tuscany.sca.monitor.Monitor; -import org.apache.tuscany.sca.monitor.MonitorFactory; -import org.apache.tuscany.sca.monitor.Problem; -import org.apache.tuscany.sca.monitor.Problem.Severity; -import org.apache.tuscany.sca.runtime.RuntimeComponent; -import org.apache.tuscany.sca.runtime.RuntimeComponentContext; -import org.apache.tuscany.sca.runtime.RuntimeComponentReference; +import org.apache.tuscany.sca.node.SCAClient; +import org.apache.tuscany.sca.node.SCAContribution; +import org.apache.tuscany.sca.node.SCANode2; +import org.apache.tuscany.sca.node.SCANode2Factory; +import org.apache.tuscany.sca.node.impl.NodeImpl; import org.osoa.sca.CallableReference; import org.osoa.sca.ServiceReference; -import org.osoa.sca.ServiceRuntimeException; /** * A default SCA domain facade implementation. @@ -86,16 +58,18 @@ public class DefaultSCADomain extends SCADomain { private String uri; private String[] composites; - private Composite domainComposite; - private List<Contribution> contributions; + // private Composite domainComposite; + // private List<Contribution> contributions; private Map<String, Component> components; - private ReallySmallRuntime runtime; private ComponentManager componentManager; - private ClassLoader runtimeClassLoader; + // private ClassLoader runtimeClassLoader; private ClassLoader applicationClassLoader; private String domainURI; - private String contributionLocation; - private Monitor monitor; + private List<String> contributionURLs; + + private CompositeActivator compositeActivator; + private SCANode2 node; + private SCAClient client; /** * Constructs a new domain facade. @@ -111,477 +85,135 @@ public class DefaultSCADomain extends SCADomain { String... composites) { this.uri = domainURI; this.composites = composites; - this.runtimeClassLoader = runtimeClassLoader; + // this.runtimeClassLoader = runtimeClassLoader; this.applicationClassLoader = applicationClassLoader; this.domainURI = domainURI; - this.contributionLocation = contributionLocation; + this.contributionURLs = new ArrayList<String>(); + if (contributionLocation != null && !"/".equals(contributionLocation)) { + this.contributionURLs.add(contributionLocation); + } this.composites = composites; init(); - } - - public void init() { - contributions = new ArrayList<Contribution>(); - components = new HashMap<String, Component>(); - runtime = new ReallySmallRuntime(runtimeClassLoader); - try { - runtime.start(); + this.componentManager = new DefaultSCADomainComponentManager(this); - } catch (ActivationException e) { - throw new ServiceRuntimeException(e); - } - - ExtensionPointRegistry registry = runtime.getExtensionPointRegistry(); - UtilityExtensionPoint utilities = registry.getExtensionPoint(UtilityExtensionPoint.class); - MonitorFactory monitorFactory = utilities.getUtility(MonitorFactory.class); - monitor = monitorFactory.createMonitor(); - - // Contribute the given contribution to an in-memory repository - ContributionService contributionService = runtime.getContributionService(); - URL contributionURL; - try { - contributionURL = getContributionLocation(applicationClassLoader, contributionLocation, this.composites); - if (contributionURL != null) { - // Make sure the URL is correctly encoded (for example, escape the space characters) - contributionURL = contributionURL.toURI().toURL(); - } - } catch (Exception e) { - throw new ServiceRuntimeException(e); - } + } + /** + * A hack to create an aggregated composite + * @param classLoader + * @param composites + * @return + */ + private String createDeploymentComposite(ClassLoader classLoader, String composites[]) { try { - String scheme = contributionURL.toURI().getScheme(); - if (scheme == null || scheme.equalsIgnoreCase("file")) { - final File contributionFile = new File(contributionURL.toURI()); - // Allow privileged access to test file. Requires FilePermission in security policy. - Boolean isDirectory = AccessController.doPrivileged(new PrivilegedAction<Boolean>() { - public Boolean run() { - return contributionFile.isDirectory(); - } - }); - if (isDirectory) { - // Allow privileged access to create file list. Requires FilePermission in - // security policy. - String[] contributions = AccessController.doPrivileged(new PrivilegedAction<String[]>() { - public String[] run() { - return contributionFile.list(new FilenameFilter() { - public boolean accept(File dir, String name) { - return name.endsWith(".jar"); - } - }); - } - }); - - if (contributions != null && contributions.length > 0 - && contributions.length == contributionFile.list().length) { - for (String contribution : contributions) { - addContribution(contributionService, new File(contributionFile, contribution).toURI() - .toURL()); - } - } else { - addContribution(contributionService, contributionURL); - } - } else { - addContribution(contributionService, contributionURL); + StringBuffer xml = + new StringBuffer("<sca:composite xmlns:sca=\"http://www.osoa.org/xmlns/sca/1.0\"") + .append(" targetNamespace=\"http://tempuri.org\" name=\"aggregated\">\n"); + XMLInputFactory factory = XMLInputFactory.newInstance(); + for (int i = 0; i < composites.length; i++) { + URL url = classLoader.getResource(composites[i]); + if (url == null) { + continue; } - } else { - addContribution(contributionService, contributionURL); - } - } catch (IOException e) { - throw new ServiceRuntimeException(e); - } catch (URISyntaxException e) { - throw new ServiceRuntimeException(e); - } - - // Create an in-memory domain level composite - AssemblyFactory assemblyFactory = runtime.getAssemblyFactory(); - domainComposite = assemblyFactory.createComposite(); - domainComposite.setName(new QName(Constants.SCA10_NS, "domain")); - domainComposite.setURI(domainURI); - - //when the deployable composites were specified when initializing the runtime - if (composites != null && composites.length > 0 && composites[0].length() > 0) { - // Include all specified deployable composites in the SCA domain - Map<String, Composite> compositeArtifacts = new HashMap<String, Composite>(); - for (Contribution contribution : contributions) { - for (Artifact artifact : contribution.getArtifacts()) { - if (artifact.getModel() instanceof Composite) { - compositeArtifacts.put(artifact.getURI(), (Composite)artifact.getModel()); - } + String location = NodeImpl.getContributionURL(url, composites[i]).toString(); + if (!contributionURLs.contains(location)) { + contributionURLs.add(location); } - } - for (String compositePath : composites) { - Composite composite = compositeArtifacts.get(compositePath); - if (composite == null) { - throw new ServiceRuntimeException("Composite not found: " + compositePath); + URLConnection connection = url.openConnection(); + connection.setUseCaches(false); + XMLStreamReader reader = factory.createXMLStreamReader(connection.getInputStream()); + reader.nextTag(); + + assert Constants.COMPOSITE_QNAME.equals(reader.getName()); + String ns = reader.getAttributeValue(null, "targetNamespace"); + if (ns == null) { + ns = XMLConstants.NULL_NS_URI; } - domainComposite.getIncludes().add(composite); - } - } else { - // in this case, a sca-contribution.xml should have been specified - for (Contribution contribution : contributions) { - for (Composite composite : contribution.getDeployables()) { - domainComposite.getIncludes().add(composite); + String name = reader.getAttributeValue(null, "name"); + reader.close(); + if (XMLConstants.NULL_NS_URI.equals(ns)) { + xml.append("<sca:include name=\"").append(name).append("\"/>\n"); + } else { + xml.append("<sca:include xmlns:ns").append(i).append("=\"").append(ns).append("\""); + xml.append(" name=\"").append("ns").append(i).append(":").append(name).append("\"/>\n"); } } + xml.append("</sca:composite>"); + // System.out.println(xml.toString()); + return xml.toString(); + } catch (Exception e) { + throw new IllegalArgumentException(e); } + } - //update the runtime for all SCA Definitions processed from the contribution.. - //so that the policyset determination done during 'build' has the all the defined - //intents and policysets - //runtime.updateSCADefinitions(null); + public void init() { + SCANode2Factory factory = SCANode2Factory.newInstance(); - // Build the SCA composites - for (Composite composite : domainComposite.getIncludes()) { - try { - runtime.buildComposite(composite); - analyseProblems(); - } catch (CompositeBuilderException e) { - throw new ServiceRuntimeException(e); - } - } + List<SCAContribution> contributions = new ArrayList<SCAContribution>(); - // Activate and start composites - CompositeActivator compositeActivator = runtime.getCompositeActivator(); - compositeActivator.setDomainComposite(domainComposite); - for (Composite composite : domainComposite.getIncludes()) { - try { - compositeActivator.activate(composite); - } catch (Exception e) { - throw new ServiceRuntimeException(e); + if (composites != null && composites.length > 1) { + // Create an aggregated composite that includes all the composites as Node API only takes one composite + String content = createDeploymentComposite(applicationClassLoader, composites); + // Create SCA contributions + for (String location : contributionURLs) { + contributions.add(new SCAContribution(location, location)); } - } - for (Composite composite : domainComposite.getIncludes()) { - try { - for (Component component : composite.getComponents()) { - compositeActivator.start(component); - } - } catch (Exception e) { - throw new ServiceRuntimeException(e); + node = + factory.createSCANode("http://tempuri.org/aggregated", content, contributions + .toArray(new SCAContribution[contributions.size()])); + } else { + for (String location : contributionURLs) { + contributions.add(new SCAContribution(location, location)); } - } - - // Index the top level components - for (Composite composite : domainComposite.getIncludes()) { - for (Component component : composite.getComponents()) { - components.put(component.getName(), component); + String composite = (composites != null && composites.length >= 1) ? composites[0] : null; + if (!contributions.isEmpty()) { + node = + factory.createSCANode(composite, contributions.toArray(new SCAContribution[contributions.size()])); + } else { + node = factory.createSCANodeFromClassLoader(composite, applicationClassLoader); } } + client = (SCAClient)node; + compositeActivator = ((NodeImpl)node).getCompositeActivator(); + components = new HashMap<String, Component>(); - this.componentManager = new DefaultSCADomainComponentManager(this); + node.start(); - // For debugging purposes, print the composites - // ExtensionPointRegistry extensionPoints = runtime.getExtensionPointRegistry(); - // StAXArtifactProcessorExtensionPoint artifactProcessors = extensionPoints.getExtensionPoint(StAXArtifactProcessorExtensionPoint.class); - // StAXArtifactProcessor processor = artifactProcessors.getProcessor(Composite.class); - // for (Composite composite : domainComposite.getIncludes()) { - // try { - // ByteArrayOutputStream bos = new ByteArrayOutputStream(); - // XMLOutputFactory outputFactory = XMLOutputFactory.newInstance(); - // outputFactory.setProperty(XMLOutputFactory.IS_REPAIRING_NAMESPACES, Boolean.TRUE); - // processor.write(composite, outputFactory.createXMLStreamWriter(bos)); - // Document document = - // DocumentBuilderFactory.newInstance().newDocumentBuilder().parse(new ByteArrayInputStream(bos - // .toByteArray())); - // OutputFormat format = new OutputFormat(); - // format.setIndenting(true); - // format.setIndent(2); - // XMLSerializer serializer = new XMLSerializer(System.out, format); - // serializer.serialize(document); - // } catch (Exception e) { - // e.printStackTrace(); - // } - // } + getComponents(compositeActivator.getDomainComposite()); } - - private void analyseProblems() throws ServiceRuntimeException { - - for (Problem problem : monitor.getProblems()){ - // look for any reported errors. Schema errors are filtered - // out as there are several that are generally reported at the - // moment and we don't want to stop - if ((problem.getSeverity() == Severity.ERROR) && - (!problem.getMessageId().equals("SchemaError"))){ - if (problem.getCause() != null){ - throw new ServiceRuntimeException(problem.getCause()); - } else { - throw new ServiceRuntimeException(problem.toString()); - } - } - } - } - protected void addContribution(final ContributionService contributionService, final URL contributionURL) throws IOException { - String contributionURI = FileHelper.getName(contributionURL.getPath()); - if (contributionURI == null || contributionURI.length() == 0) { - contributionURI = contributionURL.toString(); + private void getComponents(Composite composite) { + for (Component c : composite.getComponents()) { + components.put(c.getName(), c); } - // Allow privileged access to load resources. Requires RuntimePermission in security - // policy. - final String finalContributionURI = contributionURI; - try { - AccessController.doPrivileged(new PrivilegedExceptionAction<Object>() { - public Object run() throws ContributionException, IOException { - contributions.add(contributionService.contribute(finalContributionURI, contributionURL, false)); - return null; - } - }); - } catch (PrivilegedActionException e) { - throw new ServiceRuntimeException(e.getException()); + for (Composite cp : composite.getIncludes()) { + getComponents(cp); } - - analyseProblems(); } @Override public void close() { - super.close(); + node.stop(); - // Stop and deactivate composites - CompositeActivator compositeActivator = runtime.getCompositeActivator(); - for (Composite composite : domainComposite.getIncludes()) { - try { - for (Component component : composite.getComponents()) { - compositeActivator.stop(component); - } - } catch (ActivationException e) { - throw new ServiceRuntimeException(e); - } - } - for (Composite composite : domainComposite.getIncludes()) { - try { - compositeActivator.deactivate(composite); - } catch (ActivationException e) { - throw new ServiceRuntimeException(e); - } - } - - // Remove the contribution from the in-memory repository - ContributionService contributionService = runtime.getContributionService(); - for (Contribution contribution : contributions) { - try { - contributionService.remove(contribution.getURI()); - } catch (ContributionException e) { - throw new ServiceRuntimeException(e); - } - } - - // Stop the runtime - try { - runtime.stop(); - } catch (ActivationException e) { - throw new ServiceRuntimeException(e); - } - } - - /** - * Determine the location of a contribution, given a contribution path and a - * list of composites. - * - * @param contributionPath - * @param composites - * @param classLoader - * @return - * @throws MalformedURLException - */ - protected URL getContributionLocation(ClassLoader classLoader, String contributionPath, String[] composites) - throws MalformedURLException { - if (contributionPath != null && contributionPath.length() > 0) { - //encode spaces as they would cause URISyntaxException - contributionPath = contributionPath.replace(" ", "%20"); - URI contributionURI = URI.create(contributionPath); - if (contributionURI.isAbsolute() || composites.length == 0) { - return new URL(contributionPath); - } - } - - String contributionArtifactPath = null; - URL contributionArtifactURL = null; - if (composites != null && composites.length > 0 && composites[0].length() > 0) { - - // Here the SCADomain was started with a reference to a composite file - contributionArtifactPath = composites[0]; - contributionArtifactURL = classLoader.getResource(contributionArtifactPath); - if (contributionArtifactURL == null) { - throw new IllegalArgumentException("Composite not found: " + contributionArtifactPath); - } - } else { - - // Here the SCADomain was started without any reference to a composite file - // We are going to look for an sca-contribution.xml or sca-contribution-generated.xml - - // Look for META-INF/sca-contribution.xml - contributionArtifactPath = Contribution.SCA_CONTRIBUTION_META; - contributionArtifactURL = classLoader.getResource(contributionArtifactPath); - - // Look for META-INF/sca-contribution-generated.xml - if (contributionArtifactURL == null) { - contributionArtifactPath = Contribution.SCA_CONTRIBUTION_GENERATED_META; - contributionArtifactURL = classLoader.getResource(contributionArtifactPath); - } - - // Look for META-INF/sca-deployables directory - if (contributionArtifactURL == null) { - contributionArtifactPath = Contribution.SCA_CONTRIBUTION_DEPLOYABLES; - contributionArtifactURL = classLoader.getResource(contributionArtifactPath); - } - } - - if (contributionArtifactURL == null) { - throw new IllegalArgumentException( - "Can't determine contribution deployables. Either specify a composite file, or use an sca-contribution.xml file to specify the deployables."); - } - - URL contributionURL = null; - // "jar:file://....../something.jar!/a/b/c/app.composite" - try { - String url = contributionArtifactURL.toExternalForm(); - String protocol = contributionArtifactURL.getProtocol(); - if ("file".equals(protocol)) { - // directory contribution - if (url.endsWith(contributionArtifactPath)) { - final String location = url.substring(0, url.lastIndexOf(contributionArtifactPath)); - // workaround from evil URL/URI form Maven - // contributionURL = FileHelper.toFile(new URL(location)).toURI().toURL(); - // Allow privileged access to open URL stream. Add FilePermission to added to - // security policy file. - try { - contributionURL = AccessController.doPrivileged(new PrivilegedExceptionAction<URL>() { - public URL run() throws IOException { - return FileHelper.toFile(new URL(location)).toURI().toURL(); - } - }); - } catch (PrivilegedActionException e) { - throw (MalformedURLException)e.getException(); - } - } - - } else if ("jar".equals(protocol)) { - // jar contribution - String location = url.substring(4, url.lastIndexOf("!/")); - // workaround for evil URL/URI from Maven - contributionURL = FileHelper.toFile(new URL(location)).toURI().toURL(); - - } else if ("wsjar".equals(protocol)) { - // See https://issues.apache.org/jira/browse/TUSCANY-2219 - // wsjar contribution - String location = url.substring(6, url.lastIndexOf("!/")); - // workaround for evil url/uri from maven - contributionURL = FileHelper.toFile(new URL(location)).toURI().toURL(); - - } else if (protocol != null && (protocol.equals("bundle") || protocol.equals("bundleresource"))) { - contributionURL = - new URL(contributionArtifactURL.getProtocol(), contributionArtifactURL.getHost(), - contributionArtifactURL.getPort(), "/"); - } - } catch (MalformedURLException mfe) { - throw new IllegalArgumentException(mfe); - } - - return contributionURL; } @Override @SuppressWarnings("unchecked") public <B, R extends CallableReference<B>> R cast(B target) throws IllegalArgumentException { - return (R)runtime.getProxyFactory().cast(target); + return (R)client.cast(target); } @Override public <B> B getService(Class<B> businessInterface, String serviceName) { - ServiceReference<B> serviceReference = getServiceReference(businessInterface, serviceName); - if (serviceReference == null) { - throw new ServiceRuntimeException("Service not found: " + serviceName); - } - return serviceReference.getService(); - } - - private <B> ServiceReference<B> createServiceReference(Class<B> businessInterface, String targetURI) { - try { - AssemblyFactory assemblyFactory = runtime.getAssemblyFactory(); - Composite composite = assemblyFactory.createComposite(); - composite.setName(new QName(Constants.SCA10_TUSCANY_NS, "default")); - RuntimeComponent component = (RuntimeComponent)assemblyFactory.createComponent(); - component.setName("default"); - component.setURI("default"); - runtime.getCompositeActivator().configureComponentContext(component); - composite.getComponents().add(component); - RuntimeComponentReference reference = (RuntimeComponentReference)assemblyFactory.createComponentReference(); - reference.setName("default"); - ModelFactoryExtensionPoint factories = - runtime.getExtensionPointRegistry().getExtensionPoint(ModelFactoryExtensionPoint.class); - JavaInterfaceFactory javaInterfaceFactory = factories.getFactory(JavaInterfaceFactory.class); - InterfaceContract interfaceContract = javaInterfaceFactory.createJavaInterfaceContract(); - interfaceContract.setInterface(javaInterfaceFactory.createJavaInterface(businessInterface)); - reference.setInterfaceContract(interfaceContract); - component.getReferences().add(reference); - reference.setComponent(component); - SCABindingFactory scaBindingFactory = factories.getFactory(SCABindingFactory.class); - SCABinding binding = scaBindingFactory.createSCABinding(); - binding.setURI(targetURI); - reference.getBindings().add(binding); - return new ServiceReferenceImpl<B>(businessInterface, component, reference, binding, runtime - .getProxyFactory(), runtime.getCompositeActivator()); - } catch (Exception e) { - throw new ServiceRuntimeException(e); - } + return client.getService(businessInterface, serviceName); } @Override public <B> ServiceReference<B> getServiceReference(Class<B> businessInterface, String name) { - - // Extract the component name - String componentName; - String serviceName; - int i = name.indexOf('/'); - if (i != -1) { - componentName = name.substring(0, i); - serviceName = name.substring(i + 1); - - } else { - componentName = name; - serviceName = null; - } - - // Lookup the component in the domain - Component component = components.get(componentName); - if (component == null) { - // The component is not local in the partition, try to create a remote service ref - return createServiceReference(businessInterface, name); - } - RuntimeComponentContext componentContext = null; - - // If the component is a composite, then we need to find the - // non-composite component that provides the requested service - if (component.getImplementation() instanceof Composite) { - for (ComponentService componentService : component.getServices()) { - if (serviceName == null || serviceName.equals(componentService.getName())) { - CompositeService compositeService = (CompositeService)componentService.getService(); - if (compositeService != null) { - if (serviceName != null) { - serviceName = "$promoted$." + component.getName() + "." + serviceName; - } - componentContext = - ((RuntimeComponent)compositeService.getPromotedComponent()).getComponentContext(); - return componentContext.createSelfReference(businessInterface, compositeService - .getPromotedService()); - } - break; - } - } - // No matching service is found - throw new ServiceRuntimeException("Composite service not found: " + name); - } else { - componentContext = ((RuntimeComponent)component).getComponentContext(); - if (serviceName != null) { - return componentContext.createSelfReference(businessInterface, serviceName); - } else { - return componentContext.createSelfReference(businessInterface); - } - } - + return client.getServiceReference(businessInterface, name); } @Override @@ -595,6 +227,8 @@ public class DefaultSCADomain extends SCADomain { } public Set<String> getComponentNames() { + return components.keySet(); + /* Set<String> componentNames = new HashSet<String>(); for (Contribution contribution : contributions) { for (Artifact artifact : contribution.getArtifacts()) { @@ -606,9 +240,12 @@ public class DefaultSCADomain extends SCADomain { } } return componentNames; + */ } public Component getComponent(String componentName) { + return components.get(componentName); + /* for (Contribution contribution : contributions) { for (Artifact artifact : contribution.getArtifacts()) { if (artifact.getModel() instanceof Composite) { @@ -621,6 +258,7 @@ public class DefaultSCADomain extends SCADomain { } } return null; + */ } public void startComponent(String componentName) throws ActivationException { @@ -628,7 +266,6 @@ public class DefaultSCADomain extends SCADomain { if (component == null) { throw new IllegalArgumentException("no component: " + componentName); } - CompositeActivator compositeActivator = runtime.getCompositeActivator(); compositeActivator.start(component); } @@ -637,7 +274,6 @@ public class DefaultSCADomain extends SCADomain { if (component == null) { throw new IllegalArgumentException("no component: " + componentName); } - CompositeActivator compositeActivator = runtime.getCompositeActivator(); compositeActivator.stop(component); } } diff --git a/java/sca/modules/host-embedded/src/main/java/org/apache/tuscany/sca/host/embedded/impl/EmbeddedSCADomain.java b/java/sca/modules/host-embedded/src/main/java/org/apache/tuscany/sca/host/embedded/impl/EmbeddedSCADomain.java index 45daf57399..5ac2ae6498 100644 --- a/java/sca/modules/host-embedded/src/main/java/org/apache/tuscany/sca/host/embedded/impl/EmbeddedSCADomain.java +++ b/java/sca/modules/host-embedded/src/main/java/org/apache/tuscany/sca/host/embedded/impl/EmbeddedSCADomain.java @@ -40,6 +40,7 @@ import org.apache.tuscany.sca.host.embedded.SCADomain; import org.apache.tuscany.sca.host.embedded.management.ComponentManager; import org.apache.tuscany.sca.interfacedef.InterfaceContract; import org.apache.tuscany.sca.interfacedef.java.JavaInterfaceFactory; +import org.apache.tuscany.sca.node.impl.RuntimeBootStrapper; import org.apache.tuscany.sca.runtime.RuntimeComponent; import org.apache.tuscany.sca.runtime.RuntimeComponentContext; import org.apache.tuscany.sca.runtime.RuntimeComponentReference; @@ -56,7 +57,7 @@ public class EmbeddedSCADomain extends SCADomain { private String uri; private Composite domainComposite; - private ReallySmallRuntime runtime; + private RuntimeBootStrapper runtime; private ComponentManagerImpl componentManager = new ComponentManagerImpl(this); /** @@ -70,7 +71,7 @@ public class EmbeddedSCADomain extends SCADomain { this.uri = domainURI; // Create a runtime - runtime = new ReallySmallRuntime(runtimeClassLoader); + runtime = new RuntimeBootStrapper(runtimeClassLoader); } public void start() throws ActivationException { diff --git a/java/sca/modules/host-embedded/src/main/java/org/apache/tuscany/sca/host/embedded/impl/ReallySmallRuntime.java b/java/sca/modules/host-embedded/src/main/java/org/apache/tuscany/sca/host/embedded/impl/ReallySmallRuntime.java index 47dd741d75..1f6dc4260d 100644 --- a/java/sca/modules/host-embedded/src/main/java/org/apache/tuscany/sca/host/embedded/impl/ReallySmallRuntime.java +++ b/java/sca/modules/host-embedded/src/main/java/org/apache/tuscany/sca/host/embedded/impl/ReallySmallRuntime.java @@ -33,7 +33,6 @@ import org.apache.tuscany.sca.assembly.EndpointFactory; import org.apache.tuscany.sca.assembly.SCABindingFactory; import org.apache.tuscany.sca.assembly.builder.CompositeBuilder; import org.apache.tuscany.sca.assembly.builder.CompositeBuilderException; -import org.apache.tuscany.sca.assembly.builder.DomainBuilder; import org.apache.tuscany.sca.contribution.ContributionFactory; import org.apache.tuscany.sca.contribution.ModelFactoryExtensionPoint; import org.apache.tuscany.sca.contribution.processor.URLArtifactProcessor; @@ -87,7 +86,7 @@ public class ReallySmallRuntime { private ContributionService contributionService; private CompositeActivator compositeActivator; private CompositeBuilder compositeBuilder; - private DomainBuilder domainBuilder; + // private DomainBuilder domainBuilder; private WorkScheduler workScheduler; private ScopeRegistry scopeRegistry; private ProxyFactory proxyFactory; diff --git a/java/sca/modules/host-embedded/src/main/java/org/apache/tuscany/sca/host/embedded/impl/ReallySmallRuntimeBuilder.java b/java/sca/modules/host-embedded/src/main/java/org/apache/tuscany/sca/host/embedded/impl/ReallySmallRuntimeBuilder.java index 3bbb5fcf34..b85b94f850 100644 --- a/java/sca/modules/host-embedded/src/main/java/org/apache/tuscany/sca/host/embedded/impl/ReallySmallRuntimeBuilder.java +++ b/java/sca/modules/host-embedded/src/main/java/org/apache/tuscany/sca/host/embedded/impl/ReallySmallRuntimeBuilder.java @@ -23,7 +23,6 @@ import java.io.IOException; import java.security.AccessController; import java.security.PrivilegedAction; import java.util.List; -import java.util.logging.Logger; import javax.xml.stream.XMLInputFactory; import javax.xml.stream.XMLOutputFactory; @@ -33,7 +32,6 @@ import org.apache.tuscany.sca.assembly.Composite; import org.apache.tuscany.sca.assembly.EndpointFactory; import org.apache.tuscany.sca.assembly.SCABindingFactory; import org.apache.tuscany.sca.assembly.builder.CompositeBuilder; -import org.apache.tuscany.sca.assembly.builder.DomainBuilder; import org.apache.tuscany.sca.assembly.builder.impl.CompositeBuilderImpl; import org.apache.tuscany.sca.assembly.xml.CompositeDocumentProcessor; import org.apache.tuscany.sca.context.ContextFactoryExtensionPoint; @@ -66,7 +64,6 @@ import org.apache.tuscany.sca.core.assembly.ActivationException; import org.apache.tuscany.sca.core.assembly.CompositeActivator; import org.apache.tuscany.sca.core.assembly.CompositeActivatorImpl; import org.apache.tuscany.sca.core.conversation.ConversationManager; -import org.apache.tuscany.sca.core.conversation.ConversationManagerImpl; import org.apache.tuscany.sca.core.invocation.ExtensibleWireProcessor; import org.apache.tuscany.sca.core.invocation.ProxyFactory; import org.apache.tuscany.sca.core.scope.CompositeScopeContainerFactory; @@ -95,7 +92,7 @@ import org.apache.tuscany.sca.work.WorkScheduler; */ public class ReallySmallRuntimeBuilder { - private static final Logger logger = Logger.getLogger(ReallySmallRuntimeBuilder.class.getName()); + // private static final Logger logger = Logger.getLogger(ReallySmallRuntimeBuilder.class.getName()); public static CompositeActivator createCompositeActivator(ExtensionPointRegistry registry, AssemblyFactory assemblyFactory, diff --git a/java/sca/modules/host-embedded/src/test/java/org/apache/tuscany/sca/host/embedded/impl/DefaultSCADomainTestCase.java b/java/sca/modules/host-embedded/src/test/java/org/apache/tuscany/sca/host/embedded/impl/DefaultSCADomainTestCase.java index 087a75426b..fd5398c8e6 100644 --- a/java/sca/modules/host-embedded/src/test/java/org/apache/tuscany/sca/host/embedded/impl/DefaultSCADomainTestCase.java +++ b/java/sca/modules/host-embedded/src/test/java/org/apache/tuscany/sca/host/embedded/impl/DefaultSCADomainTestCase.java @@ -36,7 +36,7 @@ public class DefaultSCADomainTestCase extends TestCase { @Override protected void setUp() throws Exception { domain = new DefaultSCADomain(getClass().getClassLoader(), getClass().getClassLoader(), - "http://localhost", ".", "test.composite"); + "http://localhost", "./target/test-classes", "test.composite"); } public void testStart() throws Exception { diff --git a/java/sca/modules/implementation-node/pom.xml b/java/sca/modules/implementation-node/pom.xml index a14e2020c7..2027eb7e30 100644 --- a/java/sca/modules/implementation-node/pom.xml +++ b/java/sca/modules/implementation-node/pom.xml @@ -50,7 +50,14 @@ <dependency> <groupId>org.apache.tuscany.sca</groupId> - <artifactId>tuscany-host-embedded</artifactId> + <artifactId>tuscany-binding-sca</artifactId> + <version>1.4-SNAPSHOT</version> + <scope>test</scope> + </dependency> + + <dependency> + <groupId>org.apache.tuscany.sca</groupId> + <artifactId>tuscany-binding-sca-xml</artifactId> <version>1.4-SNAPSHOT</version> <scope>test</scope> </dependency> diff --git a/java/sca/modules/node2-impl/pom.xml b/java/sca/modules/node2-impl/pom.xml index 62846f08b2..4f6f73d9be 100644 --- a/java/sca/modules/node2-impl/pom.xml +++ b/java/sca/modules/node2-impl/pom.xml @@ -58,17 +58,78 @@ <dependency> <groupId>org.apache.tuscany.sca</groupId> - <artifactId>tuscany-host-embedded</artifactId> + <artifactId>tuscany-core</artifactId> <version>1.4-SNAPSHOT</version> </dependency> <dependency> <groupId>org.apache.tuscany.sca</groupId> + <artifactId>tuscany-contribution-impl</artifactId> + <version>1.4-SNAPSHOT</version> + </dependency> + + <dependency> + <groupId>org.apache.tuscany.sca</groupId> <artifactId>tuscany-assembly-xml</artifactId> <version>1.4-SNAPSHOT</version> + </dependency> + + <dependency> + <groupId>org.apache.tuscany.sca</groupId> + <artifactId>tuscany-definitions-xml</artifactId> + <version>1.4-SNAPSHOT</version> + <scope>runtime</scope> + </dependency> + + <dependency> + <groupId>org.apache.tuscany.sca</groupId> + <artifactId>tuscany-binding-sca</artifactId> + <version>1.4-SNAPSHOT</version> + <scope>runtime</scope> + </dependency> + + <dependency> + <groupId>org.apache.tuscany.sca</groupId> + <artifactId>tuscany-binding-sca-xml</artifactId> + <version>1.4-SNAPSHOT</version> + <scope>runtime</scope> + </dependency> + + <dependency> + <groupId>org.apache.tuscany.sca</groupId> + <artifactId>tuscany-policy-xml</artifactId> + <version>1.4-SNAPSHOT</version> + <scope>runtime</scope> + </dependency> + + <dependency> + <groupId>org.apache.tuscany.sca</groupId> + <artifactId>tuscany-core-databinding</artifactId> + <version>1.4-SNAPSHOT</version> + <scope>runtime</scope> + </dependency> + + <dependency> + <groupId>org.apache.tuscany.sca</groupId> + <artifactId>tuscany-assembly-xsd</artifactId> + <version>1.4-SNAPSHOT</version> <scope>runtime</scope> </dependency> + <dependency> + <groupId>org.apache.tuscany.sca</groupId> + <artifactId>tuscany-endpoint</artifactId> + <version>1.4-SNAPSHOT</version> + <scope>runtime</scope> + </dependency> + + <dependency> + <groupId>org.apache.tuscany.sca</groupId> + <artifactId>tuscany-implementation-java-runtime</artifactId> + <version>1.4-SNAPSHOT</version> + <scope>test</scope> + </dependency> + </dependencies> diff --git a/java/sca/modules/node2-impl/src/main/java/org/apache/tuscany/sca/node/impl/NodeImpl.java b/java/sca/modules/node2-impl/src/main/java/org/apache/tuscany/sca/node/impl/NodeImpl.java index 76cc89f59e..fc16257bff 100644 --- a/java/sca/modules/node2-impl/src/main/java/org/apache/tuscany/sca/node/impl/NodeImpl.java +++ b/java/sca/modules/node2-impl/src/main/java/org/apache/tuscany/sca/node/impl/NodeImpl.java @@ -19,10 +19,10 @@ package org.apache.tuscany.sca.node.impl; +import java.io.ByteArrayInputStream; import java.io.File; import java.io.IOException; import java.io.InputStream; -import java.io.StringReader; import java.net.MalformedURLException; import java.net.URI; import java.net.URL; @@ -47,6 +47,7 @@ import org.apache.tuscany.sca.assembly.Component; import org.apache.tuscany.sca.assembly.ComponentService; import org.apache.tuscany.sca.assembly.Composite; import org.apache.tuscany.sca.assembly.CompositeService; +import org.apache.tuscany.sca.assembly.xml.CompositeDocumentProcessor; import org.apache.tuscany.sca.contribution.Artifact; import org.apache.tuscany.sca.contribution.Contribution; import org.apache.tuscany.sca.contribution.ContributionFactory; @@ -54,6 +55,8 @@ import org.apache.tuscany.sca.contribution.ContributionMetadata; import org.apache.tuscany.sca.contribution.ModelFactoryExtensionPoint; import org.apache.tuscany.sca.contribution.processor.StAXArtifactProcessor; import org.apache.tuscany.sca.contribution.processor.StAXArtifactProcessorExtensionPoint; +import org.apache.tuscany.sca.contribution.processor.URLArtifactProcessor; +import org.apache.tuscany.sca.contribution.processor.URLArtifactProcessorExtensionPoint; import org.apache.tuscany.sca.contribution.resolver.ModelResolver; import org.apache.tuscany.sca.contribution.service.ContributionService; import org.apache.tuscany.sca.contribution.service.util.FileHelper; @@ -61,7 +64,6 @@ import org.apache.tuscany.sca.core.ExtensionPointRegistry; import org.apache.tuscany.sca.core.UtilityExtensionPoint; import org.apache.tuscany.sca.core.assembly.ActivationException; import org.apache.tuscany.sca.core.assembly.CompositeActivator; -import org.apache.tuscany.sca.host.embedded.impl.ReallySmallRuntime; import org.apache.tuscany.sca.implementation.node.ConfiguredNodeImplementation; import org.apache.tuscany.sca.implementation.node.NodeImplementationFactory; import org.apache.tuscany.sca.monitor.Monitor; @@ -90,13 +92,15 @@ public class NodeImpl implements SCANode2, SCAClient { private String configurationName; // The Tuscany runtime that does the hard work - private ReallySmallRuntime runtime; + private RuntimeBootStrapper runtime; private CompositeActivator compositeActivator; private XMLInputFactory inputFactory; private ModelFactoryExtensionPoint modelFactories; private StAXArtifactProcessorExtensionPoint artifactProcessors; + private URLArtifactProcessorExtensionPoint documentProcessors; private Monitor monitor; + private List<Contribution> contributions; private ContributionMetadata metadata; // The composite loaded into this node @@ -147,6 +151,12 @@ public class NodeImpl implements SCANode2, SCAClient { configurationName = compositeURI; logger.log(Level.INFO, "Creating node: " + configurationName); + if (compositeURI != null) { + URI uri = URI.create(compositeURI); + if (uri.isAbsolute()) { + throw new IllegalArgumentException("Composite URI must be a resource name: " + compositeURI); + } + } try { // Initialize the runtime initRuntime(); @@ -174,7 +184,8 @@ public class NodeImpl implements SCANode2, SCAClient { if (contributionArtifactURL == null) { throw new IllegalArgumentException("Composite not found: " + contributionArtifactPath); } - composite = createComposite(contributionArtifactURL.toString()); + // Set to relative URI to avoid duplicate loading + Composite composite = createComposite(compositeURI); config.setComposite(composite); } else { @@ -199,12 +210,15 @@ public class NodeImpl implements SCANode2, SCAClient { StAXArtifactProcessor<ContributionMetadata> processor = artifactProcessors.getProcessor(ContributionMetadata.class); XMLStreamReader reader = inputFactory.createXMLStreamReader(contributionArtifactURL.openStream()); + reader.nextTag(); metadata = processor.read(reader); reader.close(); if (metadata.getDeployables().isEmpty()) { throw new IllegalArgumentException( "No deployable composite is declared in " + contributionArtifactPath); } + Composite composite = metadata.getDeployables().get(0); + config.setComposite(composite); } } @@ -229,7 +243,7 @@ public class NodeImpl implements SCANode2, SCAClient { return c; } - private URL getContributionURL(URL contributionArtifactURL, String contributionArtifactPath) { + public static URL getContributionURL(URL contributionArtifactURL, String contributionArtifactPath) { URL contributionURL = null; // "jar:file://....../something.jar!/a/b/c/app.composite" try { @@ -300,8 +314,12 @@ public class NodeImpl implements SCANode2, SCAClient { // Initialize the runtime initRuntime(); + URI uri = URI.create(compositeURI); ConfiguredNodeImplementation configuration = null; if (contributions == null || contributions.length == 0) { + if (uri.getScheme() != null) { + throw new IllegalArgumentException("No SCA contributions are provided"); + } configuration = findNodeConfiguration(compositeURI, null); } else { @@ -358,16 +376,23 @@ public class NodeImpl implements SCANode2, SCAClient { configuration = findNodeConfiguration(compositeURI, null); } else { // Create a node configuration - NodeImplementationFactory nodeImplementationFactory = modelFactories.getFactory(NodeImplementationFactory.class); + NodeImplementationFactory nodeImplementationFactory = + modelFactories.getFactory(NodeImplementationFactory.class); configuration = nodeImplementationFactory.createConfiguredNodeImplementation(); // Read the composite model StAXArtifactProcessor<Composite> compositeProcessor = artifactProcessors.getProcessor(Composite.class); - URL compositeURL = new URL(compositeURI); - logger.log(Level.INFO, "Loading composite: " + compositeURL); - XMLStreamReader reader = inputFactory.createXMLStreamReader(new StringReader(compositeContent)); - Composite composite = compositeProcessor.read(reader); - reader.close(); + // URL compositeURL = new URL(compositeURI); + logger.log(Level.INFO, "Loading composite: " + compositeURI); + + CompositeDocumentProcessor compositeDocProcessor = + (CompositeDocumentProcessor)documentProcessors.getProcessor(Composite.class); + composite = + compositeDocProcessor.read(URI.create(compositeURI), new ByteArrayInputStream(compositeContent + .getBytes("UTF-8"))); + + analyzeProblems(); + configuration.setComposite(composite); // Create contribution models @@ -402,7 +427,7 @@ public class NodeImpl implements SCANode2, SCAClient { private void initRuntime() throws Exception { // Create a node runtime - runtime = new ReallySmallRuntime(Thread.currentThread().getContextClassLoader()); + runtime = new RuntimeBootStrapper(Thread.currentThread().getContextClassLoader()); runtime.start(); // Get the various factories we need @@ -413,6 +438,8 @@ public class NodeImpl implements SCANode2, SCAClient { // Create the required artifact processors artifactProcessors = registry.getExtensionPoint(StAXArtifactProcessorExtensionPoint.class); + documentProcessors = registry.getExtensionPoint(URLArtifactProcessorExtensionPoint.class); + // Save the composite activator compositeActivator = runtime.getCompositeActivator(); @@ -421,7 +448,7 @@ public class NodeImpl implements SCANode2, SCAClient { MonitorFactory monitorFactory = utilities.getUtility(MonitorFactory.class); monitor = monitorFactory.createMonitor(); } - + /** * Escape the space in URL string * @param uri @@ -441,7 +468,7 @@ public class NodeImpl implements SCANode2, SCAClient { // Load the specified contributions ContributionService contributionService = runtime.getContributionService(); - List<Contribution> contributions = new ArrayList<Contribution>(); + contributions = new ArrayList<Contribution>(); for (Contribution contribution : configuration.getContributions()) { URI uri = createURI(contribution.getLocation()); if (uri.getScheme() == null) { @@ -468,9 +495,12 @@ public class NodeImpl implements SCANode2, SCAClient { contributions.add(contributionService.contribute(contribution.getURI(), contributionURL, false)); analyzeProblems(); } - + + composite = configuration.getComposite(); + // Resolve the metadata within the context of the contribution - //FIXME This doesn't seem to make sense here + // FIXME The deployable composite should have been picked by the SCA Domain Manager already and we should not + // pollute the Node impl to deal with this if (metadata != null) { StAXArtifactProcessor<ContributionMetadata> processor = artifactProcessors.getProcessor(ContributionMetadata.class); @@ -481,55 +511,65 @@ public class NodeImpl implements SCANode2, SCAClient { } } List<Composite> composites = metadata.getDeployables(); - configuration.setComposite(composites.get(0)); - } - - // Load the specified composite - Contribution contribution; - URL compositeURL; - - URI uri = createURI(configuration.getComposite().getURI()); - if (uri.getScheme() == null) { - - // If the composite URI is a relative URI, try to resolve it within the contributions - contribution = contribution(contributions, uri.toString()); - if (contribution == null) { - throw new IllegalArgumentException("Composite is not found in contributions: " + uri); - } - compositeURL = new URL(location(contribution, uri.toString())); - - } else { - - // If the composite URI is an absolute URI, use it as is - compositeURL = uri.toURL(); - - // And resolve the composite within the scope of the last contribution - if (contributions.size() != 0) { - contribution = contributions.get(contributions.size() -1); + if (composites.size() == 0) { + throw new IllegalArgumentException("No deployable composite is declared"); + } else if (composites.size() == 1) { + composite = composites.get(0); } else { - contribution = null; + // This is temporary to include all composites + AssemblyFactory assemblyFactory = runtime.getAssemblyFactory(); + Composite tempComposite = assemblyFactory.createComposite(); + tempComposite.setName(new QName("http://tempuri.org", "aggregated")); + tempComposite.setURI("http://tempuri.org/aggregated"); + tempComposite.getIncludes().addAll(composites); + composite = tempComposite; } + configuration.setComposite(composite); } - - // Read the composite - StAXArtifactProcessor<Composite> compositeProcessor = artifactProcessors.getProcessor(Composite.class); - composite = configuration.getComposite(); + + Contribution contribution = null; if (composite.getName() == null) { + // Load the specified composite + URL compositeURL; + + URI uri = createURI(configuration.getComposite().getURI()); + if (uri.getScheme() == null) { + + // If the composite URI is a relative URI, try to resolve it within the contributions + contribution = contribution(contributions, uri.toString()); + if (contribution == null) { + throw new IllegalArgumentException("Composite is not found in contributions: " + uri); + } + compositeURL = new URL(location(contribution, uri.toString())); + + } else { + + // If the composite URI is an absolute URI, use it as is + compositeURL = uri.toURL(); + } + + URLArtifactProcessor<Composite> compositeDocProcessor = documentProcessors.getProcessor(Composite.class); + // Read the composite logger.log(Level.INFO, "Loading composite: " + compositeURL); - InputStream is = compositeURL.openStream(); - XMLStreamReader reader = inputFactory.createXMLStreamReader(is); - composite = compositeProcessor.read(reader); - reader.close(); + // InputStream is = compositeURL.openStream(); + // XMLStreamReader reader = inputFactory.createXMLStreamReader(is); + composite = compositeDocProcessor.read(null, uri, compositeURL); + // reader.close(); analyzeProblems(); + + } + // And resolve the composite within the scope of the last contribution + if (contribution == null && contributions.size() != 0) { + contribution = contributions.get(contributions.size() - 1); } // Resolve the given composite within the scope of the selected contribution if (contribution != null) { + StAXArtifactProcessor<Composite> compositeProcessor = artifactProcessors.getProcessor(Composite.class); compositeProcessor.resolve(composite, contribution.getModelResolver()); analyzeProblems(); } - // Create a top level composite to host our composite // This is temporary to make the activator happy AssemblyFactory assemblyFactory = runtime.getAssemblyFactory(); @@ -550,7 +590,7 @@ public class NodeImpl implements SCANode2, SCAClient { analyzeProblems(); } - + /** * Returns the artifact representing the given composite. * @@ -640,6 +680,7 @@ public class NodeImpl implements SCANode2, SCAClient { // Deactivate the composite compositeActivator.deactivate(composite); + runtime.stop(); } catch (ActivationException e) { throw new ServiceRuntimeException(e); } @@ -774,4 +815,9 @@ public class NodeImpl implements SCANode2, SCAClient { // Collect JARs from the parent ClassLoader collectJARs(urls, cl.getParent()); } + + public CompositeActivator getCompositeActivator() { + return compositeActivator; + } + } diff --git a/java/sca/modules/node2-impl/src/main/java/org/apache/tuscany/sca/node/impl/RuntimeBootStrapper.java b/java/sca/modules/node2-impl/src/main/java/org/apache/tuscany/sca/node/impl/RuntimeBootStrapper.java new file mode 100644 index 0000000000..c9995d6024 --- /dev/null +++ b/java/sca/modules/node2-impl/src/main/java/org/apache/tuscany/sca/node/impl/RuntimeBootStrapper.java @@ -0,0 +1,385 @@ +/* + * 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.tuscany.sca.node.impl; + +import java.io.IOException; +import java.util.ArrayList; +import java.util.HashSet; +import java.util.List; +import java.util.Set; +import java.util.logging.Level; +import java.util.logging.Logger; + +import org.apache.tuscany.sca.assembly.AssemblyFactory; +import org.apache.tuscany.sca.assembly.Composite; +import org.apache.tuscany.sca.assembly.EndpointFactory; +import org.apache.tuscany.sca.assembly.SCABindingFactory; +import org.apache.tuscany.sca.assembly.builder.CompositeBuilder; +import org.apache.tuscany.sca.assembly.builder.CompositeBuilderException; +import org.apache.tuscany.sca.contribution.ContributionFactory; +import org.apache.tuscany.sca.contribution.ModelFactoryExtensionPoint; +import org.apache.tuscany.sca.contribution.processor.URLArtifactProcessor; +import org.apache.tuscany.sca.contribution.processor.URLArtifactProcessorExtensionPoint; +import org.apache.tuscany.sca.contribution.resolver.DefaultModelResolver; +import org.apache.tuscany.sca.contribution.resolver.ModelResolver; +import org.apache.tuscany.sca.contribution.service.ContributionService; +import org.apache.tuscany.sca.core.DefaultExtensionPointRegistry; +import org.apache.tuscany.sca.core.ExtensionPointRegistry; +import org.apache.tuscany.sca.core.ModuleActivator; +import org.apache.tuscany.sca.core.UtilityExtensionPoint; +import org.apache.tuscany.sca.core.assembly.ActivationException; +import org.apache.tuscany.sca.core.assembly.CompositeActivator; +import org.apache.tuscany.sca.core.assembly.RuntimeAssemblyFactory; +import org.apache.tuscany.sca.core.invocation.ExtensibleProxyFactory; +import org.apache.tuscany.sca.core.invocation.ProxyFactory; +import org.apache.tuscany.sca.core.invocation.ProxyFactoryExtensionPoint; +import org.apache.tuscany.sca.core.scope.ScopeRegistry; +import org.apache.tuscany.sca.definitions.SCADefinitions; +import org.apache.tuscany.sca.definitions.impl.SCADefinitionsImpl; +import org.apache.tuscany.sca.definitions.util.SCADefinitionsUtil; +import org.apache.tuscany.sca.extensibility.ServiceDeclaration; +import org.apache.tuscany.sca.extensibility.ServiceDiscovery; +import org.apache.tuscany.sca.interfacedef.InterfaceContractMapper; +import org.apache.tuscany.sca.invocation.MessageFactory; +import org.apache.tuscany.sca.monitor.Monitor; +import org.apache.tuscany.sca.monitor.MonitorFactory; +import org.apache.tuscany.sca.monitor.impl.DefaultMonitorFactoryImpl; +import org.apache.tuscany.sca.policy.DefaultIntentAttachPointTypeFactory; +import org.apache.tuscany.sca.policy.DefaultPolicyFactory; +import org.apache.tuscany.sca.policy.Intent; +import org.apache.tuscany.sca.policy.IntentAttachPointType; +import org.apache.tuscany.sca.policy.IntentAttachPointTypeFactory; +import org.apache.tuscany.sca.policy.PolicyFactory; +import org.apache.tuscany.sca.policy.PolicySet; +import org.apache.tuscany.sca.provider.SCADefinitionsProvider; +import org.apache.tuscany.sca.provider.SCADefinitionsProviderExtensionPoint; +import org.apache.tuscany.sca.work.WorkScheduler; + +/** + * + * @version $Rev$ $Date$ + */ +public class RuntimeBootStrapper { + private static final Logger logger = Logger.getLogger(RuntimeBootStrapper.class.getName()); + private List<ModuleActivator> modules; + private ExtensionPointRegistry registry; + + private ClassLoader classLoader; + private AssemblyFactory assemblyFactory; + private ContributionService contributionService; + private CompositeActivator compositeActivator; + private CompositeBuilder compositeBuilder; + // private DomainBuilder domainBuilder; + private WorkScheduler workScheduler; + private ScopeRegistry scopeRegistry; + private ProxyFactory proxyFactory; + private List<SCADefinitions> policyDefinitions; + private ModelResolver policyDefinitionsResolver; + private Monitor monitor; + + public RuntimeBootStrapper(ClassLoader classLoader) { + this.classLoader = classLoader; + } + + public void start() throws ActivationException { + long start = System.currentTimeMillis(); + + // Create our extension point registry + registry = new DefaultExtensionPointRegistry(); + UtilityExtensionPoint utilities = registry.getExtensionPoint(UtilityExtensionPoint.class); + + // Get work scheduler + workScheduler = utilities.getUtility(WorkScheduler.class); + + // Create an interface contract mapper + InterfaceContractMapper mapper = utilities.getUtility(InterfaceContractMapper.class); + + // Get factory extension point + ModelFactoryExtensionPoint factories = registry.getExtensionPoint(ModelFactoryExtensionPoint.class); + + // Get Message factory + MessageFactory messageFactory = factories.getFactory(MessageFactory.class); + + // Get proxy factory + ProxyFactoryExtensionPoint proxyFactories = registry.getExtensionPoint(ProxyFactoryExtensionPoint.class); + proxyFactory = new ExtensibleProxyFactory(proxyFactories); + + // Create model factories + assemblyFactory = new RuntimeAssemblyFactory(); + factories.addFactory(assemblyFactory); + PolicyFactory policyFactory = new DefaultPolicyFactory(); + factories.addFactory(policyFactory); + + // Load the runtime modules + modules = loadModules(registry); + + // Start the runtime modules + startModules(registry, modules); + + SCABindingFactory scaBindingFactory = factories.getFactory(SCABindingFactory.class); + IntentAttachPointTypeFactory intentAttachPointTypeFactory = new DefaultIntentAttachPointTypeFactory(); + factories.addFactory(intentAttachPointTypeFactory); + ContributionFactory contributionFactory = factories.getFactory(ContributionFactory.class); + + // Create a monitor + MonitorFactory monitorFactory = utilities.getUtility(MonitorFactory.class); + + if (monitorFactory != null) { + monitor = monitorFactory.createMonitor(); + } else { + monitorFactory = new DefaultMonitorFactoryImpl(); + monitor = monitorFactory.createMonitor(); + utilities.addUtility(monitorFactory); + //logger.fine("No MonitorFactory is found on the classpath."); + } + + // Create a contribution service + policyDefinitions = new ArrayList<SCADefinitions>(); + policyDefinitionsResolver = new DefaultModelResolver(); + contributionService = + RuntimeBuilder.createContributionService(classLoader, + registry, + contributionFactory, + assemblyFactory, + policyFactory, + mapper, + policyDefinitions, + policyDefinitionsResolver, + monitor); + + // Create the ScopeRegistry + scopeRegistry = RuntimeBuilder.createScopeRegistry(registry); + + // Create a composite activator + compositeActivator = + RuntimeBuilder.createCompositeActivator(registry, + assemblyFactory, + messageFactory, + scaBindingFactory, + mapper, + proxyFactory, + scopeRegistry, + workScheduler); + + // Load the definitions.xml + loadSCADefinitions(); + + if (logger.isLoggable(Level.FINE)) { + long end = System.currentTimeMillis(); + logger.fine("The tuscany runtime is started in " + (end - start) + " ms."); + } + } + + public void stop() throws ActivationException { + long start = System.currentTimeMillis(); + + // Stop the runtime modules + stopModules(registry, modules); + + // Stop and destroy the work manager + workScheduler.destroy(); + + // Cleanup + modules = null; + registry = null; + assemblyFactory = null; + contributionService = null; + compositeActivator = null; + workScheduler = null; + scopeRegistry = null; + + if (logger.isLoggable(Level.FINE)) { + long end = System.currentTimeMillis(); + logger.fine("The tuscany runtime is stopped in " + (end - start) + " ms."); + } + } + + public void buildComposite(Composite composite) throws CompositeBuilderException { + //Get factory extension point + ModelFactoryExtensionPoint factories = registry.getExtensionPoint(ModelFactoryExtensionPoint.class); + SCABindingFactory scaBindingFactory = factories.getFactory(SCABindingFactory.class); + IntentAttachPointTypeFactory intentAttachPointTypeFactory = + factories.getFactory(IntentAttachPointTypeFactory.class); + EndpointFactory endpointFactory = factories.getFactory(EndpointFactory.class); + UtilityExtensionPoint utilities = registry.getExtensionPoint(UtilityExtensionPoint.class); + InterfaceContractMapper mapper = utilities.getUtility(InterfaceContractMapper.class); + + //Create a composite builder + SCADefinitions aggregatedDefinitions = new SCADefinitionsImpl(); + for (SCADefinitions definition : ((List<SCADefinitions>)policyDefinitions)) { + SCADefinitionsUtil.aggregateSCADefinitions(definition, aggregatedDefinitions); + } + compositeBuilder = + RuntimeBuilder.createCompositeBuilder(monitor, + assemblyFactory, + scaBindingFactory, + endpointFactory, + intentAttachPointTypeFactory, + mapper, + aggregatedDefinitions); + compositeBuilder.build(composite); + + } + + public ContributionService getContributionService() { + return contributionService; + } + + public CompositeActivator getCompositeActivator() { + return compositeActivator; + } + + public CompositeBuilder getCompositeBuilder() { + return compositeBuilder; + } + + public AssemblyFactory getAssemblyFactory() { + return assemblyFactory; + } + + private void loadSCADefinitions() throws ActivationException { + try { + URLArtifactProcessorExtensionPoint documentProcessors = + registry.getExtensionPoint(URLArtifactProcessorExtensionPoint.class); + URLArtifactProcessor<SCADefinitions> definitionsProcessor = + documentProcessors.getProcessor(SCADefinitions.class); + SCADefinitionsProviderExtensionPoint scaDefnProviders = + registry.getExtensionPoint(SCADefinitionsProviderExtensionPoint.class); + + SCADefinitions systemSCADefinitions = new SCADefinitionsImpl(); + SCADefinitions aSCADefn = null; + for (SCADefinitionsProvider aProvider : scaDefnProviders.getSCADefinitionsProviders()) { + aSCADefn = aProvider.getSCADefinition(); + SCADefinitionsUtil.aggregateSCADefinitions(aSCADefn, systemSCADefinitions); + } + + policyDefinitions.add(systemSCADefinitions); + + //we cannot expect that providers will add the intents and policysets into the resolver + //so we do this here explicitly + for (Intent intent : systemSCADefinitions.getPolicyIntents()) { + policyDefinitionsResolver.addModel(intent); + } + + for (PolicySet policySet : systemSCADefinitions.getPolicySets()) { + policyDefinitionsResolver.addModel(policySet); + } + + for (IntentAttachPointType attachPoinType : systemSCADefinitions.getBindingTypes()) { + policyDefinitionsResolver.addModel(attachPoinType); + } + + for (IntentAttachPointType attachPoinType : systemSCADefinitions.getImplementationTypes()) { + policyDefinitionsResolver.addModel(attachPoinType); + } + + //now that all system sca definitions have been read, lets resolve them right away + definitionsProcessor.resolve(systemSCADefinitions, policyDefinitionsResolver); + } catch (Exception e) { + throw new ActivationException(e); + } + } + + private List<ModuleActivator> loadModules(ExtensionPointRegistry registry) throws ActivationException { + + // Load and instantiate the modules found on the classpath (or any registered ClassLoaders) + modules = new ArrayList<ModuleActivator>(); + try { + Set<ServiceDeclaration> moduleActivators = + ServiceDiscovery.getInstance().getServiceDeclarations(ModuleActivator.class); + Set<String> moduleClasses = new HashSet<String>(); + for (ServiceDeclaration moduleDeclarator : moduleActivators) { + if (moduleClasses.contains(moduleDeclarator.getClassName())) { + continue; + } + moduleClasses.add(moduleDeclarator.getClassName()); + Class<?> moduleClass = moduleDeclarator.loadClass(); + ModuleActivator module = (ModuleActivator)moduleClass.newInstance(); + modules.add(module); + } + } catch (IOException e) { + throw new ActivationException(e); + } catch (ClassNotFoundException e) { + throw new ActivationException(e); + } catch (InstantiationException e) { + throw new ActivationException(e); + } catch (IllegalAccessException e) { + throw new ActivationException(e); + } + + return modules; + } + + private void startModules(ExtensionPointRegistry registry, List<ModuleActivator> modules) + throws ActivationException { + boolean debug = logger.isLoggable(Level.FINE); + // Start all the extension modules + for (ModuleActivator module : modules) { + long start = 0L; + if (debug) { + logger.fine(module.getClass().getName() + " is starting."); + start = System.currentTimeMillis(); + } + try { + module.start(registry); + if (debug) { + long end = System.currentTimeMillis(); + logger.fine(module.getClass().getName() + " is started in " + (end - start) + " ms."); + } + } catch (Throwable e) { + logger.log(Level.WARNING, "Exception starting module " + module.getClass().getName() + + " :" + + e.getMessage()); + logger.log(Level.FINE, "Exception starting module " + module.getClass().getName(), e); + } + } + } + + private void stopModules(final ExtensionPointRegistry registry, List<ModuleActivator> modules) { + boolean debug = logger.isLoggable(Level.FINE); + for (ModuleActivator module : modules) { + long start = 0L; + if (debug) { + logger.fine(module.getClass().getName() + " is stopping."); + start = System.currentTimeMillis(); + } + module.stop(registry); + if (debug) { + long end = System.currentTimeMillis(); + logger.fine(module.getClass().getName() + " is stopped in " + (end - start) + " ms."); + } + } + } + + /** + * @return the proxyFactory + */ + public ProxyFactory getProxyFactory() { + return proxyFactory; + } + + /** + * @return the registry + */ + public ExtensionPointRegistry getExtensionPointRegistry() { + return registry; + } + +} diff --git a/java/sca/modules/node2-impl/src/main/java/org/apache/tuscany/sca/node/impl/RuntimeBuilder.java b/java/sca/modules/node2-impl/src/main/java/org/apache/tuscany/sca/node/impl/RuntimeBuilder.java new file mode 100644 index 0000000000..98a44cef9e --- /dev/null +++ b/java/sca/modules/node2-impl/src/main/java/org/apache/tuscany/sca/node/impl/RuntimeBuilder.java @@ -0,0 +1,261 @@ +/* + * 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.tuscany.sca.node.impl; + +import java.io.IOException; +import java.security.AccessController; +import java.security.PrivilegedAction; +import java.util.List; + +import javax.xml.stream.XMLInputFactory; +import javax.xml.stream.XMLOutputFactory; + +import org.apache.tuscany.sca.assembly.AssemblyFactory; +import org.apache.tuscany.sca.assembly.Composite; +import org.apache.tuscany.sca.assembly.EndpointFactory; +import org.apache.tuscany.sca.assembly.SCABindingFactory; +import org.apache.tuscany.sca.assembly.builder.CompositeBuilder; +import org.apache.tuscany.sca.assembly.builder.impl.CompositeBuilderImpl; +import org.apache.tuscany.sca.assembly.xml.CompositeDocumentProcessor; +import org.apache.tuscany.sca.context.ContextFactoryExtensionPoint; +import org.apache.tuscany.sca.context.RequestContextFactory; +import org.apache.tuscany.sca.contribution.ContributionFactory; +import org.apache.tuscany.sca.contribution.ModelFactoryExtensionPoint; +import org.apache.tuscany.sca.contribution.processor.DefaultValidatingXMLInputFactory; +import org.apache.tuscany.sca.contribution.processor.DefaultValidationSchemaExtensionPoint; +import org.apache.tuscany.sca.contribution.processor.ExtensiblePackageProcessor; +import org.apache.tuscany.sca.contribution.processor.ExtensibleStAXArtifactProcessor; +import org.apache.tuscany.sca.contribution.processor.ExtensibleURLArtifactProcessor; +import org.apache.tuscany.sca.contribution.processor.PackageProcessor; +import org.apache.tuscany.sca.contribution.processor.PackageProcessorExtensionPoint; +import org.apache.tuscany.sca.contribution.processor.StAXArtifactProcessorExtensionPoint; +import org.apache.tuscany.sca.contribution.processor.URLArtifactProcessorExtensionPoint; +import org.apache.tuscany.sca.contribution.processor.ValidationSchemaExtensionPoint; +import org.apache.tuscany.sca.contribution.resolver.ModelResolver; +import org.apache.tuscany.sca.contribution.resolver.ModelResolverExtensionPoint; +import org.apache.tuscany.sca.contribution.service.ContributionListenerExtensionPoint; +import org.apache.tuscany.sca.contribution.service.ContributionRepository; +import org.apache.tuscany.sca.contribution.service.ContributionService; +import org.apache.tuscany.sca.contribution.service.ExtensibleContributionListener; +import org.apache.tuscany.sca.contribution.service.TypeDescriber; +import org.apache.tuscany.sca.contribution.service.impl.ContributionRepositoryImpl; +import org.apache.tuscany.sca.contribution.service.impl.ContributionServiceImpl; +import org.apache.tuscany.sca.contribution.service.impl.PackageTypeDescriberImpl; +import org.apache.tuscany.sca.core.ExtensionPointRegistry; +import org.apache.tuscany.sca.core.UtilityExtensionPoint; +import org.apache.tuscany.sca.core.assembly.ActivationException; +import org.apache.tuscany.sca.core.assembly.CompositeActivator; +import org.apache.tuscany.sca.core.assembly.CompositeActivatorImpl; +import org.apache.tuscany.sca.core.conversation.ConversationManager; +import org.apache.tuscany.sca.core.invocation.ExtensibleWireProcessor; +import org.apache.tuscany.sca.core.invocation.ProxyFactory; +import org.apache.tuscany.sca.core.scope.CompositeScopeContainerFactory; +import org.apache.tuscany.sca.core.scope.ConversationalScopeContainerFactory; +import org.apache.tuscany.sca.core.scope.RequestScopeContainerFactory; +import org.apache.tuscany.sca.core.scope.ScopeContainerFactory; +import org.apache.tuscany.sca.core.scope.ScopeRegistry; +import org.apache.tuscany.sca.core.scope.ScopeRegistryImpl; +import org.apache.tuscany.sca.core.scope.StatelessScopeContainerFactory; +import org.apache.tuscany.sca.definitions.SCADefinitions; +import org.apache.tuscany.sca.endpointresolver.EndpointResolverFactoryExtensionPoint; +import org.apache.tuscany.sca.interfacedef.InterfaceContractMapper; +import org.apache.tuscany.sca.interfacedef.java.JavaInterfaceFactory; +import org.apache.tuscany.sca.invocation.MessageFactory; +import org.apache.tuscany.sca.monitor.Monitor; +import org.apache.tuscany.sca.policy.IntentAttachPointTypeFactory; +import org.apache.tuscany.sca.policy.PolicyFactory; +import org.apache.tuscany.sca.provider.ProviderFactoryExtensionPoint; +import org.apache.tuscany.sca.runtime.RuntimeWireProcessor; +import org.apache.tuscany.sca.runtime.RuntimeWireProcessorExtensionPoint; +import org.apache.tuscany.sca.work.WorkScheduler; + +/** + * + * @version $Rev$ $Date$ + */ +public class RuntimeBuilder { + + // private static final Logger logger = Logger.getLogger(RuntimeBuilder.class.getName()); + + public static CompositeActivator createCompositeActivator(ExtensionPointRegistry registry, + AssemblyFactory assemblyFactory, + MessageFactory messageFactory, + SCABindingFactory scaBindingFactory, + InterfaceContractMapper mapper, + ProxyFactory proxyFactory, + ScopeRegistry scopeRegistry, + WorkScheduler workScheduler) { + + // Create a wire post processor extension point + RuntimeWireProcessorExtensionPoint wireProcessors = + registry.getExtensionPoint(RuntimeWireProcessorExtensionPoint.class); + RuntimeWireProcessor wireProcessor = new ExtensibleWireProcessor(wireProcessors); + + // Retrieve the processors extension point + StAXArtifactProcessorExtensionPoint processors = + registry.getExtensionPoint(StAXArtifactProcessorExtensionPoint.class); + + // Create a provider factory extension point + ProviderFactoryExtensionPoint providerFactories = + registry.getExtensionPoint(ProviderFactoryExtensionPoint.class); + + // Create a endpoint resolver factory extension point + EndpointResolverFactoryExtensionPoint endpointResolverFactories = + registry.getExtensionPoint(EndpointResolverFactoryExtensionPoint.class); + + JavaInterfaceFactory javaInterfaceFactory = + registry.getExtensionPoint(ModelFactoryExtensionPoint.class).getFactory(JavaInterfaceFactory.class); + RequestContextFactory requestContextFactory = + registry.getExtensionPoint(ContextFactoryExtensionPoint.class).getFactory(RequestContextFactory.class); + + UtilityExtensionPoint utilities = registry.getExtensionPoint(UtilityExtensionPoint.class); + ConversationManager conversationManager = utilities.getUtility(ConversationManager.class); + + // Create the composite activator + CompositeActivator compositeActivator = + new CompositeActivatorImpl(assemblyFactory, messageFactory, javaInterfaceFactory, scaBindingFactory, + mapper, scopeRegistry, workScheduler, wireProcessor, requestContextFactory, + proxyFactory, providerFactories, endpointResolverFactories, processors, + conversationManager); + + return compositeActivator; + } + + public static CompositeBuilder createCompositeBuilder(Monitor monitor, + AssemblyFactory assemblyFactory, + SCABindingFactory scaBindingFactory, + EndpointFactory endpointFactory, + IntentAttachPointTypeFactory intentAttachPointTypeFactory, + InterfaceContractMapper interfaceContractMapper, + SCADefinitions policyDefinitions) { + + return new CompositeBuilderImpl(assemblyFactory, endpointFactory, scaBindingFactory, + intentAttachPointTypeFactory, interfaceContractMapper, policyDefinitions, + monitor); + } + + /** + * Create the contribution service used by this domain. + * + * @throws ActivationException + */ + public static ContributionService createContributionService(ClassLoader classLoader, + ExtensionPointRegistry registry, + ContributionFactory contributionFactory, + AssemblyFactory assemblyFactory, + PolicyFactory policyFactory, + InterfaceContractMapper mapper, + List<SCADefinitions> policyDefinitions, + ModelResolver policyDefinitionResolver, + Monitor monitor) throws ActivationException { + + // Get the model factory extension point + ModelFactoryExtensionPoint modelFactories = registry.getExtensionPoint(ModelFactoryExtensionPoint.class); + + // Create a new XML input factory + // Allow privileged access to factory. Requires RuntimePermission in security policy file. + XMLInputFactory inputFactory = AccessController.doPrivileged(new PrivilegedAction<XMLInputFactory>() { + public XMLInputFactory run() { + return XMLInputFactory.newInstance(); + } + }); + modelFactories.addFactory(inputFactory); + + // Create a validation XML schema extension point + ValidationSchemaExtensionPoint schemas = new DefaultValidationSchemaExtensionPoint(); + + // Create a validating XML input factory + XMLInputFactory validatingInputFactory = new DefaultValidatingXMLInputFactory(inputFactory, schemas, monitor); + modelFactories.addFactory(validatingInputFactory); + + // Create StAX artifact processor extension point + StAXArtifactProcessorExtensionPoint staxProcessors = + registry.getExtensionPoint(StAXArtifactProcessorExtensionPoint.class); + + // Create and register StAX processors for SCA assembly XML + // Allow privileged access to factory. Requires RuntimePermission in security policy file. + XMLOutputFactory outputFactory = AccessController.doPrivileged(new PrivilegedAction<XMLOutputFactory>() { + public XMLOutputFactory run() { + return XMLOutputFactory.newInstance(); + } + }); + ExtensibleStAXArtifactProcessor staxProcessor = + new ExtensibleStAXArtifactProcessor(staxProcessors, inputFactory, outputFactory, monitor); + + // Create URL artifact processor extension point + URLArtifactProcessorExtensionPoint documentProcessors = + registry.getExtensionPoint(URLArtifactProcessorExtensionPoint.class); + + // Create and register document processors for SCA assembly XML + documentProcessors.getProcessor(Composite.class); + documentProcessors.addArtifactProcessor(new CompositeDocumentProcessor(staxProcessor, validatingInputFactory, + policyDefinitions, monitor)); + + // Create Model Resolver extension point + ModelResolverExtensionPoint modelResolvers = registry.getExtensionPoint(ModelResolverExtensionPoint.class); + + // Create contribution package processor extension point + TypeDescriber describer = new PackageTypeDescriberImpl(); + PackageProcessor packageProcessor = + new ExtensiblePackageProcessor(registry.getExtensionPoint(PackageProcessorExtensionPoint.class), describer, + monitor); + + // Create contribution listener + ExtensibleContributionListener contributionListener = + new ExtensibleContributionListener(registry.getExtensionPoint(ContributionListenerExtensionPoint.class)); + + // Create a contribution repository + ContributionRepository repository; + try { + repository = new ContributionRepositoryImpl("target", inputFactory, monitor); + } catch (IOException e) { + throw new ActivationException(e); + } + + ExtensibleURLArtifactProcessor documentProcessor = + new ExtensibleURLArtifactProcessor(documentProcessors, monitor); + + // Create the contribution service + ContributionService contributionService = + new ContributionServiceImpl(repository, packageProcessor, documentProcessor, staxProcessor, + contributionListener, policyDefinitionResolver, modelResolvers, modelFactories, + assemblyFactory, contributionFactory, inputFactory, policyDefinitions, monitor); + return contributionService; + } + + public static ScopeRegistry createScopeRegistry(ExtensionPointRegistry registry) { + ScopeRegistry scopeRegistry = new ScopeRegistryImpl(); + ScopeContainerFactory[] factories = + new ScopeContainerFactory[] {new CompositeScopeContainerFactory(), new StatelessScopeContainerFactory(), + new RequestScopeContainerFactory(), + new ConversationalScopeContainerFactory(null), + // new HttpSessionScopeContainer(monitor) + }; + for (ScopeContainerFactory f : factories) { + scopeRegistry.register(f); + } + + //FIXME Pass the scope container differently as it's not an extension point + registry.addExtensionPoint(scopeRegistry); + + return scopeRegistry; + } + +} diff --git a/java/sca/modules/node2-impl/src/test/java/hello/HelloWorld.java b/java/sca/modules/node2-impl/src/test/java/hello/HelloWorld.java new file mode 100644 index 0000000000..2f519cb81d --- /dev/null +++ b/java/sca/modules/node2-impl/src/test/java/hello/HelloWorld.java @@ -0,0 +1,30 @@ +/* + * 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 hello; + +import org.osoa.sca.annotations.Remotable; + +/** + * HelloWorld interface + */ +@Remotable +public interface HelloWorld { + String hello(String name); +} diff --git a/java/sca/modules/node2-impl/src/test/java/hello/HelloWorldImpl.java b/java/sca/modules/node2-impl/src/test/java/hello/HelloWorldImpl.java new file mode 100644 index 0000000000..c9a7560b12 --- /dev/null +++ b/java/sca/modules/node2-impl/src/test/java/hello/HelloWorldImpl.java @@ -0,0 +1,30 @@ +/* + * 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 hello; + +/** + * HelloWorldImpl + */ +public class HelloWorldImpl implements HelloWorld { + public String hello(String name) { + System.out.println("Hello: " + name); + return "Hello, " + name; + } +} diff --git a/java/sca/modules/node2-impl/src/test/java/org/apache/tuscany/sca/node/impl/NodeImplTestCase.java b/java/sca/modules/node2-impl/src/test/java/org/apache/tuscany/sca/node/impl/NodeImplTestCase.java new file mode 100644 index 0000000000..bdf3dfca4f --- /dev/null +++ b/java/sca/modules/node2-impl/src/test/java/org/apache/tuscany/sca/node/impl/NodeImplTestCase.java @@ -0,0 +1,104 @@ +/* + * 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.tuscany.sca.node.impl; + +import hello.HelloWorld; + +import java.io.File; + +import junit.framework.Assert; + +import org.apache.tuscany.sca.node.SCAClient; +import org.apache.tuscany.sca.node.SCAContribution; +import org.apache.tuscany.sca.node.SCANode2; +import org.apache.tuscany.sca.node.SCANode2Factory; +import org.junit.Test; + +/** + * + */ +public class NodeImplTestCase { + private static String composite = + "<composite xmlns=\"http://www.osoa.org/xmlns/sca/1.0\"" + " xmlns:tuscany=\"http://tuscany.apache.org/xmlns/sca/1.0\"" + + " targetNamespace=\"http://sample/composite\"" + + " xmlns:sc=\"http://sample/composite\"" + + " name=\"HelloWorld\">" + + " <component name=\"HelloWorld\">" + + " <implementation.java class=\"hello.HelloWorldImpl\"/>" + + " </component>" + + " </composite>"; + + @Test + public void testNodeWithCompositeContent() { + SCANode2Factory factory = new NodeFactoryImpl(); + SCAContribution contribution = new SCAContribution("c1", new File("target/classes").toURI().toString()); + SCANode2 node = factory.createSCANode("HelloWorld.composite", composite, contribution); + node.start(); + HelloWorld hw = ((SCAClient)node).getService(HelloWorld.class, "HelloWorld"); + Assert.assertEquals("Hello, Node", hw.hello("Node")); + node.stop(); + } + + @Test + public void testNodeWithCompositeURI() { + SCANode2Factory factory = new NodeFactoryImpl(); + SCAContribution contribution = new SCAContribution("c1", new File("target/classes").toURI().toString()); + String compositeURI = new File("target/test-classes/HelloWorld.composite").toURI().toString(); + SCANode2 node = factory.createSCANode(compositeURI, contribution); + node.start(); + HelloWorld hw = ((SCAClient)node).getService(HelloWorld.class, "HelloWorld"); + Assert.assertEquals("Hello, Node", hw.hello("Node")); + node.stop(); + } + + @Test + public void testNodeWithRelativeCompositeURI() { + SCANode2Factory factory = new NodeFactoryImpl(); + SCAContribution contribution = new SCAContribution("c1", new File("target/test-classes").toURI().toString()); + String compositeURI = "HelloWorld.composite"; + SCANode2 node = factory.createSCANode(compositeURI, contribution); + node.start(); + HelloWorld hw = ((SCAClient)node).getService(HelloWorld.class, "HelloWorld"); + Assert.assertEquals("Hello, Node", hw.hello("Node")); + node.stop(); + } + + @Test + public void testNodeWithRelativeCompositeURIAndNoContribution() { + SCANode2Factory factory = new NodeFactoryImpl(); + String compositeURI = "HelloWorld.composite"; + SCANode2 node = factory.createSCANode(compositeURI, new SCAContribution[0]); + node.start(); + HelloWorld hw = ((SCAClient)node).getService(HelloWorld.class, "HelloWorld"); + Assert.assertEquals("Hello, Node", hw.hello("Node")); + node.stop(); + } + + @Test + public void testNodeWithClassLoader() { + SCANode2Factory factory = new NodeFactoryImpl(); + String compositeURI = "HelloWorld.composite"; + SCANode2 node = factory.createSCANodeFromClassLoader(compositeURI, HelloWorld.class.getClassLoader()); + node.start(); + HelloWorld hw = ((SCAClient)node).getService(HelloWorld.class, "HelloWorld"); + Assert.assertEquals("Hello, Node", hw.hello("Node")); + node.stop(); + } +} diff --git a/java/sca/modules/node2-impl/src/test/resources/HelloWorld.composite b/java/sca/modules/node2-impl/src/test/resources/HelloWorld.composite new file mode 100644 index 0000000000..9e3299d691 --- /dev/null +++ b/java/sca/modules/node2-impl/src/test/resources/HelloWorld.composite @@ -0,0 +1,30 @@ +<?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.
+-->
+<composite xmlns="http://www.osoa.org/xmlns/sca/1.0"
+ xmlns:tuscany="http://tuscany.apache.org/xmlns/sca/1.0"
+ targetNamespace="http://sample/composite"
+ xmlns:sc="http://sample/composite"
+ name="HelloWorld">
+
+ <component name="HelloWorld">
+ <implementation.java class="hello.HelloWorldImpl"/>
+ </component>
+
+</composite>
diff --git a/java/sca/modules/node2-launcher/src/main/java/org/apache/tuscany/sca/node/launcher/DomainManagerLauncher.java b/java/sca/modules/node2-launcher/src/main/java/org/apache/tuscany/sca/node/launcher/DomainManagerLauncher.java index e962491b25..f0c0a0a2f4 100644 --- a/java/sca/modules/node2-launcher/src/main/java/org/apache/tuscany/sca/node/launcher/DomainManagerLauncher.java +++ b/java/sca/modules/node2-launcher/src/main/java/org/apache/tuscany/sca/node/launcher/DomainManagerLauncher.java @@ -72,7 +72,7 @@ public class DomainManagerLauncher { } public static void main(String[] args) throws Exception { - logger.info("Apache Tuscany SCA Domain Manager starting..."); + logger.info("Apache Tuscany SCA Domain Manager is starting..."); // Create a domain manager DomainManagerLauncher launcher = newInstance(); @@ -85,9 +85,13 @@ public class DomainManagerLauncher { logger.log(Level.SEVERE, "SCA Domain Manager could not be started", e); throw e; } - logger.info("SCA Domain Manager started."); + logger.info("SCA Domain Manager is now started."); + + ShutdownThread hook = new ShutdownThread(domainManager); + Runtime.getRuntime().addShutdownHook(hook); logger.info("Press enter to shutdown."); + try { System.in.read(); } catch (IOException e) { @@ -98,13 +102,40 @@ public class DomainManagerLauncher { lock.wait(); } } + + stop(domainManager); + // Remove the hook + Runtime.getRuntime().removeShutdownHook(hook); + } + private static void stop(Object domainManager) throws Exception { // Stop the domain manager + if (domainManager == null) { + return; + } try { domainManager.getClass().getMethod("stop").invoke(domainManager); + logger.info("SCA Domain Manager is now stopped."); } catch (Exception e) { logger.log(Level.SEVERE, "SCA Domain Manager could not be stopped", e); throw e; } } + + private static class ShutdownThread extends Thread { + private Object domainManager; + + public ShutdownThread(Object domainManager) { + super(); + this.domainManager = domainManager; + } + + public void run() { + try { + DomainManagerLauncher.stop(domainManager); + } catch (Exception e) { + // Ignore + } + } + } } diff --git a/java/sca/modules/policy/src/main/java/org/apache/tuscany/sca/policy/util/PolicyComputationUtils.java b/java/sca/modules/policy/src/main/java/org/apache/tuscany/sca/policy/util/PolicyComputationUtils.java index 1c56cbacbc..aace23edf6 100644 --- a/java/sca/modules/policy/src/main/java/org/apache/tuscany/sca/policy/util/PolicyComputationUtils.java +++ b/java/sca/modules/policy/src/main/java/org/apache/tuscany/sca/policy/util/PolicyComputationUtils.java @@ -21,8 +21,8 @@ package org.apache.tuscany.sca.policy.util; import static javax.xml.XMLConstants.XMLNS_ATTRIBUTE_NS_URI; +import java.io.InputStream; import java.io.StringWriter; -import java.net.URL; import java.security.AccessController; import java.security.PrivilegedActionException; import java.security.PrivilegedExceptionAction; @@ -341,11 +341,12 @@ public class PolicyComputationUtils { } } - public static byte[] addApplicablePolicySets(URL artifactUrl, Collection<PolicySet> domainPolicySets) throws Exception { + public static byte[] addApplicablePolicySets(InputStream is, Collection<PolicySet> domainPolicySets) throws Exception { DocumentBuilderFactory dbFac = DocumentBuilderFactory.newInstance(); dbFac.setNamespaceAware(true); DocumentBuilder db = dbFac.newDocumentBuilder(); - Document doc = db.parse(artifactUrl.toURI().toString()); + Document doc = db.parse(is); + is.close(); return addApplicablePolicySets(doc, domainPolicySets); } |