Author: shane.bryzak(a)jboss.com
Date: 2009-12-06 17:13:38 -0500 (Sun, 06 Dec 2009)
New Revision: 11762
Modified:
modules/remoting/trunk/src/main/java/org/jboss/seam/remoting/ExecutionHandler.java
modules/remoting/trunk/src/main/java/org/jboss/seam/remoting/model/Model.java
modules/remoting/trunk/src/main/java/org/jboss/seam/remoting/model/ModelHandler.java
Log:
unmarshal the bean requests
Modified:
modules/remoting/trunk/src/main/java/org/jboss/seam/remoting/ExecutionHandler.java
===================================================================
---
modules/remoting/trunk/src/main/java/org/jboss/seam/remoting/ExecutionHandler.java 2009-12-06
20:35:02 UTC (rev 11761)
+++
modules/remoting/trunk/src/main/java/org/jboss/seam/remoting/ExecutionHandler.java 2009-12-06
22:13:38 UTC (rev 11762)
@@ -120,7 +120,7 @@
// First reconstruct all the references
Element refsNode = e.element("refs");
-
+
Iterator iter = refsNode.elementIterator("ref");
while (iter.hasNext())
{
Modified: modules/remoting/trunk/src/main/java/org/jboss/seam/remoting/model/Model.java
===================================================================
---
modules/remoting/trunk/src/main/java/org/jboss/seam/remoting/model/Model.java 2009-12-06
20:35:02 UTC (rev 11761)
+++
modules/remoting/trunk/src/main/java/org/jboss/seam/remoting/model/Model.java 2009-12-06
22:13:38 UTC (rev 11762)
@@ -1,8 +1,8 @@
package org.jboss.seam.remoting.model;
import java.io.Serializable;
+import java.lang.annotation.Annotation;
import java.util.HashMap;
-import java.util.HashSet;
import java.util.Map;
import java.util.Set;
import java.util.UUID;
@@ -10,22 +10,38 @@
import javax.enterprise.inject.spi.Bean;
import javax.enterprise.inject.spi.BeanManager;
+import org.jboss.seam.remoting.AnnotationsParser;
+import org.jboss.seam.remoting.Call;
+import org.jboss.seam.remoting.CallContext;
+
public class Model implements Serializable
{
private static final long serialVersionUID = 8318288750036758325L;
private BeanManager beanManager;
private String id;
- private Map<Integer,Object> refs;
+ private CallContext callContext;
- private Set<Bean<?>> beans;
+ private class BeanProperty
+ {
+ public Bean<?> bean;
+ public String propertyName;
+
+ public BeanProperty(Bean<?> bean, String propertyName)
+ {
+ this.bean = bean;
+ this.propertyName = propertyName;
+ }
+ }
+ private Map<String, BeanProperty> beanProperties;
+
public Model(BeanManager beanManager)
{
this.beanManager = beanManager;
id = UUID.randomUUID().toString();
- refs = new HashMap<Integer,Object>();
- beans = new HashSet<Bean<?>>();
+ callContext = new CallContext(beanManager);
+ beanProperties = new HashMap<String, BeanProperty>();
}
public String getId()
@@ -33,8 +49,44 @@
return id;
}
- public void addBean()
+ public CallContext getCallContext()
+ {
+ return callContext;
+ }
+ public void addBean(String alias, String beanName, String qualifiers, String
propertyName)
+ {
+ Set<Bean<?>> beans = beanManager.getBeans(beanName);
+
+ if (beans.isEmpty())
+ {
+ try
+ {
+ Class<?> beanType = Class.forName(beanName);
+
+ Annotation[] q = qualifiers != null && !qualifiers.isEmpty() ?
+ new AnnotationsParser(beanType, qualifiers,
beanManager).getAnnotations() :
+ Call.EMPTY_ANNOTATIONS;
+
+ beans = beanManager.getBeans(beanType, q);
+ }
+ catch (ClassNotFoundException ex)
+ {
+ throw new IllegalArgumentException("Invalid bean class specified: "
+ beanName);
+ }
+
+ if (beans.isEmpty())
+ {
+ throw new IllegalArgumentException(
+ "Could not find bean with bean with type/name " + beanName +
+ ", qualifiers [" + qualifiers + "]");
+ }
+ }
+
+ Bean<?> bean = beans.iterator().next();
+ beanProperties.put(alias, new BeanProperty(bean, propertyName));
+ }
+
public void applyChanges(Set<ChangeSet> delta)
{
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-06
20:35:02 UTC (rev 11761)
+++
modules/remoting/trunk/src/main/java/org/jboss/seam/remoting/model/ModelHandler.java 2009-12-06
22:13:38 UTC (rev 11762)
@@ -1,8 +1,12 @@
package org.jboss.seam.remoting.model;
import java.io.ByteArrayOutputStream;
+import java.io.IOException;
+import java.io.OutputStream;
import java.io.StringReader;
-import java.util.Iterator;
+import java.util.HashSet;
+import java.util.List;
+import java.util.Set;
import javax.enterprise.context.Conversation;
import javax.enterprise.inject.spi.BeanManager;
@@ -21,6 +25,11 @@
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
+/**
+ * Handles incoming model fetch/applyUpdate requests
+ *
+ * @author Shane Bryzak
+ */
public class ModelHandler implements RequestHandler
{
private static final Logger log = LoggerFactory.getLogger(ModelHandler.class);
@@ -30,6 +39,7 @@
@Inject Conversation conversation;
@Inject ModelRegistry registry;
+ @SuppressWarnings("unchecked")
public void handle(HttpServletRequest request, HttpServletResponse response)
throws Exception
{
@@ -58,59 +68,101 @@
conversationManager.beginOrRestoreConversation(ctx.getConversationId());
}
- Element modelElement = env.element("body").element("model");
+ Set<Model> models = new HashSet<Model>();
- String operation = modelElement.attributeValue("operation");
- String id = modelElement.attributeValue("id");
-
- if ("fetch".equals(operation))
- {
- Call action = null;
- if (modelElement.elements("action").size() > 0)
+ for (Element modelElement : (List<Element>)
env.element("body").elements("model"))
+ {
+ String operation = modelElement.attributeValue("operation");
+ String id = modelElement.attributeValue("id");
+
+ if ("fetch".equals(operation))
{
- Element actionElement = modelElement.element("action");
- Element targetElement = actionElement.element("target");
- Element qualifiersElement = actionElement.element("qualifiers");
- Element methodElement = actionElement.element("method");
- Element paramsElement = actionElement.element("params");
- action = new Call(beanManager, id, targetElement.getTextTrim(),
- qualifiersElement.getTextTrim(), methodElement.getTextTrim());
+ Model model = registry.createModel();
- Element refsNode = modelElement.element("refs");
-
- Iterator<?> iter = refsNode.elementIterator("ref");
- while (iter.hasNext())
+ Call action = null;
+ if (modelElement.elements("action").size() > 0)
{
- action.getContext().createWrapperFromElement((Element) iter.next());
+ Element actionElement = modelElement.element("action");
+ Element targetElement = actionElement.element("target");
+ Element qualifiersElement =
actionElement.element("qualifiers");
+ Element methodElement = actionElement.element("method");
+ Element paramsElement = actionElement.element("params");
+ Element refsElement = actionElement.element("refs");
+
+ action = new Call(beanManager, id, targetElement.getTextTrim(),
+ qualifiersElement.getTextTrim(), methodElement.getTextTrim());
+
+ for (Element refElement : (List<Element>)
refsElement.elements("ref"))
+ {
+ action.getContext().createWrapperFromElement(refElement);
+ }
+
+ for (Wrapper w : action.getContext().getInRefs().values())
+ {
+ w.unmarshal();
+ }
+
+ for (Element paramElement : (List<Element>)
paramsElement.elements("param"))
+ {
+ action.addParameter(action.getContext().createWrapperFromElement(
+ paramElement));
+ }
}
-
- for (Wrapper w : action.getContext().getInRefs().values())
+
+ for (Element beanElement : (List<Element>)
modelElement.elements("bean"))
{
- w.unmarshal();
+ Element beanNameElement = beanElement.element("name");
+ Element beanQualifierElement =
beanElement.element("qualifier");
+ Element beanPropertyElement = beanElement.element("property");
+
+ model.addBean(beanElement.attributeValue("alias"),
+ beanNameElement.getTextTrim(),
+ beanQualifierElement.getTextTrim(),
+ beanPropertyElement.getTextTrim());
}
-
- iter = paramsElement.elementIterator("param");
- while (iter.hasNext())
+
+ // TODO Unmarshal expressions - don't support this until security
implications investigated
+ for (Element exprElement : (List<Element>)
modelElement.elements("expression"))
{
- Element param = (Element) iter.next();
-
- action.addParameter(action.getContext().createWrapperFromElement(
- (Element) param.elementIterator().next()));
+
}
+
+ if (action != null)
+ {
+ action.execute();
+ }
}
-
- // Unmarshal beans
-
- // Unmarshal expressions
-
- if (action != null)
- {
- action.execute();
- }
-
}
- // Store the conversation ID in the outgoing context
- ctx.setConversationId(conversation.getId());
+ ctx.setConversationId(conversation.getId());
+ marshalResponse(models, ctx, response.getOutputStream());
}
+
+ private void marshalResponse(Set<Model> models, RequestContext ctx,
+ OutputStream out) throws IOException
+ {
+ out.write(ENVELOPE_TAG_OPEN);
+
+ if (ctx.getConversationId() != null)
+ {
+ out.write(HEADER_OPEN);
+ out.write(CONTEXT_TAG_OPEN);
+ out.write(CONVERSATION_ID_TAG_OPEN);
+ out.write(ctx.getConversationId().getBytes());
+ out.write(CONVERSATION_ID_TAG_CLOSE);
+ out.write(CONTEXT_TAG_CLOSE);
+ out.write(HEADER_CLOSE);
+ }
+
+ out.write(BODY_TAG_OPEN);
+
+ for (Model model : models)
+ {
+ //MarshalUtils.marshalResult(call, out);
+ }
+
+ out.write(BODY_TAG_CLOSE);
+ out.write(ENVELOPE_TAG_CLOSE);
+ out.flush();
+ }
}