KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > tc > aspectwerkz > hook > impl > StdoutPreProcessor


1 /*
2  * All content copyright (c) 2003-2006 Terracotta, Inc., except as may otherwise be noted in a separate copyright notice. All rights reserved.
3  */

4 package com.tc.aspectwerkz.hook.impl;
5
6 import com.tc.aspectwerkz.hook.ClassPreProcessor;
7
8 import java.io.File JavaDoc;
9 import java.io.IOException JavaDoc;
10 import java.net.MalformedURLException JavaDoc;
11 import java.net.URL JavaDoc;
12 import java.util.ArrayList JavaDoc;
13 import java.util.Collections JavaDoc;
14 import java.util.Enumeration JavaDoc;
15 import java.util.Iterator JavaDoc;
16 import java.util.List JavaDoc;
17 import java.util.Map JavaDoc;
18 import java.util.WeakHashMap JavaDoc;
19
20 /**
21  * A simple implementation of class preprocessor. <p/>It does not modify the bytecode. It just prints on stdout some
22  * messages.
23  *
24  * @author <a HREF="mailto:alex@gnilux.com">Alexandre Vasseur </a>
25  */

26 public class StdoutPreProcessor implements ClassPreProcessor {
27   /**
28    * Classloaders repository, based on a synchronized weak hashmap key = classloader value = List of URL[]
29    * representing the local search path for .class files of the classloader value is completed at each class loading
30    */

31   private static Map JavaDoc classloaders;
32
33   /**
34    * ms interval betwee classloader hierarchy printing
35    */

36   private static final long stepms = 15000;
37
38   private static transient long lastPrinted = 0;
39
40   private void log(String JavaDoc s) {
41     System.out.println(Thread.currentThread().getName() + ": StdoutPreProcessor: " + s);
42   }
43
44   public void initialize() {
45     log("initialize");
46     log("loaded by " + this.getClass().getClassLoader());
47     classloaders = Collections.synchronizedMap(new WeakHashMap JavaDoc());
48
49     // register the classloader - except bootstrapCL
50
registerClassLoader(this.getClass().getClassLoader(), this.getClass().getName());
51   }
52
53   public byte[] preProcess(String JavaDoc klass, byte[] abyte, ClassLoader JavaDoc caller) {
54     // emulate a -verbose:class mode
55
klass = klass.replace('.', '/') + ".class";
56     URL JavaDoc u = caller.getResource(klass);
57     log("> " + klass + " [" + ((u == null) ? "?" : u.toString()) + "] [" + caller + "]");
58
59     /*
60     * URL uRoot = null; if (u!=null) { // u =
61     * jar:file:/C:/bea/weblogic81/server/lib/weblogic.jar!/weblogic/t3/srvr/T3Srvr.class // getPath =
62     * file:/C:/bea/weblogic81/server/lib/weblogic.jar!/weblogic/t3/srvr/T3Srvr.class // getFile =
63     * file:/C:/bea/weblogic81/server/lib/weblogic.jar!/weblogic/t3/srvr/T3Srvr.class int i =
64     * u.toString().indexOf('!'); if (i > 0) { try { uRoot = new URL(u.toString().substring(0, i)+"!/"); } catch
65     * (MalformedURLException e) { ; } } }
66     */

67     // register the classloader
68
registerClassLoader(caller, klass);
69
70     // complete the url path of the classloader
71
registerSearchPath(caller, klass);
72
73     // dump the hierarchy if needed
74
if (System.currentTimeMillis() > (lastPrinted + stepms)) {
75       lastPrinted = System.currentTimeMillis();
76       log("*******************************");
77       log("size=" + classloaders.size());
78       dumpHierarchy(null, "");
79       log("*******************************");
80     }
81     return abyte;
82   }
83
84   /**
85    * Register a weak reference on the classloader Looks for META-INF/manifest.mf resource and log a line
86    *
87    * @param loader
88    * @param firstClassLoaded
89    */

90   private void registerClassLoader(ClassLoader JavaDoc loader, String JavaDoc firstClassLoaded) {
91     if (loader != null) {
92       if (!classloaders.containsKey(loader)) {
93         // register the loader and the parent hierarchy if not already registered
94
registerClassLoader(loader.getParent(), loader.getClass().getName());
95         registerSearchPath(loader.getParent(), loader.getClass().getName());
96         URL JavaDoc u = null;
97
98         // *** THIS IS NOT WORKING for other than .class files
99
// *** since manifest.mf can be several time
100
// *** but getResource follow parent first delegation model
101
// try to locate the first META-INF/manifest.mf (should be the aw.xml)
102
// case sensitive of META-INF, meta-inf, Meta-Inf, Meta-inf ? YES IT IS
103
//u = loader.getResource("META-INF/MANIFEST.MF");
104
// *** THIS could be enough for early registration
105
// add resources in some struct, allow multiple deploy of same resource
106
// if in parallel CL hierarchy - got it ?
107
try {
108           //@todo - case sensitive stuff is dangerous
109
// we could merge all aw.xml in META-INF and WEB-INF and meta-inf ...
110
Enumeration JavaDoc ue = loader.getResources("META-INF/MANIFEST.MF");
111           if (ue.hasMoreElements()) {
112             log("--- in scope for " + loader);
113           }
114           while (ue.hasMoreElements()) {
115             log("--- " + ue.nextElement().toString());
116           }
117         } catch (IOException JavaDoc e) {
118           ;
119         }
120
121         // register this loader
122
log("****" + loader + " [" + ((u == null) ? "?" : u.toString()) + "] [" + firstClassLoaded + ']');
123         classloaders.put(loader, new ArrayList JavaDoc());
124       }
125
126       // register search path based on firstClassLoaded
127
}
128   }
129
130   /**
131    * Dumps on stdout the registered classloader hierarchy child of "parent" Using the depth to track recursivity level
132    */

133   private void dumpHierarchy(ClassLoader JavaDoc parent, String JavaDoc depth) {
134     // do a copy of the registered CL to allow access on classloaders structure
135
List JavaDoc cl = new ArrayList JavaDoc(classloaders.keySet());
136     ClassLoader JavaDoc current = null;
137     for (Iterator JavaDoc i = cl.iterator(); i.hasNext();) {
138       current = (ClassLoader JavaDoc) i.next();
139       if (current.getParent() == parent) {
140         log(depth + current + '[' + classloaders.get(current));
141
142         // handcheck for duplicate path (?)
143
List JavaDoc path = (List JavaDoc) classloaders.get(current);
144         ClassLoader JavaDoc currentParent = current.getParent();
145         while (currentParent != null) {
146           for (Iterator JavaDoc us = path.iterator(); us.hasNext();) {
147             URL JavaDoc u = (URL JavaDoc) us.next();
148             if (((List JavaDoc) classloaders.get(currentParent)).contains(u)) {
149               log("!!!! duplicate detected for " + u + " in " + current);
150             }
151           }
152           currentParent = currentParent.getParent();
153         }
154         dumpHierarchy(current, depth + " ");
155       }
156     }
157   }
158
159   private void registerSearchPath(final ClassLoader JavaDoc loader, final String JavaDoc klass) {
160     // bootCL
161
if (loader == null) {
162       return;
163     }
164
165     // locate the klass
166
String JavaDoc klassFile = klass.replace('.', '/') + ".class";
167     URL JavaDoc uKlass = loader.getResource(klassFile);
168     if (uKlass == null) {
169       return;
170     }
171
172     // retrieve the location root (jar/zip or directory)
173
URL JavaDoc uRoot = null;
174     int i = uKlass.toString().indexOf('!');
175     if (i > 0) {
176       // jar/zip
177
try {
178         // remove the jar: zip: prefix
179
//@todo !! zip: seems to be BEA specific
180
uRoot = (new File JavaDoc(uKlass.toString().substring(4, i))).getCanonicalFile().toURL();
181
182         //uRoot = new URL(uKlass.toString().substring(0, i)+"!/");
183
} catch (MalformedURLException JavaDoc e) {
184         e.printStackTrace();
185         return;
186       } catch (IOException JavaDoc e2) {
187         e2.printStackTrace();
188         return;
189       }
190     } else {
191       // directory
192
i = uKlass.toString().indexOf(klassFile);
193       try {
194         uRoot = (new File JavaDoc(uKlass.toString().substring(0, i))).getCanonicalFile().toURL();
195       } catch (MalformedURLException JavaDoc e) {
196         e.printStackTrace();
197         return;
198       } catch (IOException JavaDoc e2) {
199         e2.printStackTrace();
200         return;
201       }
202     }
203
204     // check if the location is not in a parent
205
ClassLoader JavaDoc parent = loader.getParent();
206     while (parent != null) {
207       if (((List JavaDoc) classloaders.get(parent)).contains(uRoot)) {
208         return;
209       }
210       parent = parent.getParent();
211     }
212
213     // add the location if not already registered
214
// @todo !! not thread safe
215
List JavaDoc path = (List JavaDoc) classloaders.get(loader);
216     if (!path.contains(uRoot)) {
217       log("adding path " + uRoot + " to " + loader);
218       path.add(uRoot);
219     }
220   }
221
222   public static void main(String JavaDoc[] args) throws Exception JavaDoc {
223     URL JavaDoc u = new URL JavaDoc(
224             "jar:file:/C:/bea/user_projects/domains/mydomain/myserver/.wlnotdelete/gallery/gallery-rar.jar!/"
225     );
226
227     // differ from a "/./"
228
URL JavaDoc u2 = new URL JavaDoc(
229             "jar:file:/C:/bea/user_projects/domains/mydomain/./myserver/.wlnotdelete/gallery/gallery-rar.jar!/"
230     );
231     if (u.sameFile(u2)) {
232       System.out.println("same");
233     } else {
234       System.out.println("differ");
235     }
236   }
237 }
Popular Tags