You are viewing a plain text version of this content. The canonical link for it is here.
Posted to scm@geronimo.apache.org by va...@apache.org on 2008/11/19 12:54:26 UTC

svn commit: r718938 - in /geronimo/plugins/tuscany/trunk: geronimo-tuscany/src/main/java/org/apache/geronimo/tuscany/ samples/helloworld-servlet/src/main/java/sample/ samples/helloworld-servlet/src/main/webapp/WEB-INF/ tuscany-jetty/ tuscany-tomcat/

Author: vamsic007
Date: Wed Nov 19 03:54:25 2008
New Revision: 718938

URL: http://svn.apache.org/viewvc?rev=718938&view=rev
Log:
Injection of references into servlet fields.

Added:
    geronimo/plugins/tuscany/trunk/geronimo-tuscany/src/main/java/org/apache/geronimo/tuscany/SCAServiceReference.java   (with props)
    geronimo/plugins/tuscany/trunk/samples/helloworld-servlet/src/main/webapp/WEB-INF/web.composite   (with props)
Modified:
    geronimo/plugins/tuscany/trunk/geronimo-tuscany/src/main/java/org/apache/geronimo/tuscany/TuscanyModuleBuilderExtension.java
    geronimo/plugins/tuscany/trunk/samples/helloworld-servlet/src/main/java/sample/HelloworldServlet.java
    geronimo/plugins/tuscany/trunk/samples/helloworld-servlet/src/main/webapp/WEB-INF/geronimo-web.xml
    geronimo/plugins/tuscany/trunk/tuscany-jetty/pom.xml
    geronimo/plugins/tuscany/trunk/tuscany-tomcat/pom.xml

Added: geronimo/plugins/tuscany/trunk/geronimo-tuscany/src/main/java/org/apache/geronimo/tuscany/SCAServiceReference.java
URL: http://svn.apache.org/viewvc/geronimo/plugins/tuscany/trunk/geronimo-tuscany/src/main/java/org/apache/geronimo/tuscany/SCAServiceReference.java?rev=718938&view=auto
==============================================================================
--- geronimo/plugins/tuscany/trunk/geronimo-tuscany/src/main/java/org/apache/geronimo/tuscany/SCAServiceReference.java (added)
+++ geronimo/plugins/tuscany/trunk/geronimo-tuscany/src/main/java/org/apache/geronimo/tuscany/SCAServiceReference.java Wed Nov 19 03:54:25 2008
@@ -0,0 +1,64 @@
+/*
+* 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.geronimo.tuscany;
+
+import javax.naming.InitialContext;
+import javax.naming.NamingException;
+
+import org.apache.tuscany.sca.assembly.Component;
+import org.apache.tuscany.sca.host.embedded.SCADomain;
+import org.apache.xbean.naming.reference.SimpleReference;
+import org.osoa.sca.ComponentContext;
+
+/**
+ * @version $Rev$ $Date$
+ */
+public class SCAServiceReference extends SimpleReference {
+    private String componentName;
+    private String interfaceName;
+    private String referenceName;
+    
+    public SCAServiceReference(String interfaceName, String componentName, String referenceName) {
+        this.interfaceName = interfaceName;
+        this.componentName = componentName;
+        this.referenceName = referenceName;
+    }
+
+    @Override
+    public Object getContent() throws NamingException {
+        InitialContext ctx = new InitialContext();
+        SCADomain domain = (SCADomain) ctx.lookup("ger:/SCADomain");
+        ComponentContext componentContext = null;
+        try {
+            Class referenceInterface = Thread.currentThread().getContextClassLoader().loadClass(interfaceName);
+            for(Component component : ((org.apache.tuscany.sca.host.embedded.impl.EmbeddedSCADomain)domain).getDomainComposite().getComponents()) {
+                if(component.getName().equals(componentName)) {
+                    componentContext = ((org.apache.tuscany.sca.core.assembly.RuntimeComponentImpl)component).getComponentContext();
+                }
+            }
+            
+            return componentContext.getService(referenceInterface, referenceName);
+        } catch (ClassNotFoundException e) {
+            // TODO Auto-generated catch block
+            e.printStackTrace();
+        }
+        return null;
+    }
+}

Propchange: geronimo/plugins/tuscany/trunk/geronimo-tuscany/src/main/java/org/apache/geronimo/tuscany/SCAServiceReference.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: geronimo/plugins/tuscany/trunk/geronimo-tuscany/src/main/java/org/apache/geronimo/tuscany/SCAServiceReference.java
------------------------------------------------------------------------------
    svn:keywords = Date Revision

Propchange: geronimo/plugins/tuscany/trunk/geronimo-tuscany/src/main/java/org/apache/geronimo/tuscany/SCAServiceReference.java
------------------------------------------------------------------------------
    svn:mime-type = text/plain

Modified: geronimo/plugins/tuscany/trunk/geronimo-tuscany/src/main/java/org/apache/geronimo/tuscany/TuscanyModuleBuilderExtension.java
URL: http://svn.apache.org/viewvc/geronimo/plugins/tuscany/trunk/geronimo-tuscany/src/main/java/org/apache/geronimo/tuscany/TuscanyModuleBuilderExtension.java?rev=718938&r1=718937&r2=718938&view=diff
==============================================================================
--- geronimo/plugins/tuscany/trunk/geronimo-tuscany/src/main/java/org/apache/geronimo/tuscany/TuscanyModuleBuilderExtension.java (original)
+++ geronimo/plugins/tuscany/trunk/geronimo-tuscany/src/main/java/org/apache/geronimo/tuscany/TuscanyModuleBuilderExtension.java Wed Nov 19 03:54:25 2008
@@ -20,12 +20,18 @@
 import java.io.File;
 import java.io.FileInputStream;
 import java.io.FileOutputStream;
+import java.lang.reflect.Field;
 import java.net.URL;
 import java.util.Collection;
+import java.util.HashMap;
+import java.util.Map;
 import java.util.jar.JarFile;
 import java.util.jar.JarOutputStream;
 import java.util.zip.ZipEntry;
 
+import javax.naming.InitialContext;
+import javax.naming.NamingException;
+
 import org.apache.geronimo.common.DeploymentException;
 import org.apache.geronimo.deployment.ModuleIDBuilder;
 import org.apache.geronimo.deployment.service.EnvironmentBuilder;
@@ -34,10 +40,13 @@
 import org.apache.geronimo.gbean.GBeanData;
 import org.apache.geronimo.gbean.GBeanInfo;
 import org.apache.geronimo.gbean.GBeanInfoBuilder;
+import org.apache.geronimo.j2ee.annotation.Holder;
+import org.apache.geronimo.j2ee.annotation.Injection;
 import org.apache.geronimo.j2ee.deployment.EARContext;
 import org.apache.geronimo.j2ee.deployment.Module;
 import org.apache.geronimo.j2ee.deployment.ModuleBuilderExtension;
 import org.apache.geronimo.j2ee.deployment.NamingBuilder;
+import org.apache.geronimo.j2ee.deployment.WebModule;
 import org.apache.geronimo.j2ee.j2eeobjectnames.NameFactory;
 import org.apache.geronimo.kernel.GBeanAlreadyExistsException;
 import org.apache.geronimo.kernel.Naming;
@@ -45,6 +54,15 @@
 import org.apache.geronimo.kernel.config.IOUtil;
 import org.apache.geronimo.kernel.repository.Environment;
 import org.apache.geronimo.kernel.repository.Repository;
+import org.apache.geronimo.xbeans.javaee.ServletType;
+import org.apache.geronimo.xbeans.javaee.WebAppType;
+import org.apache.tuscany.sca.assembly.Component;
+import org.apache.tuscany.sca.assembly.Composite;
+import org.apache.tuscany.sca.contribution.Artifact;
+import org.apache.tuscany.sca.contribution.Contribution;
+import org.apache.tuscany.sca.host.embedded.impl.EmbeddedSCADomain;
+import org.apache.xmlbeans.XmlObject;
+import org.osoa.sca.annotations.Reference;
 
 /**
  * A ModuleBuilderExtension to process SCA artifacts.
@@ -69,9 +87,11 @@
     }
 
     public void installModule(JarFile earFile, EARContext earContext, Module module, Collection configurationStores, ConfigurationStore targetConfigurationStore, Collection repository) throws DeploymentException {
+        System.out.println("Inside TuscanyModuleBuilderExtension.installModule");
     }
 
     public void initContext(EARContext earContext, Module module, ClassLoader cl) throws DeploymentException {
+        System.out.println("Inside TuscanyModuleBuilderExtension.initContext");
     }
 
     public void addGBeans(EARContext earContext, Module module, ClassLoader cl, Collection repository) throws DeploymentException {
@@ -115,6 +135,69 @@
         } catch (GBeanAlreadyExistsException e) {
             throw new DeploymentException(e);
         }
+        
+        EARContext moduleContext = module.getEarContext();
+        Map sharedContext = module.getSharedContext();
+        moduleName = moduleContext.getModuleName();
+        Map<NamingBuilder.Key, Object> buildingContext = new HashMap<NamingBuilder.Key, Object>();
+        buildingContext.put(NamingBuilder.GBEAN_NAME_KEY, moduleName);
+
+        //use the same jndi context as the web app
+        Map compContext = NamingBuilder.JNDI_KEY.get(module.getSharedContext());
+        buildingContext.put(NamingBuilder.JNDI_KEY, compContext);
+
+        //use the same holder object as the web app.
+        Holder holder = NamingBuilder.INJECTION_KEY.get(sharedContext);
+        buildingContext.put(NamingBuilder.INJECTION_KEY, holder);
+        
+        String thisComponentName = null;
+        ModelResolverImpl modelResolver = new ModelResolverImpl(cl);
+        try {
+            Contribution contribution = getEmbeddedSCADomain().getContributionService().contribute(appCompositeJarFile.toURL().toString(), appCompositeJarFile.toURL(), modelResolver, false);
+            for(Artifact artifact : contribution.getArtifacts()) {
+                if (artifact.getModel() instanceof Composite) {
+                    for(Component component : ((Composite)artifact.getModel()).getComponents()) {
+                        thisComponentName = component.getName();
+                        break;
+                    }
+                }
+            }
+        } catch (Exception e1) {
+            // TODO Auto-generated catch block
+            e1.printStackTrace();
+        }
+        
+        WebModule webModule = (WebModule) module;
+        WebAppType webApp = (WebAppType) webModule.getSpecDD();
+        XmlObject vendorWebApp = webModule.getVendorDD();
+        
+        // Find the injection points
+        ServletType[] servlets = webApp.getServletArray();
+        for(ServletType servlet:servlets) {
+            String servletClassName = servlet.getServletClass().getStringValue().trim();
+            try {
+                Class servletClass  = cl.loadClass(servletClassName);
+                for(Field field : servletClass.getDeclaredFields()) {
+                    if(field.isAnnotationPresent(Reference.class)) {
+                        Reference ref = field.getAnnotation(Reference.class);
+                        String referenceName = ref.name() != null && !ref.name().equals("") ? ref.name() : field.getName();
+                        holder.addInjection(servletClassName, new Injection(servletClassName, field.getName(), servletClassName+"/"+field.getName()));
+                        compContext.put("env/"+servletClassName+"/"+field.getName(), new SCAServiceReference(field.getType().getName(), thisComponentName, referenceName));
+                    }
+                }
+            } catch (ClassNotFoundException e) {
+                // TODO Auto-generated catch block
+                e.printStackTrace();
+            }
+        }
+
+        namingBuilders.buildNaming(webApp, vendorWebApp, webModule, buildingContext);
+    }
+
+    private EmbeddedSCADomain getEmbeddedSCADomain() throws NamingException {
+        InitialContext ctx = new InitialContext();
+        EmbeddedSCADomain domain = (EmbeddedSCADomain) ctx.lookup("ger:/SCADomain");
+        return domain;
     }
 
     public static final GBeanInfo GBEAN_INFO;

Modified: geronimo/plugins/tuscany/trunk/samples/helloworld-servlet/src/main/java/sample/HelloworldServlet.java
URL: http://svn.apache.org/viewvc/geronimo/plugins/tuscany/trunk/samples/helloworld-servlet/src/main/java/sample/HelloworldServlet.java?rev=718938&r1=718937&r2=718938&view=diff
==============================================================================
--- geronimo/plugins/tuscany/trunk/samples/helloworld-servlet/src/main/java/sample/HelloworldServlet.java (original)
+++ geronimo/plugins/tuscany/trunk/samples/helloworld-servlet/src/main/java/sample/HelloworldServlet.java Wed Nov 19 03:54:25 2008
@@ -41,21 +41,24 @@
     @Override
     public void init(ServletConfig config) {
         if (service == null) {
+            System.out.println("service is initialized in init method.");
             // TODO:The helloworldService reference will only be injected from the @Reference 
             // annotation in containers supporting SCA "deep" integration. In other 
             // environments in can be looked up from the ComponentContext.
-        	// for now do it this way:
-        	ComponentContext componentContext = ComponentContextLocator.getComponentContext(config.getServletContext());
-        	service = componentContext.getService(Helloworld.class, "service");
+            // for now do it this way:
+            ComponentContext componentContext = ComponentContextLocator.getComponentContext(config.getServletContext());
+            service = componentContext.getService(Helloworld.class, "service");
+        } else {
+            System.out.println("service is injected.");
         }
     }
     
     @Override
     protected void service(HttpServletRequest request, HttpServletResponse response) throws IOException {
 
-    	String name = request.getParameter("name");
-    	String greeting = service.sayHello(name);
-    	
+        String name = request.getParameter("name");
+        String greeting = service.sayHello(name);
+        
         Writer out = response.getWriter();
         out.write("<html><head><title>Apache Tuscany Helloworld Servlet Sample</title></head><body>");
         out.write("<h2>Apache Tuscany Helloworld Servlet Sample</h2>");

Modified: geronimo/plugins/tuscany/trunk/samples/helloworld-servlet/src/main/webapp/WEB-INF/geronimo-web.xml
URL: http://svn.apache.org/viewvc/geronimo/plugins/tuscany/trunk/samples/helloworld-servlet/src/main/webapp/WEB-INF/geronimo-web.xml?rev=718938&r1=718937&r2=718938&view=diff
==============================================================================
--- geronimo/plugins/tuscany/trunk/samples/helloworld-servlet/src/main/webapp/WEB-INF/geronimo-web.xml (original)
+++ geronimo/plugins/tuscany/trunk/samples/helloworld-servlet/src/main/webapp/WEB-INF/geronimo-web.xml Wed Nov 19 03:54:25 2008
@@ -25,13 +25,7 @@
       <version>2.0</version>
       <type>car</type>
     </moduleId>
-    <dependencies>
-      <dependency>
-        <groupId> org.apache.geronimo.plugins</groupId>
-        <artifactId>tuscany-tomcat</artifactId>
-        <type>car</type>
-      </dependency>
-    </dependencies>
+    <dependencies/>
     <hidden-classes/>
     <non-overridable-classes/>    
   </environment>

Added: geronimo/plugins/tuscany/trunk/samples/helloworld-servlet/src/main/webapp/WEB-INF/web.composite
URL: http://svn.apache.org/viewvc/geronimo/plugins/tuscany/trunk/samples/helloworld-servlet/src/main/webapp/WEB-INF/web.composite?rev=718938&view=auto
==============================================================================
--- geronimo/plugins/tuscany/trunk/samples/helloworld-servlet/src/main/webapp/WEB-INF/web.composite (added)
+++ geronimo/plugins/tuscany/trunk/samples/helloworld-servlet/src/main/webapp/WEB-INF/web.composite Wed Nov 19 03:54:25 2008
@@ -0,0 +1,32 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<!--
+ * 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.    
+-->
+<composite xmlns="http://www.osoa.org/xmlns/sca/1.0"
+           targetNamespace="http://sample"
+           name="Helloworld-servlet-web">
+
+    <component name="bar">
+        <implementation.web web-uri="helloworld-servlet.war"/>
+        <reference name="service" target="HelloworldComponent" >
+            <binding.sca/>
+        </reference>
+    </component>
+
+</composite>
+

Propchange: geronimo/plugins/tuscany/trunk/samples/helloworld-servlet/src/main/webapp/WEB-INF/web.composite
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: geronimo/plugins/tuscany/trunk/samples/helloworld-servlet/src/main/webapp/WEB-INF/web.composite
------------------------------------------------------------------------------
    svn:keywords = Date Revision

Propchange: geronimo/plugins/tuscany/trunk/samples/helloworld-servlet/src/main/webapp/WEB-INF/web.composite
------------------------------------------------------------------------------
    svn:mime-type = text/xml

Modified: geronimo/plugins/tuscany/trunk/tuscany-jetty/pom.xml
URL: http://svn.apache.org/viewvc/geronimo/plugins/tuscany/trunk/tuscany-jetty/pom.xml?rev=718938&r1=718937&r2=718938&view=diff
==============================================================================
--- geronimo/plugins/tuscany/trunk/tuscany-jetty/pom.xml (original)
+++ geronimo/plugins/tuscany/trunk/tuscany-jetty/pom.xml Wed Nov 19 03:54:25 2008
@@ -129,6 +129,11 @@
         </dependency>
         <dependency>
             <groupId>org.apache.geronimo.configs</groupId>
+            <artifactId>j2ee-deployer</artifactId>
+            <type>car</type>
+        </dependency>
+        <dependency>
+            <groupId>org.apache.geronimo.configs</groupId>
             <artifactId>jasper</artifactId>
             <type>car</type>
         </dependency>

Modified: geronimo/plugins/tuscany/trunk/tuscany-tomcat/pom.xml
URL: http://svn.apache.org/viewvc/geronimo/plugins/tuscany/trunk/tuscany-tomcat/pom.xml?rev=718938&r1=718937&r2=718938&view=diff
==============================================================================
--- geronimo/plugins/tuscany/trunk/tuscany-tomcat/pom.xml (original)
+++ geronimo/plugins/tuscany/trunk/tuscany-tomcat/pom.xml Wed Nov 19 03:54:25 2008
@@ -438,6 +438,7 @@
                                 </gbean>
                                 <gbean name="GeronimoSCADomain">
                                     <attribute name="domainUri">http://localhost</attribute>
+                                    <attribute name="jndiName">ger:/SCADomain</attribute>
                                 </gbean>
                             </config-xml-content>
                             <source-repository>http://repo1.maven.org/maven2/</source-repository>