comparison src/org/dancres/io/AnnotatingObjectInputStream.java @ 0:3dc0c5604566

Initial checkin of blitz 2.0 fcs - no installer yet.
author Dan Creswell <dan.creswell@gmail.com>
date Sat, 21 Mar 2009 11:00:06 +0000
parents
children
comparison
equal deleted inserted replaced
-1:000000000000 0:3dc0c5604566
1 package org.dancres.io;
2
3 import java.io.IOException;
4 import java.io.ObjectInputStream;
5 import java.io.InputStream;
6 import java.io.ObjectStreamClass;
7
8 import java.util.Map;
9 import java.util.HashMap;
10 import java.util.Collection;
11 import java.util.Collections;
12 import java.util.logging.Logger;
13 import java.util.logging.Level;
14
15 import net.jini.loader.ClassLoading;
16
17 import net.jini.io.ObjectStreamContext;
18 import net.jini.io.context.IntegrityEnforcement;
19
20 /**
21 Takes a stream of codebase annotations and a second stream of object data
22 and combines them to produce de-serialized objects. Object verification
23 can be optionally enabled at construction time.
24 */
25 public class AnnotatingObjectInputStream
26 extends ObjectInputStream implements ObjectStreamContext {
27
28 private static final Logger theLogger =
29 Logger.getLogger("org.dancres.io.AnnotatingObjectInputStream");
30
31 private ObjectInputStream theAnnotations;
32 private ClassLoader theLoader;
33
34 private Collection theContext;
35
36 private boolean requireIntegrityCheck;
37
38 private static final Map thePrimitiveClasses = new HashMap();
39 static {
40 thePrimitiveClasses.put("boolean", boolean.class);
41 thePrimitiveClasses.put("byte", byte.class);
42 thePrimitiveClasses.put("char", char.class);
43 thePrimitiveClasses.put("short", short.class);
44 thePrimitiveClasses.put("int", int.class);
45 thePrimitiveClasses.put("long", long.class);
46 thePrimitiveClasses.put("float", float.class);
47 thePrimitiveClasses.put("double", double.class);
48 thePrimitiveClasses.put("void", void.class);
49 }
50
51 public AnnotatingObjectInputStream(ClassLoader aLoader,
52 InputStream anObjStream,
53 InputStream anAnnStream,
54 final boolean checkIntegrity)
55 throws IOException {
56
57 super(anObjStream);
58 theAnnotations = new ObjectInputStream(anAnnStream);
59 theLoader = aLoader;
60 requireIntegrityCheck = checkIntegrity;
61
62 theContext =
63 Collections.singleton(
64 new IntegrityEnforcement() {
65 public boolean integrityEnforced() {
66 return checkIntegrity;
67 }
68 }
69 );
70 }
71
72 public Collection getObjectStreamContext() {
73 return theContext;
74 }
75
76 private String getAnnotation() throws IOException, ClassNotFoundException {
77 return (String) theAnnotations.readObject();
78 }
79
80 protected Class resolveClass(ObjectStreamClass aClassDesc)
81 throws IOException, ClassNotFoundException {
82
83 String myAnnotation = getAnnotation();
84 String myClassName = aClassDesc.getName();
85
86 Class myClass = (Class) thePrimitiveClasses.get(myClassName);
87
88 if (myClass != null)
89 return myClass;
90
91 if (theLogger.isLoggable(Level.FINEST)) {
92 theLogger.log(Level.FINEST, "Attempting to load " + myClassName +
93 ".class from " + myAnnotation);
94 }
95
96 return ClassLoading.loadClass(myAnnotation, myClassName, theLoader,
97 requireIntegrityCheck, null);
98 }
99
100 protected Class resolveProxyClass(String[] aListOfInterfaces)
101 throws IOException, ClassNotFoundException {
102
103 String myAnnotation = getAnnotation();
104
105 return ClassLoading.loadProxyClass(myAnnotation, aListOfInterfaces,
106 theLoader, requireIntegrityCheck,
107 null);
108 }
109
110 public void close() throws IOException {
111 super.close();
112 theAnnotations.close();
113 }
114 }