diff options
Diffstat (limited to 'sca-java-1.x/branches/sca-java-0.91/modules/host-embedded/src/main/java/org/apache/tuscany/sca/host/embedded/impl')
4 files changed, 1097 insertions, 0 deletions
diff --git a/sca-java-1.x/branches/sca-java-0.91/modules/host-embedded/src/main/java/org/apache/tuscany/sca/host/embedded/impl/DefaultSCADomain.java b/sca-java-1.x/branches/sca-java-0.91/modules/host-embedded/src/main/java/org/apache/tuscany/sca/host/embedded/impl/DefaultSCADomain.java new file mode 100644 index 0000000000..1db2aecc26 --- /dev/null +++ b/sca-java-1.x/branches/sca-java-0.91/modules/host-embedded/src/main/java/org/apache/tuscany/sca/host/embedded/impl/DefaultSCADomain.java @@ -0,0 +1,351 @@ +/* + * 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.host.embedded.impl; + +import java.io.IOException; +import java.net.MalformedURLException; +import java.net.URI; +import java.net.URL; +import java.util.HashMap; +import java.util.Map; + +import javax.xml.namespace.QName; + +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.contribution.Contribution; +import org.apache.tuscany.sca.contribution.DeployedArtifact; +import org.apache.tuscany.sca.contribution.resolver.impl.ModelResolverImpl; +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.runtime.ActivationException; +import org.apache.tuscany.sca.core.runtime.CompositeActivator; +import org.apache.tuscany.sca.host.embedded.SCADomain; +import org.osoa.sca.CallableReference; +import org.osoa.sca.ComponentContext; +import org.osoa.sca.Constants; +import org.osoa.sca.ServiceReference; +import org.osoa.sca.ServiceRuntimeException; + +/** + * A default SCA domain facade implementation. + * + * @version $Rev$ $Date$ + */ +public class DefaultSCADomain extends SCADomain { + + private String uri; + private String location; + private String[] composites; + private Composite domainComposite; + private Contribution contribution; + private Map<String, Component> components = new HashMap<String, Component>(); + private ReallySmallRuntime runtime; + + /** + * Constructs a new domain facade. + * + * @param domainURI + * @param contributionLocation + * @param composites + */ + public DefaultSCADomain(ClassLoader runtimeClassLoader, + ClassLoader applicationClassLoader, + String domainURI, + String contributionLocation, + String... composites) { + this.uri = domainURI; + this.location = contributionLocation; + this.composites = composites; + + // Create and start the runtime + runtime = new ReallySmallRuntime(runtimeClassLoader); + try { + runtime.start(); + + } catch (ActivationException e) { + throw new ServiceRuntimeException(e); + } + + // Contribute the given contribution to an in-memory repository + ContributionService contributionService = runtime.getContributionService(); + URL contributionURL; + try { + contributionURL = getContributionLocation(applicationClassLoader, location, this.composites); + } catch (MalformedURLException e) { + throw new ServiceRuntimeException(e); + } + + try { + ModelResolverImpl modelResolver = new ModelResolverImpl(applicationClassLoader); + String contributionURI = FileHelper.getName(contributionURL.getPath()); + contribution = contributionService.contribute(contributionURI, contributionURL, modelResolver, false); + } catch (ContributionException e) { + throw new ServiceRuntimeException(e); + } catch (IOException e) { + throw new ServiceRuntimeException(e); + } + + // Create an in-memory domain level composite + AssemblyFactory assemblyFactory = runtime.getAssemblyFactory(); + domainComposite = assemblyFactory.createComposite(); + domainComposite.setName(new QName(Constants.SCA_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 (DeployedArtifact artifact : contribution.getArtifacts()) { + if (artifact.getModel() instanceof Composite) { + compositeArtifacts.put(artifact.getURI(), (Composite)artifact.getModel()); + } + } + for (String compositePath : composites) { + Composite composite = compositeArtifacts.get(compositePath); + if (composite == null) { + throw new ServiceRuntimeException("Composite not found: " + compositePath); + } + domainComposite.getIncludes().add(composite); + } + } else { + // in this case, a sca-contribution.xml should have been specified + for(Composite composite : contribution.getDeployables()) { + domainComposite.getIncludes().add(composite); + } + + } + + + // Activate and start the SCA domain composite + CompositeActivator compositeActivator = runtime.getCompositeActivator(); + try { + compositeActivator.activate(domainComposite); + compositeActivator.start(domainComposite); + } catch (ActivationException e) { + throw new ServiceRuntimeException(e); + } + + // Index the top level components + for (Component component : domainComposite.getComponents()) { + components.put(component.getName(), component); + } + } + + @Override + public void close() { + + super.close(); + + // Remove the contribution from the in-memory repository + ContributionService contributionService = runtime.getContributionService(); + try { + contributionService.remove(contribution.getURI()); + } catch (ContributionException e) { + throw new ServiceRuntimeException(e); + } + + // Stop the SCA domain composite + CompositeActivator compositeActivator = runtime.getCompositeActivator(); + try { + compositeActivator.stop(domainComposite); + } catch (ActivationException 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 + */ + private URL getContributionLocation(ClassLoader classLoader, String contributionPath, String[] composites) + throws MalformedURLException { + if (contributionPath != null && contributionPath.length() > 0) { + 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)) { + String location = url.substring(0, url.lastIndexOf(contributionArtifactPath)); + // workaround from evil url/uri form maven + contributionURL = FileHelper.toFile(new URL(location)).toURI().toURL(); + } + + } 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(); + } + } catch (MalformedURLException mfe) { + throw new IllegalArgumentException(mfe); + } + + return contributionURL; + } + + @Override + public <B, R extends CallableReference<B>> R cast(B target) throws IllegalArgumentException { + // TODO Auto-generated method stub + return null; + } + + @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(); + } + + @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) { + throw new ServiceRuntimeException("Component not found: " + componentName); + } + ComponentContext 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) { + ComponentService promotedService = null; + for (ComponentService componentService : component.getServices()) { + if (serviceName == null || serviceName.equals(componentService.getName())) { + + CompositeService compositeService = (CompositeService)componentService.getService(); + if (compositeService != null) { + promotedService = compositeService.getPromotedService(); + SCABinding scaBinding = promotedService.getBinding(SCABinding.class); + if (scaBinding != null) { + Component promotedComponent = scaBinding.getComponent(); + if (serviceName != null) { + serviceName = "$promoted$." + serviceName; + } + componentContext = (ComponentContext)promotedComponent; + } + } + break; + } + } + if (componentContext == null) { + throw new ServiceRuntimeException("Composite service not found: " + name); + } + } else { + componentContext = (ComponentContext)component; + } + + ServiceReference<B> serviceReference; + if (serviceName != null) { + serviceReference = componentContext.createSelfReference(businessInterface, serviceName); + } else { + serviceReference = componentContext.createSelfReference(businessInterface); + } + return serviceReference; + + } + + @Override + public String getURI() { + return uri; + } + +} diff --git a/sca-java-1.x/branches/sca-java-0.91/modules/host-embedded/src/main/java/org/apache/tuscany/sca/host/embedded/impl/EmbeddedSCADomain.java b/sca-java-1.x/branches/sca-java-0.91/modules/host-embedded/src/main/java/org/apache/tuscany/sca/host/embedded/impl/EmbeddedSCADomain.java new file mode 100644 index 0000000000..f6d5d8377b --- /dev/null +++ b/sca-java-1.x/branches/sca-java-0.91/modules/host-embedded/src/main/java/org/apache/tuscany/sca/host/embedded/impl/EmbeddedSCADomain.java @@ -0,0 +1,287 @@ +/* + * 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.host.embedded.impl; + +import java.util.HashMap; +import java.util.Map; + +import javax.xml.namespace.QName; + +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.contribution.service.ContributionService; +import org.apache.tuscany.sca.core.runtime.ActivationException; +import org.apache.tuscany.sca.core.runtime.CompositeActivator; +import org.apache.tuscany.sca.host.embedded.SCADomain; +import org.osoa.sca.CallableReference; +import org.osoa.sca.ComponentContext; +import org.osoa.sca.Constants; +import org.osoa.sca.ServiceReference; +import org.osoa.sca.ServiceRuntimeException; + +/** + * An SCA domain facade implementation. + * + * @version $Rev$ $Date$ + */ +public class EmbeddedSCADomain extends SCADomain { + + private String uri; + private Composite domainComposite; + private ReallySmallRuntime runtime; + private Map<String, Component> components = new HashMap<String, Component>(); + private DomainCompositeHelper domainCompositeHelper; + + public class DomainCompositeHelper { + + /** + * Add a composite to the domain + * @param composite + * @return + */ + public Composite addComposite(Composite composite) throws ActivationException { + domainComposite.getIncludes().add(composite); + //CompositeActivator compositeActivator = runtime.getCompositeActivator(); + //compositeActivator.activate(composite); + //for (Component component : composite.getComponents()) { + // components.put(component.getName(), component); + //} + return composite; + } + + /** + * Remove a composite from the domain + * @param composite + * @throws ActivationException + */ + public void removeComposite(Composite composite) throws ActivationException { + CompositeActivator compositeActivator = runtime.getCompositeActivator(); + compositeActivator.deactivate(composite); + domainComposite.getIncludes().remove(composite); + for (Component component : composite.getComponents()) { + components.remove(component.getName()); + } + } + + /** + * Start a composite + * @deprecated + * @param composite + * @throws ActivationException + */ + public void startComposite(Composite composite) throws ActivationException { + CompositeActivator compositeActivator = runtime.getCompositeActivator(); + compositeActivator.start(composite); + } + + /** + * Stop a composite + * @deprecated + * @param composite + * @throws ActivationException + */ + public void stopComposite(Composite composite) throws ActivationException { + CompositeActivator compositeActivator = runtime.getCompositeActivator(); + compositeActivator.stop(composite); + } + + /** + * Get a reference to a component by name + * @param componentName + * @return + */ + public Component getComponent(String componentName){ + return (Component) components.get(componentName); + } + + /** + * Start a component + * @param component + * @throws ActivationException + */ + public void startComponent(Component component) throws ActivationException { + CompositeActivator compositeActivator = runtime.getCompositeActivator(); + compositeActivator.start(component); + } + + /** + * Stop a component + * @param component + * @throws ActivationException + */ + public void stopComponent(Component component) throws ActivationException { + CompositeActivator compositeActivator = runtime.getCompositeActivator(); + compositeActivator.stop(component); + } + + /** + * Activate SCA Domain + * @throws ActivationException + */ + public void activateDomain() throws ActivationException { + CompositeActivator compositeActivator = runtime.getCompositeActivator(); + compositeActivator.activate(domainComposite); + for (Component component : domainComposite.getComponents()) { + components.put(component.getName(), component); + } + + } + } + + /** + * Constructs a new domain facade. + * + * @param runtimeClassLoader + * @param domainURI + */ + public EmbeddedSCADomain(ClassLoader runtimeClassLoader, + String domainURI) { + this.uri = domainURI; + + // Create a runtime + runtime = new ReallySmallRuntime(runtimeClassLoader); + } + + public void start() throws ActivationException { + + // Start the runtime + runtime.start(); + + // Create an in-memory domain level composite + AssemblyFactory assemblyFactory = runtime.getAssemblyFactory(); + domainComposite = assemblyFactory.createComposite(); + domainComposite.setName(new QName(Constants.SCA_NS, "domain")); + domainComposite.setURI(uri); + + // Create a domain composite helper + domainCompositeHelper = new DomainCompositeHelper(); + } + + public void stop() throws ActivationException { + + // Stop the runtime + runtime.stop(); + + // Cleanup + domainComposite = null; + domainCompositeHelper = null; + } + + public ContributionService getContributionService() { + return runtime.getContributionService(); + } + + public DomainCompositeHelper getDomainCompositeHelper() { + return domainCompositeHelper; + } + + @Override + public void close() { + super.close(); + } + + @Override + public <B, R extends CallableReference<B>> R cast(B target) throws IllegalArgumentException { + // TODO Auto-generated method stub + return null; + } + + @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(); + } + + @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) { + throw new ServiceRuntimeException("Component not found: " + componentName); + } + ComponentContext 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) { + ComponentService promotedService = null; + for (ComponentService componentService : component.getServices()) { + if (serviceName == null || serviceName.equals(componentService.getName())) { + + CompositeService compositeService = (CompositeService)componentService.getService(); + if (compositeService != null) { + promotedService = compositeService.getPromotedService(); + SCABinding scaBinding = promotedService.getBinding(SCABinding.class); + if (scaBinding != null) { + Component promotedComponent = scaBinding.getComponent(); + if (serviceName != null) { + serviceName = "$promoted$." + serviceName; + } + componentContext = (ComponentContext)promotedComponent; + } + } + break; + } + } + if (componentContext == null) { + throw new ServiceRuntimeException("Composite service not found: " + name); + } + } else { + componentContext = (ComponentContext)component; + } + + ServiceReference<B> serviceReference; + if (serviceName != null) { + serviceReference = componentContext.createSelfReference(businessInterface, serviceName); + } else { + serviceReference = componentContext.createSelfReference(businessInterface); + } + return serviceReference; + + } + + @Override + public String getURI() { + return uri; + } + +} diff --git a/sca-java-1.x/branches/sca-java-0.91/modules/host-embedded/src/main/java/org/apache/tuscany/sca/host/embedded/impl/ReallySmallRuntime.java b/sca-java-1.x/branches/sca-java-0.91/modules/host-embedded/src/main/java/org/apache/tuscany/sca/host/embedded/impl/ReallySmallRuntime.java new file mode 100644 index 0000000000..dd25585d73 --- /dev/null +++ b/sca-java-1.x/branches/sca-java-0.91/modules/host-embedded/src/main/java/org/apache/tuscany/sca/host/embedded/impl/ReallySmallRuntime.java @@ -0,0 +1,178 @@ +/* + * 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.host.embedded.impl; + +import java.util.List; + +import org.apache.tuscany.sca.assembly.AssemblyFactory; +import org.apache.tuscany.sca.assembly.DefaultSCABindingFactory; +import org.apache.tuscany.sca.assembly.SCABindingFactory; +import org.apache.tuscany.sca.contribution.ContributionFactory; +import org.apache.tuscany.sca.contribution.impl.ContributionFactoryImpl; +import org.apache.tuscany.sca.contribution.service.ContributionService; +import org.apache.tuscany.sca.core.DefaultExtensionPointRegistry; +import org.apache.tuscany.sca.core.DefaultModelFactoryExtensionPoint; +import org.apache.tuscany.sca.core.ExtensionPointRegistry; +import org.apache.tuscany.sca.core.ModelFactoryExtensionPoint; +import org.apache.tuscany.sca.core.ModuleActivator; +import org.apache.tuscany.sca.core.invocation.MessageFactoryImpl; +import org.apache.tuscany.sca.core.invocation.ProxyFactory; +import org.apache.tuscany.sca.core.runtime.ActivationException; +import org.apache.tuscany.sca.core.runtime.CompositeActivator; +import org.apache.tuscany.sca.core.runtime.RuntimeAssemblyFactory; +import org.apache.tuscany.sca.core.work.ThreadPoolWorkManager; +import org.apache.tuscany.sca.interfacedef.InterfaceContractMapper; +import org.apache.tuscany.sca.interfacedef.impl.InterfaceContractMapperImpl; +import org.apache.tuscany.sca.invocation.MessageFactory; +import org.apache.tuscany.sca.policy.DefaultPolicyFactory; +import org.apache.tuscany.sca.policy.PolicyFactory; +import org.apache.tuscany.sca.scope.ScopeRegistry; + +public class ReallySmallRuntime { + + private List<ModuleActivator> modules; + private ExtensionPointRegistry registry; + + private ClassLoader classLoader; + private AssemblyFactory assemblyFactory; + private ContributionService contributionService; + private CompositeActivator compositeActivator; + private ThreadPoolWorkManager workManager; + private ScopeRegistry scopeRegistry; + + public ReallySmallRuntime(ClassLoader classLoader) { + this.classLoader = classLoader; + } + + public void start() throws ActivationException { + + // Create our extension point registry + registry = new DefaultExtensionPointRegistry(); + + // Create a work manager + workManager = new ThreadPoolWorkManager(10); + + // Create an interface contract mapper + InterfaceContractMapper mapper = new InterfaceContractMapperImpl(); + + // Create factory extension point + ModelFactoryExtensionPoint factories = new DefaultModelFactoryExtensionPoint(); + registry.addExtensionPoint(factories); + + // Create Message factory + MessageFactory messageFactory = new MessageFactoryImpl(); + factories.addFactory(messageFactory); + + // Create a proxy factory + ProxyFactory proxyFactory = ReallySmallRuntimeBuilder.createProxyFactory(registry, mapper, messageFactory); + + // Create model factories + assemblyFactory = new RuntimeAssemblyFactory(mapper, proxyFactory); + factories.addFactory(assemblyFactory); + PolicyFactory policyFactory = new DefaultPolicyFactory(); + factories.addFactory(policyFactory); + SCABindingFactory scaBindingFactory = new DefaultSCABindingFactory(); + factories.addFactory(scaBindingFactory); + ContributionFactory contributionFactory = new ContributionFactoryImpl(); + factories.addFactory(contributionFactory); + + // Create a contribution service + contributionService = ReallySmallRuntimeBuilder.createContributionService(registry, + contributionFactory, + assemblyFactory, + policyFactory, + mapper); + + // Create the ScopeRegistry + scopeRegistry = ReallySmallRuntimeBuilder.createScopeRegistry(registry); + + // Create a composite activator + compositeActivator = ReallySmallRuntimeBuilder.createCompositeActivator(registry, + assemblyFactory, + scaBindingFactory, + mapper, + scopeRegistry, + workManager); + + // Start the runtime modules + modules = startModules(registry, classLoader); + + } + + public void stop() throws ActivationException { + + // Stop the runtime modules + stopModules(registry, modules); + + // Stop and destroy the work manager + workManager.destroy(); + + // Cleanup + modules = null; + registry = null; + assemblyFactory = null; + contributionService = null; + compositeActivator = null; + workManager = null; + scopeRegistry = null; + } + + public ContributionService getContributionService() { + return contributionService; + } + + public CompositeActivator getCompositeActivator() { + return compositeActivator; + } + + public AssemblyFactory getAssemblyFactory() { + return assemblyFactory; + } + + @SuppressWarnings("unchecked") + private List<ModuleActivator> startModules(ExtensionPointRegistry registry, ClassLoader classLoader) + throws ActivationException { + + // Load and instantiate the modules found on the classpath + List<ModuleActivator> modules = ReallySmallRuntimeBuilder.getServices(classLoader, ModuleActivator.class); + for (ModuleActivator module : modules) { + Object[] extensionPoints = module.getExtensionPoints(); + if (extensionPoints != null) { + for (Object e : extensionPoints) { + registry.addExtensionPoint(e); + } + } + } + + // Start all the extension modules + for (ModuleActivator activator : modules) { + activator.start(registry); + } + + return modules; + } + + private void stopModules(ExtensionPointRegistry registry, List<ModuleActivator> modules) { + for (ModuleActivator module : modules) { + module.stop(registry); + } + } + +} diff --git a/sca-java-1.x/branches/sca-java-0.91/modules/host-embedded/src/main/java/org/apache/tuscany/sca/host/embedded/impl/ReallySmallRuntimeBuilder.java b/sca-java-1.x/branches/sca-java-0.91/modules/host-embedded/src/main/java/org/apache/tuscany/sca/host/embedded/impl/ReallySmallRuntimeBuilder.java new file mode 100644 index 0000000000..fab6ca9af1 --- /dev/null +++ b/sca-java-1.x/branches/sca-java-0.91/modules/host-embedded/src/main/java/org/apache/tuscany/sca/host/embedded/impl/ReallySmallRuntimeBuilder.java @@ -0,0 +1,281 @@ +/* + * 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.host.embedded.impl; + +import java.io.BufferedReader; +import java.io.IOException; +import java.io.InputStream; +import java.io.InputStreamReader; +import java.net.URL; +import java.util.ArrayList; +import java.util.Enumeration; +import java.util.HashSet; +import java.util.List; +import java.util.Set; + +import javax.xml.stream.XMLInputFactory; +import javax.xml.stream.XMLOutputFactory; + +import org.apache.tuscany.sca.assembly.AssemblyFactory; +import org.apache.tuscany.sca.assembly.SCABindingFactory; +import org.apache.tuscany.sca.assembly.xml.ComponentTypeDocumentProcessor; +import org.apache.tuscany.sca.assembly.xml.ComponentTypeProcessor; +import org.apache.tuscany.sca.assembly.xml.CompositeDocumentProcessor; +import org.apache.tuscany.sca.assembly.xml.CompositeProcessor; +import org.apache.tuscany.sca.assembly.xml.ConstrainingTypeDocumentProcessor; +import org.apache.tuscany.sca.assembly.xml.ConstrainingTypeProcessor; +import org.apache.tuscany.sca.contribution.ContributionFactory; +import org.apache.tuscany.sca.contribution.processor.DefaultPackageProcessorExtensionPoint; +import org.apache.tuscany.sca.contribution.processor.DefaultStAXArtifactProcessorExtensionPoint; +import org.apache.tuscany.sca.contribution.processor.DefaultURLArtifactProcessorExtensionPoint; +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.impl.FolderContributionProcessor; +import org.apache.tuscany.sca.contribution.processor.impl.JarContributionProcessor; +import org.apache.tuscany.sca.contribution.service.ContributionRepository; +import org.apache.tuscany.sca.contribution.service.ContributionService; +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.invocation.ExtensibleWireProcessor; +import org.apache.tuscany.sca.core.invocation.JDKProxyService; +import org.apache.tuscany.sca.core.invocation.ProxyFactory; +import org.apache.tuscany.sca.core.runtime.ActivationException; +import org.apache.tuscany.sca.core.runtime.CompositeActivator; +import org.apache.tuscany.sca.core.runtime.CompositeActivatorImpl; +import org.apache.tuscany.sca.core.runtime.RuntimeSCABindingProviderFactory; +import org.apache.tuscany.sca.core.scope.CompositeScopeContainerFactory; +import org.apache.tuscany.sca.core.scope.RequestScopeContainerFactory; +import org.apache.tuscany.sca.core.scope.ScopeRegistryImpl; +import org.apache.tuscany.sca.core.scope.StatelessScopeContainerFactory; +import org.apache.tuscany.sca.core.work.Jsr237WorkScheduler; +import org.apache.tuscany.sca.interfacedef.InterfaceContractMapper; +import org.apache.tuscany.sca.invocation.MessageFactory; +import org.apache.tuscany.sca.policy.PolicyFactory; +import org.apache.tuscany.sca.provider.DefaultProviderFactoryExtensionPoint; +import org.apache.tuscany.sca.provider.ProviderFactoryExtensionPoint; +import org.apache.tuscany.sca.runtime.DefaultWireProcessorExtensionPoint; +import org.apache.tuscany.sca.runtime.RuntimeWireProcessor; +import org.apache.tuscany.sca.runtime.RuntimeWireProcessorExtensionPoint; +import org.apache.tuscany.sca.scope.ScopeContainerFactory; +import org.apache.tuscany.sca.scope.ScopeRegistry; +import org.apache.tuscany.sca.work.WorkScheduler; + +import commonj.work.WorkManager; + +public class ReallySmallRuntimeBuilder { + + public static ProxyFactory createProxyFactory(ExtensionPointRegistry registry, InterfaceContractMapper mapper, MessageFactory messageFactory) { + + // Create a proxy factory + ProxyFactory proxyFactory = new JDKProxyService(messageFactory, mapper); + + // FIXME remove this + registry.addExtensionPoint(proxyFactory); + registry.addExtensionPoint(mapper); + + return proxyFactory; + } + + public static CompositeActivator createCompositeActivator(ExtensionPointRegistry registry, + AssemblyFactory assemblyFactory, + SCABindingFactory scaBindingFactory, + InterfaceContractMapper mapper, + ScopeRegistry scopeRegistry, + WorkManager workManager) { + + // Create a work scheduler + WorkScheduler workScheduler = new Jsr237WorkScheduler(workManager); + registry.addExtensionPoint(workScheduler); + + // Create a wire post processor extension point + RuntimeWireProcessorExtensionPoint wireProcessors = new DefaultWireProcessorExtensionPoint(); + registry.addExtensionPoint(wireProcessors); + RuntimeWireProcessor wireProcessor = new ExtensibleWireProcessor(wireProcessors); + + // Create a provider factory extension point + ProviderFactoryExtensionPoint providerFactories = new DefaultProviderFactoryExtensionPoint(); + registry.addExtensionPoint(providerFactories); + providerFactories.addProviderFactory(new RuntimeSCABindingProviderFactory()); + + // Create the composite activator + CompositeActivator compositeActivator = new CompositeActivatorImpl( + assemblyFactory, scaBindingFactory, + mapper, scopeRegistry, + workScheduler, wireProcessor, + providerFactories); + + return compositeActivator; + } + + /** + * Create the contribution service used by this domain. + * + * @throws ActivationException + */ + public static ContributionService createContributionService(ExtensionPointRegistry registry, + ContributionFactory contributionFactory, + AssemblyFactory assemblyFactory, + PolicyFactory policyFactory, + InterfaceContractMapper mapper) + throws ActivationException { + + XMLInputFactory xmlFactory = XMLInputFactory.newInstance(); + + // Create STAX artifact processor extension point + DefaultStAXArtifactProcessorExtensionPoint staxProcessors = new DefaultStAXArtifactProcessorExtensionPoint(); + registry.addExtensionPoint(staxProcessors); + + // Create and register STAX processors for SCA assembly XML + ExtensibleStAXArtifactProcessor staxProcessor = new ExtensibleStAXArtifactProcessor(staxProcessors, xmlFactory, + XMLOutputFactory + .newInstance()); + staxProcessors.addArtifactProcessor(new CompositeProcessor(assemblyFactory, policyFactory, mapper, + staxProcessor)); + staxProcessors.addArtifactProcessor(new ComponentTypeProcessor(assemblyFactory, policyFactory, staxProcessor)); + staxProcessors + .addArtifactProcessor(new ConstrainingTypeProcessor(assemblyFactory, policyFactory, staxProcessor)); + + // Create URL artifact processor extension point + // FIXME use the interface instead of the class + DefaultURLArtifactProcessorExtensionPoint documentProcessors = new DefaultURLArtifactProcessorExtensionPoint(); + registry.addExtensionPoint(documentProcessors); + + // Create and register document processors for SCA assembly XML + XMLInputFactory inputFactory = XMLInputFactory.newInstance(); + documentProcessors.addArtifactProcessor(new CompositeDocumentProcessor(staxProcessor, inputFactory)); + documentProcessors.addArtifactProcessor(new ComponentTypeDocumentProcessor(staxProcessor, inputFactory)); + documentProcessors.addArtifactProcessor(new ConstrainingTypeDocumentProcessor(staxProcessor, inputFactory)); + + // Create contribution package processor extension point + PackageTypeDescriberImpl describer = new PackageTypeDescriberImpl(); + PackageProcessorExtensionPoint packageProcessors = new DefaultPackageProcessorExtensionPoint(); + PackageProcessor packageProcessor = new ExtensiblePackageProcessor(packageProcessors, describer); + registry.addExtensionPoint(packageProcessors); + + // Register base package processors + packageProcessors.addPackageProcessor(new JarContributionProcessor()); + packageProcessors.addPackageProcessor(new FolderContributionProcessor()); + + // Create a contribution repository + ContributionRepository repository; + try { + repository = new ContributionRepositoryImpl("target"); + } catch (IOException e) { + throw new ActivationException(e); + } + + ExtensibleURLArtifactProcessor documentProcessor = new ExtensibleURLArtifactProcessor(documentProcessors); + ContributionService contributionService = new ContributionServiceImpl(repository, packageProcessor, + documentProcessor, assemblyFactory, + contributionFactory, xmlFactory); + return contributionService; + } + + public static ScopeRegistry createScopeRegistry(ExtensionPointRegistry registry) { + ScopeRegistry scopeRegistry = new ScopeRegistryImpl(); + ScopeContainerFactory[] factories = new ScopeContainerFactory[] {new CompositeScopeContainerFactory(), + new StatelessScopeContainerFactory(), + new RequestScopeContainerFactory(), + // new ConversationalScopeContainer(monitor), + // new HttpSessionScopeContainer(monitor) + }; + for (ScopeContainerFactory f : factories) { + scopeRegistry.register(f); + } + + registry.addExtensionPoint(scopeRegistry); + + return scopeRegistry; + } + + /** + * 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 Set<String> getServiceClassNames(ClassLoader classLoader, String name) throws IOException { + Set<String> set = new HashSet<String>(); + Enumeration<URL> urls = classLoader.getResources("META-INF/services/" + name); + while (urls.hasMoreElements()) { + URL url = urls.nextElement(); + Set<String> service = getServiceClassNames(url); + if (service != null) { + set.addAll(service); + + } + } + return set; + } + + private static Set<String> getServiceClassNames(URL url) throws IOException { + Set<String> names = new HashSet<String>(); + InputStream is = url.openStream(); + BufferedReader reader = null; + try { + reader = new BufferedReader(new InputStreamReader(is)); + while (true) { + String line = reader.readLine(); + if (line == null) { + break; + } + line = line.trim(); + if (!line.startsWith("#") && !"".equals(line)) { + names.add(line.trim()); + } + } + } finally { + if (reader != null) { + reader.close(); + } + + if (is != null){ + try { + is.close(); + } catch( IOException ioe) { + //ignore + } + } + } + return names; + } + + public static <T> List<T> getServices(final ClassLoader classLoader, Class<T> serviceClass) { + List<T> instances = new ArrayList<T>(); + try { + Set<String> services = getServiceClassNames(classLoader, serviceClass.getName()); + for (String className : services) { + Class cls = Class.forName(className, true, classLoader); + instances.add(serviceClass.cast(cls.newInstance())); + } + } catch (Exception e) { + throw new IllegalStateException(e); + } + return instances; + } + +} |