001: /*
002: * JBoss, Home of Professional Open Source.
003: * Copyright 2006, Red Hat Middleware LLC, and individual contributors
004: * as indicated by the @author tags. See the copyright.txt file in the
005: * distribution for a full listing of individual contributors.
006: *
007: * This is free software; you can redistribute it and/or modify it
008: * under the terms of the GNU Lesser General Public License as
009: * published by the Free Software Foundation; either version 2.1 of
010: * the License, or (at your option) any later version.
011: *
012: * This software is distributed in the hope that it will be useful,
013: * but WITHOUT ANY WARRANTY; without even the implied warranty of
014: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
015: * Lesser General Public License for more details.
016: *
017: * You should have received a copy of the GNU Lesser General Public
018: * License along with this software; if not, write to the Free
019: * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
020: * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
021: */
022: package org.jboss.aop.deployment;
023:
024: import java.io.File;
025: import java.io.FileOutputStream;
026: import java.net.URL;
027: import java.security.ProtectionDomain;
028:
029: import org.jboss.aop.classpool.AOPClassPool;
030: import org.jboss.mx.loading.RepositoryClassLoader;
031: import javassist.CannotCompileException;
032: import javassist.ClassPool;
033: import javassist.CtClass;
034: import javassist.scopedpool.ScopedClassPoolRepository;
035:
036: /**
037: * Comment
038: *
039: * @author <a href="mailto:bill@jboss.org">Bill Burke</a>
040: * @version $Revision: 60560 $
041: */
042: public class JBossClassPool extends AOPClassPool {
043: /**
044: * Used for dynamically created classes (see loadClass(String, byte[]), ClassLoader)
045: */
046: protected File tempdir = null;
047: protected URL tempURL = null;
048: // For loadClass tmpdir creation for UCL
049: protected final Object tmplock = new Object();
050:
051: protected JBossClassPool(ClassLoader cl, ClassPool src,
052: ScopedClassPoolRepository repository, File tmp, URL tmpURL) {
053: super (cl, src, repository);
054: tempdir = tmp;
055: tempURL = tmpURL;
056: }
057:
058: protected JBossClassPool(ClassPool src,
059: ScopedClassPoolRepository repository) {
060: super (src, repository);
061: }
062:
063: public boolean isUnloadedClassLoader() {
064: if (getClassLoader() instanceof RepositoryClassLoader) {
065: RepositoryClassLoader rcl = (RepositoryClassLoader) getClassLoader();
066: return rcl.getLoaderRepository() == null;
067: }
068: return false;
069: }
070:
071: public Class toClass(CtClass cc, ClassLoader loader,
072: ProtectionDomain domain) throws CannotCompileException {
073: lockInCache(cc);
074: if (getClassLoader() == null || tempdir == null) {
075: return super .toClass(cc, loader, domain);
076: }
077: Class dynClass = null;
078: try {
079: File classFile = null;
080: String classFileName = getResourceName(cc.getName());
081: // Write the clas file to the tmpdir
082: synchronized (tmplock) {
083: classFile = new File(tempdir, classFileName);
084: File pkgDirs = classFile.getParentFile();
085: pkgDirs.mkdirs();
086: FileOutputStream stream = new FileOutputStream(
087: classFile);
088: stream.write(cc.toBytecode());
089: stream.flush();
090: stream.close();
091: classFile.deleteOnExit();
092: }
093: // We have to clear Blacklist caches or the class will never
094: // be found
095: //((UnifiedClassLoader)dcl).clearBlacklists();
096: // To be backward compatible
097: RepositoryClassLoader rcl = (RepositoryClassLoader) getClassLoader();
098: rcl.clearClassBlackList();
099: rcl.clearResourceBlackList();
100:
101: // Now load the class through the cl
102: dynClass = getClassLoader().loadClass(cc.getName());
103: } catch (Exception ex) {
104: ClassFormatError cfe = new ClassFormatError(
105: "Failed to load dyn class: " + cc.getName()
106: + " with " + getClassLoader());
107: cfe.initCause(ex);
108: throw cfe;
109: }
110:
111: return dynClass;
112: }
113:
114: protected boolean isLocalResource(String resourceName) {
115: if (super .isLocalResource(resourceName)) {
116: return true;
117: }
118:
119: File file = new File(tempdir, resourceName);
120: if (file.exists()) {
121: return true;
122: }
123:
124: return false;
125: }
126: }
|