01: /**
02: *
03: */package org.drools.common;
04:
05: import java.io.IOException;
06: import java.io.InputStream;
07: import java.io.ObjectInputStream;
08: import java.io.ObjectStreamClass;
09: import java.util.HashMap;
10:
11: public class DroolsObjectInputStream extends ObjectInputStream {
12: private final ClassLoader classLoader;
13: private InternalRuleBase ruleBase;
14: private InternalWorkingMemory workingMemory;
15:
16: /** table mapping primitive type names to corresponding class objects */
17: private static final HashMap primClasses = new HashMap(8, 1.0F);
18: static {
19: primClasses.put("boolean", boolean.class);
20: primClasses.put("byte", byte.class);
21: primClasses.put("char", char.class);
22: primClasses.put("short", short.class);
23: primClasses.put("int", int.class);
24: primClasses.put("long", long.class);
25: primClasses.put("float", float.class);
26: primClasses.put("double", double.class);
27: primClasses.put("void", void.class);
28: }
29:
30: public DroolsObjectInputStream(final InputStream in)
31: throws IOException {
32: this (in, null);
33: }
34:
35: public DroolsObjectInputStream(final InputStream in,
36: ClassLoader classLoader) throws IOException {
37: super (in);
38: if (classLoader == null) {
39: classLoader = Thread.currentThread()
40: .getContextClassLoader();
41: if (classLoader == null) {
42: classLoader = this .getClass().getClassLoader();
43: }
44: }
45:
46: this .classLoader = classLoader;
47: enableResolveObject(true);
48: }
49:
50: public ClassLoader getClassLoader() {
51: return this .classLoader;
52: }
53:
54: protected Class resolveClass(final ObjectStreamClass desc)
55: throws IOException, ClassNotFoundException {
56: if (this .classLoader == null) {
57: return super .resolveClass(desc);
58: } else {
59: final String name = desc.getName();
60: Class clazz = (Class) primClasses.get(name);
61: if (clazz == null) {
62: try {
63: clazz = this .classLoader.loadClass(name);
64: } catch (final ClassNotFoundException cnf) {
65: clazz = super .resolveClass(desc);
66: }
67: }
68: return clazz;
69: }
70: }
71:
72: public InternalRuleBase getRuleBase() {
73: return ruleBase;
74: }
75:
76: public void setRuleBase(InternalRuleBase ruleBase) {
77: this .ruleBase = ruleBase;
78: }
79:
80: public void setWorkingMemory(InternalWorkingMemory workingMemory) {
81: this .workingMemory = workingMemory;
82: }
83:
84: public InternalWorkingMemory getWorkingMemory() {
85: return workingMemory;
86: }
87:
88: }
|