From d5f1d093fe6fa491cdec392dca7137639e98d149 Mon Sep 17 00:00:00 2001 From: jsdelfino Date: Mon, 15 Sep 2008 00:26:00 +0000 Subject: Pulled a recent revision of trunk into the sca-android branch, to apply the android patches from JIRA TUSCANY-2440 to it. git-svn-id: http://svn.us.apache.org/repos/asf/tuscany@695318 13f79535-47bb-0310-9956-ffa450edef68 --- .../tomcat/EndpointResolverFactoryImpl.java | 49 ++++ .../sca/runtime/tomcat/EndpointResolverImpl.java | 136 +++++++++++ .../runtime/tomcat/TuscanyAnnotationProcessor.java | 108 +++++++++ .../tuscany/sca/runtime/tomcat/TuscanyContext.java | 270 +++++++++++++++++++++ .../sca/runtime/tomcat/TuscanyContextListener.java | 69 ++++++ .../tuscany/sca/runtime/tomcat/TuscanyHost.java | 159 ++++++++++++ .../tuscany/sca/runtime/tomcat/TuscanyServlet.java | 65 +++++ .../tuscany/sca/runtime/tomcat/TuscanyWrapper.java | 51 ++++ 8 files changed, 907 insertions(+) create mode 100644 branches/sca-android/modules/runtime-tomcat/src/main/java/org/apache/tuscany/sca/runtime/tomcat/EndpointResolverFactoryImpl.java create mode 100644 branches/sca-android/modules/runtime-tomcat/src/main/java/org/apache/tuscany/sca/runtime/tomcat/EndpointResolverImpl.java create mode 100644 branches/sca-android/modules/runtime-tomcat/src/main/java/org/apache/tuscany/sca/runtime/tomcat/TuscanyAnnotationProcessor.java create mode 100644 branches/sca-android/modules/runtime-tomcat/src/main/java/org/apache/tuscany/sca/runtime/tomcat/TuscanyContext.java create mode 100644 branches/sca-android/modules/runtime-tomcat/src/main/java/org/apache/tuscany/sca/runtime/tomcat/TuscanyContextListener.java create mode 100644 branches/sca-android/modules/runtime-tomcat/src/main/java/org/apache/tuscany/sca/runtime/tomcat/TuscanyHost.java create mode 100644 branches/sca-android/modules/runtime-tomcat/src/main/java/org/apache/tuscany/sca/runtime/tomcat/TuscanyServlet.java create mode 100644 branches/sca-android/modules/runtime-tomcat/src/main/java/org/apache/tuscany/sca/runtime/tomcat/TuscanyWrapper.java (limited to 'branches/sca-android/modules/runtime-tomcat/src/main/java/org/apache/tuscany/sca') diff --git a/branches/sca-android/modules/runtime-tomcat/src/main/java/org/apache/tuscany/sca/runtime/tomcat/EndpointResolverFactoryImpl.java b/branches/sca-android/modules/runtime-tomcat/src/main/java/org/apache/tuscany/sca/runtime/tomcat/EndpointResolverFactoryImpl.java new file mode 100644 index 0000000000..45ea279889 --- /dev/null +++ b/branches/sca-android/modules/runtime-tomcat/src/main/java/org/apache/tuscany/sca/runtime/tomcat/EndpointResolverFactoryImpl.java @@ -0,0 +1,49 @@ +/* + * 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.runtime.tomcat; + +import org.apache.tuscany.sca.assembly.Binding; +import org.apache.tuscany.sca.assembly.Endpoint; +import org.apache.tuscany.sca.core.ExtensionPointRegistry; +import org.apache.tuscany.sca.endpointresolver.EndpointResolver; +import org.apache.tuscany.sca.endpointresolver.EndpointResolverFactory; + +/** + * The factory for creating endpoint Binding providers + * + * @version $Rev$ $Date$ + */ +public class EndpointResolverFactoryImpl implements EndpointResolverFactory { + + private ExtensionPointRegistry extensionPoints; + + public EndpointResolverFactoryImpl(ExtensionPointRegistry extensionPoints) { + this.extensionPoints = extensionPoints; + } + + public EndpointResolver createEndpointResolver(Endpoint endpoint, Binding binding) { + + return new EndpointResolverImpl(extensionPoints, endpoint); + } + + public Class getModelType() { + return Endpoint.class; + } +} diff --git a/branches/sca-android/modules/runtime-tomcat/src/main/java/org/apache/tuscany/sca/runtime/tomcat/EndpointResolverImpl.java b/branches/sca-android/modules/runtime-tomcat/src/main/java/org/apache/tuscany/sca/runtime/tomcat/EndpointResolverImpl.java new file mode 100644 index 0000000000..e05004160c --- /dev/null +++ b/branches/sca-android/modules/runtime-tomcat/src/main/java/org/apache/tuscany/sca/runtime/tomcat/EndpointResolverImpl.java @@ -0,0 +1,136 @@ +/* + * 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.runtime.tomcat; + + +import java.util.ArrayList; +import java.util.List; +import java.util.logging.Logger; + +import org.apache.catalina.core.StandardContext; +import org.apache.tuscany.sca.assembly.Binding; +import org.apache.tuscany.sca.assembly.Component; +import org.apache.tuscany.sca.assembly.Endpoint; +import org.apache.tuscany.sca.assembly.builder.DefaultEndpointBuilder; +import org.apache.tuscany.sca.core.ExtensionPointRegistry; +import org.apache.tuscany.sca.endpointresolver.EndpointResolver; +import org.apache.tuscany.sca.endpointresolver.EndpointResolverFactory; +import org.apache.tuscany.sca.endpointresolver.EndpointResolverFactoryExtensionPoint; +import org.apache.tuscany.sca.host.embedded.SCADomain; +import org.apache.tuscany.sca.host.embedded.impl.DefaultSCADomain; +import org.apache.tuscany.sca.host.webapp.WebAppServletHost; +import org.apache.tuscany.sca.monitor.Monitor; +import org.apache.tuscany.sca.monitor.Problem; + + +/** + * The endpoint resolver allows unresolved endpoints to be plumbed into + * the runtime start and message send processing as a hook to late resolution + * of target services + * + * @version $Rev$ $Date$ + */ +public class EndpointResolverImpl implements EndpointResolver { + + private final static Logger logger = Logger.getLogger(EndpointResolverImpl.class.getName()); + + private Endpoint endpoint; + private List endpointResolvers = new ArrayList(); + + public EndpointResolverImpl(ExtensionPointRegistry extensionPoints, + Endpoint endpoint) { + this.endpoint = endpoint; + + EndpointResolverFactoryExtensionPoint resolverFactories = + extensionPoints.getExtensionPoint(EndpointResolverFactoryExtensionPoint.class); + + for (Binding binding : endpoint.getCandidateBindings()){ + EndpointResolverFactory resolverFactory = resolverFactories.getEndpointResolverFactory(binding.getClass()); + + // if the binding in question has a endpoint resolver factory they try and + // create an endpoint resolver + if (resolverFactory != null){ + EndpointResolver resolver = resolverFactory.createEndpointResolver(endpoint, binding); + + if (resolver != null){ + endpointResolvers.add(resolver); + } + } + } + } + + public void start(){ + // do nothing + } + + public void resolve() { + if (endpoint.isUnresolved()){ + logger.info("resolving endpoint: " + endpoint.getTargetName()); + + Component target = findTarget(); + if (target != null) { + logger.info("endpoint target found: " + endpoint.getTargetName() + " component " + target); + resolveEndpoint(target); + } else { + logger.info("endpoint target not found: " + endpoint.getTargetName()); + } + + } + } + + protected void resolveEndpoint(Component targetComponent) { + + endpoint.setTargetComponent(targetComponent); + endpoint.setTargetComponentService(targetComponent.getServices().get(0)); // TODO real service + + DefaultEndpointBuilder ebi = new DefaultEndpointBuilder(new Monitor() { + public void problem(Problem problem) { + logger.warning(problem.toString()); + } + + public List getProblems() { + return null; + } + }); + + ebi.build(endpoint); + } + + protected Component findTarget() { + for (StandardContext sc : TuscanyHost.scaApps) { + SCADomain scaDomain = (SCADomain)sc.getServletContext().getAttribute(WebAppServletHost.SCA_DOMAIN_ATTRIBUTE); + if (scaDomain != null) { + Component component = ((DefaultSCADomain)scaDomain).getComponent(endpoint.getTargetName()); + if ( component != null) { + return component; + } + } + } + return null; + } + + public void stop() { + if (!endpoint.isUnresolved()){ + // Currently the CompositeActivator stop() should take care of the providers and + // wires that have been added. + } + } + +} diff --git a/branches/sca-android/modules/runtime-tomcat/src/main/java/org/apache/tuscany/sca/runtime/tomcat/TuscanyAnnotationProcessor.java b/branches/sca-android/modules/runtime-tomcat/src/main/java/org/apache/tuscany/sca/runtime/tomcat/TuscanyAnnotationProcessor.java new file mode 100644 index 0000000000..7351ecabe0 --- /dev/null +++ b/branches/sca-android/modules/runtime-tomcat/src/main/java/org/apache/tuscany/sca/runtime/tomcat/TuscanyAnnotationProcessor.java @@ -0,0 +1,108 @@ +/* + * 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.runtime.tomcat; + +import java.lang.reflect.Field; +import java.lang.reflect.InvocationTargetException; +import java.lang.reflect.Method; + +import javax.naming.NamingException; + +import org.apache.AnnotationProcessor; +import org.apache.catalina.core.StandardContext; +import org.apache.tuscany.sca.host.embedded.SCADomain; +import org.apache.tuscany.sca.host.webapp.WebAppServletHost; +import org.osoa.sca.annotations.Reference; + +/** + * A Tuscany specific Tomcat annotation processor for processing SCA annotations + */ +public class TuscanyAnnotationProcessor implements AnnotationProcessor { + + private StandardContext scaApp; + + public TuscanyAnnotationProcessor(StandardContext scaApp) { + this.scaApp = scaApp; + } + + public void postConstruct(Object instance) throws IllegalAccessException, InvocationTargetException { + } + + public void preDestroy(Object instance) throws IllegalAccessException, InvocationTargetException { + } + + public void processAnnotations(Object instance) throws IllegalAccessException, InvocationTargetException, NamingException { + + // Initialize fields annotations + Field[] fields = instance.getClass().getDeclaredFields(); + for (int i = 0; i < fields.length; i++) { + if (fields[i].isAnnotationPresent(Reference.class)) { + Reference annotation = fields[i].getAnnotation(Reference.class); + injectFieldResource(instance, fields[i], annotation); + } + } + + // Initialize methods annotations + Method[] methods = instance.getClass().getDeclaredMethods(); + for (int i = 0; i < methods.length; i++) { + if (methods[i].isAnnotationPresent(Reference.class)) { + Reference annotation = methods[i].getAnnotation(Reference.class); + injectMethodResource(instance, methods[i], annotation); + } + } + + } + + protected void injectFieldResource(Object instance, Field field, Reference annotation) throws IllegalArgumentException, IllegalAccessException { + + String serviceName = annotation.name(); + if (serviceName == null || serviceName.length() < 1) { + serviceName = field.getName(); + } + + Object service = getSCADomain().getService(field.getType(), serviceName); + + boolean accessibility = field.isAccessible(); + field.setAccessible(true); + field.set(instance, service); + field.setAccessible(accessibility); + } + + protected void injectMethodResource(Object instance, Method method, Reference annotation) throws IllegalArgumentException, IllegalAccessException, InvocationTargetException { + + String serviceName = annotation.name(); + if (serviceName == null || serviceName.length() < 1) { + StringBuilder setterName = new StringBuilder(method.getName()); + setterName.setCharAt(4, Character.toLowerCase(setterName.charAt(4))); + serviceName = setterName.substring(4); + } + + Object service = getSCADomain().getService(method.getParameterTypes()[0], serviceName); + + boolean accessibility = method.isAccessible(); + method.setAccessible(true); + method.invoke(instance, service); + method.setAccessible(accessibility); + } + + protected SCADomain getSCADomain() { + return (SCADomain)scaApp.getServletContext().getAttribute(WebAppServletHost.SCA_DOMAIN_ATTRIBUTE); + } +} diff --git a/branches/sca-android/modules/runtime-tomcat/src/main/java/org/apache/tuscany/sca/runtime/tomcat/TuscanyContext.java b/branches/sca-android/modules/runtime-tomcat/src/main/java/org/apache/tuscany/sca/runtime/tomcat/TuscanyContext.java new file mode 100644 index 0000000000..c8d3aa7a46 --- /dev/null +++ b/branches/sca-android/modules/runtime-tomcat/src/main/java/org/apache/tuscany/sca/runtime/tomcat/TuscanyContext.java @@ -0,0 +1,270 @@ +/* + * 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.runtime.tomcat; + +import java.util.Hashtable; + +import javax.naming.Binding; +import javax.naming.Context; +import javax.naming.Name; +import javax.naming.NameClassPair; +import javax.naming.NameParser; +import javax.naming.NamingEnumeration; +import javax.naming.NamingException; +import javax.naming.directory.Attributes; +import javax.naming.directory.DirContext; +import javax.naming.directory.ModificationItem; +import javax.naming.directory.SearchControls; +import javax.naming.directory.SearchResult; + +import org.apache.catalina.core.StandardContext; + +/** + * + * @version $Rev$ $Date$ + */ +public class TuscanyContext extends StandardContext { + private static final long serialVersionUID = 1L; + + public TuscanyContext() { + setProcessTlds(false); + } + + @Override + public boolean getConfigured() { + return true; + } + + @Override + public DirContext getResources() { + return DUMMY_CONTEXT; + } + + @Override + public synchronized void setResources(DirContext resources) { + setDocBase("tuscany"); + super.setResources(DUMMY_CONTEXT); + } + + private static final DirContext DUMMY_CONTEXT = new DirContext(){ + + public void bind(Name name, Object obj, Attributes attrs) throws NamingException { + } + + public void bind(String name, Object obj, Attributes attrs) throws NamingException { + } + + public DirContext createSubcontext(Name name, Attributes attrs) throws NamingException { + return null; + } + + public DirContext createSubcontext(String name, Attributes attrs) throws NamingException { + return null; + } + + public Attributes getAttributes(Name name) throws NamingException { + return null; + } + + public Attributes getAttributes(String name) throws NamingException { + return null; + } + + public Attributes getAttributes(Name name, String[] attrIds) throws NamingException { + return null; + } + + public Attributes getAttributes(String name, String[] attrIds) throws NamingException { + return null; + } + + public DirContext getSchema(Name name) throws NamingException { + return null; + } + + public DirContext getSchema(String name) throws NamingException { + return null; + } + + public DirContext getSchemaClassDefinition(Name name) throws NamingException { + return null; + } + + public DirContext getSchemaClassDefinition(String name) throws NamingException { + return null; + } + + public void modifyAttributes(Name name, ModificationItem[] mods) throws NamingException { + } + + public void modifyAttributes(String name, ModificationItem[] mods) throws NamingException { + } + + public void modifyAttributes(Name name, int mod_op, Attributes attrs) throws NamingException { + } + + public void modifyAttributes(String name, int mod_op, Attributes attrs) throws NamingException { + } + + public void rebind(Name name, Object obj, Attributes attrs) throws NamingException { + } + + public void rebind(String name, Object obj, Attributes attrs) throws NamingException { + } + + public NamingEnumeration search(Name name, Attributes matchingAttributes) throws NamingException { + return null; + } + + public NamingEnumeration search(String name, Attributes matchingAttributes) throws NamingException { + return null; + } + + public NamingEnumeration search(Name name, Attributes matchingAttributes, String[] attributesToReturn) throws NamingException { + return null; + } + + public NamingEnumeration search(String name, Attributes matchingAttributes, String[] attributesToReturn) throws NamingException { + return null; + } + + public NamingEnumeration search(Name name, String filter, SearchControls cons) throws NamingException { + return null; + } + + public NamingEnumeration search(String name, String filter, SearchControls cons) throws NamingException { + return null; + } + + public NamingEnumeration search(Name name, String filterExpr, Object[] filterArgs, SearchControls cons) throws NamingException { + return null; + } + + public NamingEnumeration search(String name, String filterExpr, Object[] filterArgs, SearchControls cons) throws NamingException { + return null; + } + + public Object addToEnvironment(String propName, Object propVal) throws NamingException { + return null; + } + + public void bind(Name name, Object obj) throws NamingException { + } + + public void bind(String name, Object obj) throws NamingException { + } + + public void close() throws NamingException { + } + + public Name composeName(Name name, Name prefix) throws NamingException { + return null; + } + + public String composeName(String name, String prefix) throws NamingException { + return null; + } + + public Context createSubcontext(Name name) throws NamingException { + return null; + } + + public Context createSubcontext(String name) throws NamingException { + return null; + } + + public void destroySubcontext(Name name) throws NamingException { + } + + public void destroySubcontext(String name) throws NamingException { + } + + public Hashtable getEnvironment() throws NamingException { + return null; + } + + public String getNameInNamespace() throws NamingException { + return null; + } + + public NameParser getNameParser(Name name) throws NamingException { + return null; + } + + public NameParser getNameParser(String name) throws NamingException { + return null; + } + + public NamingEnumeration list(Name name) throws NamingException { + return null; + } + + public NamingEnumeration list(String name) throws NamingException { + return null; + } + + public NamingEnumeration listBindings(Name name) throws NamingException { + return null; + } + + public NamingEnumeration listBindings(String name) throws NamingException { + throw new NamingException(); + } + + public Object lookup(Name name) throws NamingException { + return null; + } + + public Object lookup(String name) throws NamingException { + return null; + } + + public Object lookupLink(Name name) throws NamingException { + return null; + } + + public Object lookupLink(String name) throws NamingException { + return null; + } + + public void rebind(Name name, Object obj) throws NamingException { + } + + public void rebind(String name, Object obj) throws NamingException { + } + + public Object removeFromEnvironment(String propName) throws NamingException { + return null; + } + + public void rename(Name oldName, Name newName) throws NamingException { + } + + public void rename(String oldName, String newName) throws NamingException { + } + + public void unbind(Name name) throws NamingException { + } + + public void unbind(String name) throws NamingException { + } + }; + +} diff --git a/branches/sca-android/modules/runtime-tomcat/src/main/java/org/apache/tuscany/sca/runtime/tomcat/TuscanyContextListener.java b/branches/sca-android/modules/runtime-tomcat/src/main/java/org/apache/tuscany/sca/runtime/tomcat/TuscanyContextListener.java new file mode 100644 index 0000000000..b19e2326a2 --- /dev/null +++ b/branches/sca-android/modules/runtime-tomcat/src/main/java/org/apache/tuscany/sca/runtime/tomcat/TuscanyContextListener.java @@ -0,0 +1,69 @@ +/* + * 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.runtime.tomcat; + +import java.io.File; + +import org.apache.catalina.Context; +import org.apache.catalina.Lifecycle; +import org.apache.catalina.LifecycleEvent; +import org.apache.catalina.LifecycleListener; +import org.apache.catalina.core.StandardContext; +//import org.apache.tuscany.sca.runtime.Launcher; + +/** + * + * @version $Rev$ $Date$ + */ +public class TuscanyContextListener implements LifecycleListener { + +// private Launcher launcher; + +// public TuscanyContextListener(Launcher launcher) { +// this.launcher = launcher; +// } + + public void lifecycleEvent(LifecycleEvent event) { + String type = event.getType(); + if (Lifecycle.AFTER_START_EVENT.equals(type)) { + startContext((Context) event.getLifecycle()); + } else if (Lifecycle.STOP_EVENT.equals(type)) { + stopContext((Context) event.getLifecycle()); + } + } + + protected void startContext(Context context) { + StandardContext sc = (StandardContext) context; + String path = sc.getServletContext().getRealPath("/"); + try { + File f = new File(path + "WEB-INF/classes"); + if (f.exists()) { + System.out.println("adding contribution: "+ path); +// launcher.addContribution(f.toURL()); + } + } catch (Exception e) { + e.printStackTrace(); + } + } + + protected void stopContext(Context context) { + } + +} diff --git a/branches/sca-android/modules/runtime-tomcat/src/main/java/org/apache/tuscany/sca/runtime/tomcat/TuscanyHost.java b/branches/sca-android/modules/runtime-tomcat/src/main/java/org/apache/tuscany/sca/runtime/tomcat/TuscanyHost.java new file mode 100644 index 0000000000..38280443f0 --- /dev/null +++ b/branches/sca-android/modules/runtime-tomcat/src/main/java/org/apache/tuscany/sca/runtime/tomcat/TuscanyHost.java @@ -0,0 +1,159 @@ +/* + * 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.runtime.tomcat; + +import java.io.File; +import java.io.IOException; +import java.util.ArrayList; +import java.util.List; +import java.util.jar.JarFile; +import java.util.logging.Level; +import java.util.logging.Logger; + +import org.apache.catalina.Container; +import org.apache.catalina.LifecycleException; +import org.apache.catalina.core.StandardContext; +import org.apache.catalina.core.StandardEngine; +import org.apache.catalina.core.StandardHost; +import org.apache.catalina.deploy.FilterDef; +import org.apache.catalina.deploy.FilterMap; + +/** + * To use this copy all the Tuscany jars to the Tomcat lib folder and update + * the Tomcat conf/server.xml to include className="org.apache.tuscany.sca.runtime.tomcat.TomcatHost" + * + * For example: + * + * + * + * + * @version $Rev$ $Date$ + */ +public class TuscanyHost extends StandardHost { + private static final long serialVersionUID = 1L; + private static final Logger logger = Logger.getLogger(TuscanyHost.class.getName()); + + // TODO static for expedience, find a better way to share when/if this is working properly + protected static List scaApps = new ArrayList(); + + @Override + public synchronized void start() throws LifecycleException { + try { + logger.log(Level.INFO, "Starting Tuscany/SCA runtime"); + + super.start(); + + } catch (Exception e) { + logger.log(Level.SEVERE, "Exception starting Tuscany/SCA runtime", e); + } + } + + @Override + public synchronized void stop() throws LifecycleException { + try { + logger.log(Level.INFO, "Stopping Tuscany/SCA runtime"); + + super.stop(); + + } catch (Exception e) { + logger.log(Level.SEVERE, "Exception Stopping Tuscany/SCA runtime", e); + } + } + + @Override + public synchronized void addChild(Container child) { + try { + if (isSCAApp(child)) { + initSCAApplication((StandardContext)child); + } + } catch (Exception e) { + logger.log(Level.WARNING, "Exception detecting SCA application " + child.getName(), e); + } + super.addChild(child); + } + + /** + * Tests if the child is an SCA application by checking for the presence + * of either an sca-contribution.xml file or a sca-deployables folder + */ + protected boolean isSCAApp(Container child) throws IOException { + if (child instanceof StandardContext) { + StandardContext sc = (StandardContext) child; + + if (sc.getDocBase().endsWith(".war")) { + JarFile jar = null; + try { + jar = new JarFile(((StandardEngine)this.getParent()).getBaseDir() + "/" + getAppBase() + "/" + sc.getDocBase()); + + if (jar.getEntry("META-INF/sca-deployables/") != null) { + return true; + } + + if (jar.getEntry("META-INF/sca-contribution.xml") != null) { + return true; + } + + } finally { + if (jar != null) { + jar.close(); + } + } + } else { + File webappRoot = new File(sc.getConfigFile()).getParentFile().getParentFile(); + + File scaDeployables = new File(webappRoot, "META-INF/sca-deployables"); + if (scaDeployables.exists()) { + return true; + } + + File scaContribution = new File(webappRoot, "META-INF/sca-contribution.xml"); + if (scaContribution.exists()) { + return true; + } + } + } + return false; + } + + protected void initSCAApplication(StandardContext scaApp) { + logger.log(Level.INFO, "Initilizing SCA application: " + scaApp.getName()); + + // Add the Tuscany ContextListener + scaApp.addApplicationListener(org.apache.tuscany.sca.host.webapp.TuscanyContextListener.class.getName()); + + // Add the Tuscany Servlet Filter + FilterDef filterDef = new FilterDef(); + filterDef.setFilterClass(org.apache.tuscany.sca.host.webapp.TuscanyServletFilter.class.getName()); + filterDef.setFilterName(org.apache.tuscany.sca.host.webapp.TuscanyServletFilter.class.getName()); + scaApp.addFilterDef(filterDef); + FilterMap filterMap = new FilterMap(); + filterMap.setFilterName(filterDef.getFilterName()); + filterMap.addURLPattern("/*"); + scaApp.addFilterMap(filterMap); + + scaApp.setAnnotationProcessor(new TuscanyAnnotationProcessor(scaApp)); + + scaApps.add(scaApp); + } + +} diff --git a/branches/sca-android/modules/runtime-tomcat/src/main/java/org/apache/tuscany/sca/runtime/tomcat/TuscanyServlet.java b/branches/sca-android/modules/runtime-tomcat/src/main/java/org/apache/tuscany/sca/runtime/tomcat/TuscanyServlet.java new file mode 100644 index 0000000000..112b404515 --- /dev/null +++ b/branches/sca-android/modules/runtime-tomcat/src/main/java/org/apache/tuscany/sca/runtime/tomcat/TuscanyServlet.java @@ -0,0 +1,65 @@ +/* + * 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.runtime.tomcat; + +import java.io.IOException; + +import javax.servlet.RequestDispatcher; +import javax.servlet.ServletConfig; +import javax.servlet.ServletException; +import javax.servlet.ServletRequest; +import javax.servlet.ServletResponse; +import javax.servlet.http.HttpServlet; +import javax.servlet.http.HttpServletRequest; + +import org.apache.tuscany.sca.host.webapp.WebAppServletHost; + +/** + * A Servlet that forwards requests to the Servlets registered with the Tuscany + * ServletHost. + * + * TODO: Copy of host-webapp TuscanyServlet. Can be removed once host-webapp TuscanyServlet + * moved over to latest SCADomain API. + * + * @version $Rev$ $Date$ + */ +public class TuscanyServlet extends HttpServlet { + private static final long serialVersionUID = 1L; + + private transient WebAppServletHost servletHost; + + @Override + public void init(ServletConfig config) throws ServletException { + servletHost = WebAppServletHost.getInstance(); + servletHost.init(config); + } + + @Override + public void service(ServletRequest req, ServletResponse res) throws ServletException, IOException { + String path = ((HttpServletRequest)req).getPathInfo(); + RequestDispatcher dispatcher = servletHost.getRequestDispatcher(path); + if (dispatcher == null) { + throw new IllegalStateException("No servlet registered for path: " + path); + } + + dispatcher.forward(req, res); + } + +} diff --git a/branches/sca-android/modules/runtime-tomcat/src/main/java/org/apache/tuscany/sca/runtime/tomcat/TuscanyWrapper.java b/branches/sca-android/modules/runtime-tomcat/src/main/java/org/apache/tuscany/sca/runtime/tomcat/TuscanyWrapper.java new file mode 100644 index 0000000000..8764094d48 --- /dev/null +++ b/branches/sca-android/modules/runtime-tomcat/src/main/java/org/apache/tuscany/sca/runtime/tomcat/TuscanyWrapper.java @@ -0,0 +1,51 @@ +/* + * 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.runtime.tomcat; + +import javax.servlet.Servlet; + +import org.apache.catalina.core.StandardWrapper; + +/** + * Override the StandardWrapper so that Servlets are not instantiated from the + * class name but just the instance Tuscany passes in the constructor is always used. + * + * @version $Rev$ $Date$ + */ +public class TuscanyWrapper extends StandardWrapper { + private static final long serialVersionUID = 1L; + + private final Servlet servlet; + + public TuscanyWrapper(Servlet servlet) { + super(); + this.servlet = servlet; + } + + @Override + public synchronized Servlet loadServlet() { + return servlet; + } + + @Override + public Servlet getServlet() { + return servlet; + } +} \ No newline at end of file -- cgit v1.2.3