[seam-commits] Seam SVN: r11712 - in modules/trunk/remoting: examples/helloworld/src/main/webapp and 1 other directories.

seam-commits at lists.jboss.org seam-commits at lists.jboss.org
Wed Dec 2 03:25:30 EST 2009


Author: shane.bryzak at jboss.com
Date: 2009-12-02 03:25:30 -0500 (Wed, 02 Dec 2009)
New Revision: 11712

Removed:
   modules/trunk/remoting/examples/helloworld/src/main/java/org/jboss/seam/remoting/examples/helloworld/HelloQualifier.java
Modified:
   modules/trunk/remoting/examples/helloworld/src/main/java/org/jboss/seam/remoting/examples/helloworld/HelloAction.java
   modules/trunk/remoting/examples/helloworld/src/main/webapp/helloworld.xhtml
   modules/trunk/remoting/src/main/java/org/jboss/seam/remoting/AnnotationsParser.java
   modules/trunk/remoting/src/main/java/org/jboss/seam/remoting/Call.java
   modules/trunk/remoting/src/main/java/org/jboss/seam/remoting/InterfaceGenerator.java
Log:
a little more flexibility in specifying qualifiers


Modified: modules/trunk/remoting/examples/helloworld/src/main/java/org/jboss/seam/remoting/examples/helloworld/HelloAction.java
===================================================================
--- modules/trunk/remoting/examples/helloworld/src/main/java/org/jboss/seam/remoting/examples/helloworld/HelloAction.java	2009-12-01 23:47:16 UTC (rev 11711)
+++ modules/trunk/remoting/examples/helloworld/src/main/java/org/jboss/seam/remoting/examples/helloworld/HelloAction.java	2009-12-02 08:25:30 UTC (rev 11712)
@@ -1,11 +1,15 @@
 package org.jboss.seam.remoting.examples.helloworld;
 
+import javax.enterprise.inject.Default;
+
 import org.jboss.seam.remoting.annotations.WebRemote;
 
- at HelloQualifier(foo = 123, bar = String.class)
-public class HelloAction {
+ at Default
+public class HelloAction 
+{
   @WebRemote
-  public String sayHello(String name) {
+  public String sayHello(String name) 
+  {
     return "Hello, " + name;
   }
 }

Deleted: modules/trunk/remoting/examples/helloworld/src/main/java/org/jboss/seam/remoting/examples/helloworld/HelloQualifier.java
===================================================================
--- modules/trunk/remoting/examples/helloworld/src/main/java/org/jboss/seam/remoting/examples/helloworld/HelloQualifier.java	2009-12-01 23:47:16 UTC (rev 11711)
+++ modules/trunk/remoting/examples/helloworld/src/main/java/org/jboss/seam/remoting/examples/helloworld/HelloQualifier.java	2009-12-02 08:25:30 UTC (rev 11712)
@@ -1,18 +0,0 @@
-package org.jboss.seam.remoting.examples.helloworld;
-
-import static java.lang.annotation.RetentionPolicy.RUNTIME;
-import static java.lang.annotation.ElementType.TYPE;
-
-import java.lang.annotation.Retention;
-import java.lang.annotation.Target;
-
-import javax.enterprise.util.Nonbinding;
-import javax.inject.Qualifier;
-
- at Retention(RUNTIME)
- at Target(TYPE)
- at Qualifier
-public @interface HelloQualifier {
-   int foo();
-   Class bar();
-}

Modified: modules/trunk/remoting/examples/helloworld/src/main/webapp/helloworld.xhtml
===================================================================
--- modules/trunk/remoting/examples/helloworld/src/main/webapp/helloworld.xhtml	2009-12-01 23:47:16 UTC (rev 11711)
+++ modules/trunk/remoting/examples/helloworld/src/main/webapp/helloworld.xhtml	2009-12-02 08:25:30 UTC (rev 11712)
@@ -25,8 +25,7 @@
       var name = prompt("What is your name?");
       if (name == null) return;
       var callback = function(result) { alert(result); };
-      Seam.Component.create("org.jboss.seam.remoting.examples.helloworld.HelloAction", 
-         "@HelloQualifier(foo = 123, bar = String.class)").sayHello(name, callback);
+      Seam.Component.create("org.jboss.seam.remoting.examples.helloworld.HelloAction", "@Casual").sayHello(name, callback);
     }
   </script>  
 

Modified: modules/trunk/remoting/src/main/java/org/jboss/seam/remoting/AnnotationsParser.java
===================================================================
--- modules/trunk/remoting/src/main/java/org/jboss/seam/remoting/AnnotationsParser.java	2009-12-01 23:47:16 UTC (rev 11711)
+++ modules/trunk/remoting/src/main/java/org/jboss/seam/remoting/AnnotationsParser.java	2009-12-02 08:25:30 UTC (rev 11712)
@@ -9,7 +9,14 @@
 import java.util.HashMap;
 import java.util.List;
 import java.util.Map;
+import java.util.Set;
 
+import javax.enterprise.inject.Any;
+import javax.enterprise.inject.Default;
+import javax.enterprise.inject.spi.Bean;
+import javax.enterprise.inject.spi.BeanManager;
+import javax.enterprise.util.AnnotationLiteral;
+
 import org.jboss.seam.remoting.annotationparser.AnnotationParser;
 import org.jboss.seam.remoting.annotationparser.ParseException;
 import org.jboss.seam.remoting.annotationparser.syntaxtree.AnnotationsUnit;
@@ -45,7 +52,7 @@
       
       public AnnotationMetadata(String name)
       {
-         this.annotationType = calcAnnotationType(name, beanType);
+         this.annotationType = determineAnnotationType(name, beanType);
       }      
       
       public void addMemberValue(String name, Object value)
@@ -63,15 +70,20 @@
          return annotationType;
       }
    }
+   
+   private class AnyQualifier extends AnnotationLiteral<Any> implements Any {};
 
    private Class<?> beanType;
+   private BeanManager beanManager;
    private List<AnnotationMetadata> meta = new ArrayList<AnnotationMetadata>();
    
    private Annotation[] annotations;
    
-   public AnnotationsParser(Class<?> beanType, String declaration)
+   public AnnotationsParser(Class<?> beanType, String declaration, BeanManager beanManager)
    {
       this.beanType = beanType;
+      this.beanManager = beanManager;
+      
       // TODO cache the results somewhere
       
       AnnotationParser parser = new AnnotationParser(new StringReader(declaration));
@@ -108,7 +120,7 @@
    }
    
    @SuppressWarnings("unchecked")
-   private Class<? extends Annotation> calcAnnotationType(String name, Class<?> beanType)
+   private Class<? extends Annotation> determineAnnotationType(String name, Class<?> beanType)
    {
       try
       {
@@ -125,6 +137,29 @@
             }
          }
          
+         // Couldn't find the annotation on the bean type itself - let's look at all beans
+         // with the same type
+         Set<Bean<?>> beans = beanManager.getBeans(beanType, new AnyQualifier());
+         for (Bean<?> bean : beans)
+         {
+            for (Annotation beanAnnotation : bean.getBeanClass().getAnnotations())
+            {
+               if (name.equals(beanAnnotation.annotationType().getSimpleName()))
+               {
+                  return beanAnnotation.annotationType();
+               }
+            }
+         }        
+         
+         if ("Default".equals(name))
+         {
+            return Default.class;
+         }
+         else if ("Any".equals(name))
+         {
+            return Any.class;
+         }
+         
          return null;         
       }
    }

Modified: modules/trunk/remoting/src/main/java/org/jboss/seam/remoting/Call.java
===================================================================
--- modules/trunk/remoting/src/main/java/org/jboss/seam/remoting/Call.java	2009-12-01 23:47:16 UTC (rev 11711)
+++ modules/trunk/remoting/src/main/java/org/jboss/seam/remoting/Call.java	2009-12-02 08:25:30 UTC (rev 11712)
@@ -27,6 +27,8 @@
  */
 public class Call
 {
+   public static Annotation[] EMPTY_ANNOTATIONS = new Annotation[]{};
+   
    private BeanManager beanManager;
 
    private String id;
@@ -61,7 +63,8 @@
             Class<?> beanType = Class.forName(beanName);
             
             Annotation[] q = qualifiers != null && !qualifiers.isEmpty() ? 
-                  new AnnotationsParser(beanType, qualifiers).getAnnotations() : null;
+                  new AnnotationsParser(beanType, qualifiers, beanManager).getAnnotations() : 
+                     EMPTY_ANNOTATIONS;
 
             beans = beanManager.getBeans(beanType, q);            
          }

Modified: modules/trunk/remoting/src/main/java/org/jboss/seam/remoting/InterfaceGenerator.java
===================================================================
--- modules/trunk/remoting/src/main/java/org/jboss/seam/remoting/InterfaceGenerator.java	2009-12-01 23:47:16 UTC (rev 11711)
+++ modules/trunk/remoting/src/main/java/org/jboss/seam/remoting/InterfaceGenerator.java	2009-12-02 08:25:30 UTC (rev 11712)
@@ -19,10 +19,8 @@
 import java.util.Map;
 import java.util.Set;
 
-import javax.enterprise.inject.Any;
 import javax.enterprise.inject.spi.Bean;
 import javax.enterprise.inject.spi.BeanManager;
-import javax.enterprise.util.AnnotationLiteral;
 import javax.inject.Inject;
 import javax.inject.Named;
 import javax.servlet.ServletException;
@@ -50,12 +48,10 @@
    private static Map<Class<?>, Set<String>> accessibleProperties = new HashMap<Class<?>, Set<String>>();
 
    /**
-    * A cache of component interfaces, keyed by component name.
+    * A cache of component interfaces, keyed by name.
     */
    private Map<Class<?>, byte[]> interfaceCache = new HashMap<Class<?>, byte[]>();
    
-   private final class AnyQualifier extends AnnotationLiteral<Any> implements Any {}
-
    /**
     * 
     * @param request
@@ -72,7 +68,7 @@
          throw new ServletException("Invalid request - no component specified");
       }
 
-      Set<Bean<?>> beansCached = new HashSet<Bean<?>>();
+      Set<Class<?>> typesCached = new HashSet<Class<?>>();
       Set<Type> types = new HashSet<Type>();
 
       response.setContentType("text/javascript");
@@ -81,50 +77,33 @@
       while (e.hasMoreElements())
       {
          String componentName = ((String) e.nextElement()).trim();
+         Class<?> beanClass = null;
 
          Set<Bean<?>> beans = beanManager.getBeans(componentName);
          
-         if (beans.isEmpty())
-         {
-            Type beanType = null;
-
-            try
-            {
-               beanType = Class.forName(componentName);
-            }
-            catch (ClassNotFoundException e1) {}
-
-            beans = beanManager.getBeans(beanType, new AnyQualifier());
-         }
-         
-         Bean<?> bean = null;
          if (!beans.isEmpty())
          {
-            bean = beans.iterator().next();
+            beanClass = beans.iterator().next().getBeanClass();
          }
-
-         if (bean == null)
+         else
          {
             try
             {
-               Class<?> c = Class.forName(componentName);
-               appendClassSource(response.getOutputStream(), c, types);
+               beanClass = Class.forName(componentName);
             }
-            catch (ClassNotFoundException ex)
+            catch (ClassNotFoundException ex) 
             {
                log.error(String.format("Component not found: [%s]",
                      componentName));
                throw new ServletException(
-                     "Invalid request - component not found.");
-            }
+                     "Invalid request - component not found.");               
+            }            
          }
-         else
-         {
-            beansCached.add(bean);
-         }
+         
+         typesCached.add(beanClass);
       }
 
-      generateBeanInterface(beansCached, response.getOutputStream(), types);
+      generateBeanInterface(typesCached, response.getOutputStream(), types);
    }
 
    /**
@@ -139,27 +118,26 @@
     * @throws IOException
     *            Thrown if there is an error writing to the OutputStream
     */
-   public void generateBeanInterface(Set<Bean<?>> beans, OutputStream out,
+   public void generateBeanInterface(Set<Class<?>> classes, OutputStream out,
          Set<Type> types) throws IOException
    {
-      for (Bean<?> bean : beans)
+      for (Class<?> cls : classes)
       {
-         if (bean != null)
+         if (cls != null)
          {
-            if (!interfaceCache.containsKey(bean.getBeanClass()))
+            if (!interfaceCache.containsKey(cls))
             {
                synchronized (interfaceCache)
                {
-                  if (!interfaceCache.containsKey(bean.getBeanClass()))
+                  if (!interfaceCache.containsKey(cls))
                   {
                      ByteArrayOutputStream bOut = new ByteArrayOutputStream();
-                     appendBeanSource(bOut, bean, types);
-                     interfaceCache
-                           .put(bean.getBeanClass(), bOut.toByteArray());
+                     appendBeanSource(bOut, cls, types);
+                     interfaceCache.put(cls, bOut.toByteArray());
                   }
                }
             }
-            out.write(interfaceCache.get(bean.getBeanClass()));
+            out.write(interfaceCache.get(cls));
          }
       }
    }
@@ -305,7 +283,7 @@
     * @throws IOException
     *            If there is an error writing to the OutputStream.
     */
-   private void appendBeanSource(OutputStream out, Bean<?> bean, Set<Type> types)
+   private void appendBeanSource(OutputStream out, Class<?> beanClass, Set<Type> types)
          throws IOException
    {
       StringBuilder componentSrc = new StringBuilder();
@@ -314,18 +292,18 @@
 
       // Check if any of the methods are annotated with @WebRemote, and if so
       // treat it as an "action" component instead of a type component
-      for (Method m : bean.getBeanClass().getDeclaredMethods())
+      for (Method m : beanClass.getDeclaredMethods())
       {
          if (m.getAnnotation(WebRemote.class) != null)
          {
-            componentTypes.add(bean.getBeanClass());
+            componentTypes.add(beanClass);
             break;
          }
       }
 
       if (componentTypes.isEmpty())
       {
-         appendTypeSource(out, bean.getBeanClass(), types);
+         appendTypeSource(out, beanClass, types);
          return;
       }
 
@@ -345,7 +323,8 @@
          return;
       }
 
-      String beanName = bean.getName() != null ? bean.getName() : bean.getBeanClass().getName();
+      String name = beanManager.getBeans(beanClass).iterator().next().getName();
+      String beanName = name != null ? name : beanClass.getName();
       if (beanName.contains("."))
       {
          componentSrc.append("Seam.Remoting.createNamespace('");



More information about the seam-commits mailing list