[seam-commits] Seam SVN: r11890 - in modules/remoting/trunk/src/main: resources/org/jboss/seam/remoting and 1 other directory.
seam-commits at lists.jboss.org
seam-commits at lists.jboss.org
Tue Dec 29 03:40:04 EST 2009
Author: shane.bryzak at jboss.com
Date: 2009-12-29 03:40:04 -0500 (Tue, 29 Dec 2009)
New Revision: 11890
Modified:
modules/remoting/trunk/src/main/java/org/jboss/seam/remoting/InterfaceGenerator.java
modules/remoting/trunk/src/main/java/org/jboss/seam/remoting/MetadataCache.java
modules/remoting/trunk/src/main/resources/org/jboss/seam/remoting/remote.js
Log:
simplify InterfaceGenerator even further, added loadBeans() remotable method
Modified: modules/remoting/trunk/src/main/java/org/jboss/seam/remoting/InterfaceGenerator.java
===================================================================
--- modules/remoting/trunk/src/main/java/org/jboss/seam/remoting/InterfaceGenerator.java 2009-12-29 04:20:43 UTC (rev 11889)
+++ modules/remoting/trunk/src/main/java/org/jboss/seam/remoting/InterfaceGenerator.java 2009-12-29 08:40:04 UTC (rev 11890)
@@ -1,24 +1,16 @@
package org.jboss.seam.remoting;
-import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.util.Enumeration;
-import java.util.HashMap;
import java.util.HashSet;
-import java.util.Map;
import java.util.Set;
-import javax.enterprise.inject.spi.Bean;
-import javax.enterprise.inject.spi.BeanManager;
import javax.inject.Inject;
-import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.jboss.seam.remoting.BeanMetadata.BeanType;
-import org.slf4j.Logger;
-import org.slf4j.LoggerFactory;
/**
* Generates JavaScript interface code.
@@ -26,16 +18,8 @@
* @author Shane Bryzak
*/
public class InterfaceGenerator implements RequestHandler
-{
- private static final Logger log = LoggerFactory.getLogger(InterfaceGenerator.class);
-
- @Inject BeanManager beanManager;
+{
@Inject MetadataCache metadataCache;
-
- /**
- * A cache of component interfaces, keyed by name.
- */
- private Map<Class<?>, byte[]> interfaceCache = new HashMap<Class<?>, byte[]>();
/**
* Handles the request
@@ -48,119 +32,29 @@
response.sendError(HttpServletResponse.SC_BAD_REQUEST,
"Invalid request - no component specified");
}
-
- Set<Class<?>> typesCached = new HashSet<Class<?>>();
+
response.setContentType("text/javascript");
+ Set<String> names = new HashSet<String>();
Enumeration<?> e = request.getParameterNames();
while (e.hasMoreElements())
{
- String componentName = ((String) e.nextElement()).trim();
- Class<?> beanClass = null;
-
- Set<Bean<?>> beans = beanManager.getBeans(componentName);
-
- if (!beans.isEmpty())
- {
- beanClass = beans.iterator().next().getBeanClass();
- }
- else
- {
- try
- {
- beanClass = Class.forName(componentName);
- }
- catch (ClassNotFoundException ex)
- {
- log.error(String.format("Component not found: [%s]",
- componentName));
- response.sendError(HttpServletResponse.SC_NOT_FOUND,
- String.format("Component not found: [%s]", componentName));
- }
- }
-
- typesCached.add(beanClass);
+ names.add(((String) e.nextElement()).trim());
}
- generateBeanInterface(typesCached, response.getOutputStream(), null);
+ appendBeanSource(response.getOutputStream(), metadataCache.loadBeans(names));
}
/**
- * Generates the JavaScript code required to invoke the methods of a bean.
+ * Appends the interface code for a set of bean classes to an OutputStream.
*
- * @param classes Set<Class<?>> The bean classes for which to generate JavaScript stubs
- * @param out OutputStream The OutputStream to write the generated JavaScript
- * @param types Set<Type> Used to keep track of which bean classes have been
- * generated, can be null
- * @throws IOException Thrown if there is an error writing to the OutputStream
- */
- public void generateBeanInterface(Set<Class<?>> classes, OutputStream out,
- Set<BeanMetadata> types) throws IOException
- {
- if (types == null)
- {
- types = new HashSet<BeanMetadata>();
- }
-
- for (Class<?> beanClass : classes)
- {
- if (beanClass != null)
- {
- if (!interfaceCache.containsKey(beanClass))
- {
- synchronized (interfaceCache)
- {
- if (!interfaceCache.containsKey(beanClass))
- {
- ByteArrayOutputStream bOut = new ByteArrayOutputStream();
- addDependency(beanClass, types);
- appendBeanSource(bOut, beanClass, types);
- interfaceCache.put(beanClass, bOut.toByteArray());
- }
- }
- }
- out.write(interfaceCache.get(beanClass));
- }
- }
- }
-
- /**
- * Appends component interface code to an OutputStream for a specified
- * component.
- *
- * @param out OutputStream The OutputStream to write to
- * @param beanClass Class<?> The bean class to generate an interface for
- * @param types
- * Set A list of types that have already been generated for this
- * request. If this component has already been generated (i.e. it
- * is in the list) then it won't be generated again
- * @throws IOException
- * If there is an error writing to the OutputStream.
- */
- private void addDependency(Class<?> beanClass, Set<BeanMetadata> types)
- throws IOException
- {
- types.add(metadataCache.getMetadata(beanClass));
-
- for (Class<?> dependencyClass : metadataCache.getDependencies(beanClass))
- {
- if (!types.contains(dependencyClass))
- {
- addDependency(dependencyClass, types);
- }
- }
- }
-
- /**
- * Appends the interface code for a JavaBean (state holding) class to an OutputStream.
- *
* @param out OutputStream
* @param classType Class<?>
* @param types Set<Type>
* @throws IOException
*/
- private void appendBeanSource(OutputStream out, Class<?> classType,
- Set<BeanMetadata> types) throws IOException
+ private void appendBeanSource(OutputStream out, Set<BeanMetadata> types)
+ throws IOException
{
StringBuilder src = new StringBuilder();
Modified: modules/remoting/trunk/src/main/java/org/jboss/seam/remoting/MetadataCache.java
===================================================================
--- modules/remoting/trunk/src/main/java/org/jboss/seam/remoting/MetadataCache.java 2009-12-29 04:20:43 UTC (rev 11889)
+++ modules/remoting/trunk/src/main/java/org/jboss/seam/remoting/MetadataCache.java 2009-12-29 08:40:04 UTC (rev 11890)
@@ -1,5 +1,6 @@
package org.jboss.seam.remoting;
+import java.io.IOException;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.lang.reflect.Modifier;
@@ -16,11 +17,15 @@
import java.util.Set;
import javax.enterprise.context.ApplicationScoped;
+import javax.enterprise.inject.spi.Bean;
import javax.enterprise.inject.spi.BeanManager;
import javax.inject.Inject;
+import javax.servlet.http.HttpServletResponse;
import org.jboss.seam.remoting.BeanMetadata.BeanType;
import org.jboss.seam.remoting.annotations.WebRemote;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
/**
* Caches BeanMetadata instances
@@ -30,6 +35,8 @@
@ApplicationScoped
public class MetadataCache
{
+ private static final Logger log = LoggerFactory.getLogger(MetadataCache.class);
+
private Map<Class<?>,BeanMetadata> metadataCache;
private Map<Class<?>, Set<Class<?>>> beanDependencies;
@@ -77,6 +84,53 @@
}
}
+ @WebRemote
+ public Set<BeanMetadata> loadBeans(Set<String> names)
+ {
+ Set<BeanMetadata> meta = new HashSet<BeanMetadata>();
+
+ for (String name : names)
+ {
+ Class<?> beanClass = null;
+
+ Set<Bean<?>> beans = beanManager.getBeans(name);
+
+ if (!beans.isEmpty())
+ {
+ beanClass = beans.iterator().next().getBeanClass();
+ }
+ else
+ {
+ try
+ {
+ beanClass = Class.forName(name);
+ }
+ catch (ClassNotFoundException ex)
+ {
+ log.error(String.format("Component not found: [%s]", name));
+ throw new IllegalArgumentException(String.format("Component not found: [%s]", name));
+ }
+ }
+
+ addBeanDependencies(beanClass, meta);
+ }
+
+ return meta;
+ }
+
+ private void addBeanDependencies(Class<?> beanClass, Set<BeanMetadata> types)
+ {
+ types.add(getMetadata(beanClass));
+
+ for (Class<?> dependencyClass : getDependencies(beanClass))
+ {
+ if (!types.contains(dependencyClass))
+ {
+ addBeanDependencies(dependencyClass, types);
+ }
+ }
+ }
+
private BeanMetadata generateBeanMetadata(Class<?> beanClass)
{
BeanType beanType = BeanType.state;
Modified: modules/remoting/trunk/src/main/resources/org/jboss/seam/remoting/remote.js
===================================================================
--- modules/remoting/trunk/src/main/resources/org/jboss/seam/remoting/remote.js 2009-12-29 04:20:43 UTC (rev 11889)
+++ modules/remoting/trunk/src/main/resources/org/jboss/seam/remoting/remote.js 2009-12-29 08:40:04 UTC (rev 11890)
@@ -32,7 +32,12 @@
return t ? t.__name : undefined;
}
+Seam.isBeanRegistered = function(name) {
+ return Seam.beans[name] != null;
+}
+
Seam.registerBean = function(name, metadata, methods) {
+ if (Seam.isBeanRegistered(name)) return;
var t = function() {};
t.__name = name;
if (metadata) {
@@ -61,8 +66,11 @@
Seam.beans[name] = t;
}
-Seam.isBeanRegistered = function(name) {
- return Seam.beans[name] != null;
+Seam.loadBeans = function() {
+ var n = "org.jboss.seam.remoting.BeanMetadata";
+ if (!Seam.isBeanRegistered(n)) Seam.registerBean(n, {name: "str", methods: "Seam.Map", properties: "Seam.Map"});
+ var cb = function() {};
+ Seam.execute("org.jboss.seam.remoting.MetadataCache", "loadBeans", arguments, cb);
}
Seam.getBeanMetadata = function(obj) {
More information about the seam-commits
mailing list