KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > aop > contrib > IDEClassPoolFactory


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.contrib;
23
24 import java.io.IOException JavaDoc;
25 import java.net.URL JavaDoc;
26 import java.net.URLClassLoader JavaDoc;
27 import java.util.ArrayList JavaDoc;
28
29 import javassist.CannotCompileException;
30 import javassist.ClassPool;
31 import javassist.CtClass;
32 import javassist.LoaderClassPath;
33 import javassist.scopedpool.ScopedClassPool;
34 import javassist.scopedpool.ScopedClassPoolFactory;
35 import javassist.scopedpool.ScopedClassPoolRepository;
36
37 import org.jboss.aop.AspectManager;
38 import org.jboss.aop.classpool.AOPClassPool;
39
40 /**
41  * @author Marshall
42  * <p/>
43  * This is a class pool factory used by JBossIDE to create an AOP Class Pool
44  */

45 public class IDEClassPoolFactory implements ScopedClassPoolFactory
46 {
47
48    private ArrayList JavaDoc classPaths;
49
50    public IDEClassPoolFactory()
51    {
52       classPaths = new ArrayList JavaDoc();
53    }
54
55    public ScopedClassPool create(ClassLoader JavaDoc loader, ClassPool pool, ScopedClassPoolRepository repository)
56    {
57       SimpleClassPool classPool = new SimpleClassPool(loader, pool, repository);
58       return classPool;
59    }
60
61    public ScopedClassPool create(ClassPool pool, ScopedClassPoolRepository repository)
62    {
63       SimpleClassPool classPool = new SimpleClassPool(pool, repository);
64       return classPool;
65    }
66
67    public void insertClasspath(String JavaDoc path)
68    {
69       try
70       {
71          classPaths.add(new URL JavaDoc(path));
72       }
73       catch (Exception JavaDoc e)
74       {
75          e.printStackTrace();
76       }
77    }
78
79    private class SimpleClassPool extends AOPClassPool
80    {
81       private Loader JavaDoc loader;
82
83       public SimpleClassPool(ClassLoader JavaDoc loader, ClassPool pool, ScopedClassPoolRepository repository)
84       {
85          super(loader, pool, repository);
86          loadClasspath();
87       }
88
89       public SimpleClassPool(ClassPool pool, ScopedClassPoolRepository repository)
90       {
91          super(pool, repository);
92          loadClasspath();
93
94       }
95
96       private void loadClasspath()
97       {
98          childFirstLookup = true;
99
100          URL JavaDoc urlPaths[] = (URL JavaDoc[]) classPaths.toArray(new URL JavaDoc[classPaths.size()]);
101          loader = new Loader JavaDoc(urlPaths, Thread.currentThread().getContextClassLoader());
102          setClassLoader(loader);
103
104          Thread.currentThread().setContextClassLoader(loader);
105
106          classPath = new LoaderClassPath(loader);
107          insertClassPath(classPath);
108       }
109
110       public Class JavaDoc toClass(CtClass cc)
111               throws CannotCompileException
112       {
113          try
114          {
115             return loader.loadClass(cc.getName());
116          }
117          catch (Exception JavaDoc e)
118          {
119             try
120             {
121                return loader.loadClass(cc.getName(), cc.toBytecode());
122             }
123             catch (IOException JavaDoc e2)
124             {
125                throw new CannotCompileException(e2);
126             }
127          }
128       }
129    }
130
131    private class Loader extends URLClassLoader JavaDoc
132    {
133       public Loader(URL JavaDoc urls[], ClassLoader JavaDoc src)
134       {
135          super(urls, src);
136       }
137
138       public Class JavaDoc loadClass(String JavaDoc name, byte[] classfile)
139               throws ClassFormatError JavaDoc
140       {
141          Class JavaDoc c = defineClass(name, classfile, 0, classfile.length);
142
143          resolveClass(c);
144          return c;
145       }
146    }
147
148    public ClassLoader JavaDoc getTCLIfScoped()
149    {
150       return null;
151    }
152 }
153
Popular Tags