Author: alessio.soldano(a)jboss.com
Date: 2013-01-25 18:23:53 -0500 (Fri, 25 Jan 2013)
New Revision: 462
Added:
webgui/branches/cdi-jsf/src/main/java/org/jboss/wise/gui/treeElement/LazyLoadWiseTreeElement.java
Modified:
webgui/branches/cdi-jsf/src/main/java/org/jboss/wise/gui/ClientConversationBean.java
webgui/branches/cdi-jsf/src/main/java/org/jboss/wise/gui/treeElement/WiseTreeElement.java
webgui/branches/cdi-jsf/src/main/java/org/jboss/wise/gui/treeElement/WiseTreeElementBuilder.java
Log:
WIP on lazy load elements (to deal with parameter type models including cycles)
Modified:
webgui/branches/cdi-jsf/src/main/java/org/jboss/wise/gui/ClientConversationBean.java
===================================================================
---
webgui/branches/cdi-jsf/src/main/java/org/jboss/wise/gui/ClientConversationBean.java 2013-01-20
23:27:29 UTC (rev 461)
+++
webgui/branches/cdi-jsf/src/main/java/org/jboss/wise/gui/ClientConversationBean.java 2013-01-25
23:23:53 UTC (rev 462)
@@ -122,22 +122,22 @@
}
private static TreeNodeImpl convertOperationParametersToGui(WSMethod wsMethod,
WSDynamicClient client) {
- WiseTreeElementBuilder builder = new WiseTreeElementBuilder();
+ WiseTreeElementBuilder builder = new WiseTreeElementBuilder(client);
TreeNodeImpl rootElement = new TreeNodeImpl();
Collection<? extends WebParameter> parameters = wsMethod.getWebParams().values();
for (WebParameter parameter : parameters) {
- WiseTreeElement wte = builder.buildTreeFromType(parameter.getType(),
parameter.getName(), client);
+ WiseTreeElement wte = builder.buildTreeFromType(parameter.getType(),
parameter.getName());
rootElement.addChild(wte.getId(), wte);
}
return rootElement;
}
private static TreeNodeImpl convertOperationResultToGui(InvocationResult result,
WSDynamicClient client) {
- WiseTreeElementBuilder builder = new WiseTreeElementBuilder();
+ WiseTreeElementBuilder builder = new WiseTreeElementBuilder(client);
TreeNodeImpl rootElement = new TreeNodeImpl();
for (Entry<String, Object> res : result.getResult().entrySet()) {
Object resObj = res.getValue();
- WiseTreeElement wte = builder.buildTreeFromType(resObj.getClass(), res.getKey(),
client, resObj);
+ WiseTreeElement wte = builder.buildTreeFromType(resObj.getClass(), res.getKey(),
resObj);
rootElement.addChild(wte.getId(), wte);
}
return rootElement;
Added:
webgui/branches/cdi-jsf/src/main/java/org/jboss/wise/gui/treeElement/LazyLoadWiseTreeElement.java
===================================================================
---
webgui/branches/cdi-jsf/src/main/java/org/jboss/wise/gui/treeElement/LazyLoadWiseTreeElement.java
(rev 0)
+++
webgui/branches/cdi-jsf/src/main/java/org/jboss/wise/gui/treeElement/LazyLoadWiseTreeElement.java 2013-01-25
23:23:53 UTC (rev 462)
@@ -0,0 +1,95 @@
+/*
+ * JBoss, Home of Professional Open Source
+ * Copyright 2013, Red Hat, Inc. and/or its affiliates, and individual
+ * contributors by the @authors tag. See the copyright.txt in the
+ * distribution for a full listing of individual contributors.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
http://www.apache.org/licenses/LICENSE-2.0
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.jboss.wise.gui.treeElement;
+
+import java.lang.reflect.Type;
+import java.util.Iterator;
+import java.util.Map;
+
+import org.jboss.wise.core.utils.IDGenerator;
+
+/**
+ * This tree element is for storing lazy load references to other elements;
+ * required because in some uncommon scenario, the parameter trees might
+ * actually be graphs... (the actually passed data won't be a graph, but
+ * the type model can have cycles in it)
+ *
+ * @author Alessio Soldano, alessio.soldano(a)jboss.com
+ */
+public class LazyLoadWiseTreeElement extends WiseTreeElement {
+
+ private static final long serialVersionUID = 1L;
+
+ private boolean resolved = false;
+ protected Map<Type, WiseTreeElement> treeTypesMap;
+
+ private LazyLoadWiseTreeElement() {
+ this.kind = LAZY;
+ this.id = IDGenerator.nextVal();
+ }
+
+ public LazyLoadWiseTreeElement(Type classType, String name, Map<Type,
WiseTreeElement> treeTypesMap) {
+ this.kind = LAZY;
+ this.id = IDGenerator.nextVal();
+ this.classType = classType;
+ this.nil = false;
+ this.name = name;
+ this.treeTypesMap = treeTypesMap;
+ }
+
+ public WiseTreeElement clone() {
+ LazyLoadWiseTreeElement element = new LazyLoadWiseTreeElement();
+ element.setName(this.name);
+ element.setNil(this.nil);
+ element.setClassType(this.classType);
+ element.setRemovable(this.isRemovable());
+ element.setNillable(this.isNillable());
+ element.setResolved(false); //copy into an unresolved element and do not copy child
+ element.setTreeTypesMap(this.treeTypesMap);
+ return element;
+ }
+
+ public void resolveReference() {
+ if (!isResolved()) {
+ WiseTreeElement ref = treeTypesMap.get(this.classType);
+ WiseTreeElement component = (WiseTreeElement) ref.clone();
+ addChild(component.getId(), component);
+ setResolved(true);
+ }
+ }
+
+ public Object toObject() {
+ Iterator<Object> keyIt = this.getChildrenKeysIterator();
+ return keyIt.hasNext() ? ((WiseTreeElement)this.getChild(keyIt.next())).toObject() :
null;
+ }
+
+ public boolean isResolved() {
+ return resolved;
+ }
+
+ public void setResolved(boolean resolved) {
+ this.resolved = resolved;
+ }
+
+ public Map<Type, WiseTreeElement> getTreeTypesMap() {
+ return treeTypesMap;
+ }
+
+ public void setTreeTypesMap(Map<Type, WiseTreeElement> treeTypesMap) {
+ this.treeTypesMap = treeTypesMap;
+ }
+}
Modified:
webgui/branches/cdi-jsf/src/main/java/org/jboss/wise/gui/treeElement/WiseTreeElement.java
===================================================================
---
webgui/branches/cdi-jsf/src/main/java/org/jboss/wise/gui/treeElement/WiseTreeElement.java 2013-01-20
23:27:29 UTC (rev 461)
+++
webgui/branches/cdi-jsf/src/main/java/org/jboss/wise/gui/treeElement/WiseTreeElement.java 2013-01-25
23:23:53 UTC (rev 462)
@@ -38,6 +38,7 @@
public static final String DURATION = "Duration";
public static final String ENUMERATION = "Enumeration";
public static final String GROUP = "group";
+ public static final String LAZY = "lazy";
public static final String PARAMETERIZED = "Parameterized";
public static final String QNAME = "qname";
public static final String ROOT = "root";
@@ -50,7 +51,7 @@
protected boolean nil; //whether this elements has the attribute xsi:nil set to
"true"
protected boolean nillable = true; //for primitives and explicitly not nillable
elements
private boolean removable = false; // to be used on array elements
-
+
protected Type classType;
protected WiseTreeElement() {
Modified:
webgui/branches/cdi-jsf/src/main/java/org/jboss/wise/gui/treeElement/WiseTreeElementBuilder.java
===================================================================
---
webgui/branches/cdi-jsf/src/main/java/org/jboss/wise/gui/treeElement/WiseTreeElementBuilder.java 2013-01-20
23:27:29 UTC (rev 461)
+++
webgui/branches/cdi-jsf/src/main/java/org/jboss/wise/gui/treeElement/WiseTreeElementBuilder.java 2013-01-25
23:23:53 UTC (rev 462)
@@ -21,6 +21,10 @@
import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;
import java.util.Collection;
+import java.util.HashMap;
+import java.util.HashSet;
+import java.util.Map;
+import java.util.Set;
import javax.xml.bind.JAXBElement;
import javax.xml.bind.annotation.XmlElement;
@@ -40,36 +44,56 @@
* @author alessio.soldano(a)jboss.com
*/
public class WiseTreeElementBuilder {
-
- public WiseTreeElement buildTreeFromType(Type type, String name, WSDynamicClient
client) {
- return buildTreeFromType(type, name, client, null, null, null);
+
+ private WSDynamicClient client;
+
+ public WiseTreeElementBuilder(WSDynamicClient client) {
+ this.client = client;
}
- public WiseTreeElement buildTreeFromType(Type type, String name, WSDynamicClient
client, Object obj) {
- return buildTreeFromType(type, name, client, obj, null, null);
+ public WiseTreeElement buildTreeFromType(Type type, String name) {
+ return buildTreeFromType(type, name, null, null, null, new HashMap<Type,
WiseTreeElement>(), new HashSet<Type>());
}
+
+ public WiseTreeElement buildTreeFromType(Type type, String name, Object obj) {
+ return buildTreeFromType(type, name, obj, null, null, new HashMap<Type,
WiseTreeElement>(), new HashSet<Type>());
+ }
- private WiseTreeElement buildTreeFromType(Type type, String name, WSDynamicClient
client, Object obj, Class<?> scope, String namespace) {
+ private WiseTreeElement buildTreeFromType(Type type,
+ String name,
+ Object obj,
+ Class<?> scope,
+ String namespace,
+ Map<Type, WiseTreeElement> typeMap,
+ Set<Type> stack) {
Logger.getLogger(this.getClass()).debug("=> Converting parameter '" +
name + "', type '" + type + "'");
if (type instanceof ParameterizedType) {
Logger.getLogger(this.getClass()).debug("Parameterized type...");
ParameterizedType pt = (ParameterizedType) type;
- return this.buildParameterizedType(pt, name, obj, client, scope, namespace);
+ return this.buildParameterizedType(pt, name, obj, scope, namespace, typeMap,
stack);
} else {
Logger.getLogger(this.getClass()).debug("Not a parameterized type... casting to
Class");
- return this.buildFromClass((Class<?>) type, name, obj, client);
+
+ return this.buildFromClass((Class<?>) type, name, obj, typeMap, stack);
}
}
@SuppressWarnings("rawtypes")
- private WiseTreeElement buildParameterizedType(ParameterizedType pt, String name,
Object obj, WSDynamicClient client, Class<?> scope, String namespace) {
+ private WiseTreeElement buildParameterizedType(ParameterizedType pt,
+ String name,
+ Object obj,
+ Class<?> scope,
+ String namespace,
+ Map<Type, WiseTreeElement> typeMap,
+ Set<Type> stack) {
+ Type firstTypeArg = pt.getActualTypeArguments()[0];
if (Collection.class.isAssignableFrom((Class<?>) pt.getRawType())) {
- WiseTreeElement prototype = this.buildTreeFromType(pt.getActualTypeArguments()[0],
name, client, null);
+ WiseTreeElement prototype = this.buildTreeFromType(firstTypeArg, name, null, null,
null, typeMap, stack);
GroupWiseTreeElement group = new GroupWiseTreeElement(pt, name, prototype);
if (obj != null) {
for (Object o : (Collection) obj) {
- group.addChild(IDGenerator.nextVal(),
this.buildTreeFromType(pt.getActualTypeArguments()[0], name, client, o));
+ group.addChild(IDGenerator.nextVal(), this.buildTreeFromType(firstTypeArg, name, o,
null, null, typeMap, stack));
}
}
return group;
@@ -78,13 +102,17 @@
if (obj != null && obj instanceof JAXBElement) {
obj = ((JAXBElement)obj).getValue();
}
- WiseTreeElement element = this.buildTreeFromType(pt.getActualTypeArguments()[0],
name, client, obj);
+ WiseTreeElement element = this.buildTreeFromType(firstTypeArg, name, obj, null,
null, typeMap, stack);
parameterized.addChild(element.getId(), element);
return parameterized;
}
}
- private WiseTreeElement buildFromClass(Class<?> cl, String name, Object obj,
WSDynamicClient client) {
+ private WiseTreeElement buildFromClass(Class<?> cl,
+ String name,
+ Object obj,
+ Map<Type, WiseTreeElement> typeMap,
+ Set<Type> stack) {
if (cl.isArray()) {
Logger.getLogger(this.getClass()).debug("* array");
@@ -92,15 +120,21 @@
throw new WiseRuntimeException("Converter doesn't support this Object[]
yet.");
}
- if (cl.isEnum() || cl.isPrimitive() || client.getClassLoader() != cl.getClassLoader())
{
+ if (isSimpleType(cl, client)) {
Logger.getLogger(this.getClass()).debug("* simple");
SimpleWiseTreeElement element = SimpleWiseTreeElementFactory.create(cl, name);
element.parseObject(obj);
return element;
} else { // complex
+ if (stack.contains(cl)) {
+ Logger.getLogger(this.getClass()).debug("* lazy");
+ return new LazyLoadWiseTreeElement(cl, name, typeMap);
+ }
+
Logger.getLogger(this.getClass()).debug("* complex");
+
ComplexWiseTreeElement complex = new ComplexWiseTreeElement(cl, name);
-
+ stack.add(cl);
for (Field field : ReflectionUtils.getAllFields(cl)) {
XmlElement elemAnnotation = field.getAnnotation(XmlElement.class);
XmlElementRef refAnnotation = field.getAnnotation(XmlElementRef.class);
@@ -126,10 +160,16 @@
throw new WiseRuntimeException("Error calling getter method for field " +
field, e);
}
}
- WiseTreeElement element = this.buildTreeFromType(field.getGenericType(), fieldName,
client, fieldValue, cl, namespace);
+ WiseTreeElement element = this.buildTreeFromType(field.getGenericType(), fieldName,
fieldValue, cl, namespace, typeMap, stack);
complex.addChild(element.getId(), element);
}
+ stack.remove(cl);
+ typeMap.put(cl, complex);
return complex;
}
}
+
+ private static boolean isSimpleType(Class<?> cl, WSDynamicClient client) {
+ return cl.isEnum() || cl.isPrimitive() || client.getClassLoader() !=
cl.getClassLoader();
+ }
}