diff options
Diffstat (limited to '')
7 files changed, 565 insertions, 269 deletions
diff --git a/sca-java-2.x/trunk/modules/contribution/src/main/java/org/apache/tuscany/sca/contribution/processor/impl/ContributionContentProcessor.java b/sca-java-2.x/trunk/modules/contribution/src/main/java/org/apache/tuscany/sca/contribution/processor/impl/ContributionContentProcessor.java index 6e6346fe47..c0f30cc634 100644 --- a/sca-java-2.x/trunk/modules/contribution/src/main/java/org/apache/tuscany/sca/contribution/processor/impl/ContributionContentProcessor.java +++ b/sca-java-2.x/trunk/modules/contribution/src/main/java/org/apache/tuscany/sca/contribution/processor/impl/ContributionContentProcessor.java @@ -62,7 +62,7 @@ import org.apache.tuscany.sca.monitor.Monitor; * * @version $Rev$ $Date$ */ -public class ContributionContentProcessor implements ExtendedURLArtifactProcessor<Contribution>{ +public class ContributionContentProcessor implements ExtendedURLArtifactProcessor<Contribution> { private ContributionFactory contributionFactory; private ModelResolverExtensionPoint modelResolvers; private FactoryExtensionPoint modelFactories; @@ -72,10 +72,12 @@ public class ContributionContentProcessor implements ExtendedURLArtifactProcesso // Marks pre-resolve phase completed private boolean preResolved = false; - public ContributionContentProcessor(ExtensionPointRegistry extensionPoints, StAXArtifactProcessor<Object> extensionProcessor) { + public ContributionContentProcessor(ExtensionPointRegistry extensionPoints, + StAXArtifactProcessor<Object> extensionProcessor) { this.modelFactories = extensionPoints.getExtensionPoint(FactoryExtensionPoint.class); this.modelResolvers = extensionPoints.getExtensionPoint(ModelResolverExtensionPoint.class); - URLArtifactProcessorExtensionPoint artifactProcessors = extensionPoints.getExtensionPoint(URLArtifactProcessorExtensionPoint.class); + URLArtifactProcessorExtensionPoint artifactProcessors = + extensionPoints.getExtensionPoint(URLArtifactProcessorExtensionPoint.class); this.artifactProcessor = new ExtensibleURLArtifactProcessor(artifactProcessors); this.extensionProcessor = extensionProcessor; this.contributionFactory = modelFactories.getFactory(ContributionFactory.class); @@ -91,12 +93,12 @@ public class ContributionContentProcessor implements ExtendedURLArtifactProcesso } private File toFile(URL url) { - if("file".equalsIgnoreCase(url.getProtocol())) { + if ("file".equalsIgnoreCase(url.getProtocol())) { try { return new File(url.toURI()); - } catch(URISyntaxException e) { + } catch (URISyntaxException e) { return new File(url.getPath()); - } catch(IllegalArgumentException e) { + } catch (IllegalArgumentException e) { // Hack for file:./a.txt or file:../a/c.wsdl return new File(url.getPath()); } @@ -104,102 +106,111 @@ public class ContributionContentProcessor implements ExtendedURLArtifactProcesso return null; } - public Contribution read(URL parentURL, URI contributionURI, URL contributionURL, ProcessorContext context) throws ContributionReadException { + public Contribution read(URL parentURL, URI contributionURI, URL contributionURL, ProcessorContext context) + throws ContributionReadException { - // Create contribution model Contribution contribution = contributionFactory.createContribution(); contribution.setURI(contributionURI.toString()); - contribution.setLocation(contributionURL.toString()); + if (contributionURL != null) { + contribution.setLocation(contributionURL.toString()); + } ModelResolver modelResolver = new ExtensibleModelResolver(contribution, modelResolvers, modelFactories); contribution.setModelResolver(modelResolver); contribution.setUnresolved(true); - + Monitor monitor = context.getMonitor(); monitor.pushContext("Contribution: " + contribution.getURI()); Contribution old = context.setContribution(contribution); try { - // Create a contribution scanner - ContributionScanner scanner = scanners.getContributionScanner(contributionURL.getProtocol()); - if (scanner == null) { - File file = toFile(contributionURL); - if (file != null && file.isDirectory()) { - scanner = new DirectoryContributionScanner(contributionFactory); - } else { - scanner = new JarContributionScanner(contributionFactory); + if (contributionURL != null) { + // Create a contribution scanner + ContributionScanner scanner = scanners.getContributionScanner(contributionURL.getProtocol()); + if (scanner == null) { + File file = toFile(contributionURL); + if (file != null && file.isDirectory()) { + scanner = new DirectoryContributionScanner(contributionFactory); + } else { + scanner = new JarContributionScanner(contributionFactory); + } } - } - - // Scan the contribution and list the artifacts contained in it - boolean contributionMetadata = false; - List<Artifact> artifacts = scanner.scan(contribution); - for (Artifact artifact : artifacts) { - // Add the deployed artifact model to the contribution - modelResolver.addModel(artifact, context); - - monitor.pushContext("Artifact: " + artifact.getURI()); - - Artifact oldArtifact = context.setArtifact(artifact); - try { - // Read each artifact - URL artifactLocationURL = null; + + // Scan the contribution and list the artifacts contained in it + boolean contributionMetadata = false; + List<Artifact> artifacts = scanner.scan(contribution); + for (Artifact artifact : artifacts) { + // Add the deployed artifact model to the contribution + modelResolver.addModel(artifact, context); + + monitor.pushContext("Artifact: " + artifact.getURI()); + + Artifact oldArtifact = context.setArtifact(artifact); try { - artifactLocationURL = new URL(artifact.getLocation()); - } catch(MalformedURLException e) { - //ignore - } - - Object model = artifactProcessor.read(contributionURL, URI.create(artifact.getURI()), artifactLocationURL, context); - if (model != null) { - artifact.setModel(model); - - // Add the loaded model to the model resolver - modelResolver.addModel(model, context); - - // Merge contribution metadata into the contribution model - if (model instanceof ContributionMetadata) { - contributionMetadata = true; - ContributionMetadata c = (ContributionMetadata)model; - contribution.getImports().addAll(c.getImports()); - contribution.getExports().addAll(c.getExports()); - contribution.getDeployables().addAll(c.getDeployables()); - contribution.getExtensions().addAll(c.getExtensions()); - contribution.getAttributeExtensions().addAll(c.getAttributeExtensions()); + // Read each artifact + URL artifactLocationURL = null; + try { + artifactLocationURL = new URL(artifact.getLocation()); + } catch (MalformedURLException e) { + //ignore } - } - } finally { - monitor.popContext(); - context.setArtifact(oldArtifact); - } - } - - List<Artifact> contributionArtifacts = contribution.getArtifacts(); - contributionArtifacts.addAll(artifacts); - - // If no sca-contribution.xml file was provided then just consider - // all composites in the contribution as deployables - if (!contributionMetadata) { - for (Artifact artifact: artifacts) { - if (artifact.getModel() instanceof Composite) { - contribution.getDeployables().add((Composite)artifact.getModel()); + + Object model = + artifactProcessor.read(contributionURL, + URI.create(artifact.getURI()), + artifactLocationURL, + context); + if (model != null) { + artifact.setModel(model); + + // Add the loaded model to the model resolver + modelResolver.addModel(model, context); + + // Merge contribution metadata into the contribution model + if (model instanceof ContributionMetadata) { + contributionMetadata = true; + ContributionMetadata c = (ContributionMetadata)model; + contribution.getImports().addAll(c.getImports()); + contribution.getExports().addAll(c.getExports()); + contribution.getDeployables().addAll(c.getDeployables()); + contribution.getExtensions().addAll(c.getExtensions()); + contribution.getAttributeExtensions().addAll(c.getAttributeExtensions()); + } + } + } finally { + monitor.popContext(); + context.setArtifact(oldArtifact); } } - - // Add default contribution import and export - DefaultImport defaultImport = contributionFactory.createDefaultImport(); - defaultImport.setModelResolver(new DefaultModelResolver()); - contribution.getImports().add(defaultImport); - DefaultExport defaultExport = contributionFactory.createDefaultExport(); - contribution.getExports().add(defaultExport); - } else { - if (contribution.getDeployables().size() > 0) { - // Update the deployable Composite objects with the correct Composite object for the artifact - for (Artifact a : contribution.getArtifacts()) { - if (a.getModel() instanceof Composite) { - for (ListIterator<Composite> lit = contribution.getDeployables().listIterator(); lit.hasNext();) { - if (lit.next().getName().equals(((Composite)a.getModel()).getName())) { - lit.set((Composite)a.getModel()); + + List<Artifact> contributionArtifacts = contribution.getArtifacts(); + contributionArtifacts.addAll(artifacts); + + // If no sca-contribution.xml file was provided then just consider + // all composites in the contribution as deployables + if (!contributionMetadata) { + for (Artifact artifact : artifacts) { + if (artifact.getModel() instanceof Composite) { + contribution.getDeployables().add((Composite)artifact.getModel()); + } + } + + // Add default contribution import and export + DefaultImport defaultImport = contributionFactory.createDefaultImport(); + defaultImport.setModelResolver(new DefaultModelResolver()); + contribution.getImports().add(defaultImport); + DefaultExport defaultExport = contributionFactory.createDefaultExport(); + contribution.getExports().add(defaultExport); + } else { + if (contribution.getDeployables().size() > 0) { + // Update the deployable Composite objects with the correct Composite object for the artifact + for (Artifact a : contribution.getArtifacts()) { + if (a.getModel() instanceof Composite) { + for (ListIterator<Composite> lit = contribution.getDeployables().listIterator(); lit + .hasNext();) { + if (lit.next().getName().equals(((Composite)a.getModel()).getName())) { + lit.set((Composite)a.getModel()); + } } } } @@ -210,7 +221,7 @@ public class ContributionContentProcessor implements ExtendedURLArtifactProcesso monitor.popContext(); context.setContribution(old); } - + return contribution; } @@ -222,7 +233,8 @@ public class ContributionContentProcessor implements ExtendedURLArtifactProcesso * @param resolver - the Resolver to use * @throws ContributionResolveException */ - public void preResolve(Contribution contribution, ModelResolver resolver, ProcessorContext context) throws ContributionResolveException { + public void preResolve(Contribution contribution, ModelResolver resolver, ProcessorContext context) + throws ContributionResolveException { // Resolve the contribution model itself ModelResolver contributionResolver = contribution.getModelResolver(); contribution.setUnresolved(false); @@ -236,47 +248,50 @@ public class ContributionContentProcessor implements ExtendedURLArtifactProcesso preResolved = true; } // end method preResolve - public void resolve(Contribution contribution, ModelResolver resolver, ProcessorContext context) throws ContributionResolveException { + public void resolve(Contribution contribution, ModelResolver resolver, ProcessorContext context) + throws ContributionResolveException { Monitor monitor = context.getMonitor(); Contribution old = context.setContribution(contribution); - try { - monitor.pushContext("Contribution: " + contribution.getURI()); - - if( !preResolved ) preResolve( contribution, resolver, context); - ModelResolver contributionResolver = contribution.getModelResolver(); - - // Validate Java Exports: [JCI100007] A Java package that is specified on an export - // element MUST be contained within the contribution containing the export element. - for (Export export: contribution.getExports()) { - if (export instanceof JavaExport) { - boolean available = false; - String packageName = ((JavaExport)export).getPackage(); - for (Artifact artifact : contribution.getArtifacts()) { - if (packageName.equals(artifact.getURI().replace("/", "."))) - available = true; - } - if (! available) - throw new ContributionResolveException("[JCI100007] A Java package "+ packageName +" that is specified on an export " + - "element MUST be contained within the contribution containing the export element."); - } - } - - // Resolve all artifact models - for (Artifact artifact : contribution.getArtifacts()) { - Object model = artifact.getModel(); - if (model != null) { - Artifact oldArtifact = context.setArtifact(artifact); - try { - artifactProcessor.resolve(model, contributionResolver, context); - } catch (Throwable e) { - throw new ContributionResolveException(e); - } finally { - context.setArtifact(oldArtifact); - } - } - } - + try { + monitor.pushContext("Contribution: " + contribution.getURI()); + + if (!preResolved) + preResolve(contribution, resolver, context); + ModelResolver contributionResolver = contribution.getModelResolver(); + + // Validate Java Exports: [JCI100007] A Java package that is specified on an export + // element MUST be contained within the contribution containing the export element. + for (Export export : contribution.getExports()) { + if (export instanceof JavaExport) { + boolean available = false; + String packageName = ((JavaExport)export).getPackage(); + for (Artifact artifact : contribution.getArtifacts()) { + if (packageName.equals(artifact.getURI().replace("/", "."))) + available = true; + } + if (!available) + throw new ContributionResolveException("[JCI100007] A Java package " + packageName + + " that is specified on an export " + + "element MUST be contained within the contribution containing the export element."); + } + } + + // Resolve all artifact models + for (Artifact artifact : contribution.getArtifacts()) { + Object model = artifact.getModel(); + if (model != null) { + Artifact oldArtifact = context.setArtifact(artifact); + try { + artifactProcessor.resolve(model, contributionResolver, context); + } catch (Throwable e) { + throw new ContributionResolveException(e); + } finally { + context.setArtifact(oldArtifact); + } + } + } + // Resolve deployable composites List<Composite> deployables = contribution.getDeployables(); Artifact oldArtifact = context.setArtifact(contribution); @@ -292,10 +307,10 @@ public class ContributionContentProcessor implements ExtendedURLArtifactProcesso } finally { context.setArtifact(oldArtifact); } - } finally { - monitor.popContext(); - context.setContribution(old); - } // end try + } finally { + monitor.popContext(); + context.setContribution(old); + } // end try } // end method resolve /** @@ -303,8 +318,9 @@ public class ContributionContentProcessor implements ExtendedURLArtifactProcesso * @param contribution * @param resolver */ - private void resolveExports(Contribution contribution, ModelResolver resolver, ProcessorContext context) throws ContributionResolveException { - for (Export export: contribution.getExports()) { + private void resolveExports(Contribution contribution, ModelResolver resolver, ProcessorContext context) + throws ContributionResolveException { + for (Export export : contribution.getExports()) { if (export instanceof DefaultExport) { // Initialize the default export's resolver export.setModelResolver(resolver); @@ -320,8 +336,9 @@ public class ContributionContentProcessor implements ExtendedURLArtifactProcesso * @param contribution * @param resolver */ - private void resolveImports(Contribution contribution, ModelResolver resolver, ProcessorContext context) throws ContributionResolveException { - for (Import import_: contribution.getImports()) { + private void resolveImports(Contribution contribution, ModelResolver resolver, ProcessorContext context) + throws ContributionResolveException { + for (Import import_ : contribution.getImports()) { extensionProcessor.resolve(import_, resolver, context); } // end for } // end method resolveImports diff --git a/sca-java-2.x/trunk/modules/core-spi/src/main/java/org/apache/tuscany/sca/context/CompositeContext.java b/sca-java-2.x/trunk/modules/core-spi/src/main/java/org/apache/tuscany/sca/context/CompositeContext.java index 6605ff3fc7..6e6df507c2 100644 --- a/sca-java-2.x/trunk/modules/core-spi/src/main/java/org/apache/tuscany/sca/context/CompositeContext.java +++ b/sca-java-2.x/trunk/modules/core-spi/src/main/java/org/apache/tuscany/sca/context/CompositeContext.java @@ -19,6 +19,9 @@ package org.apache.tuscany.sca.context; +import java.util.HashMap; +import java.util.Map; + import org.apache.tuscany.sca.assembly.Composite; import org.apache.tuscany.sca.assembly.Endpoint; import org.apache.tuscany.sca.assembly.EndpointReference; @@ -35,7 +38,6 @@ import org.apache.tuscany.sca.runtime.RuntimeComponentContext; * @version $Rev$ $Date$ */ public class CompositeContext { - protected ExtensionPointRegistry extensionPointRegistry; protected EndpointRegistry endpointRegistry; protected ComponentContextFactory componentContextFactory; @@ -43,8 +45,15 @@ public class CompositeContext { protected String nodeURI; protected String domainURI; protected Definitions systemDefinitions; - - public CompositeContext(ExtensionPointRegistry registry, EndpointRegistry endpointRegistry, Composite domainComposite, String domainURI, String nodeURI, Definitions systemDefinitions) { + + protected Map<String, Object> attributes = new HashMap<String, Object>(); + + public CompositeContext(ExtensionPointRegistry registry, + EndpointRegistry endpointRegistry, + Composite domainComposite, + String domainURI, + String nodeURI, + Definitions systemDefinitions) { this.extensionPointRegistry = registry; this.endpointRegistry = endpointRegistry; ContextFactoryExtensionPoint contextFactories = registry.getExtensionPoint(ContextFactoryExtensionPoint.class); @@ -53,12 +62,12 @@ public class CompositeContext { this.domainURI = domainURI; this.nodeURI = nodeURI; this.systemDefinitions = systemDefinitions; - } - + } + public CompositeContext(ExtensionPointRegistry registry, EndpointRegistry endpointRegistry) { this(registry, endpointRegistry, null, "default", "default", null); } - + /** * @return */ @@ -91,13 +100,14 @@ public class CompositeContext { RuntimeComponentContext componentContext = (RuntimeComponentContext)componentContextFactory.createComponentContext(this, runtimeComponent); runtimeComponent.setComponentContext(componentContext); - } + } + /** * * @param endpointReference */ public void bindEndpointReference(EndpointReference endpointReference) { - + } /** @@ -113,13 +123,13 @@ public class CompositeContext { * @return The EndpointRegistry for this node */ public EndpointRegistry getEndpointRegistry() { - return endpointRegistry; + return endpointRegistry; } - + public Composite getDomainComposite() { - return domainComposite; + return domainComposite; } - + public String getNodeURI() { return nodeURI; } @@ -127,7 +137,7 @@ public class CompositeContext { public String getDomainURI() { return domainURI; } - + /** * The system definitions that result from starting the runtime. * TODO - these can be null when the SCAClient starts the runtime @@ -137,4 +147,28 @@ public class CompositeContext { public Definitions getSystemDefinitions() { return systemDefinitions; } + + /** + * Look up an attribute value by name + * @param <T> + * @param name The name of the attribute + * @return The value of the attribute + */ + @SuppressWarnings("unchecked") + public <T> T getAttribute(String name) { + return (T)attributes.get(name); + } + + /** + * Set the value of an attribute + * @param name The name of the attribute + * @param value + */ + public void setAttribute(String name, Object value) { + attributes.put(name, value); + } + + public Map<String, Object> getAttributes() { + return attributes; + } } diff --git a/sca-java-2.x/trunk/modules/host-webapp/src/main/java/org/apache/tuscany/sca/host/webapp/TuscanyContextListener.java b/sca-java-2.x/trunk/modules/host-webapp/src/main/java/org/apache/tuscany/sca/host/webapp/TuscanyContextListener.java index 45c1bbf3da..d71abdacb2 100644 --- a/sca-java-2.x/trunk/modules/host-webapp/src/main/java/org/apache/tuscany/sca/host/webapp/TuscanyContextListener.java +++ b/sca-java-2.x/trunk/modules/host-webapp/src/main/java/org/apache/tuscany/sca/host/webapp/TuscanyContextListener.java @@ -25,6 +25,8 @@ import java.util.logging.Logger; import javax.servlet.ServletContextEvent; import javax.servlet.ServletContextListener; +import org.apache.tuscany.sca.host.webapp.WebAppHelper.Configurator; + /** * A ServletContextListener to create and close the SCADomain * when the webapp is initialized or destroyed. @@ -36,7 +38,8 @@ public class TuscanyContextListener implements ServletContextListener { public void contextInitialized(ServletContextEvent event) { logger.info(event.getServletContext().getServletContextName() + " is starting."); try { - WebAppHelper.init(event.getServletContext()); + Configurator configurator = WebAppHelper.getConfigurator(event.getServletContext()); + WebAppHelper.init(configurator); } catch (Throwable e) { logger.log(Level.SEVERE, e.getMessage(), e); } @@ -49,7 +52,8 @@ public class TuscanyContextListener implements ServletContextListener { return; } try { - WebAppHelper.stop(event.getServletContext()); + Configurator configurator = WebAppHelper.getConfigurator(event.getServletContext()); + WebAppHelper.stop(configurator); } catch (Throwable e) { logger.log(Level.SEVERE, e.getMessage(), e); } diff --git a/sca-java-2.x/trunk/modules/host-webapp/src/main/java/org/apache/tuscany/sca/host/webapp/TuscanyServlet.java b/sca-java-2.x/trunk/modules/host-webapp/src/main/java/org/apache/tuscany/sca/host/webapp/TuscanyServlet.java new file mode 100644 index 0000000000..6f5c35d3fb --- /dev/null +++ b/sca-java-2.x/trunk/modules/host-webapp/src/main/java/org/apache/tuscany/sca/host/webapp/TuscanyServlet.java @@ -0,0 +1,62 @@ +/* + * 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.webapp; + +import java.util.logging.Level; +import java.util.logging.Logger; + +import javax.servlet.ServletConfig; +import javax.servlet.ServletException; +import javax.servlet.http.HttpServlet; + +import org.apache.tuscany.sca.host.webapp.WebAppHelper.Configurator; + +/** + * A Servlet that provides a hook to control the lifecycle of Tuscany node + * + * @version $Rev$ $Date$ + */ +public class TuscanyServlet extends HttpServlet { + private static final long serialVersionUID = 1L; + private Logger logger = Logger.getLogger(TuscanyServlet.class.getName()); + + private transient Configurator configurator; + + public TuscanyServlet() { + super(); + } + + @Override + public void init(ServletConfig config) throws ServletException { + try { + super.init(config); + WebAppHelper.init(WebAppHelper.getConfigurator(this)); + } catch (Throwable e) { + logger.log(Level.SEVERE, e.getMessage(), e); + configurator.getServletContext().log(e.getMessage(), e); + throw new ServletException(e); + } + } + + public void destroy() { + WebAppHelper.stop(configurator); + } + +} diff --git a/sca-java-2.x/trunk/modules/host-webapp/src/main/java/org/apache/tuscany/sca/host/webapp/TuscanyServletFilter.java b/sca-java-2.x/trunk/modules/host-webapp/src/main/java/org/apache/tuscany/sca/host/webapp/TuscanyServletFilter.java index a12466a9fd..a3800f221b 100644 --- a/sca-java-2.x/trunk/modules/host-webapp/src/main/java/org/apache/tuscany/sca/host/webapp/TuscanyServletFilter.java +++ b/sca-java-2.x/trunk/modules/host-webapp/src/main/java/org/apache/tuscany/sca/host/webapp/TuscanyServletFilter.java @@ -20,20 +20,19 @@ package org.apache.tuscany.sca.host.webapp; import java.io.IOException; -import java.util.Enumeration; import java.util.logging.Level; import java.util.logging.Logger; import javax.servlet.Filter; import javax.servlet.FilterConfig; import javax.servlet.RequestDispatcher; -import javax.servlet.ServletContext; import javax.servlet.ServletException; import javax.servlet.ServletRequest; import javax.servlet.ServletResponse; import javax.servlet.http.HttpServletRequest; import org.apache.tuscany.sca.host.http.ServletHost; +import org.apache.tuscany.sca.host.webapp.WebAppHelper.Configurator; /** * A Servlet filter that forwards service requests to the Servlets registered with @@ -45,7 +44,7 @@ public class TuscanyServletFilter implements Filter { private static final long serialVersionUID = 1L; private Logger logger = Logger.getLogger(TuscanyServletFilter.class.getName()); - private transient ServletContext context; + private transient Configurator configurator; private transient ServletHost servletHost; public TuscanyServletFilter() { @@ -54,22 +53,18 @@ public class TuscanyServletFilter implements Filter { public void init(final FilterConfig config) throws ServletException { try { - context = config.getServletContext(); - for (Enumeration<String> e = config.getInitParameterNames(); e.hasMoreElements();) { - String name = e.nextElement(); - String value = config.getInitParameter(name); - context.setAttribute(name, value); - } - servletHost = WebAppHelper.init(context); + configurator = WebAppHelper.getConfigurator(config); + WebAppHelper.init(configurator); + servletHost = WebAppHelper.getServletHost(); } catch (Throwable e) { logger.log(Level.SEVERE, e.getMessage(), e); - context.log(e.getMessage(), e); + configurator.getServletContext().log(e.getMessage(), e); throw new ServletException(e); } } public void destroy() { - WebAppHelper.stop(context); + WebAppHelper.stop(configurator); servletHost = null; } @@ -101,7 +96,7 @@ public class TuscanyServletFilter implements Filter { } } catch (Throwable e) { logger.log(Level.SEVERE, e.getMessage(), e); - context.log(e.getMessage(), e); + configurator.getServletContext().log(e.getMessage(), e); throw new ServletException(e); } } diff --git a/sca-java-2.x/trunk/modules/host-webapp/src/main/java/org/apache/tuscany/sca/host/webapp/WebAppHelper.java b/sca-java-2.x/trunk/modules/host-webapp/src/main/java/org/apache/tuscany/sca/host/webapp/WebAppHelper.java index 0d905eb6eb..5c4cfe8bfe 100644 --- a/sca-java-2.x/trunk/modules/host-webapp/src/main/java/org/apache/tuscany/sca/host/webapp/WebAppHelper.java +++ b/sca-java-2.x/trunk/modules/host-webapp/src/main/java/org/apache/tuscany/sca/host/webapp/WebAppHelper.java @@ -26,17 +26,19 @@ import java.net.URISyntaxException; import java.net.URL; import java.util.Enumeration; +import javax.servlet.FilterConfig; +import javax.servlet.Servlet; import javax.servlet.ServletConfig; import javax.servlet.ServletContext; import javax.servlet.ServletException; import org.apache.tuscany.sca.core.ExtensionPointRegistry; import org.apache.tuscany.sca.core.UtilityExtensionPoint; -import org.apache.tuscany.sca.host.http.ServletHost; import org.apache.tuscany.sca.host.http.ServletHostExtensionPoint; import org.apache.tuscany.sca.node.Node; import org.apache.tuscany.sca.node.NodeFactory; import org.apache.tuscany.sca.node.configuration.NodeConfiguration; +import org.oasisopen.sca.ServiceRuntimeException; public class WebAppHelper { private static final String ROOT = "/"; @@ -46,9 +48,9 @@ public class WebAppHelper { // The prefix for the parameters in web.xml which configure the individual SCA contributions private static final String CONTRIBUTION = "contribution"; private static final String NODE_CONFIGURATION = "node.configuration"; - private static final String WEB_COMPOSITE = "/WEB-INF/web.composite"; private static final String DOMAIN_URI = "domain.uri"; private static final String NODE_URI = "node.uri"; + private static final String COMPOSITE_URI = "composite.uri"; public static final String DOMAIN_NAME_ATTR = "org.apache.tuscany.sca.domain.name"; public static final String SCA_NODE_ATTRIBUTE = Node.class.getName(); private static NodeFactory factory; @@ -74,7 +76,7 @@ public class WebAppHelper { } } } - + private static String[] parse(String listOfValues) { if (listOfValues == null) { return null; @@ -82,26 +84,165 @@ public class WebAppHelper { return listOfValues.split("(\\s|,)+"); } - @SuppressWarnings("unchecked") - private static NodeConfiguration getNodeConfiguration(ServletContext servletContext) throws IOException, + // TODO: Temp for now to get the old samples working till i clean up all the domain uri/name after the ML discussion. + private static String getDomainName(String configURI) { + String domainName; + if (configURI.startsWith("tuscany:vm:")) { + domainName = configURI.substring("tuscany:vm:".length()); + } else if (configURI.startsWith("tuscany:")) { + int i = configURI.indexOf('?'); + if (i == -1) { + domainName = configURI.substring("tuscany:".length()); + } else { + domainName = configURI.substring("tuscany:".length(), i); + } + } else { + domainName = configURI; + } + return domainName; + } + + public static WebAppServletHost getServletHost() { + return host; + } + + public static Node init(final Configurator configurator) { + synchronized (configurator) { + + bootstrapRuntime(configurator); + Node node = (Node)configurator.getAttribute(SCA_NODE_ATTRIBUTE); + if (node == null) { + try { + node = createAndStartNode(configurator); + } catch (ServletException e) { + throw new ServiceRuntimeException(e); + } + configurator.setAttribute(SCA_NODE_ATTRIBUTE, node); + } + return node; + } + } + + /** + * Bootstrap the Tuscany runtime for the given scope + * @param configurator + */ + private synchronized static void bootstrapRuntime(final Configurator configurator) { + if (host == null) { + try { + + String configValue = configurator.getInitParameter("org.apache.tuscany.sca.config"); + if (configValue != null) { + factory = NodeFactory.newInstance(configValue); + } else { + factory = NodeFactory.newInstance(); + } + + // Add ServletContext as a utility + ExtensionPointRegistry registry = factory.getExtensionPointRegistry(); + UtilityExtensionPoint utilityExtensionPoint = registry.getExtensionPoint(UtilityExtensionPoint.class); + utilityExtensionPoint.addUtility(ServletContext.class, configurator.getServletContext()); + + ServletHostExtensionPoint servletHosts = registry.getExtensionPoint(ServletHostExtensionPoint.class); + servletHosts.setWebApp(true); + + host = getServletHost(configurator); + + } catch (ServletException e) { + throw new ServiceRuntimeException(e); + } + } + } + + private static WebAppServletHost getServletHost(final Configurator configurator) throws ServletException { + ExtensionPointRegistry registry = factory.getExtensionPointRegistry(); + WebAppServletHost host = + (WebAppServletHost)org.apache.tuscany.sca.host.http.ServletHostHelper.getServletHost(registry); + + host.init(new ServletConfig() { + public String getInitParameter(String name) { + return configurator.getInitParameter(name); + } + + public Enumeration<?> getInitParameterNames() { + return configurator.getInitParameterNames(); + } + + public ServletContext getServletContext() { + return configurator.getServletContext(); + } + + public String getServletName() { + return configurator.getServletContext().getServletContextName(); + } + }); + return host; + } + + private static Node createAndStartNode(final Configurator configurator) throws ServletException { + NodeConfiguration configuration = null; + try { + configuration = getNodeConfiguration(configurator); + } catch (IOException e) { + throw new ServletException(e); + } catch (URISyntaxException e) { + throw new ServletException(e); + } + Node node = null; + if (configuration != null) { + factory.createNode(configuration).start(); + } + return node; + } + + public static void stop(Configurator configurator) { + Node node = (Node)configurator.getAttribute(SCA_NODE_ATTRIBUTE); + if (node != null) { + node.stop(); + configurator.setAttribute(SCA_NODE_ATTRIBUTE, null); + } + } + + public static void destroy() { + if (factory != null) { + factory.destroy(); + } + factory = null; + host = null; + } + + public static NodeFactory getNodeFactory() { + return factory; + } + + private static String getDefaultComposite(Configurator configurator) { + String name = configurator.getName(); + if ("".equals(name)) { + return "/WEB-INF/web.composite"; + } else { + return "/WEB-INF/" + name + "/servlet.composite"; + } + } + + private static NodeConfiguration getNodeConfiguration(Configurator configurator) throws IOException, URISyntaxException { NodeConfiguration configuration = null; - String nodeConfigURI = (String)servletContext.getAttribute(NODE_CONFIGURATION); + String nodeConfigURI = configurator.getInitParameter(NODE_CONFIGURATION); + ServletContext servletContext = configurator.getServletContext(); if (nodeConfigURI != null) { URL url = getResource(servletContext, nodeConfigURI); configuration = factory.loadConfiguration(url.openStream(), url); } else { configuration = factory.createNodeConfiguration(); - - + boolean explicitContributions = false; - Enumeration<String> names = servletContext.getAttributeNames(); + Enumeration<String> names = configurator.getInitParameterNames(); while (names.hasMoreElements()) { String name = names.nextElement(); if (name.equals(CONTRIBUTION) || name.startsWith(CONTRIBUTION + ".")) { explicitContributions = true; // We need to have a way to select one or more folders within the webapp as the contributions - String listOfValues = (String)servletContext.getAttribute(name); + String listOfValues = configurator.getInitParameter(name); if (listOfValues != null) { for (String path : parse(listOfValues)) { if ("".equals(path)) { @@ -113,7 +254,7 @@ public class WebAppHelper { } } else if (name.equals(CONTRIBUTIONS) || name.startsWith(CONTRIBUTIONS + ".")) { explicitContributions = true; - String listOfValues = (String)servletContext.getAttribute(name); + String listOfValues = (String)configurator.getInitParameter(name); if (listOfValues != null) { for (String path : parse(listOfValues)) { if ("".equals(path)) { @@ -132,10 +273,19 @@ public class WebAppHelper { } } - URL composite = getResource(servletContext, WEB_COMPOSITE); + String compositeURI = configurator.getInitParameter(COMPOSITE_URI); + if (compositeURI == null) { + compositeURI = getDefaultComposite(configurator); + } + URL composite = getResource(servletContext, compositeURI); if (configuration.getContributions().isEmpty() || (!explicitContributions && composite != null)) { - // TODO: Which path should be the default root - configuration.addContribution(getResource(servletContext, ROOT)); + if ("".equals(configurator.getName())) { + // Add the root of the web application + configuration.addContribution(getResource(servletContext, ROOT)); + } else { + // Add a dummy contribution + configuration.addContribution(URI.create("sca:contributions/" + configurator.getName()), null); + } } if (composite != null) { configuration.getContributions().get(0).addDeploymentComposite(composite); @@ -151,16 +301,16 @@ public class WebAppHelper { } } } - String nodeURI = (String)servletContext.getAttribute(NODE_URI); + String nodeURI = configurator.getInitParameter(NODE_URI); if (nodeURI == null) { - nodeURI = new File(servletContext.getRealPath(ROOT)).getName(); + nodeURI = getResource(servletContext, ROOT).getPath() + configurator.getName(); } configuration.setURI(nodeURI); - String domainURI = (String)servletContext.getAttribute(DOMAIN_URI); + String domainURI = configurator.getInitParameter(DOMAIN_URI); if (domainURI != null) { configuration.setDomainURI(domainURI); } else { - domainURI = servletContext.getInitParameter("org.apache.tuscany.sca.defaultDomainURI"); + domainURI = configurator.getInitParameter("org.apache.tuscany.sca.defaultDomainURI"); if (domainURI != null) { configuration.setDomainURI(getDomainName(domainURI)); configuration.setDomainRegistryURI(domainURI); @@ -170,118 +320,152 @@ public class WebAppHelper { return configuration; } - // TODO: Temp for now to get the old samples working till i clean up all the domain uri/name after the ML discussion. - private static String getDomainName(String configURI) { - String domainName; - if (configURI.startsWith("tuscany:vm:")) { - domainName = configURI.substring("tuscany:vm:".length()); - } else if (configURI.startsWith("tuscany:")) { - int i = configURI.indexOf('?'); - if (i == -1) { - domainName = configURI.substring("tuscany:".length()); + static Configurator getConfigurator(FilterConfig config) { + return new FilterConfigurator(config); + } + + static Configurator getConfigurator(ServletContext context) { + return new ServletContextConfigurator(context); + } + + static Configurator getConfigurator(Servlet context) { + return new ServletConfigurator(context); + } + + /** + * The interface that represents a given scope (Webapp vs Servlet) that provides the configuration of the Tuscany node + */ + public static interface Configurator { + String getInitParameter(String name); + + Enumeration<String> getInitParameterNames(); + + ServletContext getServletContext(); + + void setAttribute(String name, Object value); + + <T> T getAttribute(String name); + + String getName(); + } + + public static class FilterConfigurator implements Configurator { + private FilterConfig config; + + public FilterConfigurator(FilterConfig config) { + super(); + this.config = config; + } + + public String getInitParameter(String name) { + String value = config.getInitParameter(name); + if (value == null) { + return config.getServletContext().getInitParameter(name); } else { - domainName = configURI.substring("tuscany:".length(), i); + return value; } - } else { - domainName = configURI; } - return domainName; + + public Enumeration<String> getInitParameterNames() { + Enumeration<String> names = config.getInitParameterNames(); + if (!names.hasMoreElements()) { + return getServletContext().getInitParameterNames(); + } else { + return names; + } + } + + public ServletContext getServletContext() { + return config.getServletContext(); + } + + public void setAttribute(String name, Object value) { + String prefix = "filter:" + config.getFilterName() + ":"; + getServletContext().setAttribute(prefix + name, value); + } + + public <T> T getAttribute(String name) { + String prefix = "filter:" + config.getFilterName() + ":"; + return (T)getServletContext().getAttribute(prefix + name); + } + + public String getName() { + return ""; + } + } - public synchronized static ServletHost init(final ServletContext servletContext) { - if (host == null) { - try { + public static class ServletContextConfigurator implements Configurator { + private ServletContext context; - String configValue = servletContext.getInitParameter("org.apache.tuscany.sca.config"); - if (configValue != null) { - factory = NodeFactory.newInstance(configValue); - } else { - factory = NodeFactory.newInstance(); - } - - // Add ServletContext as a utility - ExtensionPointRegistry registry = factory.getExtensionPointRegistry(); - UtilityExtensionPoint utilityExtensionPoint = registry.getExtensionPoint(UtilityExtensionPoint.class); - utilityExtensionPoint.addUtility(ServletContext.class, servletContext); - - ServletHostExtensionPoint servletHosts = registry.getExtensionPoint(ServletHostExtensionPoint.class); - servletHosts.setWebApp(true); + public ServletContextConfigurator(ServletContext context) { + super(); + this.context = context; + } - // TODO: why are the init parameters copied to the attributes? - for (Enumeration<?> e = servletContext.getInitParameterNames(); e.hasMoreElements();) { - String name = (String)e.nextElement(); - String value = servletContext.getInitParameter(name); - servletContext.setAttribute(name, value); - } + public String getInitParameter(String name) { + return context.getInitParameter(name); + } - host = getServletHost(servletContext); + public Enumeration<String> getInitParameterNames() { + return context.getInitParameterNames(); + } - } catch (ServletException e) { - throw new RuntimeException(e); - } + public ServletContext getServletContext() { + return context; } - Node node = (Node)servletContext.getAttribute(SCA_NODE_ATTRIBUTE); - if (node == null) { - try { - node = createAndStartNode(servletContext); - } catch (ServletException e) { - throw new RuntimeException(e); - } - servletContext.setAttribute(SCA_NODE_ATTRIBUTE, node); + + public void setAttribute(String name, Object value) { + context.setAttribute(name, value); } - return host; + public <T> T getAttribute(String name) { + return (T)context.getAttribute(name); + } + + public String getName() { + return ""; + } } - private static WebAppServletHost getServletHost(final ServletContext servletContext) throws ServletException { - WebAppServletHost host = getServletHost(factory); - host.init(new ServletConfig() { - public String getInitParameter(String name) { - return servletContext.getInitParameter(name); - } + public static class ServletConfigurator implements Configurator { + private ServletConfig config; - public Enumeration<?> getInitParameterNames() { - return servletContext.getInitParameterNames(); - } + public ServletConfigurator(Servlet servlet) { + super(); + this.config = servlet.getServletConfig(); + } - public ServletContext getServletContext() { - return servletContext; + public String getInitParameter(String name) { + String value = config.getInitParameter(name); + if (value == null) { + return config.getServletContext().getInitParameter(name); + } else { + return value; } + } - public String getServletName() { - return servletContext.getServletContextName(); - } - }); - return host; - } + public Enumeration<String> getInitParameterNames() { + return config.getInitParameterNames(); + } - private static WebAppServletHost getServletHost(NodeFactory factory) { - ExtensionPointRegistry registry = factory.getExtensionPointRegistry(); - return (WebAppServletHost)org.apache.tuscany.sca.host.http.ServletHostHelper.getServletHost(registry); - } + public ServletContext getServletContext() { + return config.getServletContext(); + } - private static Node createAndStartNode(final ServletContext servletContext) throws ServletException { - NodeConfiguration configuration; - try { - configuration = getNodeConfiguration(servletContext); - } catch (IOException e) { - throw new ServletException(e); - } catch (URISyntaxException e) { - throw new ServletException(e); + public void setAttribute(String name, Object value) { + String prefix = "servlet:" + config.getServletName() + ":"; + getServletContext().setAttribute(prefix + name, value); } - Node node = factory.createNode(configuration).start(); - return node; - } - public static void stop(ServletContext servletContext) { - Node node = (Node)servletContext.getAttribute(SCA_NODE_ATTRIBUTE); - if (node != null) { - node.stop(); - servletContext.setAttribute(SCA_NODE_ATTRIBUTE, null); + public <T> T getAttribute(String name) { + String prefix = "servlet:" + config.getServletName() + ":"; + return (T)getServletContext().getAttribute(prefix + name); + } + + public String getName() { + return config.getServletName(); } - } - public static NodeFactory getNodeFactory() { - return factory; } } diff --git a/sca-java-2.x/trunk/modules/host-webapp/src/main/java/org/apache/tuscany/sca/host/webapp/WebAppServletHost.java b/sca-java-2.x/trunk/modules/host-webapp/src/main/java/org/apache/tuscany/sca/host/webapp/WebAppServletHost.java index e6f63681a9..fed2f67893 100644 --- a/sca-java-2.x/trunk/modules/host-webapp/src/main/java/org/apache/tuscany/sca/host/webapp/WebAppServletHost.java +++ b/sca-java-2.x/trunk/modules/host-webapp/src/main/java/org/apache/tuscany/sca/host/webapp/WebAppServletHost.java @@ -306,7 +306,7 @@ public class WebAppServletHost implements ServletHost { } // Close the SCA domain - WebAppHelper.stop(servletContext); + WebAppHelper.destroy(); } public String getContextPath() { |