Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Code review: pulled up some functionality into AbstractMarshallingSes…

…sion. Reduces several cut-n-pasted code chunks.
  • Loading branch information...
commit fd35809b84f4c2b8d1162655d5a8c983bcee60da 1 parent 7889408
@jfuerth authored
View
24 ...ling/src/main/java/org/jboss/errai/marshalling/client/api/AbstractMarshallingSession.java
@@ -20,13 +20,35 @@
import java.util.IdentityHashMap;
import java.util.Map;
+import org.jboss.errai.common.client.framework.Assert;
+
/**
* @author Mike Brock
*/
public abstract class AbstractMarshallingSession implements MarshallingSession {
+ private final MappingContext context;
+
private Map<Object, Integer> objects = new IdentityHashMap<Object, Integer>();
private Map<String, Object> objectMap = new HashMap<String, Object>();
+ protected AbstractMarshallingSession(MappingContext context) {
+ this.context = Assert.notNull(context);
+ }
+
+ @Override
+ public Marshaller<Object> getMarshallerInstance(String fqcn) {
+ Marshaller<Object> marshaller = context.getMarshaller(fqcn);
+ if (marshaller == null) {
+ throw new IllegalArgumentException("No marshaller for " + fqcn);
+ }
+ return marshaller;
+ }
+
+ @Override
+ public MappingContext getMappingContext() {
+ return context;
+ }
+
@Override
public boolean hasObjectHash(String hashCode) {
return objectMap.containsKey(hashCode);
@@ -58,7 +80,7 @@ public boolean isEncoded(Object ref) {
public String getObjectHash(Object o) {
Integer i = objects.get(o);
String s;
-
+
if (i == null) {
objects.put(o, (i = objects.size() + 1));
recordObjectHash(s = i.toString(), o);
View
83 ...marshalling/src/main/java/org/jboss/errai/marshalling/client/api/MarshallerFramework.java
@@ -16,9 +16,9 @@
package org.jboss.errai.marshalling.client.api;
-import com.google.gwt.core.client.EntryPoint;
-import com.google.gwt.core.client.GWT;
-import com.google.gwt.json.client.JSONParser;
+import java.util.List;
+import java.util.Map;
+
import org.jboss.errai.common.client.api.extension.InitVotes;
import org.jboss.errai.common.client.protocols.SerializationParts;
import org.jboss.errai.marshalling.client.MarshallingSessionProviderFactory;
@@ -27,15 +27,15 @@
import org.jboss.errai.marshalling.client.api.json.impl.gwt.GWTJSON;
import org.jboss.errai.marshalling.client.protocols.MarshallingSessionProvider;
-import java.util.List;
-import java.util.Map;
+import com.google.gwt.core.client.EntryPoint;
+import com.google.gwt.core.client.GWT;
+import com.google.gwt.json.client.JSONParser;
/**
* @author Mike Brock <cbrock@redhat.com>
*/
public class MarshallerFramework implements EntryPoint {
private static MarshallerFactory marshallerFactory;
- private static boolean automarshal = true;
static {
InitVotes.waitFor(MarshallerFramework.class);
@@ -85,57 +85,24 @@ public Marshaller getMarshaller(String fqcn) {
public static class JSONMarshallingSession extends AbstractMarshallingSession {
- private static final MappingContext mappingContext = new MappingContext() {
- @Override
- public Marshaller<Object> getMarshaller(String clazz) {
- return marshallerFactory.getMarshaller("json", clazz);
- }
-
- @Override
- public boolean hasMarshaller(String clazzName) {
- return marshallerFactory.getMarshaller(clazzName, "json") != null;
- }
-
- @Override
- public boolean canMarshal(String cls) {
- return marshallerFactory.getMarshaller("json", cls) != null;
- }
-
- };
+ public JSONMarshallingSession() {
+ super(new MappingContext() {
+ @Override
+ public Marshaller<Object> getMarshaller(String clazz) {
+ return marshallerFactory.getMarshaller("json", clazz);
+ }
- @Override
- public MappingContext getMappingContext() {
- return mappingContext;
- }
+ @Override
+ public boolean hasMarshaller(String clazzName) {
+ return marshallerFactory.getMarshaller(clazzName, "json") != null;
+ }
- @Override
- public Marshaller<Object> getMarshallerInstance(String fqcn) {
- if (automarshal) {
- return marshallerFactory.getMarshaller(null, fqcn);
- }
- else {
- Marshaller<Object> m = marshallerFactory.getMarshaller(null, fqcn);
- if (m == null) {
- m = new Marshaller<Object>() {
- @Override
- public Class<Object> getTypeHandled() {
- return Object.class;
- }
-
- @Override
- public Object demarshall(EJValue o, MarshallingSession ctx) {
- return o.getRawValue().toString();
- }
-
- @Override
- public String marshall(Object o, MarshallingSession ctx) {
- return "\"MarshallingNotSupported\"";
- }
-
- };
+ @Override
+ public boolean canMarshal(String cls) {
+ return marshallerFactory.getMarshaller("json", cls) != null;
}
- return m;
- }
+
+ });
}
@Override
@@ -170,14 +137,6 @@ else if (jsonValue.isNull()) {
}
}
- public static void setAutomarshal(boolean auto) {
- automarshal = auto;
- }
-
- public static boolean isAutomarshal() {
- return automarshal;
- }
-
public static MarshallerFactory getMarshallerFactory() {
return marshallerFactory;
}
View
13 ...halling/src/main/java/org/jboss/errai/marshalling/rebind/api/GeneratorMappingContext.java
@@ -16,6 +16,11 @@
package org.jboss.errai.marshalling.rebind.api;
+import java.util.ArrayList;
+import java.util.HashSet;
+import java.util.List;
+import java.util.Set;
+
import org.jboss.errai.codegen.Context;
import org.jboss.errai.codegen.builder.ClassStructureBuilder;
import org.jboss.errai.codegen.meta.MetaClass;
@@ -28,11 +33,6 @@
import org.jboss.errai.marshalling.rebind.DefinitionsFactorySingleton;
import org.jboss.errai.marshalling.server.ServerMappingContext;
-import java.util.ArrayList;
-import java.util.HashSet;
-import java.util.List;
-import java.util.Set;
-
/**
* @author Mike Brock <cbrock@redhat.com>
*/
@@ -71,7 +71,7 @@ public void registerGeneratedMarshaller(String clazzName) {
generatedMarshallers.add(clazzName);
}
-
+ @Override
public boolean hasMarshaller(String clazzName) {
return definitionsFactory.hasDefinition(clazzName);
}
@@ -85,6 +85,7 @@ private boolean hasGeneratedMarshaller(String clazzName) {
return generatedMarshallers.contains(clazzName);
}
+ @Override
public boolean canMarshal(String clazz) {
return hasMarshaller(clazz) || hasGeneratedMarshaller(clazz);
}
View
26 errai-marshalling/src/main/java/org/jboss/errai/marshalling/server/DecodingSession.java
@@ -1,38 +1,20 @@
package org.jboss.errai.marshalling.server;
+import java.util.List;
+import java.util.Map;
+
import org.jboss.errai.common.client.protocols.SerializationParts;
import org.jboss.errai.marshalling.client.api.AbstractMarshallingSession;
-import org.jboss.errai.marshalling.client.api.Marshaller;
import org.jboss.errai.marshalling.client.api.json.EJObject;
import org.jboss.errai.marshalling.client.api.json.EJValue;
-import org.jboss.errai.marshalling.rebind.api.model.MappingDefinition;
-
-import java.util.List;
-import java.util.Map;
/**
* @author Mike Brock <cbrock@redhat.com>
*/
public class DecodingSession extends AbstractMarshallingSession {
- private final ServerMappingContext context;
public DecodingSession(ServerMappingContext context) {
- this.context = context;
- }
-
- @Override
- public ServerMappingContext getMappingContext() {
- return context;
- }
-
- @Override
- public Marshaller<Object> getMarshallerInstance(String fqcn) {
- final Marshaller<Object> m = context.getMarshaller(fqcn);
- if (m == null) {
- throw new RuntimeException("no marshalling definition available for type:" + fqcn);
- }
-
- return m;
+ super(context);
}
@Override
View
27 errai-marshalling/src/main/java/org/jboss/errai/marshalling/server/EncodingSession.java
@@ -6,7 +6,7 @@
* 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.
@@ -16,23 +16,18 @@
package org.jboss.errai.marshalling.server;
+import java.util.Map;
+
import org.jboss.errai.common.client.protocols.SerializationParts;
import org.jboss.errai.marshalling.client.api.AbstractMarshallingSession;
-import org.jboss.errai.marshalling.client.api.Marshaller;
-import org.jboss.errai.marshalling.client.api.exceptions.MarshallingException;
-import org.jboss.errai.marshalling.client.api.json.EJValue;
-import org.jboss.errai.marshalling.rebind.api.model.MappingDefinition;
-
-import java.util.Map;
/**
* @author Mike Brock
*/
public class EncodingSession extends AbstractMarshallingSession {
- private final ServerMappingContext context;
public EncodingSession(ServerMappingContext context) {
- this.context = context;
+ super(context);
}
@Override
@@ -52,18 +47,4 @@ public String determineTypeFor(String formatType, Object o) {
return o.getClass().getName();
}
}
-
- @Override
- public Marshaller<Object> getMarshallerInstance(String fqcn) {
- final Marshaller<Object> m = context.getMarshaller(fqcn);
- if (m == null) {
- throw new RuntimeException("no marshaller available for: " + fqcn);
- }
- return m;
- }
-
- @Override
- public ServerMappingContext getMappingContext() {
- return context;
- }
}
Please sign in to comment.
Something went wrong with that request. Please try again.