Skip to content

Commit

Permalink
WELD-2014 - Change Utils deserialize method to use TCCLObjectInputStr…
Browse files Browse the repository at this point in the history
…eam.
  • Loading branch information
tremes authored and jharting committed Aug 21, 2015
1 parent 02a060e commit bc77bfb
Showing 1 changed file with 28 additions and 14 deletions.
42 changes: 28 additions & 14 deletions tests-common/src/main/java/org/jboss/weld/test/util/Utils.java
Expand Up @@ -19,6 +19,7 @@
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.ObjectStreamClass;
Expand All @@ -45,7 +46,6 @@
import org.jboss.weld.util.collections.EnumerationList;
import org.jboss.weld.util.reflection.Reflections;


public class Utils {

private Utils() {
Expand Down Expand Up @@ -98,18 +98,9 @@ public static byte[] serialize(Object instance) throws IOException {
}

public static <T> T deserialize(byte[] bytes) throws IOException, ClassNotFoundException {
ObjectInputStream in = new ObjectInputStream(new ByteArrayInputStream(bytes)) {
@Override
protected Class<?> resolveClass(ObjectStreamClass desc) throws IOException, ClassNotFoundException {
try {
return super.resolveClass(desc);
} catch (ClassNotFoundException e) {
// if a weld-internal class is not available (JBoss AS 7) let's use the weld classloader to load them
return BeanManagerImpl.class.getClassLoader().loadClass(desc.getName());
}
}
};
return Reflections.<T>cast(in.readObject());
try (TCCLObjectInputStream in = new TCCLObjectInputStream(new ByteArrayInputStream(bytes))) {
return Reflections.<T>cast(in.readObject());
}
}

public static boolean isExceptionInHierarchy(Throwable exception, Class<? extends Throwable> expectedException) {
Expand Down Expand Up @@ -170,7 +161,6 @@ public static boolean isProxy(Object proxy) {
return proxy instanceof ProxyObject;
}


public static <T extends Context> T getActiveContext(WeldManager beanManager, Class<T> type) {
for (T context : beanManager.instance().select(type)) {
if (context.isActive()) {
Expand All @@ -180,4 +170,28 @@ public static <T extends Context> T getActiveContext(WeldManager beanManager, Cl
throw new ContextNotActiveException();
}

private static class TCCLObjectInputStream extends ObjectInputStream {

private final ClassLoader classLoader;

public TCCLObjectInputStream(InputStream in) throws IOException {
super(in);
this.classLoader = Thread.currentThread().getContextClassLoader();
}

@Override
protected Class<?> resolveClass(ObjectStreamClass desc) throws IOException, ClassNotFoundException {
try {
String name = desc.getName();
return Class.forName(name, false, classLoader);
} catch (ClassNotFoundException e) {
try {
return super.resolveClass(desc);
} catch (ClassNotFoundException e1) {
return BeanManagerImpl.class.getClassLoader().loadClass(desc.getName());
}
}
}
}

}

0 comments on commit bc77bfb

Please sign in to comment.