KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > mx > loading > MLetRepositoryClassLoader


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.mx.loading;
23
24 import java.net.URL JavaDoc;
25
26 import javax.management.MalformedObjectNameException JavaDoc;
27 import javax.management.ObjectName JavaDoc;
28 import javax.management.loading.MLet JavaDoc;
29
30 import org.jboss.logging.Logger;
31
32 /**
33  * A RepositoryClassLoader that wraps an MLet.
34  *
35  * @author <a HREF="adrian@jboss.com">Adrian Brock</a>
36  * @version $Revision: 37459 $
37  */

38 class MLetRepositoryClassLoader extends RepositoryClassLoader
39 {
40    // Constants -----------------------------------------------------
41

42    /** The log */
43    private static final Logger log = Logger.getLogger(MLetRepositoryClassLoader.class);
44
45    // Attributes -----------------------------------------------------
46

47    /** The MLet */
48    private MLet JavaDoc mlet;
49
50    // Static --------------------------------------------------------
51

52    // Constructors --------------------------------------------------
53

54    /**
55     * Create a new LoaderRepositoryClassLoader
56     *
57     * @param urls the urls
58     * @param parent the parent classloader
59     */

60    protected MLetRepositoryClassLoader(MLet JavaDoc mlet)
61    {
62       super(mlet.getURLs(), mlet);
63       this.mlet = mlet;
64    }
65    
66    // Public --------------------------------------------------------
67

68    /**
69     * Get the ObjectName
70     *
71     * @return the object name
72     */

73    public ObjectName JavaDoc getObjectName() throws MalformedObjectNameException JavaDoc
74    {
75       throw new UnsupportedOperationException JavaDoc("Not relevent");
76    }
77
78    /**
79     * This method simply invokes the super.getURLs() method to access the
80     * list of URLs that make up the RepositoryClassLoader classpath.
81     *
82     * @return the urls that make up the classpath
83     */

84    public URL JavaDoc[] getClasspath()
85    {
86       return mlet.getURLs();
87    }
88
89    /**
90     * Return all library URLs associated with this RepositoryClassLoader
91     *
92     * <p>Do not remove this method without running the WebIntegrationTestSuite
93     */

94    public URL JavaDoc[] getAllURLs()
95    {
96       return repository.getURLs();
97    }
98    
99    public synchronized Class JavaDoc loadClassImpl(String JavaDoc name, boolean resolve, int stopAt)
100       throws ClassNotFoundException JavaDoc
101    {
102       loadClassDepth ++;
103       boolean trace = log.isTraceEnabled();
104
105       if( trace )
106          log.trace("loadClassImpl, name="+name+", resolve="+resolve);
107       if( repository == null )
108       {
109          String JavaDoc msg = "Invalid use of destroyed classloader, UCL destroyed at:";
110          throw new ClassNotFoundException JavaDoc(msg, this.unregisterTrace);
111       }
112
113       /* Since loadClass can be called from loadClassInternal with the monitor
114          already held, we need to determine if there is a ClassLoadingTask
115          which requires this UCL. If there is, we release the UCL monitor
116          so that the ClassLoadingTask can use the UCL.
117        */

118       boolean acquired = attempt(1);
119       while( acquired == false )
120       {
121          /* Another thread needs this UCL to load a class so release the
122           monitor acquired by the synchronized method. We loop until
123           we can acquire the class loading lock.
124          */

125         try
126          {
127             if( trace )
128                log.trace("Waiting for loadClass lock");
129             this.wait();
130          }
131          catch(InterruptedException JavaDoc ignore)
132          {
133          }
134          acquired = attempt(1);
135       }
136
137       ClassLoadingTask task = null;
138       try
139       {
140          Thread JavaDoc t = Thread.currentThread();
141          // Register this thread as owning this UCL
142
if( loadLock.holds() == 1 )
143             LoadMgr3.registerLoaderThread(this, t);
144
145          // Create a class loading task and submit it to the repository
146
task = new ClassLoadingTask(name, this, t, stopAt);
147          /* Process class loading tasks needing this UCL until our task has
148             been completed by the thread owning the required UCL(s).
149           */

150          UnifiedLoaderRepository3 ulr3 = (UnifiedLoaderRepository3) repository;
151          if( LoadMgr3.beginLoadTask(task, ulr3) == false )
152          {
153             while( task.threadTaskCount != 0 )
154             {
155                try
156                {
157                   LoadMgr3.nextTask(t, task, ulr3);
158                }
159                catch(InterruptedException JavaDoc e)
160                {
161                   // Abort the load or retry?
162
break;
163                }
164             }
165          }
166       }
167       finally
168       {
169          // Unregister as the UCL owner to reschedule any remaining load tasks
170
if( loadLock.holds() == 1 )
171             LoadMgr3.endLoadTask(task);
172          // Notify any threads waiting to use this UCL
173
this.release();
174          this.notifyAll();
175          loadClassDepth --;
176       }
177
178       if( task.loadedClass == null )
179       {
180          if( task.loadException instanceof ClassNotFoundException JavaDoc )
181             throw (ClassNotFoundException JavaDoc) task.loadException;
182          else if( task.loadException != null )
183          {
184             if( log.isTraceEnabled() )
185                log.trace("Unexpected error during load of:"+name, task.loadException);
186             String JavaDoc msg = "Unexpected error during load of: "+name
187                + ", msg="+task.loadException.getMessage();
188             throw new ClassNotFoundException JavaDoc(msg);
189          }
190          // Assert that loadedClass is not null
191
else
192             throw new IllegalStateException JavaDoc("ClassLoadingTask.loadedTask is null, name: "+name);
193       }
194
195       return task.loadedClass;
196    }
197
198    // URLClassLoader overrides --------------------------------------
199

200    public Class JavaDoc loadClassLocally(String JavaDoc name, boolean resolve)
201       throws ClassNotFoundException JavaDoc
202    {
203       boolean trace = log.isTraceEnabled();
204       if( trace )
205          log.trace("loadClassLocally, " + this + " name=" + name);
206       Class JavaDoc result = null;
207       try
208       {
209          result = mlet.loadClass(name, null);
210          return result;
211       }
212       finally
213       {
214          if (trace)
215          {
216             if (result != null)
217                log.trace("loadClassLocally, " + this + " name=" + name + " class=" + result + " cl=" + result.getClassLoader());
218             else
219                log.trace("loadClassLocally, " + this + " name=" + name + " not found");
220          }
221       }
222    }
223    
224    // Object overrides ----------------------------------------------
225

226    // Protected -----------------------------------------------------
227

228    // Package Private -----------------------------------------------
229

230    // Private -------------------------------------------------------
231

232    // Inner classes -------------------------------------------------
233
}
234
Popular Tags