You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@myfaces.apache.org by lu...@apache.org on 2014/12/17 22:46:22 UTC

svn commit: r1646340 - in /myfaces/orchestra/trunk/core20: pom.xml src/main/java/org/apache/myfaces/orchestra/annotation/ src/main/java/org/apache/myfaces/orchestra/annotation/AnnotationInfoManager.java

Author: lu4242
Date: Wed Dec 17 21:46:22 2014
New Revision: 1646340

URL: http://svn.apache.org/r1646340
Log:
ORCHESTRA-67 Allow Orchestra annotations to be used in meta annotations (Thanks to Martin Maier-Moessner for provide this hint)

Added:
    myfaces/orchestra/trunk/core20/src/main/java/org/apache/myfaces/orchestra/annotation/
    myfaces/orchestra/trunk/core20/src/main/java/org/apache/myfaces/orchestra/annotation/AnnotationInfoManager.java
Modified:
    myfaces/orchestra/trunk/core20/pom.xml

Modified: myfaces/orchestra/trunk/core20/pom.xml
URL: http://svn.apache.org/viewvc/myfaces/orchestra/trunk/core20/pom.xml?rev=1646340&r1=1646339&r2=1646340&view=diff
==============================================================================
--- myfaces/orchestra/trunk/core20/pom.xml (original)
+++ myfaces/orchestra/trunk/core20/pom.xml Wed Dec 17 21:46:22 2014
@@ -81,7 +81,7 @@
     <dependency>
       <groupId>org.springframework</groupId>
       <artifactId>spring</artifactId>
-      <version>2.0.6</version>
+      <version>2.5</version>
       <scope>compile</scope>
       <optional>true</optional>
     </dependency>
@@ -104,8 +104,8 @@
 
     <dependency>
       <groupId>org.springframework</groupId>
-      <artifactId>spring-mock</artifactId>
-      <version>2.0.6</version>
+      <artifactId>spring-test</artifactId>
+      <version>2.5</version>
       <scope>test</scope>
     </dependency>
 

Added: myfaces/orchestra/trunk/core20/src/main/java/org/apache/myfaces/orchestra/annotation/AnnotationInfoManager.java
URL: http://svn.apache.org/viewvc/myfaces/orchestra/trunk/core20/src/main/java/org/apache/myfaces/orchestra/annotation/AnnotationInfoManager.java?rev=1646340&view=auto
==============================================================================
--- myfaces/orchestra/trunk/core20/src/main/java/org/apache/myfaces/orchestra/annotation/AnnotationInfoManager.java (added)
+++ myfaces/orchestra/trunk/core20/src/main/java/org/apache/myfaces/orchestra/annotation/AnnotationInfoManager.java Wed Dec 17 21:46:22 2014
@@ -0,0 +1,149 @@
+/*
+ * 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.myfaces.orchestra.annotation;
+
+import java.lang.reflect.Method;
+import java.util.HashMap;
+import java.util.Map;
+
+import org.apache.commons.logging.Log;
+import org.apache.commons.logging.LogFactory;
+import org.apache.myfaces.orchestra.conversation.annotations.ConversationName;
+import org.apache.myfaces.orchestra.conversation.annotations.ConversationRequire;
+import org.apache.myfaces.orchestra.viewController.annotations.InitView;
+import org.apache.myfaces.orchestra.viewController.annotations.PreProcess;
+import org.apache.myfaces.orchestra.viewController.annotations.PreRenderView;
+import org.apache.myfaces.orchestra.viewController.annotations.ViewController;
+import org.springframework.core.annotation.AnnotationUtils;
+
+/**
+ * Inspects a class for Orchestra annotations, and if found then caches this information
+ * for later access.
+ * <p>
+ * The processing of Class objects is expected to happen only at application startup.
+ * <p>
+ * Note that annotation scanning is driven by the dependency-injection framework, i.e.
+ * only classes declared to the framework are scanned. 
+ */
+public class AnnotationInfoManager
+{
+    private final Log log = LogFactory.getLog(AnnotationInfoManager.class);
+
+    private Map<String, AnnotationInfo> annotationsInfoByName = new HashMap<String, AnnotationInfo>();
+    private Map<String, AnnotationInfo> annotationsInfoByViewId = new HashMap<String, AnnotationInfo>();
+
+    protected void addAnnotationsInfo(AnnotationInfo annotationInfo)
+    {
+        if (annotationsInfoByName.containsKey(annotationInfo.getBeanName()))
+        {
+            if (log.isInfoEnabled())
+            {
+                log.info("duplicate bean definition: " + annotationInfo.getBeanName());
+            }
+        }
+
+        annotationsInfoByName.put(annotationInfo.getBeanName(), annotationInfo);
+
+        ViewController viewController = annotationInfo.getViewController();
+        if (viewController != null)
+        {
+            String[] viewIds = viewController.viewIds();
+            for (int i = 0; i<viewIds.length; i++)
+            {
+                String viewId = viewIds[i];
+
+                if (log.isInfoEnabled())
+                {
+                    if (annotationsInfoByViewId.containsKey(annotationInfo.getBeanName()))
+                    {
+                        log.info("duplicate viewId definition: " + annotationInfo.getBeanName());
+                    }
+                }
+
+                annotationsInfoByViewId.put(viewId, annotationInfo);
+            }
+        }
+    }
+
+    public AnnotationInfo getAnnotationInfoByBeanName(String beanName)
+    {
+        return annotationsInfoByName.get(beanName);
+    }
+
+    public AnnotationInfo getAnnotationInfoByViewId(String viewId)
+    {
+        return annotationsInfoByViewId.get(viewId);
+    }
+
+    /**
+     * Inspect the provided class for annotations, and if found then cache the info
+     * keyed by the specified beanName.
+     * <p>
+     * Currently the class-level annotations looked for are:
+     * <ul>
+     * <li>ConversationName
+     * <li>ConversationRequire
+     * <li>ViewController
+     * </ul>
+     * <p>
+     * If the ViewController annotation is present, then the class is also scanned
+     * for related annotations on class methods.
+     */
+    public void processBeanAnnotations(String beanName, Class<?> clazz)
+    {
+        ConversationName conversationName = (ConversationName) AnnotationUtils.findAnnotation(
+                clazz, ConversationName.class);
+        ViewController viewController = (ViewController) AnnotationUtils.findAnnotation(clazz, ViewController.class);
+        ConversationRequire conversationRequire = (ConversationRequire) AnnotationUtils.findAnnotation(
+                clazz, ConversationRequire.class);
+
+        if (conversationName == null && viewController == null && conversationRequire == null)
+        {
+            return;
+        }
+
+        AnnotationInfo annotationInfo = new AnnotationInfo(beanName, clazz);
+        annotationInfo.setConversationName(conversationName);
+        annotationInfo.setConversationRequire(conversationRequire);
+        
+        if (viewController != null)
+        {
+            annotationInfo.setViewController(viewController);
+            Method[] methods = clazz.getMethods();
+            for (int i = 0; i<methods.length; i++)
+            {
+                Method method = methods[i];
+                if (method.isAnnotationPresent(InitView.class))
+                {
+                    annotationInfo.setInitViewMethod(method);
+                }
+                if (method.isAnnotationPresent(PreProcess.class))
+                {
+                    annotationInfo.setPreProcessMethod(method);
+                }
+                if (method.isAnnotationPresent(PreRenderView.class))
+                {
+                    annotationInfo.setPreRenderViewMethod(method);
+                }
+            }
+        }
+
+        addAnnotationsInfo(annotationInfo);
+    }
+}