Seam SVN: r11899 - modules/remoting/trunk/src/main/resources/org/jboss/seam/remoting.
by seam-commits@lists.jboss.org
Author: shane.bryzak(a)jboss.com
Date: 2010-01-03 04:45:39 -0500 (Sun, 03 Jan 2010)
New Revision: 11899
Modified:
modules/remoting/trunk/src/main/resources/org/jboss/seam/remoting/remote.js
Log:
client side handler for model expand operation
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 2010-01-03 07:58:56 UTC (rev 11898)
+++ modules/remoting/trunk/src/main/resources/org/jboss/seam/remoting/remote.js 2010-01-03 09:45:39 UTC (rev 11899)
@@ -537,6 +537,37 @@
}
}
+Seam.preProcessModelExpandResponse = 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.processModelExpandResponse;
+ 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.processModelExpandResponse(call);
+ }
+ }
+ }
+}
+
+Seam.processModelExpandResponse = 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.processExpandResponse(n, call.refId, call.property);
+ }
+}
+
Seam.processModelResponse = function(call) {
Seam.pendingCalls.remove(call.callId);
var cn = Seam.Xml.childNode;
@@ -596,9 +627,9 @@
return unknowns;
}
-Seam.unmarshalRefs = function(refsNode) {
+Seam.unmarshalRefs = function(refsNode, refs) {
if (!refsNode) return;
- var refs = [];
+ if (!refs) refs = [];
var objs = [];
var cn = Seam.Xml.childNodes(refsNode, "ref");
for (var i=0; i<cn.length; i++) {
@@ -1057,4 +1088,30 @@
}
return -1;
}
+
+ Seam.Model.prototype.expand = function(v, p, callback) {
+ var refId = this.getRefId(v);
+ var r = this.createExpandRequest(refId, p);
+ var env = Seam.createEnvelope(Seam.createHeader(r.id), r.data);
+ this.callback = callback;
+ Seam.pendingCalls.put(r.id, r);
+ Seam.sendAjaxRequest(env, Seam.PATH_MODEL, Seam.processResponse, false);
+ }
+
+ Seam.Model.prototype.createExpandRequest = function(refId, propName) {
+ var callId = "" + Seam.__callId++;
+ var d = "<model id=\"" + this.id + "\" operation=\"expand\">" +
+ "<ref id=\"" + refId + "\"><member name=\"" + propName + "\"/></ref></model>";
+ return {data: d, id: callId, model: this, refId: refId, property: propName, handler: Seam.preProcessModelExpandResponse};
+ }
+
+ Seam.Model.prototype.processExpandResponse = function(modelNode, refId, propName) {
+ var refsNode = Seam.Xml.childNode(modelNode, "refs");
+ var valueNode = Seam.Xml.childNode(modelNode, "value");
+ Seam.unmarshalRefs(refsNode, this.sourceRefs);
+ Seam.unmarshalRefs(refsNode, this.workingRefs);
+ this.sourceRefs[refId][propName] = Seam.unmarshalValue(valueNode.firstChild,this.sourceRefs);
+ this.workingRefs[refId][propName] = Seam.unmarshalValue(valueNode.firstChild,this.workingRefs);
+ if (this.callback) this.callback(this);
+ }
}
\ No newline at end of file
14 years, 10 months
Seam SVN: r11898 - in modules/remoting/trunk/src/main/java/org/jboss/seam/remoting: model and 1 other directories.
by seam-commits@lists.jboss.org
Author: shane.bryzak(a)jboss.com
Date: 2010-01-03 02:58:56 -0500 (Sun, 03 Jan 2010)
New Revision: 11898
Modified:
modules/remoting/trunk/src/main/java/org/jboss/seam/remoting/MarshalUtils.java
modules/remoting/trunk/src/main/java/org/jboss/seam/remoting/model/ModelHandler.java
modules/remoting/trunk/src/main/java/org/jboss/seam/remoting/wrapper/BagWrapper.java
Log:
added handler for model expand requests
Modified: modules/remoting/trunk/src/main/java/org/jboss/seam/remoting/MarshalUtils.java
===================================================================
--- modules/remoting/trunk/src/main/java/org/jboss/seam/remoting/MarshalUtils.java 2010-01-02 11:04:40 UTC (rev 11897)
+++ modules/remoting/trunk/src/main/java/org/jboss/seam/remoting/MarshalUtils.java 2010-01-03 07:58:56 UTC (rev 11898)
@@ -95,6 +95,53 @@
out.write(MODEL_TAG_CLOSE);
}
+ public static void marshalModelExpand(Model model, Wrapper value,
+ OutputStream out, int newRefIdx)
+ throws IOException
+ {
+ out.write(MODEL_TAG_OPEN_START);
+ out.write(model.getId().getBytes());
+ out.write(MODEL_TAG_OPEN_END);
+
+ out.write(RESULT_TAG_OPEN);
+ value.marshal(out);
+ out.write(RESULT_TAG_CLOSE);
+
+ if (model.getCallContext().getOutRefs().size() > newRefIdx)
+ {
+ out.write(RequestHandler.REFS_TAG_OPEN);
+ marshalNewRefs(model.getCallContext().getOutRefs(), newRefIdx, null, out);
+ out.write(RequestHandler.REFS_TAG_CLOSE);
+ }
+
+ out.write(MODEL_TAG_CLOSE);
+ }
+
+ public static void marshalNewRefs(List<Wrapper> refs, int startIdx,
+ List<String> constraints, OutputStream out)
+ throws IOException
+ {
+ for (int i = startIdx; i < refs.size(); i++)
+ {
+ Wrapper wrapper = refs.get(i);
+
+ out.write(RequestHandler.REF_TAG_OPEN_START);
+ out.write(Integer.toString(i).getBytes());
+ out.write(RequestHandler.REF_TAG_OPEN_END);
+
+ if (wrapper instanceof BeanWrapper && constraints != null)
+ {
+ ((BeanWrapper) wrapper).serialize(out, constraints);
+ }
+ else
+ {
+ wrapper.serialize(out);
+ }
+
+ out.write(RequestHandler.REF_TAG_CLOSE);
+ }
+ }
+
public static void marshalRefs(List<Wrapper> refs, List<String> constraints,
OutputStream out)
throws IOException
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 2010-01-02 11:04:40 UTC (rev 11897)
+++ modules/remoting/trunk/src/main/java/org/jboss/seam/remoting/model/ModelHandler.java 2010-01-03 07:58:56 UTC (rev 11898)
@@ -6,7 +6,6 @@
import java.io.StringReader;
import java.lang.reflect.Array;
import java.lang.reflect.Type;
-import java.util.Collection;
import java.util.List;
import java.util.Map;
import java.util.Set;
@@ -95,34 +94,40 @@
Element modelElement = env.element("body").element("model");
String operation = modelElement.attributeValue("operation");
-
- Model model = null;
- if ("fetch".equals(operation))
+ if ("expand".equals(operation))
{
- model = processFetchRequest(modelElement);
+ processExpandRequest(modelElement, ctx, response.getOutputStream());
}
- else if ("apply".equals(operation))
+ else
{
- model = processApplyRequest(modelElement);
+ Model model = null;
+ if ("fetch".equals(operation))
+ {
+ model = processFetchRequest(modelElement);
+ }
+ else if ("apply".equals(operation))
+ {
+ model = processApplyRequest(modelElement);
+ }
+
+ 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.evaluate();
+
+ ctx.setConversationId(conversation.isTransient() ? null : conversation.getId());
+ marshalResponse(model, ctx, response.getOutputStream());
}
-
- 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.evaluate();
-
- ctx.setConversationId(conversation.isTransient() ? null : conversation.getId());
- marshalResponse(model, ctx, response.getOutputStream());
}
finally
{
@@ -322,6 +327,57 @@
return model;
}
+ private void processExpandRequest(Element modelElement, RequestContext ctx, OutputStream out)
+ throws Exception
+ {
+ Model model = registry.getModel(modelElement.attributeValue("id"));
+ model.setAction(null);
+
+ Element refElement = modelElement.element("ref");
+ if (refElement == null)
+ {
+ throw new IllegalStateException("Invalid request state - no object ref found");
+ }
+
+ int refId = Integer.parseInt(refElement.attributeValue("refid"));
+ Wrapper target = model.getCallContext().getOutRefs().get(refId);
+
+ int newRefIdx = model.getCallContext().getOutRefs().size();
+
+ Element memberElement = refElement.element("member");
+ String memberName = memberElement.attributeValue("name");
+
+ Wrapper value = ((BeanWrapper) target).getBeanProperty(memberName);
+ if (value instanceof BagWrapper)
+ {
+ ((BagWrapper) value).setLoadLazy(true);
+ }
+
+ out.write(ENVELOPE_TAG_OPEN);
+
+ out.write(HEADER_OPEN);
+ out.write(CONTEXT_TAG_OPEN);
+ if (ctx.getConversationId() != null)
+ {
+ 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);
+
+ MarshalUtils.marshalModelExpand(model, value, out, newRefIdx);
+
+ out.write(BODY_TAG_CLOSE);
+ out.write(ENVELOPE_TAG_CLOSE);
+ out.flush();
+ }
+
/**
* Clones the contents of the specified source bag into the specified target
* bag. If the contents can be cloned, this method returns true, otherwise it
Modified: modules/remoting/trunk/src/main/java/org/jboss/seam/remoting/wrapper/BagWrapper.java
===================================================================
--- modules/remoting/trunk/src/main/java/org/jboss/seam/remoting/wrapper/BagWrapper.java 2010-01-02 11:04:40 UTC (rev 11897)
+++ modules/remoting/trunk/src/main/java/org/jboss/seam/remoting/wrapper/BagWrapper.java 2010-01-03 07:58:56 UTC (rev 11898)
@@ -35,6 +35,13 @@
private static final byte[] ELEMENT_TAG_OPEN = "<element>".getBytes();
private static final byte[] ELEMENT_TAG_CLOSE = "</element>".getBytes();
+
+ private boolean loadLazy = false;
+
+ public void setLoadLazy(boolean loadLazy)
+ {
+ this.loadLazy = loadLazy;
+ }
@SuppressWarnings("unchecked")
public void marshal(OutputStream out) throws IOException
@@ -42,7 +49,7 @@
out.write(BAG_TAG_OPEN);
// Fix to prevent uninitialized lazy loading in Hibernate
- if (value instanceof PersistentCollection)
+ if (value instanceof PersistentCollection && !loadLazy)
{
if (!((PersistentCollection) value).wasInitialized())
{
14 years, 10 months
Seam SVN: r11897 - in modules/remoting/trunk/src/main/java/org/jboss/seam/remoting: wrapper and 1 other directory.
by seam-commits@lists.jboss.org
Author: shane.bryzak(a)jboss.com
Date: 2010-01-02 06:04:40 -0500 (Sat, 02 Jan 2010)
New Revision: 11897
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/BagWrapper.java
modules/remoting/trunk/src/main/java/org/jboss/seam/remoting/wrapper/BeanWrapper.java
Log:
support for updating bag and map model properties
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 2010-01-01 04:42:31 UTC (rev 11896)
+++ modules/remoting/trunk/src/main/java/org/jboss/seam/remoting/model/ModelHandler.java 2010-01-02 11:04:40 UTC (rev 11897)
@@ -4,7 +4,12 @@
import java.io.IOException;
import java.io.OutputStream;
import java.io.StringReader;
+import java.lang.reflect.Array;
+import java.lang.reflect.Type;
+import java.util.Collection;
import java.util.List;
+import java.util.Map;
+import java.util.Set;
import javax.enterprise.context.Conversation;
import javax.enterprise.inject.spi.BeanManager;
@@ -22,6 +27,7 @@
import org.jboss.seam.remoting.RequestHandler;
import org.jboss.seam.remoting.wrapper.BagWrapper;
import org.jboss.seam.remoting.wrapper.BeanWrapper;
+import org.jboss.seam.remoting.wrapper.MapWrapper;
import org.jboss.seam.remoting.wrapper.Wrapper;
import org.jboss.weld.Container;
import org.jboss.weld.context.ContextLifecycle;
@@ -32,7 +38,7 @@
import org.slf4j.LoggerFactory;
/**
- * Handles incoming model fetch/applyUpdate requests
+ * Handles incoming model fetch/apply requests
*
* @author Shane Bryzak
*/
@@ -45,7 +51,6 @@
@Inject ModelRegistry registry;
@Inject Conversation conversation;
- @SuppressWarnings("unchecked")
public void handle(HttpServletRequest request, HttpServletResponse response)
throws Exception
{
@@ -154,10 +159,10 @@
}
// TODO Unmarshal expressions - don't support this until security implications investigated
- for (Element exprElement : (List<Element>) modelElement.elements("expression"))
- {
+ //for (Element exprElement : (List<Element>) modelElement.elements("expression"))
+ //{
- }
+ //}
if (model.getAction() != null)
{
@@ -228,38 +233,79 @@
if (changeset.elements("member").size() > 0)
{
+ Wrapper target = model.getCallContext().getOutRefs().get(refId);
+ if (!(target instanceof BeanWrapper))
+ {
+ throw new IllegalStateException("Changeset for refId [" +
+ refId + "] does not reference a valid bean object");
+ }
+
for (Element member : (List<Element>) changeset.elements("member"))
{
String name = member.attributeValue("name");
- Wrapper w = model.getCallContext().createWrapperFromElement(
+ Wrapper source = model.getCallContext().createWrapperFromElement(
(Element) member.elementIterator().next());
-
- if (w instanceof BagWrapper)
- {
- // TODO process collection updates
+
+ if (source instanceof BagWrapper)
+ {
+ Object targetBag = ((BeanWrapper) target).getBeanProperty(name);
+ if (targetBag == null)
+ {
+ ((BeanWrapper) target).setBeanProperty(name, source);
+ }
+ else
+ {
+ Type t = ((BeanWrapper) target).getBeanPropertyType(name);
+ if (!cloneBagContents(source.convert(t), targetBag))
+ {
+ ((BeanWrapper) target).setBeanProperty(name, source);
+ }
+ }
}
- else
+ else if (source instanceof MapWrapper)
{
- Wrapper ref = model.getCallContext().getOutRefs().get(refId);
- if (ref instanceof BeanWrapper)
+ Object targetMap = ((BeanWrapper) target).getBeanProperty(name);
+ if (!Map.class.isAssignableFrom(targetMap.getClass()))
{
- ((BeanWrapper) ref).setBeanProperty(name, w);
+ throw new IllegalStateException("Cannot assign Map value " +
+ "to non Map property [" + target.getClass().getName() +
+ "." + name + "]");
}
- else
+
+ if (targetMap == null)
{
- throw new IllegalStateException("Changeset for refId [" +
- refId + "] does not reference a valid bean object");
+ ((BeanWrapper) target).setBeanProperty(name, source);
}
+ else
+ {
+ Type t = ((BeanWrapper) target).getBeanPropertyType(name);
+ cloneMapContents((Map) source.convert(t), (Map) targetMap);
+ }
+ }
+ else
+ {
+ ((BeanWrapper) target).setBeanProperty(name, source);
}
-
}
}
if (changeset.elements("bag").size() > 0)
{
- // TODO process root node collection updates
+ Wrapper target = model.getCallContext().getOutRefs().get(refId);
+ Wrapper source = model.getCallContext().createWrapperFromElement(
+ (Element) changeset.element("bag"));
+ cloneBagContents(source.convert(target.getValue().getClass()),
+ target.getValue());
}
+ else if (changeset.elements("map").size() > 0)
+ {
+ Wrapper target = model.getCallContext().getOutRefs().get(refId);
+ Wrapper source = model.getCallContext().createWrapperFromElement(
+ (Element) changeset.element("map"));
+ cloneMapContents((Map) source.convert(target.getValue().getClass()),
+ (Map) target.getValue());
+ }
}
}
@@ -276,6 +322,98 @@
return model;
}
+ /**
+ * Clones the contents of the specified source bag into the specified target
+ * bag. If the contents can be cloned, this method returns true, otherwise it
+ * returns false.
+ *
+ * @param sourceBag
+ * @param targetBag
+ * @return
+ */
+ @SuppressWarnings("unchecked")
+ private boolean cloneBagContents(Object sourceBag, Object targetBag)
+ {
+ Class<?> cls = sourceBag.getClass();
+ if (cls.isArray())
+ {
+ int sourceLen = Array.getLength(sourceBag);
+ int targetLen = Array.getLength(targetBag);
+ if (targetLen != sourceLen) return false;
+ for (int i = 0; i < sourceLen; i++)
+ {
+ Array.set(targetBag, i, Array.get(sourceBag, i));
+ }
+ return true;
+ }
+ else if (List.class.isAssignableFrom(cls))
+ {
+ List sourceList = (List) sourceBag;
+ List targetList = (List) targetBag;
+
+ while (targetList.size() > sourceList.size())
+ {
+ targetList.remove(targetList.size() - 1);
+ }
+
+ for (int i = 0; i < sourceList.size(); i++)
+ {
+ targetList.set(i, sourceList.get(i));
+ }
+ return true;
+ }
+ else if (Set.class.isAssignableFrom(cls))
+ {
+ Set sourceSet = (Set) sourceBag;
+ Set targetSet = (Set) targetBag;
+
+ for (Object e : sourceSet)
+ {
+ if (!targetSet.contains(e))
+ {
+ targetSet.add(e);
+ }
+ }
+
+ for (Object e : targetSet)
+ {
+ if (!sourceSet.contains(e))
+ {
+ targetSet.remove(e);
+ }
+ }
+ return true;
+ }
+
+ return false;
+ }
+
+ /**
+ * Clones the contents of one Map into another
+ *
+ * @param sourceMap
+ * @param targetMap
+ */
+ @SuppressWarnings("unchecked")
+ private void cloneMapContents(Map sourceMap, Map targetMap)
+ {
+ for (Object key : sourceMap.keySet())
+ {
+ if (!targetMap.containsKey(key))
+ {
+ targetMap.put(key, sourceMap.get(key));
+ }
+ }
+
+ for (Object key : targetMap.keySet())
+ {
+ if (!sourceMap.containsKey(key))
+ {
+ targetMap.remove(key);
+ }
+ }
+ }
+
private void marshalResponse(Model model, RequestContext ctx,
OutputStream out) throws IOException
{
Modified: modules/remoting/trunk/src/main/java/org/jboss/seam/remoting/wrapper/BagWrapper.java
===================================================================
--- modules/remoting/trunk/src/main/java/org/jboss/seam/remoting/wrapper/BagWrapper.java 2010-01-01 04:42:31 UTC (rev 11896)
+++ modules/remoting/trunk/src/main/java/org/jboss/seam/remoting/wrapper/BagWrapper.java 2010-01-02 11:04:40 UTC (rev 11897)
@@ -58,12 +58,19 @@
{
vals = new ArrayList<Object>();
for (int i = 0; i < Array.getLength(value); i++)
+ {
vals.add(Array.get(value, i));
- } else if (Collection.class.isAssignableFrom(value.getClass()))
+ }
+ }
+ else if (Collection.class.isAssignableFrom(value.getClass()))
+ {
vals = (Collection) value;
+ }
else
+ {
throw new RuntimeException(String.format(
"Can not marshal object as bag: [%s]", value));
+ }
for (Object val : vals)
{
@@ -82,32 +89,39 @@
List<Wrapper> vals = new ArrayList<Wrapper>();
for (Element e : (List<Element>) element.elements("element"))
- vals.add(context.createWrapperFromElement((Element) e.elements()
- .get(0)));
+ {
+ vals.add(context.createWrapperFromElement((Element) e.elements().get(0)));
+ }
if (type instanceof Class && ((Class) type).isArray())
{
Class arrayType = ((Class) type).getComponentType();
value = Array.newInstance(arrayType, vals.size()); // Fix this
for (int i = 0; i < vals.size(); i++)
+ {
Array.set(value, i, vals.get(i).convert(arrayType));
- } else if (type instanceof Class
- && Collection.class.isAssignableFrom((Class) type))
+ }
+ }
+ else if (type instanceof Class &&
+ Collection.class.isAssignableFrom((Class) type))
{
try
{
value = getConcreteClass((Class) type).newInstance();
- } catch (Exception ex)
+ }
+ catch (Exception ex)
{
throw new ConversionException(String.format(
"Could not create instance of target type [%s].", type));
}
+
for (Wrapper w : vals)
+ {
((Collection) value).add(w.convert(Object.class));
- } else if (type instanceof ParameterizedType
- && Collection.class
- .isAssignableFrom((Class) ((ParameterizedType) type)
- .getRawType()))
+ }
+ }
+ else if (type instanceof ParameterizedType &&
+ Collection.class.isAssignableFrom((Class) ((ParameterizedType) type).getRawType()))
{
Class rawType = (Class) ((ParameterizedType) type).getRawType();
Type genType = Object.class;
@@ -121,14 +135,17 @@
try
{
value = getConcreteClass(rawType).newInstance();
- } catch (Exception ex)
+ }
+ catch (Exception ex)
{
throw new ConversionException(String.format(
"Could not create instance of target type [%s].", rawType));
}
for (Wrapper w : vals)
+ {
((Collection) value).add(w.convert(genType));
+ }
}
return value;
@@ -140,13 +157,22 @@
{
// Support Set, Queue and (by default, and as a last resort) List
if (Set.class.isAssignableFrom(c))
+ {
return HashSet.class;
+ }
else if (Queue.class.isAssignableFrom(c))
+ {
return LinkedList.class;
+ }
else
+ {
return ArrayList.class;
- } else
+ }
+ }
+ else
+ {
return c;
+ }
}
/**
@@ -158,18 +184,14 @@
public ConversionScore conversionScore(Class<?> cls)
{
// There's no such thing as an exact match for a bag, so we'll just look
- // for
- // a compatible match
-
- if (cls.isArray())
+ // for a compatible match
+ if (cls.isArray() || cls.equals(Object.class) || Collection.class.isAssignableFrom(cls))
+ {
return ConversionScore.compatible;
-
- if (cls.equals(Object.class))
- return ConversionScore.compatible;
-
- if (Collection.class.isAssignableFrom(cls))
- return ConversionScore.compatible;
-
- return ConversionScore.nomatch;
+ }
+ else
+ {
+ return ConversionScore.nomatch;
+ }
}
}
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 2010-01-01 04:42:31 UTC (rev 11896)
+++ modules/remoting/trunk/src/main/java/org/jboss/seam/remoting/wrapper/BeanWrapper.java 2010-01-02 11:04:40 UTC (rev 11897)
@@ -50,20 +50,19 @@
}
return metadataCache;
}
-
+
+ @Override
@SuppressWarnings("unchecked")
- @Override
public void setElement(Element element)
{
super.setElement(element);
String beanType = element.attributeValue("type");
- // TODO do it this way, it might not be a managed bean...
- Bean bean = beanManager.getBeans(beanType).iterator().next();
-
- if (bean != null)
+ Set<Bean<?>> beans = beanManager.getBeans(beanType);
+ if (beans.size() > 0)
{
+ Bean bean = beans.iterator().next();
value = bean.create(beanManager.createCreationalContext(bean));
}
else
@@ -80,12 +79,113 @@
}
}
+ public Type getBeanPropertyType(String propertyName)
+ {
+ Class<?> cls = value.getClass();
+
+ String getter = "get" + Character.toUpperCase(propertyName.charAt(0)) +
+ propertyName.substring(1);
+
+ for (Method m : cls.getMethods())
+ {
+ if (getter.equals(m.getName())) return m.getGenericReturnType();
+ }
+
+ Field field = null;
+ while (field == null && !cls.equals(Object.class))
+ {
+ try
+ {
+ field = cls.getDeclaredField(propertyName);
+ }
+ catch (NoSuchFieldException e)
+ {
+ cls = cls.getSuperclass();
+ }
+ }
+
+ if (field == null)
+ {
+ throw new IllegalArgumentException("Invalid property name [" + propertyName +
+ "] specified for target class [" + value.getClass() + "]");
+ }
+
+ return field.getGenericType();
+ }
+
+ public Wrapper getBeanProperty(String propertyName)
+ {
+ Class<?> cls = value.getClass();
+
+ Field f = null;
+ try
+ {
+ f = cls.getField(propertyName);
+ }
+ catch (NoSuchFieldException ex) { }
+
+ boolean accessible = false;
+ try
+ {
+ // Temporarily set the field's accessibility so we can read it
+ if (f != null)
+ {
+ accessible = f.isAccessible();
+ f.setAccessible(true);
+ return context.createWrapperFromObject(f.get(value), null);
+ }
+ else
+ {
+ Method accessor = null;
+ try
+ {
+ accessor = cls.getMethod(String.format("get%s%s",
+ Character.toUpperCase(propertyName.charAt(0)),
+ propertyName.substring(1)));
+ }
+ catch (NoSuchMethodException ex)
+ {
+ try
+ {
+ accessor = cls.getMethod(String.format("is%s%s",
+ Character.toUpperCase(propertyName.charAt(0)),
+ propertyName.substring(1)));
+ }
+ catch (NoSuchMethodException ex2)
+ {
+ // uh oh... continue with the next one
+ return null;
+ }
+ }
+
+ try
+ {
+ return context.createWrapperFromObject(accessor.invoke(value), null);
+ }
+ catch (InvocationTargetException ex)
+ {
+ throw new RuntimeException(String.format(
+ "Failed to read property [%s] for object [%s]",
+ propertyName, value));
+ }
+ }
+ }
+ catch (IllegalAccessException ex)
+ {
+ throw new RuntimeException("Error reading value from field.");
+ }
+ finally
+ {
+ if (f != null)
+ f.setAccessible(accessible);
+ }
+ }
+
public void setBeanProperty(String propertyName, Wrapper valueWrapper)
{
Class<?> cls = value.getClass();
// We're going to try a combination of ways to set the property value
- // here
Method method = null;
Field field = null;
@@ -164,8 +264,7 @@
boolean accessible = field.isAccessible();
try
{
- if (!accessible)
- field.setAccessible(true);
+ if (!accessible) field.setAccessible(true);
field.set(value, fieldValue);
}
catch (Exception ex)
@@ -210,14 +309,15 @@
}
}
+ /**
+ * Writes the object reference ID to the specified OutputStream
+ */
public void marshal(OutputStream out) throws IOException
{
context.addOutRef(this);
out.write(REF_START_TAG_OPEN);
- out
- .write(Integer.toString(context.getOutRefs().indexOf(this))
- .getBytes());
+ out.write(Integer.toString(context.getOutRefs().indexOf(this)).getBytes());
out.write(REF_START_TAG_END);
}
@@ -227,6 +327,14 @@
serialize(out, null);
}
+ /**
+ * Writes a serialized representation of the object's properties to the specified
+ * OutputStream.
+ *
+ * @param out
+ * @param constraints
+ * @throws IOException
+ */
public void serialize(OutputStream out, List<String> constraints)
throws IOException
{
@@ -275,81 +383,20 @@
componentName != null ? componentName : cls.getName(),
propertyName);
- if (constraints == null
- || (!constraints.contains(fieldPath) && !constraints
- .contains(wildCard)))
+ if (constraints == null || (!constraints.contains(fieldPath) &&
+ !constraints.contains(wildCard)))
{
out.write(MEMBER_START_TAG_OPEN);
out.write(propertyName.getBytes());
out.write(MEMBER_START_TAG_CLOSE);
- Field f = null;
- try
+ Wrapper w = getBeanProperty(propertyName);
+ if (w != null)
{
- f = cls.getField(propertyName);
+ w.setPath(fieldPath);
+ w.marshal(out);
}
- catch (NoSuchFieldException ex)
- {
- }
- boolean accessible = false;
- try
- {
- // Temporarily set the field's accessibility so we can read it
- if (f != null)
- {
- accessible = f.isAccessible();
- f.setAccessible(true);
- context.createWrapperFromObject(f.get(value), fieldPath)
- .marshal(out);
- }
- else
- {
- Method accessor = null;
- try
- {
- accessor = cls.getMethod(String.format("get%s%s",
- Character.toUpperCase(propertyName.charAt(0)),
- propertyName.substring(1)));
- }
- catch (NoSuchMethodException ex)
- {
- try
- {
- accessor = cls.getMethod(String.format("is%s%s",
- Character.toUpperCase(propertyName.charAt(0)),
- propertyName.substring(1)));
- }
- catch (NoSuchMethodException ex2)
- {
- // uh oh... continue with the next one
- continue;
- }
- }
-
- try
- {
- context.createWrapperFromObject(accessor.invoke(value),
- fieldPath).marshal(out);
- }
- catch (InvocationTargetException ex)
- {
- throw new RuntimeException(String.format(
- "Failed to read property [%s] for object [%s]",
- propertyName, value));
- }
- }
- }
- catch (IllegalAccessException ex)
- {
- throw new RuntimeException("Error reading value from field.");
- }
- finally
- {
- if (f != null)
- f.setAccessible(accessible);
- }
-
out.write(MEMBER_CLOSE_TAG);
}
}
14 years, 10 months
Seam SVN: r11896 - in modules/remoting/trunk/src/main/java/org/jboss/seam/remoting: wrapper and 1 other directory.
by seam-commits@lists.jboss.org
Author: shane.bryzak(a)jboss.com
Date: 2009-12-31 23:42:31 -0500 (Thu, 31 Dec 2009)
New Revision: 11896
Removed:
modules/remoting/trunk/src/main/java/org/jboss/seam/remoting/model/ChangeSet.java
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/ModelHandler.java
modules/remoting/trunk/src/main/java/org/jboss/seam/remoting/wrapper/BeanWrapper.java
Log:
cleanup, removed unnecessary ChangeSet class
Deleted: modules/remoting/trunk/src/main/java/org/jboss/seam/remoting/model/ChangeSet.java
===================================================================
--- modules/remoting/trunk/src/main/java/org/jboss/seam/remoting/model/ChangeSet.java 2009-12-30 23:38:56 UTC (rev 11895)
+++ modules/remoting/trunk/src/main/java/org/jboss/seam/remoting/model/ChangeSet.java 2010-01-01 04:42:31 UTC (rev 11896)
@@ -1,6 +0,0 @@
-package org.jboss.seam.remoting.model;
-
-public class ChangeSet
-{
-
-}
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-30 23:38:56 UTC (rev 11895)
+++ modules/remoting/trunk/src/main/java/org/jboss/seam/remoting/model/Model.java 2010-01-01 04:42:31 UTC (rev 11896)
@@ -17,6 +17,7 @@
import org.jboss.seam.remoting.AnnotationsParser;
import org.jboss.seam.remoting.Call;
import org.jboss.seam.remoting.CallContext;
+import org.jboss.seam.remoting.wrapper.BeanWrapper;
import org.jboss.seam.remoting.wrapper.ConversionException;
import org.jboss.seam.remoting.wrapper.Wrapper;
@@ -182,63 +183,6 @@
beanProperties.put(alias, new BeanProperty(bean, propertyName));
}
- public void setModelProperty(int refId, String name, Wrapper wrapper)
- {
- Wrapper outRef = callContext.getOutRefs().get(refId);
-
- try
- {
- Field f = outRef.getValue().getClass().getField(name);
- if (wrapper.conversionScore(f.getType()).getScore() > 0)
- {
- // found a match
- boolean accessible = f.isAccessible();
- try
- {
- if (!f.isAccessible()) f.setAccessible(true);
- f.set(outRef.getValue(), wrapper.convert(f.getType()));
- }
- catch (ConversionException ex)
- {
- throw new RuntimeException("Exception converting model property value", ex);
- }
- catch (IllegalAccessException ex)
- {
- throw new RuntimeException("Exception setting model property", ex);
- }
- finally
- {
- f.setAccessible(accessible);
- }
- }
- }
- catch (NoSuchFieldException ex)
- {
- String methodName = "set" + name.substring(0, 1).toUpperCase() + name.substring(1);
- for (Method m : outRef.getValue().getClass().getMethods())
- {
- if (m.getName().equals(methodName) && m.getParameterTypes().length == 1 &&
- wrapper.conversionScore(m.getParameterTypes()[0]).getScore() > 0)
- {
- try
- {
- m.invoke(outRef.getValue(), wrapper.convert(m.getParameterTypes()[0]));
- break;
- }
- catch (Exception ex2)
- {
- throw new RuntimeException("Exception converting model property value", ex2);
- }
- }
- }
- }
- }
-
- public void applyChanges(Set<ChangeSet> delta)
- {
-
- }
-
public void setAction(Call action)
{
this.action = action;
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 23:38:56 UTC (rev 11895)
+++ modules/remoting/trunk/src/main/java/org/jboss/seam/remoting/model/ModelHandler.java 2010-01-01 04:42:31 UTC (rev 11896)
@@ -4,9 +4,7 @@
import java.io.IOException;
import java.io.OutputStream;
import java.io.StringReader;
-import java.util.HashSet;
import java.util.List;
-import java.util.Set;
import javax.enterprise.context.Conversation;
import javax.enterprise.inject.spi.BeanManager;
@@ -22,6 +20,8 @@
import org.jboss.seam.remoting.MarshalUtils;
import org.jboss.seam.remoting.RequestContext;
import org.jboss.seam.remoting.RequestHandler;
+import org.jboss.seam.remoting.wrapper.BagWrapper;
+import org.jboss.seam.remoting.wrapper.BeanWrapper;
import org.jboss.seam.remoting.wrapper.Wrapper;
import org.jboss.weld.Container;
import org.jboss.weld.context.ContextLifecycle;
@@ -231,12 +231,35 @@
for (Element member : (List<Element>) changeset.elements("member"))
{
String name = member.attributeValue("name");
+
Wrapper w = model.getCallContext().createWrapperFromElement(
(Element) member.elementIterator().next());
- model.setModelProperty(refId, name, w);
+
+ if (w instanceof BagWrapper)
+ {
+ // TODO process collection updates
+ }
+ else
+ {
+ Wrapper ref = model.getCallContext().getOutRefs().get(refId);
+ if (ref instanceof BeanWrapper)
+ {
+ ((BeanWrapper) ref).setBeanProperty(name, w);
+ }
+ else
+ {
+ throw new IllegalStateException("Changeset for refId [" +
+ refId + "] does not reference a valid bean object");
+ }
+ }
+
}
}
+ if (changeset.elements("bag").size() > 0)
+ {
+ // TODO process root node collection updates
+ }
}
}
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 23:38:56 UTC (rev 11895)
+++ modules/remoting/trunk/src/main/java/org/jboss/seam/remoting/wrapper/BeanWrapper.java 2010-01-01 04:42:31 UTC (rev 11896)
@@ -79,120 +79,123 @@
}
}
}
-
- @SuppressWarnings("unchecked")
- @Override
- public void unmarshal()
+
+ public void setBeanProperty(String propertyName, Wrapper valueWrapper)
{
- List<Element> members = element.elements("member");
+ Class<?> cls = value.getClass();
- for (Element member : members)
- {
- String name = member.attributeValue("name");
+ // We're going to try a combination of ways to set the property value
+ // here
+ Method method = null;
+ Field field = null;
- Wrapper w = context.createWrapperFromElement((Element) member
- .elementIterator().next());
+ // First try to find the best matching method
+ String setter = "set" + Character.toUpperCase(propertyName.charAt(0))
+ + propertyName.substring(1);
- Class cls = value.getClass();
-
- // We're going to try a combination of ways to set the property value
- // here
- Method method = null;
- Field field = null;
-
- // First try to find the best matching method
- String setter = "set" + Character.toUpperCase(name.charAt(0))
- + name.substring(1);
-
- ConversionScore score = ConversionScore.nomatch;
- for (Method m : cls.getMethods())
+ ConversionScore score = ConversionScore.nomatch;
+ for (Method m : cls.getMethods())
+ {
+ if (setter.equals(m.getName()) && m.getParameterTypes().length == 1)
{
- if (setter.equals(m.getName()) && m.getParameterTypes().length == 1)
+ ConversionScore s = valueWrapper.conversionScore(m.getParameterTypes()[0]);
+ if (s.getScore() > score.getScore())
{
- ConversionScore s = w.conversionScore(m.getParameterTypes()[0]);
- if (s.getScore() > score.getScore())
- {
- method = m;
- score = s;
- }
+ method = m;
+ score = s;
}
}
+ }
- // If we can't find a method, look for a matching field name
- if (method == null)
+ // If we can't find a method, look for a matching field name
+ if (method == null)
+ {
+ while (field == null && !cls.equals(Object.class))
{
- while (field == null && !cls.equals(Object.class))
+ try
{
- try
- {
- // First check the declared fields
- field = cls.getDeclaredField(name);
- }
- catch (NoSuchFieldException ex)
- {
- // Couldn't find the field.. try the superclass
- cls = cls.getSuperclass();
- }
+ // First check the declared fields
+ field = cls.getDeclaredField(propertyName);
}
-
- if (field == null)
+ catch (NoSuchFieldException ex)
{
- throw new RuntimeException(
- String
- .format(
- "Error while unmarshalling - property [%s] not found in class [%s]",
- name, value.getClass().getName()));
+ // Couldn't find the field.. try the superclass
+ cls = cls.getSuperclass();
}
}
- // Now convert the field value to the correct target type
- Object fieldValue = null;
+ if (field == null)
+ {
+ throw new RuntimeException(String.format(
+ "Error while unmarshalling - property [%s] not found in class [%s]",
+ propertyName, value.getClass().getName()));
+ }
+ }
+
+ // Now convert the field value to the correct target type
+ Object fieldValue = null;
+ try
+ {
+ fieldValue = valueWrapper.convert(method != null ? method
+ .getGenericParameterTypes()[0] : field.getGenericType());
+ }
+ catch (ConversionException ex)
+ {
+ throw new RuntimeException(
+ "Could not convert value while unmarshaling", ex);
+ }
+
+ // If we have a setter method, invoke it
+ if (method != null)
+ {
try
{
- fieldValue = w.convert(method != null ? method
- .getGenericParameterTypes()[0] : field.getGenericType());
+ method.invoke(value, fieldValue);
}
- catch (ConversionException ex)
+ catch (Exception e)
{
- throw new RuntimeException(
- "Could not convert value while unmarshaling", ex);
+ throw new RuntimeException(String.format(
+ "Could not invoke setter method [%s]", method.getName()));
}
-
- // If we have a setter method, invoke it
- if (method != null)
+ }
+ else
+ {
+ // Otherwise try to set the field value directly
+ boolean accessible = field.isAccessible();
+ try
{
- try
- {
- method.invoke(value, fieldValue);
- }
- catch (Exception e)
- {
- throw new RuntimeException(String.format(
- "Could not invoke setter method [%s]", method.getName()));
- }
+ if (!accessible)
+ field.setAccessible(true);
+ field.set(value, fieldValue);
}
- else
+ catch (Exception ex)
{
- // Otherwise try to set the field value directly
- boolean accessible = field.isAccessible();
- try
- {
- if (!accessible)
- field.setAccessible(true);
- field.set(value, fieldValue);
- }
- catch (Exception ex)
- {
- throw new RuntimeException("Could not set field value.", ex);
- }
- finally
- {
- field.setAccessible(accessible);
- }
+ throw new RuntimeException("Could not set field value.", ex);
}
+ finally
+ {
+ field.setAccessible(accessible);
+ }
}
+
}
+ @SuppressWarnings("unchecked")
+ @Override
+ public void unmarshal()
+ {
+ List<Element> members = element.elements("member");
+
+ for (Element member : members)
+ {
+ String name = member.attributeValue("name");
+
+ Wrapper w = context.createWrapperFromElement((Element) member
+ .elementIterator().next());
+ setBeanProperty(name, w);
+ }
+ }
+
public Object convert(Type type) throws ConversionException
{
if (type instanceof Class<?>
14 years, 11 months