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.security.ProtectionDomain;
027:
028: import org.jboss.aop.AspectManager;
029: import org.jboss.aop.classpool.AOPClassPool;
030: import org.jboss.mx.loading.RepositoryClassLoader;
031: import org.jboss.mx.loading.UnifiedClassLoader;
032: import javassist.CannotCompileException;
033: import javassist.ClassPool;
034: import javassist.CtClass;
035: import javassist.scopedpool.ScopedClassPoolRepository;
036:
037: /**
038: * Comment
039: *
040: * @author <a href="mailto:bill@jboss.org">Bill Burke</a>
041: * @version $Revision: 57186 $
042: */
043: public class JBossClassPool32 extends AOPClassPool {
044: /**
045: * Used for dynamically created classes (see loadClass(String, byte[]), ClassLoader)
046: */
047: protected File tempdir = null;
048: // For loadClass tmpdir creation for UCL
049: protected final Object tmplock = new Object();
050:
051: protected JBossClassPool32(ClassLoader cl, ClassPool src,
052: ScopedClassPoolRepository repository, File tmp) {
053: super (cl, src, repository);
054: tempdir = tmp;
055: }
056:
057: protected JBossClassPool32(ClassPool src,
058: ScopedClassPoolRepository repository) {
059: super (src, repository);
060: }
061:
062: public boolean isUnloadedClassLoader() {
063: if (getClassLoader() instanceof UnifiedClassLoader) {
064: UnifiedClassLoader rcl = (UnifiedClassLoader) getClassLoader();
065: return rcl.getLoaderRepository() == null;
066: }
067: return false;
068: }
069:
070: public Class toClass(CtClass cc, ClassLoader loader,
071: ProtectionDomain domain) throws CannotCompileException {
072: lockInCache(cc);
073: if (getClassLoader() == null || tempdir == null) {
074: return super .toClass(cc, loader, domain);
075: }
076: Class dynClass = null;
077: try {
078: File classFile = null;
079: String classFileName = cc.getName().replace('.', '/')
080: + ".class";
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: UnifiedClassLoader rcl = (UnifiedClassLoader) getClassLoader();
098: rcl.clearBlackLists();
099:
100: // Now load the class through the cl
101: dynClass = getClassLoader().loadClass(cc.getName());
102: } catch (Exception ex) {
103: ClassFormatError cfe = new ClassFormatError(
104: "Failed to load dyn class: " + cc.getName());
105: cfe.initCause(ex);
106: throw cfe;
107: }
108:
109: return dynClass;
110: }
111:
112: }
|