KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > aop > classpool > AOPClassPoolRepository


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.classpool;
23
24 import java.lang.reflect.Field JavaDoc;
25 import java.security.AccessController JavaDoc;
26 import java.security.PrivilegedActionException JavaDoc;
27 import java.security.PrivilegedExceptionAction JavaDoc;
28 import java.util.HashMap JavaDoc;
29 import java.util.HashSet JavaDoc;
30 import java.util.Iterator JavaDoc;
31 import java.util.Map JavaDoc;
32
33 import org.jboss.aop.AspectManager;
34 import org.jboss.aop.instrument.Instrumentor;
35
36 import javassist.ClassPool;
37 import javassist.scopedpool.ScopedClassPool;
38 import javassist.scopedpool.ScopedClassPoolFactory;
39 import javassist.scopedpool.ScopedClassPoolRepository;
40 import javassist.scopedpool.ScopedClassPoolRepositoryImpl;
41
42 /**
43  * Singleton classpool repository used by aop
44  *
45  * @author <a HREF="kabir.khan@jboss.com">Kabir Khan</a>
46  * @version $Revision: 46253 $
47  */

48 public class AOPClassPoolRepository implements ScopedClassPoolRepository
49 {
50    private final static AOPClassPoolRepository instance = new AOPClassPoolRepository();
51    
52    /** The classes per classppol */
53    protected final HashMap JavaDoc ucl2classes = new HashMap JavaDoc();
54
55    /** The top-level AspectManager this pool belongs to */
56    AspectManager manager;
57    
58    ScopedClassPoolRepository delegate;
59
60    public static AOPClassPoolRepository getInstance()
61    {
62       return instance;
63    }
64    
65    private AOPClassPoolRepository()
66    {
67       this.delegate = ScopedClassPoolRepositoryImpl.getInstance();
68       delegate.setClassPoolFactory(new AOPClassPoolFactory());
69    }
70
71
72    public void setClassPoolFactory(ScopedClassPoolFactory factory)
73    {
74       delegate.setClassPoolFactory(factory);
75    }
76    
77    public ScopedClassPoolFactory getClassPoolFactory()
78    {
79       return delegate.getClassPoolFactory();
80    }
81
82    public boolean isPrune()
83    {
84       return delegate.isPrune();
85    }
86
87    public void setPrune(boolean prune)
88    {
89       delegate.setPrune(prune);
90    }
91
92    public ScopedClassPool createScopedClassPool(ClassLoader JavaDoc cl, ClassPool src)
93    {
94       return delegate.createScopedClassPool(cl, src);
95    }
96
97    public ClassPool findClassPool(ClassLoader JavaDoc cl)
98    {
99       return delegate.findClassPool(cl);
100    }
101
102    public void setAspectManager(AspectManager manager)
103    {
104       this.manager = manager;
105    }
106    
107    /**
108     * Get the registered classloaders
109     *
110     * @return the registered classloaders
111     */

112    public Map JavaDoc getRegisteredCLs()
113    {
114       return delegate.getRegisteredCLs();
115    }
116
117    /**
118     * This method will check to see if a register classloader has been undeployed (as in JBoss)
119     */

120    public void clearUnregisteredClassLoaders()
121    {
122       delegate.clearUnregisteredClassLoaders();
123    }
124    
125    public ClassPool registerClassLoader(ClassLoader JavaDoc ucl)
126    {
127       return delegate.registerClassLoader(ucl);
128    }
129
130    public void unregisterClassLoader(ClassLoader JavaDoc cl)
131    {
132       delegate.unregisterClassLoader(cl);
133    }
134    
135    public void registerClass(Class JavaDoc clazz)
136    {
137       HashSet JavaDoc classes = (HashSet JavaDoc) ucl2classes.get(clazz.getClassLoader());
138       if (classes == null)
139       {
140          classes = new HashSet JavaDoc();
141          ucl2classes.put(clazz.getClassLoader(), classes);
142       }
143       classes.add(clazz);
144    }
145
146    public void perfomUnregisterClassLoader(ClassLoader JavaDoc cl)
147    {
148       if (System.getSecurityManager() == null)
149       {
150          UnregisterClassLoaderAction.NON_PRIVILEGED.unregister(this, cl);
151       }
152       else
153       {
154          UnregisterClassLoaderAction.PRIVILEGED.unregister(this, cl);
155       }
156    }
157    
158    private void doUnregisterClassLoader(ClassLoader JavaDoc cl)
159    {
160       synchronized (delegate.getRegisteredCLs())
161       {
162          HashSet JavaDoc classes = (HashSet JavaDoc) ucl2classes.remove(cl);
163          if (classes != null)
164          {
165             Iterator JavaDoc it = classes.iterator();
166             while (it.hasNext())
167             {
168                Object JavaDoc clazz = it.next();
169                synchronized (manager.getAdvisors())
170                {
171                   manager.getAdvisors().remove(clazz);
172                   Class JavaDoc advisedClass = (Class JavaDoc)clazz;
173                   try
174                   {
175                      //The static advisor field should be the only remaining hard reference to the advisor
176
Field JavaDoc f = advisedClass.getDeclaredField(Instrumentor.HELPER_FIELD_NAME);
177                      f.setAccessible(true);
178                      f.set(null, null);
179                   }
180                   catch(NoSuchFieldException JavaDoc e)
181                   {
182                      System.out.println("[warn] Error unsetting advisor for " + advisedClass.getName() + " " + e);
183                   }
184                   catch(IllegalAccessException JavaDoc e)
185                   {
186                      System.out.println("[warn] Error unsetting advisor for " + advisedClass.getName() + " " + e);
187                   }
188                }
189             }
190          }
191       }
192    }
193
194    
195    interface UnregisterClassLoaderAction
196    {
197       void unregister(AOPClassPoolRepository repository, ClassLoader JavaDoc loader);
198       
199       UnregisterClassLoaderAction PRIVILEGED = new UnregisterClassLoaderAction()
200       {
201          public void unregister(final AOPClassPoolRepository repository, final ClassLoader JavaDoc loader)
202          {
203             try
204             {
205                AccessController.doPrivileged(new PrivilegedExceptionAction JavaDoc()
206                {
207                   public Object JavaDoc run()
208                   {
209                      repository.doUnregisterClassLoader(loader);
210                      return null;
211                   }
212                });
213             }
214             catch (PrivilegedActionException JavaDoc e)
215             {
216                Exception JavaDoc ex = e.getException();
217                if (ex instanceof RuntimeException JavaDoc)
218                {
219                   throw (RuntimeException JavaDoc)ex;
220                }
221                throw new RuntimeException JavaDoc(ex);
222             }
223          }
224       };
225
226       UnregisterClassLoaderAction NON_PRIVILEGED = new UnregisterClassLoaderAction()
227       {
228          public void unregister(AOPClassPoolRepository repository, ClassLoader JavaDoc loader)
229          {
230             repository.doUnregisterClassLoader(loader);
231          }
232       };
233    }
234 }
235
Popular Tags