Author: shane.bryzak(a)jboss.com
Date: 2009-12-30 15:57:38 -0500 (Wed, 30 Dec 2009)
New Revision: 11894
Modified:
modules/remoting/trunk/src/main/java/org/jboss/seam/remoting/model/ModelHandler.java
modules/remoting/trunk/src/main/java/org/jboss/seam/remoting/wrapper/BeanWrapper.java
modules/remoting/trunk/src/main/resources/org/jboss/seam/remoting/remote.js
Log:
implemented dynamic type loading for model fetch requests
Modified:
modules/remoting/trunk/src/main/java/org/jboss/seam/remoting/model/ModelHandler.java
===================================================================
---
modules/remoting/trunk/src/main/java/org/jboss/seam/remoting/model/ModelHandler.java 2009-12-30
11:06:46 UTC (rev 11893)
+++
modules/remoting/trunk/src/main/java/org/jboss/seam/remoting/model/ModelHandler.java 2009-12-30
20:57:38 UTC (rev 11894)
@@ -88,44 +88,36 @@
conversationContext.setActive(true);
}
- Set<Model> models = new HashSet<Model>();
+ Element modelElement =
env.element("body").element("model");
+ String operation = modelElement.attributeValue("operation");
- for (Element modelElement : (List<Element>)
env.element("body").elements("model"))
- {
- String operation = modelElement.attributeValue("operation");
-
- if ("fetch".equals(operation))
- {
- processFetchRequest(modelElement, models);
- }
- else if ("apply".equals(operation))
- {
- processApplyRequest(modelElement, models);
- }
+ Model model = null;
+
+ if ("fetch".equals(operation))
+ {
+ model = processFetchRequest(modelElement);
}
-
- for (Model model : models)
+ else if ("apply".equals(operation))
{
- if (model.getAction() != null && model.getAction().getException() !=
null)
- {
- response.getOutputStream().write(ENVELOPE_TAG_OPEN);
- response.getOutputStream().write(BODY_TAG_OPEN);
- MarshalUtils.marshalException(model.getAction().getException(),
- model.getAction().getContext(), response.getOutputStream());
- response.getOutputStream().write(BODY_TAG_CLOSE);
- response.getOutputStream().write(ENVELOPE_TAG_CLOSE);
- response.getOutputStream().flush();
- return;
- }
+ model = processApplyRequest(modelElement);
}
-
- for (Model model : models)
+
+ if (model.getAction() != null && model.getAction().getException() !=
null)
{
- model.evaluate();
+ response.getOutputStream().write(ENVELOPE_TAG_OPEN);
+ response.getOutputStream().write(BODY_TAG_OPEN);
+ MarshalUtils.marshalException(model.getAction().getException(),
+ model.getAction().getContext(), response.getOutputStream());
+ response.getOutputStream().write(BODY_TAG_CLOSE);
+ response.getOutputStream().write(ENVELOPE_TAG_CLOSE);
+ response.getOutputStream().flush();
+ return;
}
+ model.evaluate();
+
ctx.setConversationId(conversation.isTransient() ? null :
conversation.getId());
- marshalResponse(models, ctx, response.getOutputStream());
+ marshalResponse(model, ctx, response.getOutputStream());
}
finally
{
@@ -139,11 +131,10 @@
}
@SuppressWarnings({ "unchecked" })
- private void processFetchRequest(Element modelElement, Set<Model> models)
+ private Model processFetchRequest(Element modelElement)
throws Exception
{
Model model = registry.createModel();
- models.add(model);
if (modelElement.elements("action").size() > 0)
{
@@ -171,7 +162,9 @@
if (model.getAction() != null)
{
model.getAction().execute();
- }
+ }
+
+ return model;
}
@SuppressWarnings("unchecked")
@@ -211,11 +204,10 @@
}
@SuppressWarnings("unchecked")
- private void processApplyRequest(Element modelElement, Set<Model> models)
+ private Model processApplyRequest(Element modelElement)
throws Exception
{
Model model = registry.getModel(modelElement.attributeValue("uid"));
- models.add(model);
model.setAction(null);
CallContext ctx = new CallContext(beanManager);
@@ -256,10 +248,12 @@
if (model.getAction() != null)
{
model.getAction().execute();
- }
+ }
+
+ return model;
}
- private void marshalResponse(Set<Model> models, RequestContext ctx,
+ private void marshalResponse(Model model, RequestContext ctx,
OutputStream out) throws IOException
{
out.write(ENVELOPE_TAG_OPEN);
@@ -271,16 +265,16 @@
out.write(CONVERSATION_ID_TAG_OPEN);
out.write(ctx.getConversationId().getBytes());
out.write(CONVERSATION_ID_TAG_CLOSE);
+ out.write(CALL_ID_TAG_OPEN);
+ out.write(ctx.getCallId().toString().getBytes());
+ out.write(CALL_ID_TAG_CLOSE);
out.write(CONTEXT_TAG_CLOSE);
out.write(HEADER_CLOSE);
}
out.write(BODY_TAG_OPEN);
- for (Model model : models)
- {
- MarshalUtils.marshalModel(model, out);
- }
+ MarshalUtils.marshalModel(model, out);
out.write(BODY_TAG_CLOSE);
out.write(ENVELOPE_TAG_CLOSE);
Modified:
modules/remoting/trunk/src/main/java/org/jboss/seam/remoting/wrapper/BeanWrapper.java
===================================================================
---
modules/remoting/trunk/src/main/java/org/jboss/seam/remoting/wrapper/BeanWrapper.java 2009-12-30
11:06:46 UTC (rev 11893)
+++
modules/remoting/trunk/src/main/java/org/jboss/seam/remoting/wrapper/BeanWrapper.java 2009-12-30
20:57:38 UTC (rev 11894)
@@ -7,6 +7,7 @@
import java.lang.reflect.Method;
import java.lang.reflect.Type;
import java.util.List;
+import java.util.Set;
import javax.enterprise.inject.spi.Bean;
import javax.enterprise.inject.spi.BeanManager;
@@ -244,19 +245,20 @@
cls = cls.getSuperclass();
}
- // TODO fix this, bean might not have a name
- Bean<?> bean = beanManager.getBeans(cls).iterator().next();
- String componentName = bean.getName();
-
- if (componentName != null)
+ String componentName = cls.getName();
+
+ Set<Bean<?>> beans = beanManager.getBeans(cls);
+ if (beans.size() > 0)
{
- out.write(componentName.getBytes());
- }
- else
- {
- out.write(cls.getName().getBytes());
- }
+ Bean<?> bean = beanManager.getBeans(cls).iterator().next();
+ if (bean.getName() != null)
+ {
+ componentName = bean.getName();
+ }
+ }
+ out.write(componentName.getBytes());
+
out.write(BEAN_START_TAG_CLOSE);
for (String propertyName :
getMetadataCache().getAccessibleProperties(cls).keySet())
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-30
11:06:46 UTC (rev 11893)
+++ modules/remoting/trunk/src/main/resources/org/jboss/seam/remoting/remote.js 2009-12-30
20:57:38 UTC (rev 11894)
@@ -66,7 +66,7 @@
Seam.beans[name] = t;
}
-Seam.loadBeans = function() {
+Seam.importBeans = function() {
var names = [];
var cb = null;
for (var i=0; i<arguments.length; i++) {
@@ -82,17 +82,22 @@
cb = arguments[i];
}
}
- var n = "org.jboss.seam.remoting.BeanMetadata";
- if (!Seam.isBeanRegistered(n)) Seam.registerBean(n, {name: "str", methods:
"Seam.Map", properties: "Seam.Map"});
- var c = Seam.createLoadBeansCall([names], cb);
+
+ var c = Seam.createImportBeansCall(names, cb);
var envelope = Seam.createEnvelope(Seam.createHeader(c.id), c.data);
Seam.pendingCalls.put(c.id, c);
Seam.sendAjaxRequest(envelope, Seam.PATH_EXECUTE, Seam.processResponse, false);
}
-Seam.createLoadBeansCall = function(names, callback, originalCall) {
- var c = Seam.createCall("org.jboss.seam.remoting.MetadataCache",
"loadBeans", names, callback);
- c.handler = Seam.processLoadBeansResponse;
+Seam.createImportBeansCall = function(names, callback, originalCall) {
+ var n = "org.jboss.seam.remoting.BeanMetadata";
+ if (!Seam.isBeanRegistered(n)) {
+ Seam.registerBean(n, {name: "str", methods: "Seam.Map",
properties: "Seam.Map"});
+ Seam.registerBean("org.jboss.seam.remoting.MetadataCache", null,
{loadBeans:1});
+ }
+
+ var c = Seam.createCall(new
Seam.beans["org.jboss.seam.remoting.MetadataCache"](), "loadBeans",
[names], callback);
+ c.handler = Seam.processImportBeansResponse;
if (originalCall) c.originalCall = originalCall;
return c;
}
@@ -315,7 +320,7 @@
Seam.createCall = function(component, methodName, params, callback, exceptionHandler) {
var callId = "" + Seam.__callId++;
- if (!callback) callback = component.__callback[methodName];
+ if (!callback && component.__callback) callback =
component.__callback[methodName];
var d = "<call><target>" + Seam.getBeanType(component).__name +
"</target>";
if (component.__qualifiers != null) {
d += "<qualifiers>";
@@ -434,7 +439,7 @@
r.send(env);
}
-Seam.processLoadBeansResponse = function(call) {
+Seam.processImportBeansResponse = function(call) {
var cn = Seam.Xml.childNode;
Seam.pendingCalls.remove(call.callId);
var n = cn(cn(call.response.documentElement, "body"), "result");
@@ -479,10 +484,10 @@
var n = cn(bodyNode, "result");
if (n) {
var refsNode = cn(n, "refs");
- var u = Seam.validateRefs(refsNode, call);
+ var u = Seam.validateRefs(refsNode);
if (u.length > 0) {
call.handler = Seam.processCallResponse;
- var c = Seam.createLoadBeansCall(u, null, call);
+ var c = Seam.createImportBeansCall(u, null, call);
var envelope = Seam.createEnvelope(Seam.createHeader(c.id), c.data);
Seam.pendingCalls.put(c.id, c);
Seam.sendAjaxRequest(envelope, Seam.PATH_EXECUTE, Seam.processResponse, false);
@@ -511,19 +516,37 @@
}
}
-Seam.preProcessModelResponse = function(call) {
- var bodyNode = cn(doc.documentElement, "body");
- if (bodyNode) {
- var n = Seam.Xml.childNodes(bodyNode, "model");
- for (var i=0; i<n.length; i++) {
- var callId = n[i].getAttribute("callId");
- var call = Seam.pendingCalls.get(callId);
- Seam.pendingCalls.remove(callId);
- if (call.model) call.model.processResponse(n[i]);
+Seam.preProcessModelFetchResponse = function(call) {
+ var cn = Seam.Xml.childNode;
+ var b = cn(call.response.documentElement, "body");
+ if (b) {
+ var m = cn(b, "model");
+ if (m) {
+ var refsNode = cn(m, "refs");
+ var u = Seam.validateRefs(refsNode);
+ if (u.length > 0) {
+ call.handler = Seam.processModelFetchResponse;
+ var c = Seam.createImportBeansCall(u, null, call);
+ var envelope = Seam.createEnvelope(Seam.createHeader(c.id), c.data);
+ Seam.pendingCalls.put(c.id, c);
+ Seam.sendAjaxRequest(envelope, Seam.PATH_EXECUTE, Seam.processResponse, false);
+ } else {
+ Seam.processModelFetchResponse(call);
+ }
}
}
}
+Seam.processModelFetchResponse = function(call) {
+ Seam.pendingCalls.remove(call.callId);
+ var cn = Seam.Xml.childNode;
+ var b = cn(call.response.documentElement, "body");
+ if (b) {
+ var n = cn(b, "model");
+ if (call.model) call.model.processResponse(n);
+ }
+}
+
Seam.displayError = function(code) {
alert("There was an error processing your request. Error code: " + code);
}
@@ -945,7 +968,7 @@
}
}
d += "</model>";
- return {data:d, id:callId, model:this, handler: null};
+ return {data:d, id:callId, model:this, handler: Seam.preProcessModelFetchResponse};
}
Seam.Model.prototype.processResponse = function(modelNode) {
@@ -974,7 +997,7 @@
Seam.Model.prototype.createApplyRequest = function(a, delta) {
var callId = "" + Seam.__callId++;
- var d = "<model uid=\"" + this.id + "\"
operation=\"apply\" callId=\"" + callId + "\">";
+ var d = "<model uid=\"" + this.id + "\"
operation=\"apply\">";
var refs = delta.buildRefs();
if (a) {
d += "<action>";