You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@tuscany.apache.org by js...@apache.org on 2007/05/05 05:45:12 UTC
svn commit: r535441 - in /incubator/tuscany/java/sca/modules:
contribution-impl/src/main/java/org/apache/tuscany/contribution/service/impl/
host-embedded/src/main/java/org/apache/tuscany/host/embedded/
host-embedded/src/main/java/org/apache/tuscany/hos...
Author: jsdelfino
Date: Fri May 4 20:45:11 2007
New Revision: 535441
URL: http://svn.apache.org/viewvc?view=rev&rev=535441
Log:
First cut of a simpler runtime bootstrap, which does not use any statics, and provides methods to get services available at the domain level.
Added:
incubator/tuscany/java/sca/modules/host-embedded/src/main/java/org/apache/tuscany/host/embedded/SCADomain.java (with props)
incubator/tuscany/java/sca/modules/host-embedded/src/main/java/org/apache/tuscany/host/embedded/impl/DefaultSCADomain.java (with props)
incubator/tuscany/java/sca/modules/host-embedded/src/main/java/org/apache/tuscany/host/embedded/impl/ReallySmallRuntime.java (with props)
incubator/tuscany/java/sca/modules/host-embedded/src/main/java/org/apache/tuscany/host/embedded/impl/ReallySmallRuntimeBuilder.java (with props)
Modified:
incubator/tuscany/java/sca/modules/contribution-impl/src/main/java/org/apache/tuscany/contribution/service/impl/ContributionServiceImpl.java
incubator/tuscany/java/sca/modules/host-embedded/src/main/java/org/apache/tuscany/host/embedded/impl/MiniRuntimeImpl.java
Modified: incubator/tuscany/java/sca/modules/contribution-impl/src/main/java/org/apache/tuscany/contribution/service/impl/ContributionServiceImpl.java
URL: http://svn.apache.org/viewvc/incubator/tuscany/java/sca/modules/contribution-impl/src/main/java/org/apache/tuscany/contribution/service/impl/ContributionServiceImpl.java?view=diff&rev=535441&r1=535440&r2=535441
==============================================================================
--- incubator/tuscany/java/sca/modules/contribution-impl/src/main/java/org/apache/tuscany/contribution/service/impl/ContributionServiceImpl.java (original)
+++ incubator/tuscany/java/sca/modules/contribution-impl/src/main/java/org/apache/tuscany/contribution/service/impl/ContributionServiceImpl.java Fri May 4 20:45:11 2007
@@ -99,14 +99,15 @@
URLArtifactProcessor artifactProcessor,
ArtifactResolver artifactResolver,
AssemblyFactory assemblyFactory,
- ContributionFactory contributionFactory) {
+ ContributionFactory contributionFactory,
+ XMLInputFactory xmlFactory) {
super();
this.contributionRepository = repository;
this.packageProcessor = packageProcessor;
this.artifactProcessor = artifactProcessor;
this.artifactResolver = artifactResolver;
+ this.xmlFactory = xmlFactory;
- this.xmlFactory = XMLInputFactory.newInstance("javax.xml.stream.XMLInputFactory", getClass().getClassLoader());
this.contributionFactory = contributionFactory;
this.contributionLoader = new ContributionMetadataLoaderImpl(assemblyFactory, contributionFactory);
}
Added: incubator/tuscany/java/sca/modules/host-embedded/src/main/java/org/apache/tuscany/host/embedded/SCADomain.java
URL: http://svn.apache.org/viewvc/incubator/tuscany/java/sca/modules/host-embedded/src/main/java/org/apache/tuscany/host/embedded/SCADomain.java?view=auto&rev=535441
==============================================================================
--- incubator/tuscany/java/sca/modules/host-embedded/src/main/java/org/apache/tuscany/host/embedded/SCADomain.java (added)
+++ incubator/tuscany/java/sca/modules/host-embedded/src/main/java/org/apache/tuscany/host/embedded/SCADomain.java Fri May 4 20:45:11 2007
@@ -0,0 +1,176 @@
+/*
+ * 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.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 org.apache.tuscany.host.embedded.impl.DefaultSCADomain;
+import org.osoa.sca.CallableReference;
+import org.osoa.sca.ServiceReference;
+import org.osoa.sca.ServiceRuntimeException;
+
+/**
+ * A handle to an SCA domain.
+ *
+ * @version $Rev$ $Date$
+ */
+public abstract class SCADomain {
+
+ /**
+ * Returns a new instance of an SCA domain. The given deployable composites will
+ * be included in the SCA domain and the contributions that contirbute them will
+ * be made available in the domain as well.
+ *
+ * @param uri the URI of the SCA domain
+ * @param composites the deployable composites to include in the SCA domain.
+ * @return
+ */
+ public static SCADomain newInstance(String domainURI, String contributionLocation, String...composites) {
+ return newInstance(SCADomain.class.getClassLoader(), domainURI, contributionLocation, composites);
+ }
+
+ /**
+ * Close the SCA domain.
+ */
+ public abstract void close();
+
+ /**
+ * Returns the URI of the SCA Domain.
+ *
+ * @return the URI of the SCA Domain
+ */
+ public abstract String getURI();
+
+ /**
+ * Cast a type-safe reference to a CallableReference.
+ * Converts a type-safe reference to an equivalent CallableReference; if the target refers to a service
+ * then a ServiceReference will be returned, if the target refers to a callback then a CallableReference
+ * will be returned.
+ *
+ * @param target a reference proxy provided by the SCA runtime
+ * @param <B> the Java type of the business interface for the reference
+ * @param <R> the type of reference to be returned
+ * @return a CallableReference equivalent for the proxy
+ * @throws IllegalArgumentException if the supplied instance is not a reference supplied by the SCA runtime
+ */
+ public abstract <B, R extends CallableReference<B>> R cast(B target) throws IllegalArgumentException;
+
+ /**
+ * Returns a proxy for a service provided by a component in the SCA domain.
+ *
+ * @param businessInterface the interface that will be used to invoke the service
+ * @param serviceName the name of the service
+ * @param <B> the Java type of the business interface for the service
+ * @return an object that implements the business interface
+ */
+ public abstract <B> B getService(Class<B> businessInterface, String serviceName);
+
+ /**
+ * Returns a ServiceReference for a service provided by a component in the SCA domain.
+ *
+ * @param businessInterface the interface that will be used to invoke the service
+ * @param serviceName the name of the service
+ * @param <B> the Java type of the business interface for the service
+ * @return a ServiceReference for the designated service
+ */
+ public abstract <B> ServiceReference<B> getServiceReference(Class<B> businessInterface, String referenceName);
+
+ /**
+ * 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(ClassLoader classLoader, String name) throws IOException {
+ InputStream is = classLoader.getResourceAsStream("META-INF/services/" + name);
+ 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.host.embedded.SCADomain" is set, its value is used as the name
+ * of the implementation class. Otherwise, if the resource
+ * "META-INF/services/org.apache.tuscany.host.embedded.SCADomain" can be loaded from
+ * the supplied classloader. Otherwise, it will use
+ * "org.apache.tuscany.host.embedded.impl.DefaultSCADomain" as the default. The
+ * named class is loaded from the supplied classloader.
+ *
+ * @param classLoader
+ * @param domainURI
+ * @param contributionLocation
+ * @param composites
+ * @return
+ */
+ private static SCADomain newInstance(
+ final ClassLoader classLoader,
+ String domainURI, String contributionLocation, String...composites) {
+
+ try {
+ 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(classLoader, name);
+ }
+ if (className == null) {
+ return new DefaultSCADomain(domainURI, contributionLocation, composites);
+ }
+ Class cls = Class.forName(className, true, classLoader);
+ Constructor<?> constructor = cls.getConstructor(String.class, String.class, String[].class);
+ SCADomain domain = (SCADomain)constructor.newInstance(domainURI, contributionLocation, composites);
+ return domain;
+
+ } catch (Exception e) {
+ throw new ServiceRuntimeException(e);
+ }
+ }
+
+}
Propchange: incubator/tuscany/java/sca/modules/host-embedded/src/main/java/org/apache/tuscany/host/embedded/SCADomain.java
------------------------------------------------------------------------------
svn:eol-style = native
Propchange: incubator/tuscany/java/sca/modules/host-embedded/src/main/java/org/apache/tuscany/host/embedded/SCADomain.java
------------------------------------------------------------------------------
svn:keywords = Rev Date
Added: incubator/tuscany/java/sca/modules/host-embedded/src/main/java/org/apache/tuscany/host/embedded/impl/DefaultSCADomain.java
URL: http://svn.apache.org/viewvc/incubator/tuscany/java/sca/modules/host-embedded/src/main/java/org/apache/tuscany/host/embedded/impl/DefaultSCADomain.java?view=auto&rev=535441
==============================================================================
--- incubator/tuscany/java/sca/modules/host-embedded/src/main/java/org/apache/tuscany/host/embedded/impl/DefaultSCADomain.java (added)
+++ incubator/tuscany/java/sca/modules/host-embedded/src/main/java/org/apache/tuscany/host/embedded/impl/DefaultSCADomain.java Fri May 4 20:45:11 2007
@@ -0,0 +1,230 @@
+/*
+ * 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.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.assembly.AssemblyFactory;
+import org.apache.tuscany.assembly.Component;
+import org.apache.tuscany.assembly.Composite;
+import org.apache.tuscany.contribution.Contribution;
+import org.apache.tuscany.contribution.service.ContributionException;
+import org.apache.tuscany.contribution.service.ContributionService;
+import org.apache.tuscany.contribution.service.util.FileHelper;
+import org.apache.tuscany.core.runtime.ActivationException;
+import org.apache.tuscany.core.runtime.CompositeActivator;
+import org.apache.tuscany.core.runtime.DefaultCompositeActivator;
+import org.apache.tuscany.host.embedded.SCADomain;
+import org.apache.tuscany.interfacedef.IncompatibleInterfaceContractException;
+import org.apache.tuscany.spi.Scope;
+import org.apache.tuscany.spi.component.GroupInitializationException;
+import org.apache.tuscany.spi.component.WorkContextTunnel;
+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;
+
+public class DefaultSCADomain extends SCADomain {
+
+ private String domainURI;
+ private String location;
+ private String[] composites;
+ private Composite domainComposite;
+ private Contribution contribution;
+ private Map<String, ComponentContext> components = new HashMap<String, ComponentContext>();
+
+ private ReallySmallRuntime runtime;
+
+ public DefaultSCADomain(String domainURI, String contributionLocation, String... composites) {
+ this.domainURI = domainURI;
+ this.location = contributionLocation;
+ this.composites = composites;
+
+ ClassLoader runtimeClassLoader = getClass().getClassLoader();
+ runtime = new ReallySmallRuntime(runtimeClassLoader);
+
+ try {
+ runtime.start();
+
+ } catch (ActivationException e) {
+ throw new ServiceRuntimeException(e);
+ }
+
+ // Contribute the given contribution to an in-memory repository
+ ClassLoader applicationClassLoader = Thread.currentThread().getContextClassLoader();
+ ContributionService contributionService = runtime.getContributionService();
+ URL contributionURL;
+ try {
+ contributionURL = getContributionLocation(location, this.composites, applicationClassLoader);
+ } catch (MalformedURLException e) {
+ throw new ServiceRuntimeException(e);
+ }
+
+ URI contributionURI = URI.create("sca://default/");
+ try {
+ contributionService.contribute(contributionURI, contributionURL, false);
+ } catch (ContributionException e) {
+ throw new ServiceRuntimeException(e);
+ } catch (IOException e) {
+ throw new ServiceRuntimeException(e);
+ }
+ contribution = contributionService.getContribution(contributionURI);
+
+ // 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);
+
+ // Add the deployable composites to the SCA domain by "include"
+ for (Composite composite : contribution.getDeployables()) {
+ domainComposite.getIncludes().add(composite);
+ }
+
+ // Activate and start the SCA domain composite
+ CompositeActivator compositeActivator = runtime.getCompositeActivator();
+ try {
+ ((DefaultCompositeActivator)compositeActivator).activate(domainComposite);
+ compositeActivator.start(domainComposite);
+ } catch (IncompatibleInterfaceContractException e) {
+ throw new ServiceRuntimeException(e);
+ } catch (ActivationException e) {
+ throw new ServiceRuntimeException(e);
+ }
+
+ //FIXME remove this
+ runtime.startDomainWorkContext(domainComposite);
+
+ // Index the top level components
+ for (Component component: domainComposite.getComponents()) {
+ components.put(component.getName(), (ComponentContext)component);
+ }
+ }
+
+ @Override
+ public void close() {
+
+ // Remove the contribution from the in-memory repository
+ ContributionService contributionService = runtime.getContributionService();
+ try {
+ contributionService.remove(URI.create(location));
+ } 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);
+ }
+ }
+
+ private URL getContributionLocation(String contributionPath, String[] composites, ClassLoader classLoader) throws MalformedURLException {
+ URI contributionURI = URI.create(contributionPath);
+ if (contributionURI.isAbsolute() || composites.length == 0) {
+ return new URL(contributionPath);
+ }
+
+ String compositePath = composites[0];
+ URL compositeURL = classLoader.getResource(compositePath);
+ if (compositeURL == null) {
+ throw new IllegalArgumentException("Composite not found: " + compositePath);
+ }
+
+ URL contributionURL = null;
+ // "jar:file://....../something.jar!/a/b/c/app.composite"
+ try {
+ String scdlUrl = compositeURL.toExternalForm();
+ String protocol = compositeURL.getProtocol();
+ if ("file".equals(protocol)) {
+ // directory contribution
+ if (scdlUrl.endsWith(compositePath)) {
+ String location = scdlUrl.substring(0, scdlUrl.lastIndexOf(compositePath));
+ // workaround from evil url/uri form maven
+ contributionURL = FileHelper.toFile(new URL(location)).toURI().toURL();
+ }
+
+ } else if ("jar".equals(protocol)) {
+ // jar contribution
+ String location = scdlUrl.substring(4, scdlUrl.lastIndexOf("!/"));
+ // workaround from evil url/uri form 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) {
+ return getServiceReference(businessInterface, serviceName).getService();
+ }
+
+ @Override
+ public <B> ServiceReference<B> getServiceReference(Class<B> businessInterface, String serviceName) {
+ int i = serviceName.indexOf('/');
+ if (i != -1) {
+ String componentName = serviceName.substring(0, i);
+ serviceName = serviceName.substring(i+1);
+
+ ComponentContext componentContext = components.get(componentName);
+ ServiceReference<B> serviceReference = componentContext.createSelfReference(businessInterface, serviceName);
+ return serviceReference;
+
+ } else {
+ ComponentContext componentContext = components.get(serviceName);
+ ServiceReference<B> serviceReference = componentContext.createSelfReference(businessInterface);
+ return serviceReference;
+ }
+ }
+
+ @Override
+ public String getURI() {
+ return domainURI;
+ }
+
+}
Propchange: incubator/tuscany/java/sca/modules/host-embedded/src/main/java/org/apache/tuscany/host/embedded/impl/DefaultSCADomain.java
------------------------------------------------------------------------------
svn:eol-style = native
Propchange: incubator/tuscany/java/sca/modules/host-embedded/src/main/java/org/apache/tuscany/host/embedded/impl/DefaultSCADomain.java
------------------------------------------------------------------------------
svn:keywords = Rev Date
Modified: incubator/tuscany/java/sca/modules/host-embedded/src/main/java/org/apache/tuscany/host/embedded/impl/MiniRuntimeImpl.java
URL: http://svn.apache.org/viewvc/incubator/tuscany/java/sca/modules/host-embedded/src/main/java/org/apache/tuscany/host/embedded/impl/MiniRuntimeImpl.java?view=diff&rev=535441&r1=535440&r2=535441
==============================================================================
--- incubator/tuscany/java/sca/modules/host-embedded/src/main/java/org/apache/tuscany/host/embedded/impl/MiniRuntimeImpl.java (original)
+++ incubator/tuscany/java/sca/modules/host-embedded/src/main/java/org/apache/tuscany/host/embedded/impl/MiniRuntimeImpl.java Fri May 4 20:45:11 2007
@@ -154,7 +154,8 @@
ContributionService contributionService = new ContributionServiceImpl(repository, packageProcessors,
documentProcessors, artifactResolver,
assemblyFactory,
- new DefaultContributionFactory());
+ new DefaultContributionFactory(),
+ xmlFactory);
return contributionService;
}
Added: incubator/tuscany/java/sca/modules/host-embedded/src/main/java/org/apache/tuscany/host/embedded/impl/ReallySmallRuntime.java
URL: http://svn.apache.org/viewvc/incubator/tuscany/java/sca/modules/host-embedded/src/main/java/org/apache/tuscany/host/embedded/impl/ReallySmallRuntime.java?view=auto&rev=535441
==============================================================================
--- incubator/tuscany/java/sca/modules/host-embedded/src/main/java/org/apache/tuscany/host/embedded/impl/ReallySmallRuntime.java (added)
+++ incubator/tuscany/java/sca/modules/host-embedded/src/main/java/org/apache/tuscany/host/embedded/impl/ReallySmallRuntime.java Fri May 4 20:45:11 2007
@@ -0,0 +1,162 @@
+/*
+ * 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.host.embedded.impl;
+
+import java.net.URI;
+import java.util.List;
+import java.util.Map;
+
+import org.apache.tuscany.assembly.AssemblyFactory;
+import org.apache.tuscany.assembly.Composite;
+import org.apache.tuscany.contribution.service.ContributionService;
+import org.apache.tuscany.core.DefaultExtensionPointRegistry;
+import org.apache.tuscany.core.ExtensionPointRegistry;
+import org.apache.tuscany.core.ModuleActivator;
+import org.apache.tuscany.core.runtime.ActivationException;
+import org.apache.tuscany.core.runtime.CompositeActivator;
+import org.apache.tuscany.core.runtime.RuntimeAssemblyFactory;
+import org.apache.tuscany.core.work.ThreadPoolWorkManager;
+import org.apache.tuscany.interfacedef.InterfaceContractMapper;
+import org.apache.tuscany.interfacedef.impl.DefaultInterfaceContractMapper;
+import org.apache.tuscany.invocation.ProxyFactory;
+import org.apache.tuscany.policy.PolicyFactory;
+import org.apache.tuscany.policy.impl.DefaultPolicyFactory;
+import org.apache.tuscany.scope.ScopeRegistry;
+import org.apache.tuscany.spi.Scope;
+import org.apache.tuscany.spi.component.GroupInitializationException;
+import org.apache.tuscany.spi.component.WorkContext;
+import org.apache.tuscany.spi.component.WorkContextTunnel;
+
+public class ReallySmallRuntime {
+
+ private List<ModuleActivator> modules;
+ private ExtensionPointRegistry registry;
+
+ private ClassLoader classLoader;
+ private AssemblyFactory assemblyFactory;
+ private ContributionService contributionService;
+ private CompositeActivator compositeActivator;
+ private WorkContext workContext;
+ 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 context
+ workContext = ReallySmallRuntimeBuilder.createWorkContext(registry);
+
+ // Create a work manager
+ workManager = new ThreadPoolWorkManager(10);
+
+ // Create an interface contract mapper
+ InterfaceContractMapper mapper = new DefaultInterfaceContractMapper();
+
+ // Create a proxy factory
+ ProxyFactory proxyFactory = ReallySmallRuntimeBuilder.createProxyFactory(registry, workContext, mapper);
+
+ // Create model factories
+ assemblyFactory = new RuntimeAssemblyFactory(proxyFactory);
+ PolicyFactory policyFactory = new DefaultPolicyFactory();
+
+ // Create a contribution service
+ contributionService = ReallySmallRuntimeBuilder.createContributionService(registry, assemblyFactory, policyFactory, mapper, classLoader);
+
+ // Create a composite activator
+ compositeActivator = ReallySmallRuntimeBuilder.createCompositeActivator(registry, assemblyFactory, mapper, workContext, workManager);
+
+ scopeRegistry = ReallySmallRuntimeBuilder.createScopeRegistry(registry);
+
+ // Start the runtime modules
+ modules = startModules(registry, classLoader);
+
+ }
+
+ public void stop() throws ActivationException {
+
+ //FIXME remove this
+ workContext.setIdentifier(Scope.COMPOSITE, null);
+
+ // Stop and destroy the work manager
+ workManager.destroy();
+
+ // Stop the runtime modules
+ stopModules(registry, modules);
+ }
+
+ 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) {
+ Map<Class, Object> extensionPoints = module.getExtensionPoints();
+ if (extensionPoints != null) {
+ for (Map.Entry<Class, Object> e : extensionPoints.entrySet()) {
+ registry.addExtensionPoint(e.getKey(), e.getValue());
+ }
+ }
+ }
+
+ // 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);
+ }
+ }
+
+ //FIXME Remove this
+ public void startDomainWorkContext(Composite domain) {
+ workContext.setIdentifier(Scope.COMPOSITE, domain);
+ WorkContextTunnel.setThreadWorkContext(workContext);
+ try {
+ scopeRegistry.getScopeContainer(Scope.COMPOSITE).startContext(domain, URI.create("/"));
+ } catch (GroupInitializationException e) {
+ // TODO Auto-generated catch block
+ e.printStackTrace();
+ }
+ }
+
+}
Propchange: incubator/tuscany/java/sca/modules/host-embedded/src/main/java/org/apache/tuscany/host/embedded/impl/ReallySmallRuntime.java
------------------------------------------------------------------------------
svn:eol-style = native
Propchange: incubator/tuscany/java/sca/modules/host-embedded/src/main/java/org/apache/tuscany/host/embedded/impl/ReallySmallRuntime.java
------------------------------------------------------------------------------
svn:keywords = Rev Date
Added: incubator/tuscany/java/sca/modules/host-embedded/src/main/java/org/apache/tuscany/host/embedded/impl/ReallySmallRuntimeBuilder.java
URL: http://svn.apache.org/viewvc/incubator/tuscany/java/sca/modules/host-embedded/src/main/java/org/apache/tuscany/host/embedded/impl/ReallySmallRuntimeBuilder.java?view=auto&rev=535441
==============================================================================
--- incubator/tuscany/java/sca/modules/host-embedded/src/main/java/org/apache/tuscany/host/embedded/impl/ReallySmallRuntimeBuilder.java (added)
+++ incubator/tuscany/java/sca/modules/host-embedded/src/main/java/org/apache/tuscany/host/embedded/impl/ReallySmallRuntimeBuilder.java Fri May 4 20:45:11 2007
@@ -0,0 +1,268 @@
+/*
+ * 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.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 org.apache.tuscany.assembly.AssemblyFactory;
+import org.apache.tuscany.assembly.xml.ComponentTypeDocumentProcessor;
+import org.apache.tuscany.assembly.xml.ComponentTypeProcessor;
+import org.apache.tuscany.assembly.xml.CompositeDocumentProcessor;
+import org.apache.tuscany.assembly.xml.CompositeProcessor;
+import org.apache.tuscany.assembly.xml.ConstrainingTypeDocumentProcessor;
+import org.apache.tuscany.assembly.xml.ConstrainingTypeProcessor;
+import org.apache.tuscany.contribution.ContributionFactory;
+import org.apache.tuscany.contribution.impl.DefaultContributionFactory;
+import org.apache.tuscany.contribution.processor.DefaultStAXArtifactProcessorExtensionPoint;
+import org.apache.tuscany.contribution.processor.DefaultURLArtifactProcessorExtensionPoint;
+import org.apache.tuscany.contribution.processor.PackageProcessorExtensionPoint;
+import org.apache.tuscany.contribution.processor.StAXArtifactProcessorExtensionPoint;
+import org.apache.tuscany.contribution.processor.URLArtifactProcessorExtensionPoint;
+import org.apache.tuscany.contribution.processor.impl.DefaultPackageProcessorExtensionPoint;
+import org.apache.tuscany.contribution.processor.impl.FolderContributionProcessor;
+import org.apache.tuscany.contribution.processor.impl.JarContributionProcessor;
+import org.apache.tuscany.contribution.resolver.DefaultArtifactResolver;
+import org.apache.tuscany.contribution.service.ContributionRepository;
+import org.apache.tuscany.contribution.service.ContributionService;
+import org.apache.tuscany.contribution.service.impl.ContributionRepositoryImpl;
+import org.apache.tuscany.contribution.service.impl.ContributionServiceImpl;
+import org.apache.tuscany.contribution.service.impl.PackageTypeDescriberImpl;
+import org.apache.tuscany.core.ExtensionPointRegistry;
+import org.apache.tuscany.core.WireProcessorExtensionPoint;
+import org.apache.tuscany.core.component.WorkContextImpl;
+import org.apache.tuscany.core.invocation.DefaultWireProcessorExtensionPoint;
+import org.apache.tuscany.core.invocation.JDKProxyService;
+import org.apache.tuscany.core.runtime.ActivationException;
+import org.apache.tuscany.core.runtime.CompositeActivator;
+import org.apache.tuscany.core.runtime.DefaultCompositeActivator;
+import org.apache.tuscany.core.scope.AbstractScopeContainer;
+import org.apache.tuscany.core.scope.CompositeScopeContainer;
+import org.apache.tuscany.core.scope.RequestScopeContainer;
+import org.apache.tuscany.core.scope.ScopeRegistryImpl;
+import org.apache.tuscany.core.scope.StatelessScopeContainer;
+import org.apache.tuscany.core.util.IOHelper;
+import org.apache.tuscany.core.work.Jsr237WorkScheduler;
+import org.apache.tuscany.interfacedef.InterfaceContractMapper;
+import org.apache.tuscany.invocation.ProxyFactory;
+import org.apache.tuscany.policy.PolicyFactory;
+import org.apache.tuscany.scope.ScopeRegistry;
+import org.apache.tuscany.spi.component.WorkContext;
+import org.apache.tuscany.spi.component.WorkContextTunnel;
+import org.apache.tuscany.spi.services.work.WorkScheduler;
+
+import commonj.work.WorkManager;
+
+public class ReallySmallRuntimeBuilder {
+
+ public static WorkContext createWorkContext(ExtensionPointRegistry registry) {
+
+ // Create a work context
+ WorkContext workContext = new WorkContextImpl();
+ registry.addExtensionPoint(WorkContext.class, workContext);
+ WorkContextTunnel.setThreadWorkContext(workContext);
+ return workContext;
+ }
+
+ public static ProxyFactory createProxyFactory(ExtensionPointRegistry registry,
+ WorkContext workContext, InterfaceContractMapper mapper) {
+
+ // Create a proxy factory
+ ProxyFactory proxyFactory = new JDKProxyService(workContext, mapper);
+
+ //FIXME remove this
+ registry.addExtensionPoint(ProxyFactory.class, proxyFactory);
+
+ return proxyFactory;
+ }
+
+ public static CompositeActivator createCompositeActivator(ExtensionPointRegistry registry,
+ AssemblyFactory assemblyFactory,
+ InterfaceContractMapper mapper,
+ WorkContext workContext,
+ WorkManager workManager) {
+
+ // Create a work scheduler
+ WorkScheduler workScheduler = new Jsr237WorkScheduler(workManager);
+
+ // Create a wire post processor extension point
+ //FIXME do we still need this?
+ WireProcessorExtensionPoint wireProcessorExtensionPoint = new DefaultWireProcessorExtensionPoint();
+ registry.addExtensionPoint(WireProcessorExtensionPoint.class, wireProcessorExtensionPoint);
+
+ // Create the composite activator
+ CompositeActivator compositeActivator = new DefaultCompositeActivator(assemblyFactory, mapper, workContext,
+ workScheduler, wireProcessorExtensionPoint);
+
+ return compositeActivator;
+ }
+
+ /**
+ * Create the contribution service used by this domain.
+ *
+ * @throws ActivationException
+ */
+ public static ContributionService createContributionService(ExtensionPointRegistry registry,
+ AssemblyFactory assemblyFactory,
+ PolicyFactory policyFactory,
+ InterfaceContractMapper mapper,
+ ClassLoader classLoader) throws ActivationException {
+
+ XMLInputFactory xmlFactory = XMLInputFactory.newInstance();
+
+ // Create STAX artifact processor extension point
+ DefaultStAXArtifactProcessorExtensionPoint staxProcessors = new DefaultStAXArtifactProcessorExtensionPoint();
+ registry.addExtensionPoint(StAXArtifactProcessorExtensionPoint.class, staxProcessors);
+
+ // Create and register STAX processors for SCA assembly XML
+ staxProcessors.addArtifactProcessor(new CompositeProcessor(assemblyFactory, policyFactory, mapper, staxProcessors));
+ staxProcessors.addArtifactProcessor(new ComponentTypeProcessor(assemblyFactory, policyFactory, staxProcessors));
+ staxProcessors.addArtifactProcessor(new ConstrainingTypeProcessor(assemblyFactory, policyFactory, staxProcessors));
+
+ // Create URL artifact processor extension point
+ //FIXME use the interface instead of the class
+ DefaultURLArtifactProcessorExtensionPoint documentProcessors = new DefaultURLArtifactProcessorExtensionPoint();
+ registry.addExtensionPoint(URLArtifactProcessorExtensionPoint.class, documentProcessors);
+
+ // Create and register document processors for SCA assembly XML
+ XMLInputFactory inputFactory = XMLInputFactory.newInstance();
+ documentProcessors.addArtifactProcessor(new CompositeDocumentProcessor(staxProcessors, inputFactory));
+ documentProcessors.addArtifactProcessor(new ComponentTypeDocumentProcessor(staxProcessors, inputFactory));
+ documentProcessors.addArtifactProcessor(new ConstrainingTypeDocumentProcessor(staxProcessors, inputFactory));
+
+ // Create contribution package processor extension point
+ PackageTypeDescriberImpl describer = new PackageTypeDescriberImpl();
+ PackageProcessorExtensionPoint packageProcessors = new DefaultPackageProcessorExtensionPoint(describer);
+ registry.addExtensionPoint(PackageProcessorExtensionPoint.class, packageProcessors);
+
+ // Register base package processors
+ new JarContributionProcessor(packageProcessors);
+ new FolderContributionProcessor(packageProcessors);
+
+ // Create a contribution repository
+ ContributionRepository repository;
+ try {
+ repository = new ContributionRepositoryImpl("target");
+ } catch (IOException e) {
+ throw new ActivationException(e);
+ }
+
+ //FIXME move artifact resolver to each contribution
+ DefaultArtifactResolver artifactResolver = new DefaultArtifactResolver(classLoader);
+ ContributionFactory contributionFactory = new DefaultContributionFactory();
+ ContributionService contributionService = new ContributionServiceImpl(
+ repository, packageProcessors,
+ documentProcessors, artifactResolver,
+ assemblyFactory,
+ contributionFactory,
+ xmlFactory);
+ return contributionService;
+ }
+
+
+ public static ScopeRegistry createScopeRegistry(ExtensionPointRegistry registry) {
+ ScopeRegistry scopeRegistry = new ScopeRegistryImpl();
+ AbstractScopeContainer[] containers = new AbstractScopeContainer[] {new CompositeScopeContainer(),
+ new StatelessScopeContainer(),
+ new RequestScopeContainer(),
+ // new ConversationalScopeContainer(monitor),
+ // new HttpSessionScopeContainer(monitor)
+ };
+ for (AbstractScopeContainer c : containers) {
+ c.start();
+ scopeRegistry.register(c);
+ }
+
+ registry.addExtensionPoint(ScopeRegistry.class, 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 = IOHelper.getInputStream(url);
+ 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();
+ }
+ }
+ 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;
+ }
+
+}
Propchange: incubator/tuscany/java/sca/modules/host-embedded/src/main/java/org/apache/tuscany/host/embedded/impl/ReallySmallRuntimeBuilder.java
------------------------------------------------------------------------------
svn:eol-style = native
Propchange: incubator/tuscany/java/sca/modules/host-embedded/src/main/java/org/apache/tuscany/host/embedded/impl/ReallySmallRuntimeBuilder.java
------------------------------------------------------------------------------
svn:keywords = Rev Date
---------------------------------------------------------------------
To unsubscribe, e-mail: tuscany-commits-unsubscribe@ws.apache.org
For additional commands, e-mail: tuscany-commits-help@ws.apache.org