comparison src/org/dancres/io/AnnotatingObjectOutputStream.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.ObjectOutputStream;
5 import java.io.OutputStream;
6
7 import java.rmi.server.RMIClassLoader;
8
9 import java.util.WeakHashMap;
10 import java.util.logging.Logger;
11 import java.util.logging.Level;
12
13 /**
14 Serializes a set of objects, separting annotations into one stream and
15 object data into another. This is useful in various situations including
16 equality checking where one may not wish to include the codebase
17 annotations.
18 */
19 public class AnnotatingObjectOutputStream extends ObjectOutputStream {
20 private static final Logger theLogger =
21 Logger.getLogger("org.dancres.io.AnnotatingObjectOutputStream");
22
23 private static final WeakHashMap theCachedCodebases = new WeakHashMap();
24
25 private ObjectOutputStream theAnnotations;
26
27 public AnnotatingObjectOutputStream(OutputStream anObjStream,
28 OutputStream anAnnStream)
29 throws IOException {
30
31 super(anObjStream);
32 theAnnotations = new ObjectOutputStream(anAnnStream);
33 }
34
35 protected void annotateClass(Class aClass) throws IOException {
36 // addAnnotation(RMIClassLoader.getClassAnnotation(aClass));
37 if (theLogger.isLoggable(Level.FINEST)) {
38 theLogger.log(Level.FINEST, "Annotating " + aClass.getName() +
39 ".class with " + getCodebase(aClass));
40 }
41
42 addAnnotation(getCodebase(aClass));
43 }
44
45 protected void annotateProxyClass(Class aClass) throws IOException {
46 // addAnnotation(RMIClassLoader.getClassAnnotation(aClass));
47 if (theLogger.isLoggable(Level.FINEST)) {
48 theLogger.log(Level.FINEST, "Annotating proxy " + aClass.getName() +
49 ".class with " + getCodebase(aClass));
50 }
51
52 addAnnotation(getCodebase(aClass));
53 }
54
55 private String getCodebase(Class aClass) {
56 CodebaseHolder myCodebase;
57
58 synchronized(theCachedCodebases) {
59 myCodebase =
60 (CodebaseHolder) theCachedCodebases.get(aClass);
61
62 if (myCodebase == null) {
63 myCodebase =
64 new CodebaseHolder(RMIClassLoader.getClassAnnotation(aClass));
65 theCachedCodebases.put(aClass, myCodebase);
66 }
67 }
68
69 return myCodebase.getCodebase();
70 }
71
72 private void addAnnotation(String anAnnotation) throws IOException {
73 theAnnotations.writeObject(anAnnotation);
74 }
75
76 public void flush() throws IOException {
77 super.flush();
78 theAnnotations.flush();
79 }
80
81 public void close() throws IOException {
82 super.close();
83 theAnnotations.close();
84 }
85
86 private static class CodebaseHolder {
87 private String theCodebase;
88
89 CodebaseHolder(String aCodebase) {
90 theCodebase = aCodebase;
91 }
92
93 public String getCodebase() {
94 return theCodebase;
95 }
96 }
97 }