KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > db4o > nativequery > main > Db4oEnhancingClassloader


1 /* Copyright (C) 2004 - 2006 db4objects Inc. http://www.db4o.com
2
3 This file is part of the db4o open source object database.
4
5 db4o is free software; you can redistribute it and/or modify it under
6 the terms of version 2 of the GNU General Public License as published
7 by the Free Software Foundation and as clarified by db4objects' GPL
8 interpretation policy, available at
9 http://www.db4o.com/about/company/legalpolicies/gplinterpretation/
10 Alternatively you can write to db4objects, Inc., 1900 S Norfolk Street,
11 Suite 350, San Mateo, CA 94403, USA.
12
13 db4o is distributed in the hope that it will be useful, but WITHOUT ANY
14 WARRANTY; without even the implied warranty of MERCHANTABILITY or
15 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
16 for more details.
17
18 You should have received a copy of the GNU General Public License along
19 with this program; if not, write to the Free Software Foundation, Inc.,
20 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */

21 package com.db4o.nativequery.main;
22
23 import java.io.*;
24 import java.net.*;
25 import java.util.*;
26
27 import EDU.purdue.cs.bloat.context.*;
28 import EDU.purdue.cs.bloat.editor.*;
29 import EDU.purdue.cs.bloat.file.*;
30 import EDU.purdue.cs.bloat.reflect.*;
31
32 import com.db4o.nativequery.bloat.*;
33 import com.db4o.nativequery.optimization.*;
34 import com.db4o.query.*;
35
36 // TODO: implement 'good guy'/'bad guy' mode switch for delegation?
37
public class Db4oEnhancingClassloader extends BloatingClassLoader {
38     private NativeQueryEnhancer enhancer=new NativeQueryEnhancer();
39     private Map cache=new HashMap();
40     private BloatUtil bloatUtil=new BloatUtil(new ClassFileLoader());
41     
42     public Db4oEnhancingClassloader(ClassLoader JavaDoc delegate) {
43         super(new URL[]{},delegate);
44     }
45
46     protected synchronized Class JavaDoc loadClass(String JavaDoc name, boolean resolve) throws ClassNotFoundException JavaDoc {
47         if(cache.containsKey(name)) {
48             return (Class JavaDoc)cache.get(name);
49         }
50         Class JavaDoc delegateClass = super.loadClass(name,resolve);
51         if(mustDelegate(name)) {
52             return delegateClass;
53         }
54         Class JavaDoc clazz=(Predicate.class.isAssignableFrom(delegateClass) ? findClass(name) : findRawClass(name));
55         cache.put(clazz.getName(),clazz);
56         if(resolve) {
57             resolveClass(clazz);
58         }
59         return clazz;
60     }
61
62     private Class JavaDoc findRawClass(String JavaDoc className) throws ClassNotFoundException JavaDoc {
63         try {
64             String JavaDoc resourcePath=className.replace('.','/')+".class";
65             InputStream resourceStream = getResourceAsStream(resourcePath);
66             ByteArrayOutputStream rawByteStream=new ByteArrayOutputStream();
67             byte[] buf=new byte[4096];
68             int bytesread=0;
69             while((bytesread=resourceStream.read(buf))>=0) {
70                 rawByteStream.write(buf,0,bytesread);
71             }
72             resourceStream.close();
73             byte[] rawBytes=rawByteStream.toByteArray();
74             return super.defineClass(className, rawBytes, 0, rawBytes.length);
75         } catch (Exception JavaDoc exc) {
76             throw new ClassNotFoundException JavaDoc(className,exc);
77         }
78     }
79
80     private boolean mustDelegate(String JavaDoc name) {
81         return name.startsWith("java.")||name.startsWith("javax.")||name.startsWith("sun.")||(name.startsWith("com.db4o.")&&!name.startsWith("com.db4o.test."));
82     }
83
84     protected void bloat(ClassEditor ce) {
85         try {
86             Type type=ce.superclass();
87             while(type!=null) {
88                 if(type.className().equals(Predicate.class.getName().replace('.','/'))) {
89                     enhancer.enhance(bloatUtil,ce,Predicate.PREDICATEMETHOD_NAME,this.getParent());
90                     return;
91                 }
92                 ClassInfo classInfo=getClassInfoLoader().loadClass(type.className());
93                 type=new ClassEditor(new CachingBloatContext(getClassInfoLoader(),new ArrayList(),false),classInfo).superclass();
94             }
95             //System.err.println("Bypassing "+ce.name());
96
} catch (Exception JavaDoc exc) {
97             throw new RuntimeException JavaDoc(exc.getMessage());
98         }
99     }
100 }
101
Popular Tags