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.IOException;
026: import java.net.URL;
027:
028: import org.jboss.aop.AspectManager;
029: import org.jboss.aop.classpool.AOPClassPool;
030: import org.jboss.mx.loading.RepositoryClassLoader;
031: import javassist.ClassPool;
032: import javassist.scopedpool.ScopedClassPool;
033: import javassist.scopedpool.ScopedClassPoolFactory;
034: import javassist.scopedpool.ScopedClassPoolRepository;
035:
036: /**
037: * Comment
038: *
039: * @author <a href="mailto:bill@jboss.org">Bill Burke</a>
040: * @version $Revision: 60381 $
041: *
042: **/
043: public class JBossClassPoolFactory implements ScopedClassPoolFactory {
044: protected File tmpClassesDir;
045:
046: public JBossClassPoolFactory(File tmpClassesDir) throws IOException {
047: this .tmpClassesDir = tmpClassesDir;
048:
049: }
050:
051: public ScopedClassPool create(ClassLoader cl, ClassPool src,
052: ScopedClassPoolRepository repository) {
053: try {
054: if (cl instanceof RepositoryClassLoader) {
055: File tempdir = getTempDirectory(cl);
056: URL tmpCP = createURLAndAddToLoader(cl, tempdir);
057: if (AspectManager.scopedCLHelper
058: .ifScopedDeploymentGetScopedParentUclForCL(cl) != null) {
059: //It is scoped
060: return new ScopedJBossClassPool(cl, src,
061: repository, tempdir, tmpCP);
062: }
063: return new JBossClassPool(cl, src, repository, tempdir,
064: tmpCP);
065: }
066: return new AOPClassPool(cl, src, repository);
067: } catch (IOException e) {
068: throw new RuntimeException(e);
069: }
070: }
071:
072: private File getTempDirectory(ClassLoader cl) {
073: File tempdir = null;
074: int attempts = 0;
075: IOException ex = null;
076: while (tempdir == null && attempts < 5) {
077: //Workaround for JBAOP-254, retry a few times
078: try {
079: tempdir = createTempDir(cl);
080: } catch (IOException e) {
081: ex = e;
082: }
083: }
084:
085: if (tempdir == null) {
086: throw new RuntimeException("", ex);
087: }
088:
089: return tempdir;
090: }
091:
092: public ScopedClassPool create(ClassPool src,
093: ScopedClassPoolRepository repository) {
094: return new JBossClassPool(src, repository);
095: }
096:
097: public File createTempDir(ClassLoader cl) throws IOException {
098: File tempdir = File.createTempFile("ucl", "", tmpClassesDir);
099: tempdir.delete();
100: tempdir.mkdir();
101: tempdir.deleteOnExit();
102:
103: return tempdir;
104: }
105:
106: private URL createURLAndAddToLoader(ClassLoader cl, File tempdir)
107: throws IOException {
108: URL tmpURL = tempdir.toURL();
109: URL tmpCP = new URL(tmpURL, "?dynamic=true");
110:
111: RepositoryClassLoader ucl = (RepositoryClassLoader) cl;
112:
113: // We may be undeploying.
114: if (ucl.getLoaderRepository() != null) {
115: ucl.addURL(tmpCP);
116: }
117:
118: return tmpCP;
119: }
120: }
|