KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > aop > deployment > JBossClassPool


1 /*
2 * JBoss, Home of Professional Open Source
3 * Copyright 2005, JBoss Inc., and individual contributors as indicated
4 * by the @authors tag. See the copyright.txt in the distribution for a
5 * full listing of individual contributors.
6 *
7 * This is free software; you can redistribute it and/or modify it
8 * under the terms of the GNU Lesser General Public License as
9 * published by the Free Software Foundation; either version 2.1 of
10 * the License, or (at your option) any later version.
11 *
12 * This software is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
16 *
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this software; if not, write to the Free
19 * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
20 * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
21 */

22 package org.jboss.aop.deployment;
23
24 import java.io.File JavaDoc;
25 import java.io.FileOutputStream JavaDoc;
26 import java.net.URL JavaDoc;
27 import java.security.ProtectionDomain JavaDoc;
28
29 import org.jboss.aop.classpool.AOPClassPool;
30 import org.jboss.mx.loading.RepositoryClassLoader;
31 import javassist.CannotCompileException;
32 import javassist.ClassPool;
33 import javassist.CtClass;
34 import javassist.scopedpool.ScopedClassPoolRepository;
35
36 /**
37  * Comment
38  *
39  * @author <a HREF="mailto:bill@jboss.org">Bill Burke</a>
40  * @version $Revision: 56777 $
41  */

42 public class JBossClassPool extends AOPClassPool
43 {
44    /**
45     * Used for dynamically created classes (see loadClass(String, byte[]), ClassLoader)
46     */

47    protected File JavaDoc tempdir = null;
48    protected URL JavaDoc tempURL = null;
49    // For loadClass tmpdir creation for UCL
50
protected final Object JavaDoc tmplock = new Object JavaDoc();
51    
52    protected JBossClassPool(ClassLoader JavaDoc cl, ClassPool src, ScopedClassPoolRepository repository, File JavaDoc tmp, URL JavaDoc tmpURL)
53    {
54       super(cl, src, repository);
55       tempdir = tmp;
56       tempURL = tmpURL;
57    }
58
59    protected JBossClassPool(ClassPool src, ScopedClassPoolRepository repository)
60    {
61       super(src, repository);
62    }
63
64    public boolean isUnloadedClassLoader()
65    {
66       if (getClassLoader() instanceof RepositoryClassLoader)
67       {
68          RepositoryClassLoader rcl = (RepositoryClassLoader) getClassLoader();
69          return rcl.getLoaderRepository() == null;
70       }
71       return false;
72    }
73
74    public Class JavaDoc toClass(CtClass cc, ClassLoader JavaDoc loader, ProtectionDomain JavaDoc domain)
75    throws CannotCompileException
76    {
77       lockInCache(cc);
78       if (getClassLoader() == null || tempdir == null)
79       {
80          return super.toClass(cc, loader, domain);
81       }
82       Class JavaDoc dynClass = null;
83       try
84       {
85          File JavaDoc classFile = null;
86          String JavaDoc classFileName = getResourceName(cc.getName());
87          // Write the clas file to the tmpdir
88
synchronized (tmplock)
89          {
90             classFile = new File JavaDoc(tempdir, classFileName);
91             File JavaDoc pkgDirs = classFile.getParentFile();
92             pkgDirs.mkdirs();
93             FileOutputStream JavaDoc stream = new FileOutputStream JavaDoc(classFile);
94             stream.write(cc.toBytecode());
95             stream.flush();
96             stream.close();
97             classFile.deleteOnExit();
98          }
99          // We have to clear Blacklist caches or the class will never
100
// be found
101
//((UnifiedClassLoader)dcl).clearBlacklists();
102
// To be backward compatible
103
RepositoryClassLoader rcl = (RepositoryClassLoader) getClassLoader();
104          rcl.clearClassBlackList();
105          rcl.clearResourceBlackList();
106
107          // Now load the class through the cl
108
dynClass = getClassLoader().loadClass(cc.getName());
109       }
110       catch (Exception JavaDoc ex)
111       {
112          ClassFormatError JavaDoc cfe = new ClassFormatError JavaDoc("Failed to load dyn class: " + cc.getName());
113          cfe.initCause(ex);
114          throw cfe;
115       }
116
117       return dynClass;
118    }
119
120    protected boolean isLocalResource(String JavaDoc resourceName)
121    {
122       if (super.isLocalResource(resourceName))
123       {
124          return true;
125       }
126       
127       File JavaDoc file = new File JavaDoc(tempdir, resourceName);
128       if (file.exists())
129       {
130          return true;
131       }
132       
133       return false;
134    }
135 }
136
Popular Tags