Add servlet scoped Node lifecycle support for web applications

git-svn-id: http://svn.us.apache.org/repos/asf/tuscany@1082810 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
rfeng 2011-03-18 03:00:29 +00:00
parent 0f30fc3fd3
commit 148ad11441
8 changed files with 598 additions and 298 deletions

View file

@ -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,13 +106,15 @@ 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());
if (contributionURL != null) {
contribution.setLocation(contributionURL.toString());
}
ModelResolver modelResolver = new ExtensibleModelResolver(contribution, modelResolvers, modelFactories);
contribution.setModelResolver(modelResolver);
contribution.setUnresolved(true);
@ -120,6 +124,7 @@ public class ContributionContentProcessor implements ExtendedURLArtifactProcesso
Contribution old = context.setContribution(contribution);
try {
if (contributionURL != null) {
// Create a contribution scanner
ContributionScanner scanner = scanners.getContributionScanner(contributionURL.getProtocol());
if (scanner == null) {
@ -146,11 +151,15 @@ public class ContributionContentProcessor implements ExtendedURLArtifactProcesso
URL artifactLocationURL = null;
try {
artifactLocationURL = new URL(artifact.getLocation());
} catch(MalformedURLException e) {
} catch (MalformedURLException e) {
//ignore
}
Object model = artifactProcessor.read(contributionURL, URI.create(artifact.getURI()), artifactLocationURL, context);
Object model =
artifactProcessor.read(contributionURL,
URI.create(artifact.getURI()),
artifactLocationURL,
context);
if (model != null) {
artifact.setModel(model);
@ -180,7 +189,7 @@ public class ContributionContentProcessor implements ExtendedURLArtifactProcesso
// If no sca-contribution.xml file was provided then just consider
// all composites in the contribution as deployables
if (!contributionMetadata) {
for (Artifact artifact: artifacts) {
for (Artifact artifact : artifacts) {
if (artifact.getModel() instanceof Composite) {
contribution.getDeployables().add((Composite)artifact.getModel());
}
@ -197,7 +206,8 @@ public class ContributionContentProcessor implements ExtendedURLArtifactProcesso
// 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();) {
for (ListIterator<Composite> lit = contribution.getDeployables().listIterator(); lit
.hasNext();) {
if (lit.next().getName().equals(((Composite)a.getModel()).getName())) {
lit.set((Composite)a.getModel());
}
@ -206,6 +216,7 @@ public class ContributionContentProcessor implements ExtendedURLArtifactProcesso
}
}
}
}
} finally {
monitor.popContext();
context.setContribution(old);
@ -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,19 +248,21 @@ 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);
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()) {
for (Export export : contribution.getExports()) {
if (export instanceof JavaExport) {
boolean available = false;
String packageName = ((JavaExport)export).getPackage();
@ -256,9 +270,10 @@ public class ContributionContentProcessor implements ExtendedURLArtifactProcesso
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.");
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.");
}
}
@ -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

View file

@ -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;
@ -44,7 +46,14 @@ public class CompositeContext {
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);
@ -92,6 +101,7 @@ public class CompositeContext {
(RuntimeComponentContext)componentContextFactory.createComponentContext(this, runtimeComponent);
runtimeComponent.setComponentContext(componentContext);
}
/**
*
* @param endpointReference
@ -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;
}
}

View file

@ -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);
}

View file

@ -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);
}
}

View file

@ -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);
}
}

View file

@ -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;
@ -82,94 +84,6 @@ public class WebAppHelper {
return listOfValues.split("(\\s|,)+");
}
@SuppressWarnings("unchecked")
private static NodeConfiguration getNodeConfiguration(ServletContext servletContext) throws IOException,
URISyntaxException {
NodeConfiguration configuration = null;
String nodeConfigURI = (String)servletContext.getAttribute(NODE_CONFIGURATION);
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();
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);
if (listOfValues != null) {
for (String path : parse(listOfValues)) {
if ("".equals(path)) {
continue;
}
File f = new File(getResource(servletContext, path).toURI());
configuration.addContribution(f.toURI().toURL());
}
}
} else if (name.equals(CONTRIBUTIONS) || name.startsWith(CONTRIBUTIONS + ".")) {
explicitContributions = true;
String listOfValues = (String)servletContext.getAttribute(name);
if (listOfValues != null) {
for (String path : parse(listOfValues)) {
if ("".equals(path)) {
continue;
}
File f = new File(getResource(servletContext, path).toURI());
if (f.isDirectory()) {
for (File n : f.listFiles()) {
configuration.addContribution(n.toURI().toURL());
}
} else {
configuration.addContribution(f.toURI().toURL());
}
}
}
}
}
URL composite = getResource(servletContext, WEB_COMPOSITE);
if (configuration.getContributions().isEmpty() || (!explicitContributions && composite != null)) {
// TODO: Which path should be the default root
configuration.addContribution(getResource(servletContext, ROOT));
}
if (composite != null) {
configuration.getContributions().get(0).addDeploymentComposite(composite);
}
if (!explicitContributions) {
URL url = getResource(servletContext, DEFAULT_CONTRIBUTIONS);
if (url != null) {
File f = new File(url.toURI());
if (f.isDirectory()) {
for (File n : f.listFiles()) {
configuration.addContribution(n.toURI().toURL());
}
}
}
}
String nodeURI = (String)servletContext.getAttribute(NODE_URI);
if (nodeURI == null) {
nodeURI = new File(servletContext.getRealPath(ROOT)).getName();
}
configuration.setURI(nodeURI);
String domainURI = (String)servletContext.getAttribute(DOMAIN_URI);
if (domainURI != null) {
configuration.setDomainURI(domainURI);
} else {
domainURI = servletContext.getInitParameter("org.apache.tuscany.sca.defaultDomainURI");
if (domainURI != null) {
configuration.setDomainURI(getDomainName(domainURI));
configuration.setDomainRegistryURI(domainURI);
}
}
}
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;
@ -188,11 +102,36 @@ public class WebAppHelper {
return domainName;
}
public synchronized static ServletHost init(final ServletContext servletContext) {
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 = servletContext.getInitParameter("org.apache.tuscany.sca.config");
String configValue = configurator.getInitParameter("org.apache.tuscany.sca.config");
if (configValue != null) {
factory = NodeFactory.newInstance(configValue);
} else {
@ -202,86 +141,331 @@ public class WebAppHelper {
// Add ServletContext as a utility
ExtensionPointRegistry registry = factory.getExtensionPointRegistry();
UtilityExtensionPoint utilityExtensionPoint = registry.getExtensionPoint(UtilityExtensionPoint.class);
utilityExtensionPoint.addUtility(ServletContext.class, servletContext);
utilityExtensionPoint.addUtility(ServletContext.class, configurator.getServletContext());
ServletHostExtensionPoint servletHosts = registry.getExtensionPoint(ServletHostExtensionPoint.class);
servletHosts.setWebApp(true);
// 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);
}
host = getServletHost(servletContext);
host = getServletHost(configurator);
} catch (ServletException e) {
throw new RuntimeException(e);
throw new ServiceRuntimeException(e);
}
}
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);
}
return host;
}
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);
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);
return configurator.getInitParameter(name);
}
public Enumeration<?> getInitParameterNames() {
return servletContext.getInitParameterNames();
return configurator.getInitParameterNames();
}
public ServletContext getServletContext() {
return servletContext;
return configurator.getServletContext();
}
public String getServletName() {
return servletContext.getServletContextName();
return configurator.getServletContext().getServletContextName();
}
});
return host;
}
private static WebAppServletHost getServletHost(NodeFactory factory) {
ExtensionPointRegistry registry = factory.getExtensionPointRegistry();
return (WebAppServletHost)org.apache.tuscany.sca.host.http.ServletHostHelper.getServletHost(registry);
}
private static Node createAndStartNode(final ServletContext servletContext) throws ServletException {
NodeConfiguration configuration;
private static Node createAndStartNode(final Configurator configurator) throws ServletException {
NodeConfiguration configuration = null;
try {
configuration = getNodeConfiguration(servletContext);
configuration = getNodeConfiguration(configurator);
} catch (IOException e) {
throw new ServletException(e);
} catch (URISyntaxException e) {
throw new ServletException(e);
}
Node node = factory.createNode(configuration).start();
Node node = null;
if (configuration != null) {
factory.createNode(configuration).start();
}
return node;
}
public static void stop(ServletContext servletContext) {
Node node = (Node)servletContext.getAttribute(SCA_NODE_ATTRIBUTE);
public static void stop(Configurator configurator) {
Node node = (Node)configurator.getAttribute(SCA_NODE_ATTRIBUTE);
if (node != null) {
node.stop();
servletContext.setAttribute(SCA_NODE_ATTRIBUTE, null);
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 = 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 = 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 = configurator.getInitParameter(name);
if (listOfValues != null) {
for (String path : parse(listOfValues)) {
if ("".equals(path)) {
continue;
}
File f = new File(getResource(servletContext, path).toURI());
configuration.addContribution(f.toURI().toURL());
}
}
} else if (name.equals(CONTRIBUTIONS) || name.startsWith(CONTRIBUTIONS + ".")) {
explicitContributions = true;
String listOfValues = (String)configurator.getInitParameter(name);
if (listOfValues != null) {
for (String path : parse(listOfValues)) {
if ("".equals(path)) {
continue;
}
File f = new File(getResource(servletContext, path).toURI());
if (f.isDirectory()) {
for (File n : f.listFiles()) {
configuration.addContribution(n.toURI().toURL());
}
} else {
configuration.addContribution(f.toURI().toURL());
}
}
}
}
}
String compositeURI = configurator.getInitParameter(COMPOSITE_URI);
if (compositeURI == null) {
compositeURI = getDefaultComposite(configurator);
}
URL composite = getResource(servletContext, compositeURI);
if (configuration.getContributions().isEmpty() || (!explicitContributions && composite != null)) {
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);
}
if (!explicitContributions) {
URL url = getResource(servletContext, DEFAULT_CONTRIBUTIONS);
if (url != null) {
File f = new File(url.toURI());
if (f.isDirectory()) {
for (File n : f.listFiles()) {
configuration.addContribution(n.toURI().toURL());
}
}
}
}
String nodeURI = configurator.getInitParameter(NODE_URI);
if (nodeURI == null) {
nodeURI = getResource(servletContext, ROOT).getPath() + configurator.getName();
}
configuration.setURI(nodeURI);
String domainURI = configurator.getInitParameter(DOMAIN_URI);
if (domainURI != null) {
configuration.setDomainURI(domainURI);
} else {
domainURI = configurator.getInitParameter("org.apache.tuscany.sca.defaultDomainURI");
if (domainURI != null) {
configuration.setDomainURI(getDomainName(domainURI));
configuration.setDomainRegistryURI(domainURI);
}
}
}
return configuration;
}
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 {
return value;
}
}
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 static class ServletContextConfigurator implements Configurator {
private ServletContext context;
public ServletContextConfigurator(ServletContext context) {
super();
this.context = context;
}
public String getInitParameter(String name) {
return context.getInitParameter(name);
}
public Enumeration<String> getInitParameterNames() {
return context.getInitParameterNames();
}
public ServletContext getServletContext() {
return context;
}
public void setAttribute(String name, Object value) {
context.setAttribute(name, value);
}
public <T> T getAttribute(String name) {
return (T)context.getAttribute(name);
}
public String getName() {
return "";
}
}
public static class ServletConfigurator implements Configurator {
private ServletConfig config;
public ServletConfigurator(Servlet servlet) {
super();
this.config = servlet.getServletConfig();
}
public String getInitParameter(String name) {
String value = config.getInitParameter(name);
if (value == null) {
return config.getServletContext().getInitParameter(name);
} else {
return value;
}
}
public Enumeration<String> getInitParameterNames() {
return config.getInitParameterNames();
}
public ServletContext getServletContext() {
return config.getServletContext();
}
public void setAttribute(String name, Object value) {
String prefix = "servlet:" + config.getServletName() + ":";
getServletContext().setAttribute(prefix + name, value);
}
public <T> T getAttribute(String name) {
String prefix = "servlet:" + config.getServletName() + ":";
return (T)getServletContext().getAttribute(prefix + name);
}
public String getName() {
return config.getServletName();
}
}
}

View file

@ -306,7 +306,7 @@ public class WebAppServletHost implements ServletHost {
}
// Close the SCA domain
WebAppHelper.stop(servletContext);
WebAppHelper.destroy();
}
public String getContextPath() {

View file

@ -1214,6 +1214,7 @@ public class org.apache.tuscany.sca.context.CompositeContext extends java.lang.O
protected java.lang.String nodeURI;
protected java.lang.String domainURI;
protected org.apache.tuscany.sca.definitions.Definitions systemDefinitions;
protected java.util.Map attributes;
public org.apache.tuscany.sca.context.CompositeContext(org.apache.tuscany.sca.core.ExtensionPointRegistry, org.apache.tuscany.sca.runtime.EndpointRegistry, org.apache.tuscany.sca.assembly.Composite, java.lang.String, java.lang.String, org.apache.tuscany.sca.definitions.Definitions);
public org.apache.tuscany.sca.context.CompositeContext(org.apache.tuscany.sca.core.ExtensionPointRegistry, org.apache.tuscany.sca.runtime.EndpointRegistry);
public static org.apache.tuscany.sca.runtime.RuntimeComponent getCurrentComponent();
@ -1226,6 +1227,9 @@ public class org.apache.tuscany.sca.context.CompositeContext extends java.lang.O
public java.lang.String getNodeURI();
public java.lang.String getDomainURI();
public org.apache.tuscany.sca.definitions.Definitions getSystemDefinitions();
public java.lang.Object getAttribute(java.lang.String);
public void setAttribute(java.lang.String, java.lang.Object);
public java.util.Map getAttributes();
}
Compiled from "ContextFactoryExtensionPoint.java"