[seam-commits] Seam SVN: r11698 - in modules/trunk/remoting: examples/helloworld/src/main/java/org/jboss/seam/remoting/examples/helloworld and 3 other directories.

seam-commits at lists.jboss.org seam-commits at lists.jboss.org
Sun Nov 29 13:01:35 EST 2009


Author: shane.bryzak at jboss.com
Date: 2009-11-29 13:01:34 -0500 (Sun, 29 Nov 2009)
New Revision: 11698

Added:
   modules/trunk/remoting/docs/
   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/HelloQualifier.java
   modules/trunk/remoting/examples/helloworld/src/main/webapp/helloworld.xhtml
   modules/trunk/remoting/examples/helloworld/src/main/webapp/index.html
   modules/trunk/remoting/readme.txt
Modified:
   modules/trunk/remoting/src/main/java/org/jboss/seam/remoting/Call.java
   modules/trunk/remoting/src/main/java/org/jboss/seam/remoting/ExecutionHandler.java
   modules/trunk/remoting/src/main/java/org/jboss/seam/remoting/InterfaceGenerator.java
   modules/trunk/remoting/src/main/resources/org/jboss/seam/remoting/remote.js
Log:
support looking up the bean by bean type and qualifiers


Added: 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	                        (rev 0)
+++ modules/trunk/remoting/examples/helloworld/src/main/java/org/jboss/seam/remoting/examples/helloworld/HelloAction.java	2009-11-29 18:01:34 UTC (rev 11698)
@@ -0,0 +1,12 @@
+package org.jboss.seam.remoting.examples.helloworld;
+
+import org.jboss.seam.remoting.annotations.WebRemote;
+
+ at HelloQualifier
+public class HelloAction {
+  @WebRemote
+  public String sayHello(String name) {
+    return "Hello, " + name;
+  }
+}
+

Added: 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	                        (rev 0)
+++ modules/trunk/remoting/examples/helloworld/src/main/java/org/jboss/seam/remoting/examples/helloworld/HelloQualifier.java	2009-11-29 18:01:34 UTC (rev 11698)
@@ -0,0 +1,16 @@
+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.inject.Qualifier;
+
+ at Retention(RUNTIME)
+ at Target(TYPE)
+ at Qualifier
+public @interface HelloQualifier {
+
+}

Added: modules/trunk/remoting/examples/helloworld/src/main/webapp/helloworld.xhtml
===================================================================
--- modules/trunk/remoting/examples/helloworld/src/main/webapp/helloworld.xhtml	                        (rev 0)
+++ modules/trunk/remoting/examples/helloworld/src/main/webapp/helloworld.xhtml	2009-11-29 18:01:34 UTC (rev 11698)
@@ -0,0 +1,36 @@
+<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
+<html xmlns="http://www.w3.org/1999/xhtml"
+	  xmlns:ui="http://java.sun.com/jsf/facelets"
+	  xmlns:h="http://java.sun.com/jsf/html"
+	  xmlns:s="http://jboss.com/products/seam/taglib">
+
+<head>
+	<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
+	<title>Seam Remoting - Hello World Example</title>
+</head>
+
+<body>
+
+  <h1>Seam Remoting - Hello World Example</h1>
+  
+  <p>
+  
+  </p>
+
+  <script type="text/javascript" src="seam/resource/remoting/resource/remote.js"></script>
+  <script type="text/javascript" src="seam/resource/remoting/interface.js?org.jboss.seam.remoting.examples.helloworld.HelloAction"></script>
+  
+  <script type="text/javascript">    
+    function sayHello() {
+      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", "org.jboss.seam.remoting.examples.helloworld.HelloQualifier").sayHello(name, callback);
+    }
+  </script>  
+
+  <button onclick="javascript:sayHello()">Say Hello</button>
+
+</body>
+</html>
+

Added: modules/trunk/remoting/examples/helloworld/src/main/webapp/index.html
===================================================================
--- modules/trunk/remoting/examples/helloworld/src/main/webapp/index.html	                        (rev 0)
+++ modules/trunk/remoting/examples/helloworld/src/main/webapp/index.html	2009-11-29 18:01:34 UTC (rev 11698)
@@ -0,0 +1,6 @@
+<html>
+  <head>
+    <meta http-equiv="Refresh" content="0; URL=helloworld.jsf"/>
+  </head>
+</html>
+

Added: modules/trunk/remoting/readme.txt
===================================================================
--- modules/trunk/remoting/readme.txt	                        (rev 0)
+++ modules/trunk/remoting/readme.txt	2009-11-29 18:01:34 UTC (rev 11698)
@@ -0,0 +1,4 @@
+Seam Remoting
+=============
+
+Seam Remoting is an AJAX remoting API for CDI-based applications.

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-11-28 15:42:15 UTC (rev 11697)
+++ modules/trunk/remoting/src/main/java/org/jboss/seam/remoting/Call.java	2009-11-29 18:01:34 UTC (rev 11698)
@@ -1,5 +1,6 @@
 package org.jboss.seam.remoting;
 
+import java.lang.annotation.Annotation;
 import java.lang.reflect.InvocationTargetException;
 import java.lang.reflect.Method;
 import java.lang.reflect.Type;
@@ -29,7 +30,8 @@
    private BeanManager beanManager;
 
    private String id;
-   private String componentName;
+   private String beanName;
+   private Annotation[] qualifiers = null;
    private String methodName;
    // private String expression;
    private Throwable exception;
@@ -41,18 +43,52 @@
    private CallContext context;
 
    private List<String> constraints = null;
+   
+   private class AnnotationLiteral implements Annotation
+   {
+      private Class<? extends Annotation> annotationType;
+      
+      public AnnotationLiteral(Class<? extends Annotation> annotationType)
+      {
+         this.annotationType = annotationType;
+      }
+      
+      public Class<? extends Annotation> annotationType()
+      {
+         return annotationType;
+      }      
+   }
 
    /**
     * Constructor.
     */
-   public Call(BeanManager beanManager, String id, String componentName,
-         String methodName)
+   public Call(BeanManager beanManager, String id, String beanName, 
+         String qualifiers, String methodName)
    {
       this.beanManager = beanManager;
       this.id = id;
-      this.componentName = componentName;
+      this.beanName = beanName;
       this.methodName = methodName;
       this.context = new CallContext(beanManager);
+      
+      if (qualifiers != null && !qualifiers.isEmpty())
+      {
+         String[] qs = qualifiers.split(",");
+         this.qualifiers = new Annotation[qs.length];
+         
+         for (int i = 0; i < qs.length; i++)
+         {
+            try
+            {
+               this.qualifiers[i] = new AnnotationLiteral(
+                     (Class<? extends Annotation>) Class.forName(qs[i]));
+            }
+            catch (ClassNotFoundException e)
+            {
+               e.printStackTrace();
+            }
+         }
+      }
    }
 
    /**
@@ -142,7 +178,7 @@
     */
    public void execute() throws Exception
    {
-      if (componentName != null)
+      if (beanName != null)
       {
          processInvocation();
       }
@@ -152,11 +188,19 @@
    private void processInvocation() throws Exception
    {
       // Find the component we're calling
-      Set<Bean<?>> beans = beanManager.getBeans(componentName);
+      Set<Bean<?>> beans = beanManager.getBeans(beanName);
       
       if (beans.isEmpty())
       {
-         throw new RuntimeException("No such component: " + componentName);         
+         // Can't find the bean by name, try looking it up by type and qualifiers
+         Type beanType = Class.forName(beanName);         
+         beans = beanManager.getBeans(beanType, qualifiers);
+         
+      }      
+      
+      if (beans.isEmpty())
+      {
+         throw new RuntimeException("No such component: " + beanName);         
       }
       else
       {      
@@ -170,7 +214,7 @@
          if (instance == null)
          {
             throw new RuntimeException(String.format(
-                  "Could not create instance of bean %s", componentName));
+                  "Could not create instance of bean %s", beanName));
          }
    
          Class<?> type = null;

Modified: modules/trunk/remoting/src/main/java/org/jboss/seam/remoting/ExecutionHandler.java
===================================================================
--- modules/trunk/remoting/src/main/java/org/jboss/seam/remoting/ExecutionHandler.java	2009-11-28 15:42:15 UTC (rev 11697)
+++ modules/trunk/remoting/src/main/java/org/jboss/seam/remoting/ExecutionHandler.java	2009-11-29 18:01:34 UTC (rev 11698)
@@ -144,9 +144,11 @@
          List<Element> callElements = env.element("body").elements("call");
 
          for (Element e : callElements)
-         {
-            Call call = new Call(beanManager, e.attributeValue("id"), e
-                  .attributeValue("component"), e.attributeValue("method"));
+         {           
+            Call call = new Call(beanManager, e.attributeValue("id"), 
+                  e.attributeValue("component"), 
+                  e.attributeValue("qualifiers"), 
+                  e.attributeValue("method"));
 
             // First reconstruct all the references
             Element refsNode = e.element("refs");

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-11-28 15:42:15 UTC (rev 11697)
+++ modules/trunk/remoting/src/main/java/org/jboss/seam/remoting/InterfaceGenerator.java	2009-11-29 18:01:34 UTC (rev 11698)
@@ -19,8 +19,10 @@
 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;
@@ -51,6 +53,8 @@
     * A cache of component interfaces, keyed by component name.
     */
    private Map<Class<?>, byte[]> interfaceCache = new HashMap<Class<?>, byte[]>();
+   
+   private final class AnyQualifier extends AnnotationLiteral<Any> implements Any {}
 
    /**
     * 
@@ -79,6 +83,20 @@
          String componentName = ((String) e.nextElement()).trim();
 
          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())
          {
@@ -327,17 +345,17 @@
          return;
       }
 
-      if (bean.getName().contains("."))
+      String beanName = bean.getName() != null ? bean.getName() : bean.getBeanClass().getName();
+      if (beanName.contains("."))
       {
          componentSrc.append("Seam.Remoting.createNamespace('");
-         componentSrc.append(bean.getName().substring(0,
-               bean.getName().lastIndexOf('.')));
+         componentSrc.append(beanName.substring(0, beanName.lastIndexOf('.')));
          componentSrc.append("');\n");
 
       }
 
       componentSrc.append("Seam.Remoting.type.");
-      componentSrc.append(bean.getName());
+      componentSrc.append(beanName);
       componentSrc.append(" = function() {\n");
       componentSrc.append("  this.__callback = new Object();\n");
 
@@ -360,7 +378,7 @@
                appendTypeSource(out, m.getGenericReturnType(), types);
 
                componentSrc.append("  Seam.Remoting.type.");
-               componentSrc.append(bean.getName());
+               componentSrc.append(beanName);
                componentSrc.append(".prototype.");
                componentSrc.append(m.getName());
                componentSrc.append(" = function(");
@@ -402,14 +420,14 @@
 
          // Set the component name
          componentSrc.append("Seam.Remoting.type.");
-         componentSrc.append(bean.getName());
+         componentSrc.append(beanName);
          componentSrc.append(".__name = \"");
-         componentSrc.append(bean.getName());
+         componentSrc.append(beanName);
          componentSrc.append("\";\n\n");
 
          // Register the component
          componentSrc.append("Seam.Component.register(Seam.Remoting.type.");
-         componentSrc.append(bean.getName());
+         componentSrc.append(beanName);
          componentSrc.append(");\n\n");
 
          out.write(componentSrc.toString().getBytes());
@@ -485,6 +503,7 @@
       // Determine whether this class is a component; if so, use its name
       // otherwise use its class name.
       Bean<?> bean = beanManager.getBeans(classType).iterator().next();
+      
       String componentName = bean.getName();
       if (componentName == null)
          componentName = classType.getName();

Modified: modules/trunk/remoting/src/main/resources/org/jboss/seam/remoting/remote.js
===================================================================
--- modules/trunk/remoting/src/main/resources/org/jboss/seam/remoting/remote.js	2009-11-28 15:42:15 UTC (rev 11697)
+++ modules/trunk/remoting/src/main/resources/org/jboss/seam/remoting/remote.js	2009-11-29 18:01:34 UTC (rev 11698)
@@ -8,24 +8,23 @@
 Seam.Component.components = new Array();
 Seam.Component.instances = new Array();
 
-Seam.Component.newInstance = function(name)
+Seam.Component.create = function(name)
 {
   for (var i = 0; i < Seam.Component.components.length; i++)
   {
     if (Seam.Component.components[i].__name == name)
-      return new Seam.Component.components[i];
-  }
-}
-
-Seam.Component.getInstance = function(name)
-{
-  for (var i = 0; i < Seam.Component.components.length; i++)
-  {
-    if (Seam.Component.components[i].__name == name)
     {
-      if (Seam.Component.components[i].__instance == null)
-        Seam.Component.components[i].__instance = new Seam.Component.components[i]();
-      return Seam.Component.components[i].__instance;
+      var value = new Seam.Component.components[i];
+      if (arguments.length > 1)
+      {
+        value.__qualifiers = new Array();
+        for (var j = 1; j < arguments.length; j++)
+        { 
+          value.__qualifiers.push(arguments[j]);
+        }
+      }
+      
+      return value;
     }
   }
   return null;
@@ -485,6 +484,18 @@
 
   var data = "<call component=\"";
   data += Seam.Component.getComponentType(component).__name;
+  
+  if (component.__qualifiers != null)
+  {
+    data += "\" qualifiers=\"";
+    
+    for (var i = 0; i < component.__qualifiers.length; i++)
+    {
+      data += component.__qualifiers[i];
+      if (i < component.__qualifiers.length - 1) data += ",";
+    }    
+  }
+  
   data += "\" method=\"";
   data += methodName;
   data += "\" id=\"";



More information about the seam-commits mailing list