KickJava   Java API By Example, From Geeks To Geeks.

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


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.security.ProtectionDomain JavaDoc;
27
28 import org.jboss.aop.classpool.AOPClassPool;
29 import org.jboss.mx.loading.UnifiedClassLoader;
30 import javassist.CannotCompileException;
31 import javassist.ClassPool;
32 import javassist.CtClass;
33 import javassist.scopedpool.ScopedClassPoolRepository;
34
35 /**
36  * Comment
37  *
38  * @author <a HREF="mailto:bill@jboss.org">Bill Burke</a>
39  * @version $Revision: 57054 $
40  */

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

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