KickJava   Java API By Example, From Geeks To Geeks.

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


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.lang.ref.WeakReference JavaDoc;
25
26 import org.jboss.aop.AspectManager;
27 import org.jboss.aop.Domain;
28 import org.jboss.aop.InterceptionMarkers;
29 import org.jboss.aop.advice.AspectDefinition;
30 import org.jboss.mx.loading.HeirarchicalLoaderRepository3;
31 import org.jboss.mx.loading.LoaderRepository;
32 import org.jboss.mx.loading.RepositoryClassLoader;
33
34 import EDU.oswego.cs.dl.util.concurrent.ConcurrentReaderHashMap;
35
36 /**
37  * A domain that is used for scoped classloaders
38  *
39  * @author <a HREF="kabir.khan@jboss.com">Kabir Khan</a>
40  * @version $Revision: 1.1 $
41  */

42 public class ScopedClassLoaderDomain extends Domain
43 {
44    
45    WeakReference JavaDoc loader;
46    boolean parentDelegation;
47    ConcurrentReaderHashMap myPerVMAspects = new ConcurrentReaderHashMap();
48    ConcurrentReaderHashMap notMyPerVMAspects = new ConcurrentReaderHashMap();
49    InterceptionMarkers interceptionMarkers = new InterceptionMarkers();
50    
51    public ScopedClassLoaderDomain(ClassLoader JavaDoc loader, String JavaDoc name, boolean parentDelegation, AspectManager manager, boolean parentFirst)
52    {
53       super(manager, name, parentFirst);
54       this.loader = new WeakReference JavaDoc(loader);
55       this.parentDelegation = parentDelegation;
56    }
57
58    protected ClassLoader JavaDoc getClassLoader()
59    {
60       ClassLoader JavaDoc cl = (ClassLoader JavaDoc)loader.get();
61       if (cl != null)
62       {
63          return cl;
64       }
65       return null;
66    }
67    
68    public Object JavaDoc getPerVMAspect(AspectDefinition def)
69    {
70       return getPerVMAspect(def.getName());
71    }
72
73    @Override JavaDoc
74    public InterceptionMarkers getInterceptionMarkers()
75    {
76       return interceptionMarkers;
77    }
78
79    public Object JavaDoc getPerVMAspect(String JavaDoc def)
80    {
81       if (parentDelegation == true)
82       {
83          //We will alway be loading up the correct class
84
Object JavaDoc aspect = super.getPerVMAspect(def);
85          return aspect;
86       }
87       else
88       {
89          return getPerVmAspectWithNoParentDelegation(def);
90       }
91    }
92    
93    private Object JavaDoc getPerVmAspectWithNoParentDelegation(String JavaDoc def)
94    {
95       Object JavaDoc aspect = myPerVMAspects.get(def);
96       if (aspect != null)
97       {
98          return aspect;
99       }
100
101       aspect = super.getPerVMAspect(def);
102       if (aspect != null)
103       {
104          LoaderRepository loadingRepository = getAspectRepository(aspect);
105          LoaderRepository myRepository = getScopedRepository();
106          if (loadingRepository == myRepository)
107          {
108             //The parent does not load this class
109
myPerVMAspects.put(def, aspect);
110          }
111          else
112          {
113             //The class has been loaded by a parent classloader, find out if we also have a copy
114
try
115             {
116                Class JavaDoc clazz = myRepository.loadClass(aspect.getClass().getName());
117                if (clazz == aspect.getClass())
118                {
119                   notMyPerVMAspects.put(def, Boolean.TRUE);
120                }
121                else
122                {
123                   //We have a different version of the class deployed
124
AspectDefinition aspectDefinition = getAspectDefinition(def);
125                   //Override the classloader to create the aspect instance
126
aspect = createPerVmAspect(def, aspectDefinition, getClassLoader());
127                   myPerVMAspects.put(def, aspect);
128                }
129             }
130             catch (ClassNotFoundException JavaDoc e)
131             {
132                throw new RuntimeException JavaDoc(e);
133             }
134          }
135       }
136       
137       return aspect;
138    }
139    
140    private LoaderRepository getAspectRepository(Object JavaDoc aspect)
141    {
142       ClassLoader JavaDoc cl = aspect.getClass().getClassLoader();
143       ClassLoader JavaDoc parent = cl;
144       while (parent != null)
145       {
146          if (parent instanceof RepositoryClassLoader)
147          {
148             return ((RepositoryClassLoader)parent).getLoaderRepository();
149          }
150          parent = parent.getParent();
151       }
152       return null;
153    }
154    
155    private HeirarchicalLoaderRepository3 getScopedRepository()
156    {
157       HeirarchicalLoaderRepository3 myRepository = (HeirarchicalLoaderRepository3)((RepositoryClassLoader)getClassLoader()).getLoaderRepository();
158       return myRepository;
159    }
160 }
161
Popular Tags