Author: jmarino
Date: Fri Jul 21 21:52:44 2006
New Revision: 424523

URL: http://svn.apache.org/viewvc?rev=424523&view=rev
Log:
support for @Monitor in CDI

Modified:
    
incubator/tuscany/java/sca/core/src/main/java/org/apache/tuscany/core/implementation/processor/AbstractPropertyProcessor.java
    
incubator/tuscany/java/sca/core/src/main/java/org/apache/tuscany/core/implementation/processor/ConstructorProcessor.java
    
incubator/tuscany/java/sca/core/src/main/java/org/apache/tuscany/core/implementation/processor/HeuristicPojoProcessor.java
    
incubator/tuscany/java/sca/core/src/test/java/org/apache/tuscany/core/implementation/processor/ConstructorProcessorExtensibilityTestCase.java
    
incubator/tuscany/java/sca/core/src/test/java/org/apache/tuscany/core/implementation/processor/ConstructorReferenceTestCase.java
    
incubator/tuscany/java/sca/core/src/test/java/org/apache/tuscany/core/implementation/processor/HeuristicConstructorTestCase.java
    
incubator/tuscany/java/sca/core/src/test/java/org/apache/tuscany/core/implementation/processor/MonitorProcessorTestCase.java
    
incubator/tuscany/java/sca/spi/src/main/java/org/apache/tuscany/spi/annotation/Monitor.java

Modified: 
incubator/tuscany/java/sca/core/src/main/java/org/apache/tuscany/core/implementation/processor/AbstractPropertyProcessor.java
URL: 
http://svn.apache.org/viewvc/incubator/tuscany/java/sca/core/src/main/java/org/apache/tuscany/core/implementation/processor/AbstractPropertyProcessor.java?rev=424523&r1=424522&r2=424523&view=diff
==============================================================================
--- 
incubator/tuscany/java/sca/core/src/main/java/org/apache/tuscany/core/implementation/processor/AbstractPropertyProcessor.java
 (original)
+++ 
incubator/tuscany/java/sca/core/src/main/java/org/apache/tuscany/core/implementation/processor/AbstractPropertyProcessor.java
 Fri Jul 21 21:52:44 2006
@@ -17,11 +17,16 @@
 package org.apache.tuscany.core.implementation.processor;
 
 import java.lang.annotation.Annotation;
+import java.lang.reflect.Constructor;
 import java.lang.reflect.Field;
 import java.lang.reflect.Member;
 import java.lang.reflect.Method;
 import java.util.Map;
 
+import org.apache.tuscany.spi.component.CompositeComponent;
+import org.apache.tuscany.spi.deployer.DeploymentContext;
+
+import org.apache.tuscany.core.implementation.ConstructorDefinition;
 import org.apache.tuscany.core.implementation.ImplementationProcessorSupport;
 import org.apache.tuscany.core.implementation.JavaMappedProperty;
 import org.apache.tuscany.core.implementation.JavaMappedReference;
@@ -29,11 +34,9 @@
 import org.apache.tuscany.core.implementation.PojoComponentType;
 import org.apache.tuscany.core.implementation.ProcessingException;
 import org.apache.tuscany.core.util.JavaIntrospectionHelper;
-import org.apache.tuscany.spi.component.CompositeComponent;
-import org.apache.tuscany.spi.deployer.DeploymentContext;
 
 /**
- * Base class for ImplementationProcessors that handle annotations that add 
Property's.
+ * Base class for ImplementationProcessors that handle annotations that add 
Properties.
  *
  * @version $Rev$ $Date$
  */
@@ -108,6 +111,37 @@
         JavaMappedProperty<?> property = createProperty(name, javaType, field);
         initProperty(property, annotation, parent, context);
         properties.put(name, property);
+    }
+
+    public void visitConstructor(CompositeComponent<?> parent, Constructor<?> 
constructor,
+                                 PojoComponentType<JavaMappedService, 
JavaMappedReference, JavaMappedProperty<?>> type,
+                                 DeploymentContext context) throws 
ProcessingException {
+
+        ConstructorDefinition definition = type.getConstructorDefinition();
+        Class[] params = constructor.getParameterTypes();
+        Map<String, JavaMappedProperty<?>> properties = type.getProperties();
+        Annotation[][] annotations = constructor.getParameterAnnotations();
+        for (int i = 0; i < params.length; i++) {
+            Class param = params[i];
+            Annotation[] paramAnnotations = annotations[i];
+            for (Annotation annotation : paramAnnotations) {
+                if (annotation.annotationType().equals(annotationClass)) {
+                    if (definition == null) {
+                        definition = new ConstructorDefinition(constructor);
+                        type.setConstructorDefinition(definition);
+                    }
+                    A monitorAnnot = annotationClass.cast(annotation);
+                    String name = getName(monitorAnnot);
+                    if (name == null || name.length() == 0) {
+                        name = param.getName();
+                    }
+                    JavaMappedProperty<?> property = createProperty(name, 
param, constructor);
+                    initProperty(property, monitorAnnot, parent, context);
+                    properties.put(name, property);
+                    ProcessorUtils.addName(definition.getInjectionNames(), i, 
name);
+                }
+            }
+        }
     }
 
     protected abstract String getName(A annotation);

Modified: 
incubator/tuscany/java/sca/core/src/main/java/org/apache/tuscany/core/implementation/processor/ConstructorProcessor.java
URL: 
http://svn.apache.org/viewvc/incubator/tuscany/java/sca/core/src/main/java/org/apache/tuscany/core/implementation/processor/ConstructorProcessor.java?rev=424523&r1=424522&r2=424523&view=diff
==============================================================================
--- 
incubator/tuscany/java/sca/core/src/main/java/org/apache/tuscany/core/implementation/processor/ConstructorProcessor.java
 (original)
+++ 
incubator/tuscany/java/sca/core/src/main/java/org/apache/tuscany/core/implementation/processor/ConstructorProcessor.java
 Fri Jul 21 21:52:44 2006
@@ -47,7 +47,7 @@
             if 
(constructor.getAnnotation(org.osoa.sca.annotations.Constructor.class) != null) 
{
                 if (found) {
                     DuplicateConstructorException e =
-                        new DuplicateConstructorException("More than one 
constructor marked with @Constructor");
+                        new DuplicateConstructorException("Multiple 
constructors marked with @Constructor");
                     e.setIdentifier(constructor.getDeclaringClass().getName());
                     throw e;
                 }
@@ -67,7 +67,7 @@
         ConstructorDefinition<?> definition = type.getConstructorDefinition();
         if (definition != null && 
!definition.getConstructor().equals(constructor)) {
             DuplicateConstructorException e =
-                new DuplicateConstructorException("More than one constructor 
marked with @Constructor");
+                new DuplicateConstructorException("Multiple constructor 
definitions found");
             e.setIdentifier(constructor.getDeclaringClass().getName());
             throw e;
         } else if (definition == null) {
@@ -77,7 +77,6 @@
         String[] names = annotation.value();
         Annotation[][] annotations = constructor.getParameterAnnotations();
         List<String> injectionNames = definition.getInjectionNames();
-        boolean validateNames = true;
         for (int i = 0; i < params.length; i++) {
             Class<?> param = params[i];
             Annotation[] paramAnnotations = annotations[i];
@@ -85,9 +84,6 @@
                 if (!processParam(param, paramAnnotations, names, i, type, 
injectionNames)) {
                     String name = (i < names.length) ? names[i] : "";
                     addName(injectionNames, i, name);
-                    if (validateNames) {
-                        validateNames = false;
-                    }
                 }
             } catch (ProcessingException
                 e) {
@@ -95,9 +91,7 @@
                 throw e;
             }
         }
-        if (validateNames && names.length != 0 && names[0].length() != 0 && 
names.length != params.length) {
-            throw new InvalidConstructorException("Names in @Constructor do 
not match number of parameters");
-        } else if (names.length != 0 && names[0].length() != 0 && names.length 
!= params.length) {
+        if (names.length != 0 && names[0].length() != 0 && names.length != 
params.length) {
             throw new InvalidConstructorException("Names in @Constructor do 
not match number of parameters");
         }
         type.setConstructorDefinition(definition);

Modified: 
incubator/tuscany/java/sca/core/src/main/java/org/apache/tuscany/core/implementation/processor/HeuristicPojoProcessor.java
URL: 
http://svn.apache.org/viewvc/incubator/tuscany/java/sca/core/src/main/java/org/apache/tuscany/core/implementation/processor/HeuristicPojoProcessor.java?rev=424523&r1=424522&r2=424523&view=diff
==============================================================================
--- 
incubator/tuscany/java/sca/core/src/main/java/org/apache/tuscany/core/implementation/processor/HeuristicPojoProcessor.java
 (original)
+++ 
incubator/tuscany/java/sca/core/src/main/java/org/apache/tuscany/core/implementation/processor/HeuristicPojoProcessor.java
 Fri Jul 21 21:52:44 2006
@@ -88,7 +88,7 @@
                     throw new ServiceTypeNotFoundException(clazz.getName());
                 }
             }
-            calcConstructor(type, clazz);
+            evaluateConstructor(type, clazz);
             return;
         }
 
@@ -101,7 +101,7 @@
             if (!isInServiceInterface(method, services)) {
                 String name = toPropertyName(method.getName());
                 // avoid duplicate property or ref names
-                if (type.getProperties().get(name) == null && 
type.getReferences().get(name) == null) {
+                if (!type.getProperties().containsKey(name) && 
!type.getReferences().containsKey(name)) {
                     Class<?> param = method.getParameterTypes()[0];
                     Type genericType = method.getGenericParameterTypes()[0];
                     if (isReferenceType(genericType)) {
@@ -120,7 +120,7 @@
             Class<?> param = method.getParameterTypes()[0];
             String name = toPropertyName(method.getName());
             // avoid duplicate property or ref names
-            if (type.getProperties().get(name) == null && 
type.getReferences().get(name) == null) {
+            if (!type.getProperties().containsKey(name) && 
!type.getReferences().containsKey(name)) {
                 if (isReferenceType(param)) {
                     type.add(createReference(name, method, param));
                 } else {
@@ -137,7 +137,7 @@
                 type.add(createProperty(field.getName(), field, paramType));
             }
         }
-        calcConstructor(type, clazz);
+        evaluateConstructor(type, clazz);
     }
 
     /**
@@ -150,18 +150,19 @@
      *                                       references and properties
      */
     @SuppressWarnings("unchecked")
-    private void calcConstructor(PojoComponentType<JavaMappedService, 
JavaMappedReference, JavaMappedProperty<?>> type,
-                                 Class<?> clazz) throws ProcessingException {
+    private void evaluateConstructor(
+        PojoComponentType<JavaMappedService, JavaMappedReference, 
JavaMappedProperty<?>> type,
+        Class<?> clazz) throws ProcessingException {
         // determine constructor if one is not annotated
         ConstructorDefinition<?> definition = type.getConstructorDefinition();
         Constructor constructor;
-        boolean explictConstructor = false;
+        boolean explict = false;
         if (definition != null
             && 
definition.getConstructor().getAnnotation(org.osoa.sca.annotations.Constructor.class)
 != null) {
             // the constructor was already defined explicitly
             return;
         } else if (definition != null) {
-            explictConstructor = true;
+            explict = true;
             constructor = definition.getConstructor();
         } else {
             // no definition, heuristically determine constructor
@@ -206,12 +207,12 @@
         Map<String, JavaMappedProperty<?>> props = type.getProperties();
         Map<String, JavaMappedReference> refs = type.getReferences();
         Annotation[][] annotations = constructor.getParameterAnnotations();
-        if (!explictConstructor) {
+        if (!explict) {
             // the constructor wasn't defined by an annotation, so check to 
see if any of the params have an annotation
             // which we can impute as explicitly defining the constructor, 
e.g. @Property, @Reference, or @Autowire
-            explictConstructor = annotationsDefined(annotations);
+            explict = annotationsDefined(annotations);
         }
-        if (explictConstructor) {
+        if (explict) {
             for (int i = 0; i < params.length; i++) {
                 Class param = params[i];
                 processParam(param, annotations[i], new String[0], i, type, 
paramNames);
@@ -250,7 +251,6 @@
             }
 
         }
-        //definition.setInjectionNames(paramNames);
     }
 
 
@@ -426,7 +426,7 @@
         // calculate methods that are not properties or references
         for (Method method : methods) {
             String name = toPropertyName(method.getName());
-            if (references.get(name) == null && properties.get(name) == null) {
+            if (!references.containsKey(name) && 
!properties.containsKey(name)) {
                 nonPropRefMethods.add(method);
             }
         }

Modified: 
incubator/tuscany/java/sca/core/src/test/java/org/apache/tuscany/core/implementation/processor/ConstructorProcessorExtensibilityTestCase.java
URL: 
http://svn.apache.org/viewvc/incubator/tuscany/java/sca/core/src/test/java/org/apache/tuscany/core/implementation/processor/ConstructorProcessorExtensibilityTestCase.java?rev=424523&r1=424522&r2=424523&view=diff
==============================================================================
--- 
incubator/tuscany/java/sca/core/src/test/java/org/apache/tuscany/core/implementation/processor/ConstructorProcessorExtensibilityTestCase.java
 (original)
+++ 
incubator/tuscany/java/sca/core/src/test/java/org/apache/tuscany/core/implementation/processor/ConstructorProcessorExtensibilityTestCase.java
 Fri Jul 21 21:52:44 2006
@@ -12,7 +12,7 @@
 import org.apache.tuscany.core.implementation.ConstructorDefinition;
 
 /**
- * Verifies the constructor processor works when parameters are marked with 
extension annotations
+ * Verifies the constructor processor works when parameters are marked with 
custom extension annotations
  *
  * @version $Rev$ $Date$
  */

Modified: 
incubator/tuscany/java/sca/core/src/test/java/org/apache/tuscany/core/implementation/processor/ConstructorReferenceTestCase.java
URL: 
http://svn.apache.org/viewvc/incubator/tuscany/java/sca/core/src/test/java/org/apache/tuscany/core/implementation/processor/ConstructorReferenceTestCase.java?rev=424523&r1=424522&r2=424523&view=diff
==============================================================================
--- 
incubator/tuscany/java/sca/core/src/test/java/org/apache/tuscany/core/implementation/processor/ConstructorReferenceTestCase.java
 (original)
+++ 
incubator/tuscany/java/sca/core/src/test/java/org/apache/tuscany/core/implementation/processor/ConstructorReferenceTestCase.java
 Fri Jul 21 21:52:44 2006
@@ -122,12 +122,12 @@
 
     private static class BadFoo {
 
-        @org.osoa.sca.annotations.Constructor()
+        @org.osoa.sca.annotations.Constructor
         public BadFoo(@Reference(name = "myRef") String prop1, @Reference(name 
= "myRef") String prop2) {
 
         }
 
-        @org.osoa.sca.annotations.Constructor()
+        @org.osoa.sca.annotations.Constructor
         public BadFoo(@Reference String prop) {
 
         }

Modified: 
incubator/tuscany/java/sca/core/src/test/java/org/apache/tuscany/core/implementation/processor/HeuristicConstructorTestCase.java
URL: 
http://svn.apache.org/viewvc/incubator/tuscany/java/sca/core/src/test/java/org/apache/tuscany/core/implementation/processor/HeuristicConstructorTestCase.java?rev=424523&r1=424522&r2=424523&view=diff
==============================================================================
--- 
incubator/tuscany/java/sca/core/src/test/java/org/apache/tuscany/core/implementation/processor/HeuristicConstructorTestCase.java
 (original)
+++ 
incubator/tuscany/java/sca/core/src/test/java/org/apache/tuscany/core/implementation/processor/HeuristicConstructorTestCase.java
 Fri Jul 21 21:52:44 2006
@@ -285,7 +285,7 @@
         }
     }
 
-    public final static class Foo14 {
+    public static final class Foo14 {
         private Foo14() {
         }
     }

Modified: 
incubator/tuscany/java/sca/core/src/test/java/org/apache/tuscany/core/implementation/processor/MonitorProcessorTestCase.java
URL: 
http://svn.apache.org/viewvc/incubator/tuscany/java/sca/core/src/test/java/org/apache/tuscany/core/implementation/processor/MonitorProcessorTestCase.java?rev=424523&r1=424522&r2=424523&view=diff
==============================================================================
--- 
incubator/tuscany/java/sca/core/src/test/java/org/apache/tuscany/core/implementation/processor/MonitorProcessorTestCase.java
 (original)
+++ 
incubator/tuscany/java/sca/core/src/test/java/org/apache/tuscany/core/implementation/processor/MonitorProcessorTestCase.java
 Fri Jul 21 21:52:44 2006
@@ -13,20 +13,22 @@
  */
 package org.apache.tuscany.core.implementation.processor;
 
+import java.lang.reflect.Constructor;
 import java.lang.reflect.Field;
 import java.lang.reflect.Method;
 import java.util.Map;
 
-import org.jmock.Mock;
-import org.jmock.MockObjectTestCase;
+import org.apache.tuscany.spi.annotation.Monitor;
+import org.apache.tuscany.spi.monitor.MonitorFactory;
 
+import org.apache.tuscany.core.implementation.ConstructorDefinition;
 import org.apache.tuscany.core.implementation.JavaMappedProperty;
 import org.apache.tuscany.core.implementation.JavaMappedReference;
 import org.apache.tuscany.core.implementation.JavaMappedService;
 import org.apache.tuscany.core.implementation.PojoComponentType;
 import org.apache.tuscany.core.injection.SingletonObjectFactory;
-import org.apache.tuscany.spi.annotation.Monitor;
-import org.apache.tuscany.spi.monitor.MonitorFactory;
+import org.jmock.Mock;
+import org.jmock.MockObjectTestCase;
 
 /**
  * @version $Rev$ $Date$
@@ -69,6 +71,57 @@
         assertTrue(properties.get("bar").getDefaultValueFactory() instanceof 
SingletonObjectFactory);
     }
 
+    public void testConstructor() throws Exception {
+        PojoComponentType<JavaMappedService, JavaMappedReference, 
JavaMappedProperty<?>> type =
+            new PojoComponentType<JavaMappedService, JavaMappedReference, 
JavaMappedProperty<?>>();
+        Constructor<Bar> ctor = Bar.class.getConstructor(BazMonitor.class);
+        
monitorFactory.expects(once()).method("getMonitor").with(eq(BazMonitor.class)).will(returnValue(null));
+        processor.visitConstructor(null, ctor, type, null);
+        Map<String, JavaMappedProperty<?>> properties = type.getProperties();
+        assertTrue(
+            
properties.get(BazMonitor.class.getName()).getDefaultValueFactory() instanceof 
SingletonObjectFactory);
+    }
+
+    /**
+     * Verifies calling the monitor processor to evaluate a constructor can be 
done after a property parameter is
+     * processed
+     */
+    public void testConstructorAfterProperty() throws Exception {
+        PojoComponentType<JavaMappedService, JavaMappedReference, 
JavaMappedProperty<?>> type =
+            new PojoComponentType<JavaMappedService, JavaMappedReference, 
JavaMappedProperty<?>>();
+        Constructor<Bar> ctor = Bar.class.getConstructor(String.class, 
BazMonitor.class);
+        
monitorFactory.expects(once()).method("getMonitor").with(eq(BazMonitor.class)).will(returnValue(null));
+        ConstructorDefinition<Bar> definition = new 
ConstructorDefinition<Bar>(ctor);
+        JavaMappedProperty prop = new JavaMappedProperty();
+        definition.getInjectionNames().add("prop");
+        type.setConstructorDefinition(definition);
+        type.getProperties().put("prop", prop);
+        processor.visitConstructor(null, ctor, type, null);
+        Map<String, JavaMappedProperty<?>> properties = type.getProperties();
+        assertEquals(BazMonitor.class.getName(), 
definition.getInjectionNames().get(1));
+        assertEquals(2, type.getProperties().size());
+        assertTrue(
+            
properties.get(BazMonitor.class.getName()).getDefaultValueFactory() instanceof 
SingletonObjectFactory);
+    }
+
+    /**
+     * Verifies calling the monitor processor to evaluate a constructor can be 
done before a property parameter is
+     * processed
+     */
+    public void testConstructorBeforeProperty() throws Exception {
+        PojoComponentType<JavaMappedService, JavaMappedReference, 
JavaMappedProperty<?>> type =
+            new PojoComponentType<JavaMappedService, JavaMappedReference, 
JavaMappedProperty<?>>();
+        Constructor<Bar> ctor = Bar.class.getConstructor(String.class, 
BazMonitor.class);
+        
monitorFactory.expects(once()).method("getMonitor").with(eq(BazMonitor.class)).will(returnValue(null));
+        processor.visitConstructor(null, ctor, type, null);
+        Map<String, JavaMappedProperty<?>> properties = type.getProperties();
+        ConstructorDefinition definition = type.getConstructorDefinition();
+        assertEquals(2, definition.getInjectionNames().size());
+        assertEquals(BazMonitor.class.getName(), 
definition.getInjectionNames().get(1));
+        assertTrue(
+            
properties.get(BazMonitor.class.getName()).getDefaultValueFactory() instanceof 
SingletonObjectFactory);
+    }
+
     protected void setUp() throws Exception {
         super.setUp();
         monitorFactory = mock(MonitorFactory.class);
@@ -90,6 +143,19 @@
 
         @Monitor
         public void setMonitor() {
+        }
+    }
+
+    private interface BazMonitor {
+
+    }
+
+    private static class Bar {
+
+        public Bar(@Monitor BazMonitor monitor) {
+        }
+
+        public Bar(String prop, @Monitor BazMonitor monitor) {
         }
     }
 }

Modified: 
incubator/tuscany/java/sca/spi/src/main/java/org/apache/tuscany/spi/annotation/Monitor.java
URL: 
http://svn.apache.org/viewvc/incubator/tuscany/java/sca/spi/src/main/java/org/apache/tuscany/spi/annotation/Monitor.java?rev=424523&r1=424522&r2=424523&view=diff
==============================================================================
--- 
incubator/tuscany/java/sca/spi/src/main/java/org/apache/tuscany/spi/annotation/Monitor.java
 (original)
+++ 
incubator/tuscany/java/sca/spi/src/main/java/org/apache/tuscany/spi/annotation/Monitor.java
 Fri Jul 21 21:52:44 2006
@@ -26,7 +26,7 @@
  *
  * @version $Rev$ $Date$
  */
[EMAIL PROTECTED]({ElementType.METHOD, ElementType.FIELD})
[EMAIL PROTECTED]({ElementType.METHOD, ElementType.FIELD, 
ElementType.PARAMETER})
 @Retention(RetentionPolicy.RUNTIME)
 public @interface Monitor {
 }



---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to