You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@tuscany.apache.org by js...@apache.org on 2007/06/27 21:53:12 UTC

svn commit: r551301 - in /incubator/tuscany/java/sca/samples/implementation-pojo2-extension: ./ src/main/java/pojo2/extension/ src/main/java/pojo2/helper/ src/main/resources/META-INF/services/ src/test/java/helloworld/ src/test/resources/helloworld/

Author: jsdelfino
Date: Wed Jun 27 12:53:11 2007
New Revision: 551301

URL: http://svn.apache.org/viewvc?view=rev&rev=551301
Log:
Added a custom StAXArtifactProcessor showing how to resolve a class, a componentType, and merge the componentType into the implementation model.

Added:
    incubator/tuscany/java/sca/samples/implementation-pojo2-extension/src/main/java/pojo2/extension/POJOImplementationProcessor.java   (with props)
    incubator/tuscany/java/sca/samples/implementation-pojo2-extension/src/test/java/helloworld/HelloWorldImpl2.java   (with props)
    incubator/tuscany/java/sca/samples/implementation-pojo2-extension/src/test/resources/helloworld/HelloWorldImpl2.componentType   (with props)
Removed:
    incubator/tuscany/java/sca/samples/implementation-pojo2-extension/src/main/java/pojo2/helper/
Modified:
    incubator/tuscany/java/sca/samples/implementation-pojo2-extension/pom.xml
    incubator/tuscany/java/sca/samples/implementation-pojo2-extension/src/main/java/pojo2/extension/POJOImplementation.java
    incubator/tuscany/java/sca/samples/implementation-pojo2-extension/src/main/resources/META-INF/services/org.apache.tuscany.sca.contribution.processor.StAXArtifactProcessor
    incubator/tuscany/java/sca/samples/implementation-pojo2-extension/src/test/java/helloworld/HelloWorldTestCase.java
    incubator/tuscany/java/sca/samples/implementation-pojo2-extension/src/test/resources/helloworld/helloworld.composite

Modified: incubator/tuscany/java/sca/samples/implementation-pojo2-extension/pom.xml
URL: http://svn.apache.org/viewvc/incubator/tuscany/java/sca/samples/implementation-pojo2-extension/pom.xml?view=diff&rev=551301&r1=551300&r2=551301
==============================================================================
--- incubator/tuscany/java/sca/samples/implementation-pojo2-extension/pom.xml (original)
+++ incubator/tuscany/java/sca/samples/implementation-pojo2-extension/pom.xml Wed Jun 27 12:53:11 2007
@@ -44,7 +44,7 @@
 
         <dependency>
             <groupId>org.apache.tuscany.sca</groupId>
-            <artifactId>tuscany-interface-java-xml</artifactId>
+            <artifactId>tuscany-interface-java-runtime</artifactId>
             <version>1.0-incubating-SNAPSHOT</version>
         </dependency>        
 

Modified: incubator/tuscany/java/sca/samples/implementation-pojo2-extension/src/main/java/pojo2/extension/POJOImplementation.java
URL: http://svn.apache.org/viewvc/incubator/tuscany/java/sca/samples/implementation-pojo2-extension/src/main/java/pojo2/extension/POJOImplementation.java?view=diff&rev=551301&r1=551300&r2=551301
==============================================================================
--- incubator/tuscany/java/sca/samples/implementation-pojo2-extension/src/main/java/pojo2/extension/POJOImplementation.java (original)
+++ incubator/tuscany/java/sca/samples/implementation-pojo2-extension/src/main/java/pojo2/extension/POJOImplementation.java Wed Jun 27 12:53:11 2007
@@ -20,6 +20,7 @@
 package pojo2.extension;
 
 import java.lang.reflect.Method;
+import java.util.ArrayList;
 import java.util.Collections;
 import java.util.HashMap;
 import java.util.List;
@@ -33,8 +34,6 @@
 import org.apache.tuscany.sca.policy.Intent;
 import org.apache.tuscany.sca.policy.PolicySet;
 
-import pojo2.helper.TemporaryExtensionHelper;
-
 
 /**
  * Represents a POJO implementation in an SCA assembly.
@@ -45,29 +44,28 @@
     
     private String pojoName;
     private Class<?> pojoClass;
+    private String uri;
     private Map<String, Method> methods;
-    private Service service;
+    private List<Service> services = new ArrayList<Service>();
+    private List<Reference> references = new ArrayList<Reference>();
+    private List<Property> properties = new ArrayList<Property>();
+    private boolean unresolved;
 
     /**
      * Returns the POJO class name
      * @return
      */
-    public String getClass_() {
+    public String getPOJOName() {
         return pojoName;
     }
 
     /**
      * Sets the POJO class name
-     * @param class_
+     * @param pojoName
      */
-    public void setClass_(String class_) {
-        this.pojoName = class_;
-        
-        try {
-            setPOJOClass(Class.forName(pojoName));
-        } catch (Exception e) {
-            throw new IllegalArgumentException(e);
-        }
+    public void setPOJOName(String pojoName) {
+        this.pojoName = pojoName;
+        setURI(pojoName.replace('.', '/'));
     }
     
     /**
@@ -84,6 +82,7 @@
      */
     public void setPOJOClass(Class<?> pojoClass) {
         this.pojoClass = pojoClass;
+        setPOJOName(pojoClass.getName());
         
         // Index the POJO's methods
         methods = new HashMap<String, Method>();
@@ -91,9 +90,6 @@
         for (int i = 0; i < m.length; i++) {
             methods.put(m[i].getName(), m[i]);
         }
-        
-        // Create a service for the POJO class
-        service = TemporaryExtensionHelper.createJavaService(pojoClass.getSimpleName(), pojoClass);
     }
   
     /**
@@ -110,23 +106,19 @@
     }
 
     public List<Property> getProperties() {
-        // The sample POJO implementation does not support properties
-        return Collections.emptyList();
+        return properties;
     }
 
     public List<Service> getServices() {
-        // The sample POJO implementation provides a single fixed POJO service
-        return Collections.singletonList(service);
+        return services;
     }
     
     public List<Reference> getReferences() {
-        // The sample POJO implementation does not support properties
-        return Collections.emptyList();
+        return references;
     }
 
     public String getURI() {
-        // The sample POJO implementation does not have a URI
-        return null;
+        return uri;
     }
 
     public void setConstrainingType(ConstrainingType constrainingType) {
@@ -134,7 +126,7 @@
     }
 
     public void setURI(String uri) {
-        // The sample POJO implementation does not have a URI
+        this.uri = uri;
     }
 
     public List<PolicySet> getPolicySets() {
@@ -153,12 +145,25 @@
     }
 
     public boolean isUnresolved() {
-        // The sample POJO implementation is always resolved
-        return false;
+        return unresolved;
     }
 
     public void setUnresolved(boolean unresolved) {
-        // The sample POJO implementation is always resolved
+        this.unresolved = unresolved;
+    }
+
+    @Override
+    public int hashCode() {
+        return uri.hashCode();
+    }
+    
+    @Override
+    public boolean equals(Object obj) {
+        if (obj instanceof POJOImplementation) {
+            return ((POJOImplementation)obj).getURI().equals(uri);
+        } else {
+            return false;
+        }
     }
 
 }

Added: incubator/tuscany/java/sca/samples/implementation-pojo2-extension/src/main/java/pojo2/extension/POJOImplementationProcessor.java
URL: http://svn.apache.org/viewvc/incubator/tuscany/java/sca/samples/implementation-pojo2-extension/src/main/java/pojo2/extension/POJOImplementationProcessor.java?view=auto&rev=551301
==============================================================================
--- incubator/tuscany/java/sca/samples/implementation-pojo2-extension/src/main/java/pojo2/extension/POJOImplementationProcessor.java (added)
+++ incubator/tuscany/java/sca/samples/implementation-pojo2-extension/src/main/java/pojo2/extension/POJOImplementationProcessor.java Wed Jun 27 12:53:11 2007
@@ -0,0 +1,161 @@
+/*
+ * 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 pojo2.extension;
+
+import static javax.xml.stream.XMLStreamConstants.END_ELEMENT;
+
+import javax.xml.namespace.QName;
+import javax.xml.stream.XMLStreamException;
+import javax.xml.stream.XMLStreamReader;
+import javax.xml.stream.XMLStreamWriter;
+
+import org.apache.tuscany.sca.assembly.AssemblyFactory;
+import org.apache.tuscany.sca.assembly.ComponentType;
+import org.apache.tuscany.sca.assembly.Service;
+import org.apache.tuscany.sca.contribution.processor.StAXArtifactProcessor;
+import org.apache.tuscany.sca.contribution.resolver.ClassReference;
+import org.apache.tuscany.sca.contribution.resolver.ModelResolver;
+import org.apache.tuscany.sca.contribution.service.ContributionReadException;
+import org.apache.tuscany.sca.contribution.service.ContributionResolveException;
+import org.apache.tuscany.sca.contribution.service.ContributionWriteException;
+import org.apache.tuscany.sca.core.ExtensionPointRegistry;
+import org.apache.tuscany.sca.core.ModelFactoryExtensionPoint;
+import org.apache.tuscany.sca.interfacedef.InvalidInterfaceException;
+import org.apache.tuscany.sca.interfacedef.java.JavaInterface;
+import org.apache.tuscany.sca.interfacedef.java.JavaInterfaceContract;
+import org.apache.tuscany.sca.interfacedef.java.JavaInterfaceFactory;
+import org.apache.tuscany.sca.interfacedef.java.introspect.DefaultJavaInterfaceIntrospectorExtensionPoint;
+import org.apache.tuscany.sca.interfacedef.java.introspect.ExtensibleJavaInterfaceIntrospector;
+import org.apache.tuscany.sca.interfacedef.java.introspect.JavaInterfaceIntrospector;
+
+/**
+ * Implements a STAX based artifact processor for POJO implementations.
+ * 
+ * The artifact processor is responsible for processing <implementation.pojo>
+ * elements in SCA assembly XML composite files and populating the POJO
+ * implementation model, resolving its references to other artifacts in the SCA
+ * contribution, and optionally write the model back to SCA assembly XML. 
+ */
+public class POJOImplementationProcessor implements StAXArtifactProcessor<POJOImplementation> {
+    private static final QName IMPLEMENTATION_POJO = new QName("http://pojo", "implementation.pojo");
+    
+    private AssemblyFactory assemblyFactory;
+    private JavaInterfaceFactory javaFactory;
+    private JavaInterfaceIntrospector javaIntrospector;
+    
+    public POJOImplementationProcessor(ExtensionPointRegistry registry) {
+        ModelFactoryExtensionPoint modelFactories = registry.getExtensionPoint(ModelFactoryExtensionPoint.class);
+        
+        // Get the assembly and Java interface factories as we'll need them to
+        // create model objects 
+        assemblyFactory = modelFactories.getFactory(AssemblyFactory.class);
+        javaFactory = modelFactories.getFactory(JavaInterfaceFactory.class);
+        
+        // Create an instance of the Java interface introspector utility class, as
+        // we may need it to introspect the POJO, if there's no componentType file
+        // describing it
+        javaIntrospector = new ExtensibleJavaInterfaceIntrospector(javaFactory, new DefaultJavaInterfaceIntrospectorExtensionPoint());
+    }
+
+    public QName getArtifactType() {
+        // Returns the qname of the XML element processed by this processor
+        return IMPLEMENTATION_POJO;
+    }
+
+    public Class<POJOImplementation> getModelType() {
+        // Returns the type of model processed by this processor
+        return POJOImplementation.class;
+    }
+
+    public POJOImplementation read(XMLStreamReader reader) throws ContributionReadException, XMLStreamException {
+    
+        // Read an <implementation.pojo> element
+        
+        // Read the POJO class attribute.
+        String className = reader.getAttributeValue(null, "class");
+
+        // Create the POJO implementation model
+        POJOImplementation implementation = new POJOImplementation();
+        implementation.setPOJOName(className);
+        
+        // Mark the POJO model unresolved to track the fact that it's not
+        // completely initialized, its class is not loaded yet and services
+        // and references not initialized either
+        implementation.setUnresolved(true);
+        
+        // Skip to end element
+        while (reader.hasNext()) {
+            if (reader.next() == END_ELEMENT && IMPLEMENTATION_POJO.equals(reader.getName())) {
+                break;
+            }
+        }
+        
+        return implementation;
+    }
+
+    public void resolve(POJOImplementation implementation, ModelResolver resolver) throws ContributionResolveException {
+        
+        // Resolve the POJO implementation
+        
+        // First resolve its class
+        ClassReference classReference = new ClassReference(implementation.getPOJOName());
+        classReference = resolver.resolveModel(ClassReference.class, classReference);
+        Class pojoClass = classReference.getJavaClass();
+        if (pojoClass == null) {
+            throw new ContributionResolveException("Class could not be resolved: " + implementation.getPOJOName());
+        }
+        implementation.setPOJOClass(pojoClass);
+        
+        // Check to see if we have a .componentType file describing the POJO class
+        ComponentType componentType = assemblyFactory.createComponentType();
+        componentType.setUnresolved(true);
+        componentType.setURI(implementation.getURI() + ".componentType");
+        componentType = resolver.resolveModel(ComponentType.class, componentType);
+        if (!componentType.isUnresolved()) {
+            
+            // We have a component type description, merge it into the POJO model
+            implementation.getServices().addAll(componentType.getServices());
+            implementation.getReferences().addAll(componentType.getReferences());
+            implementation.getProperties().addAll(componentType.getProperties());
+            
+        } else {
+            
+            // We have no component type description, simply introspect the POJO and
+            // create a single Service for it
+            Service service = assemblyFactory.createService();
+            service.setName(pojoClass.getSimpleName());
+            JavaInterface javaInterface;
+            try {
+                javaInterface = javaIntrospector.introspect(pojoClass);
+            } catch (InvalidInterfaceException e) {
+                throw new ContributionResolveException(e);
+            }
+            JavaInterfaceContract interfaceContract = javaFactory.createJavaInterfaceContract();
+            interfaceContract.setInterface(javaInterface);
+            service.setInterfaceContract(interfaceContract);
+            implementation.getServices().add(service);
+        }
+        
+        // Mark the implementation resolved now
+        implementation.setUnresolved(false);
+    }
+
+    public void write(POJOImplementation model, XMLStreamWriter outputSource) throws ContributionWriteException {
+    }
+}

Propchange: incubator/tuscany/java/sca/samples/implementation-pojo2-extension/src/main/java/pojo2/extension/POJOImplementationProcessor.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: incubator/tuscany/java/sca/samples/implementation-pojo2-extension/src/main/java/pojo2/extension/POJOImplementationProcessor.java
------------------------------------------------------------------------------
    svn:keywords = Rev Date

Modified: incubator/tuscany/java/sca/samples/implementation-pojo2-extension/src/main/resources/META-INF/services/org.apache.tuscany.sca.contribution.processor.StAXArtifactProcessor
URL: http://svn.apache.org/viewvc/incubator/tuscany/java/sca/samples/implementation-pojo2-extension/src/main/resources/META-INF/services/org.apache.tuscany.sca.contribution.processor.StAXArtifactProcessor?view=diff&rev=551301&r1=551300&r2=551301
==============================================================================
--- incubator/tuscany/java/sca/samples/implementation-pojo2-extension/src/main/resources/META-INF/services/org.apache.tuscany.sca.contribution.processor.StAXArtifactProcessor (original)
+++ incubator/tuscany/java/sca/samples/implementation-pojo2-extension/src/main/resources/META-INF/services/org.apache.tuscany.sca.contribution.processor.StAXArtifactProcessor Wed Jun 27 12:53:11 2007
@@ -16,4 +16,4 @@
 # under the License. 
 
 # Implementation class for the artifact processor extension
-org.apache.tuscany.sca.assembly.xml.DefaultBeanModelProcessor;type=implementation.pojo,model=pojo2.extension.POJOImplementation
+pojo2.extension.POJOImplementationProcessor;type=http://pojo#implementation.pojo,model=pojo2.extension.POJOImplementation

Added: incubator/tuscany/java/sca/samples/implementation-pojo2-extension/src/test/java/helloworld/HelloWorldImpl2.java
URL: http://svn.apache.org/viewvc/incubator/tuscany/java/sca/samples/implementation-pojo2-extension/src/test/java/helloworld/HelloWorldImpl2.java?view=auto&rev=551301
==============================================================================
--- incubator/tuscany/java/sca/samples/implementation-pojo2-extension/src/test/java/helloworld/HelloWorldImpl2.java (added)
+++ incubator/tuscany/java/sca/samples/implementation-pojo2-extension/src/test/java/helloworld/HelloWorldImpl2.java Wed Jun 27 12:53:11 2007
@@ -0,0 +1,37 @@
+/*
+ * 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 helloworld;
+
+public class HelloWorldImpl2 {
+    
+    public void init() {
+        System.out.println("Initializing POJO");
+    }
+
+    public void destroy() {
+        System.out.println("Destroying POJO");
+    }
+
+    public String sayHello(String name) {
+        System.out.println("Executing POJO sayHello");
+        return "Hello " + name;
+    }
+
+}

Propchange: incubator/tuscany/java/sca/samples/implementation-pojo2-extension/src/test/java/helloworld/HelloWorldImpl2.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: incubator/tuscany/java/sca/samples/implementation-pojo2-extension/src/test/java/helloworld/HelloWorldImpl2.java
------------------------------------------------------------------------------
    svn:keywords = Rev Date

Modified: incubator/tuscany/java/sca/samples/implementation-pojo2-extension/src/test/java/helloworld/HelloWorldTestCase.java
URL: http://svn.apache.org/viewvc/incubator/tuscany/java/sca/samples/implementation-pojo2-extension/src/test/java/helloworld/HelloWorldTestCase.java?view=diff&rev=551301&r1=551300&r2=551301
==============================================================================
--- incubator/tuscany/java/sca/samples/implementation-pojo2-extension/src/test/java/helloworld/HelloWorldTestCase.java (original)
+++ incubator/tuscany/java/sca/samples/implementation-pojo2-extension/src/test/java/helloworld/HelloWorldTestCase.java Wed Jun 27 12:53:11 2007
@@ -25,11 +25,26 @@
 /**
  */
 public class HelloWorldTestCase extends TestCase {
+    
+    private SCADomain scaDomain;
+    
+    @Override
+    protected void setUp() throws Exception {
+        scaDomain = SCADomain.newInstance("helloworld/helloworld.composite");
+    }
+    
+    @Override
+    protected void tearDown() throws Exception {
+        scaDomain.close();
+    }
 
     public void testHello() throws Exception {
-        SCADomain scaDomain = SCADomain.newInstance("helloworld/helloworld.composite");
         HelloWorld helloworld = scaDomain.getService(HelloWorld.class, "HelloWorldComponent");
         assertEquals("Hello petra", helloworld.sayHello("petra"));
-        scaDomain.close();
+    }
+
+    public void testHello2() throws Exception {
+        HelloWorld helloworld = scaDomain.getService(HelloWorld.class, "HelloWorldComponent2/HelloWorld2");
+        assertEquals("Hello petra", helloworld.sayHello("petra"));
     }
 }

Added: incubator/tuscany/java/sca/samples/implementation-pojo2-extension/src/test/resources/helloworld/HelloWorldImpl2.componentType
URL: http://svn.apache.org/viewvc/incubator/tuscany/java/sca/samples/implementation-pojo2-extension/src/test/resources/helloworld/HelloWorldImpl2.componentType?view=auto&rev=551301
==============================================================================
--- incubator/tuscany/java/sca/samples/implementation-pojo2-extension/src/test/resources/helloworld/HelloWorldImpl2.componentType (added)
+++ incubator/tuscany/java/sca/samples/implementation-pojo2-extension/src/test/resources/helloworld/HelloWorldImpl2.componentType Wed Jun 27 12:53:11 2007
@@ -0,0 +1,26 @@
+<?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.    
+-->
+<componentType xmlns="http://www.osoa.org/xmlns/sca/1.0">
+
+    <service name="HelloWorld2">
+        <interface.java interface="helloworld.HelloWorld"/>
+    </service>
+
+</componentType>
\ No newline at end of file

Propchange: incubator/tuscany/java/sca/samples/implementation-pojo2-extension/src/test/resources/helloworld/HelloWorldImpl2.componentType
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: incubator/tuscany/java/sca/samples/implementation-pojo2-extension/src/test/resources/helloworld/HelloWorldImpl2.componentType
------------------------------------------------------------------------------
    svn:keywords = Rev Date

Modified: incubator/tuscany/java/sca/samples/implementation-pojo2-extension/src/test/resources/helloworld/helloworld.composite
URL: http://svn.apache.org/viewvc/incubator/tuscany/java/sca/samples/implementation-pojo2-extension/src/test/resources/helloworld/helloworld.composite?view=diff&rev=551301&r1=551300&r2=551301
==============================================================================
--- incubator/tuscany/java/sca/samples/implementation-pojo2-extension/src/test/resources/helloworld/helloworld.composite (original)
+++ incubator/tuscany/java/sca/samples/implementation-pojo2-extension/src/test/resources/helloworld/helloworld.composite Wed Jun 27 12:53:11 2007
@@ -19,10 +19,15 @@
 -->
 <composite xmlns="http://www.osoa.org/xmlns/sca/1.0"
 	targetNamespace="http://test/helloworld"
+	xmlns:p="http://pojo"
 	name="helloworld">
 
     <component name="HelloWorldComponent">
-        <implementation.pojo class="helloworld.HelloWorldImpl" />
+        <p:implementation.pojo class="helloworld.HelloWorldImpl" />
+    </component>
+
+    <component name="HelloWorldComponent2">
+        <p:implementation.pojo class="helloworld.HelloWorldImpl2" />
     </component>
 
 </composite>



---------------------------------------------------------------------
To unsubscribe, e-mail: tuscany-commits-unsubscribe@ws.apache.org
For additional commands, e-mail: tuscany-commits-help@ws.apache.org