KickJava   Java API By Example, From Geeks To Geeks.

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


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.IOException JavaDoc;
26 import java.net.URL JavaDoc;
27
28 import org.jboss.aop.AspectManager;
29 import org.jboss.aop.classpool.AOPClassPool;
30 import org.jboss.mx.loading.RepositoryClassLoader;
31 import javassist.ClassPool;
32 import javassist.scopedpool.ScopedClassPool;
33 import javassist.scopedpool.ScopedClassPoolFactory;
34 import javassist.scopedpool.ScopedClassPoolRepository;
35
36 /**
37  * Comment
38  *
39  * @author <a HREF="mailto:bill@jboss.org">Bill Burke</a>
40  * @version $Revision: 57974 $
41  *
42  **/

43 public class JBossClassPoolFactory implements ScopedClassPoolFactory
44 {
45    protected File JavaDoc tmpClassesDir;
46
47    public JBossClassPoolFactory(File JavaDoc tmpClassesDir) throws IOException JavaDoc
48    {
49       this.tmpClassesDir = tmpClassesDir;
50
51    }
52    public ScopedClassPool create(ClassLoader JavaDoc cl, ClassPool src, ScopedClassPoolRepository repository)
53    {
54       try
55       {
56          if (cl instanceof RepositoryClassLoader)
57          {
58             File JavaDoc tempdir = createTempDir(cl);
59             URL JavaDoc tmpCP = createURLAndAddToLoader(cl, tempdir);
60             if (AspectManager.scopedCLHelper.ifScopedDeploymentGetScopedParentUclForCL(cl) != null)
61             {
62                //It is scoped
63
return new ScopedJBossClassPool(cl, src, repository, tempdir, tmpCP);
64             }
65             return new JBossClassPool(cl, src, repository, tempdir, tmpCP);
66          }
67          return new AOPClassPool(cl, src, repository);
68       }
69       catch (IOException JavaDoc e)
70       {
71          throw new RuntimeException JavaDoc(e);
72       }
73    }
74
75    public ScopedClassPool create(ClassPool src, ScopedClassPoolRepository repository)
76    {
77       return new TempJBossClassPool(src, repository);
78    }
79
80    public File JavaDoc createTempDir(ClassLoader JavaDoc cl) throws IOException JavaDoc
81    {
82       File JavaDoc tempdir = File.createTempFile("ucl", "", tmpClassesDir);
83       tempdir.delete();
84       tempdir.mkdir();
85       tempdir.deleteOnExit();
86
87       return tempdir;
88    }
89    
90    private URL JavaDoc createURLAndAddToLoader(ClassLoader JavaDoc cl, File JavaDoc tempdir) throws IOException JavaDoc
91    {
92       URL JavaDoc tmpURL = tempdir.toURL();
93       URL JavaDoc tmpCP = new URL JavaDoc(tmpURL, "?dynamic=true");
94
95       RepositoryClassLoader ucl = (RepositoryClassLoader) cl;
96
97       // We may be undeploying.
98
if (ucl.getLoaderRepository() != null)
99       {
100          ucl.addURL(tmpCP);
101       }
102       
103       return tmpCP;
104    }
105 }
106
Popular Tags