KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > test > classloader > circularity > test > RecursiveCCETests


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.test.classloader.circularity.test;
23
24 import java.net.URL JavaDoc;
25
26 import org.jboss.logging.Logger;
27
28 import org.jboss.mx.loading.UnifiedClassLoader;
29 import org.jboss.mx.loading.UnifiedClassLoader3;
30 import org.jboss.mx.loading.UnifiedLoaderRepository3;
31 import org.jboss.mx.loading.ClassLoadingTask;
32 import org.jboss.mx.loading.LoadMgr3;
33 import EDU.oswego.cs.dl.util.concurrent.Semaphore;
34
35 /** Deadlock tests of the UnifiedClassLoader3
36  * @author Scott.Stark@jboss.org
37  * @version $Revision: 58115 $
38  */

39 public class RecursiveCCETests
40 {
41    private static Logger log = Logger.getLogger(RecursiveCCETests.class);
42    //private CyclicBarrier ifaceBarrier = new CyclicBarrier(2);
43

44    public RecursiveCCETests()
45    {
46    }
47
48    /** The scenario is:
49     - Thread0 starts to load HARMIServerImpl via UCL0 which has ha.jar and
50     contains HARMIServerImpl, HARMIServer and HARMIServerImpl_Stub.
51     
52     - Thread1 loads HARMIServerImpl_Stub via UCL1 which has none of the
53     HARMIServerImpl, HARMIServer and HARMIServerImpl_Stub classes.
54
55     @throws Exception
56     */

57    public void testRecursiveLoadMT() throws Exception JavaDoc
58    {
59       log.info("Begin testRecursiveLoadMT");
60       UnifiedLoaderRepository3 repository = new UnifiedLoaderRepository3();
61       Class JavaDoc thisClass = getClass();
62       UnifiedClassLoader thisUCL = (UnifiedClassLoader) thisClass.getClassLoader();
63       URL JavaDoc origURL = thisUCL.getOrigURL();
64       log.info("Service origURL="+origURL);
65       URL JavaDoc j0 = new URL JavaDoc(origURL, "ha.jar");
66       log.info("j0 = "+j0);
67
68       Semaphore s0 = new Semaphore(0);
69       MyUCL ucl0 = new MyUCL(j0, s0);
70       repository.addClassLoader(ucl0);
71       Semaphore s1 = new Semaphore(0);
72       MyUCL ucl1 = new MyUCL(origURL, s1);
73       repository.addClassLoader(ucl1);
74       String JavaDoc class0 = "org.jboss.test.classloader.circularity.support.HARMIServerImpl";
75       MyThread t0 = new MyThread(ucl0, "testRecursiveLoadMT.T0", class0);
76       {
77          log.info("Starting T0");
78          t0.start();
79          log.info("Started T0, waiting on ucl="+System.identityHashCode(ucl0));
80          s0.acquire();
81          log.info("UCL0 notify received");
82       }
83
84       String JavaDoc class1 = "org.jboss.test.classloader.circularity.support.HARMIServerImpl_Stub";
85       MyThread t1 = new MyThread(ucl1, "testRecursiveLoadMT.T1", class1);
86       {
87          log.info("Starting T1");
88          t1.start();
89          log.info("Started T1, waiting on ucl="+System.identityHashCode(ucl1));
90          s1.acquire();
91          log.info("UCL1 notify received");
92       }
93
94       t1.join(10000);
95       if( t1.loadedClass == null || t1.loadedClass.getName().equals(class1) == false )
96       {
97          String JavaDoc msg = "Thread1 failed to load HARMIServerImpl_Stub, class="+t1.loadedClass;
98          log.error(msg, t1.loadEx);
99          throw new Exception JavaDoc(msg);
100       }
101       t0.join(5000);
102       if( t0.loadedClass == null || t0.loadedClass.getName().equals(class0) == false )
103       {
104          String JavaDoc msg = "Thread0 failed to load HARMIServerImpl, class="+t0.loadedClass;
105          log.error(msg, t0.loadEx);
106          throw new Exception JavaDoc(msg);
107       }
108       log.info("End testRecursiveLoadMT");
109    }
110
111    /** Load org.jboss.test.classloader.circularity.support.pkg0.Derived via
112     * UCL0
113     */

114    static class MyThread extends Thread JavaDoc
115    {
116       String JavaDoc className;
117       Class JavaDoc loadedClass;
118       Throwable JavaDoc loadEx;
119       UnifiedClassLoader3 ucl;
120
121       MyThread(UnifiedClassLoader3 ucl, String JavaDoc id, String JavaDoc className)
122       {
123          super(id);
124          this.className = className;
125          this.ucl = ucl;
126       }
127       public void run()
128       {
129          try
130          {
131             loadedClass = ucl.loadClass(className, false);
132          }
133          catch(Throwable JavaDoc t)
134          {
135             loadEx = t;
136             log.error("Failed to load: "+className, t);
137          }
138       }
139    }
140
141    public static class MyUCL extends UnifiedClassLoader3
142    {
143       private static final Logger log = Logger.getLogger(MyUCL.class);
144       Semaphore s;
145       boolean passedBarriers;
146
147       public MyUCL(URL JavaDoc url, Semaphore s)
148       {
149          super(url);
150          this.s = s;
151       }
152
153       /** Override to
154        */

155       public synchronized Class JavaDoc loadClass(String JavaDoc name, boolean resolve)
156          throws ClassNotFoundException JavaDoc
157       {
158          log.info("loadClass, name="+name);
159          boolean acquired = attempt(1);
160          if( acquired == false )
161             throw new IllegalStateException JavaDoc("Failed to acquire loadClass lock");
162          log.info("Acquired loadClass lock");
163
164          MyClassLoadingTask task = null;
165          try
166          {
167             Thread JavaDoc t = Thread.currentThread();
168             // Register this thread as owning this UCL
169
if( loadLock.holds() == 1 )
170                LoadMgr3.registerLoaderThread(this, t);
171
172             s.release();
173             log.info("notifyAll, ucl="+System.identityHashCode(this));
174             try
175             {
176                if( name.endsWith("HARMIServer") )
177                {
178                   t.sleep(5000);
179                   log.info("Passed HARMIServer barrier");
180                }
181             }
182             catch(InterruptedException JavaDoc e)
183             {
184                throw new IllegalStateException JavaDoc("MyUCL failed to enter HARMIServer barrier");
185             }
186
187             // Create a class loading task and submit it to the repository
188
task = new MyClassLoadingTask(name, this, t);
189             /* Process class loading tasks needing this UCL until our task has
190                been completed by the thread owning the required UCL(s).
191              */

192             UnifiedLoaderRepository3 ulr3 = (UnifiedLoaderRepository3) repository;
193             if( LoadMgr3.beginLoadTask(task, ulr3) == false )
194             {
195                while( task.threadTaskCount() != 0 )
196                {
197                   try
198                   {
199                      LoadMgr3.nextTask(t, task, ulr3);
200                   }
201                   catch(InterruptedException JavaDoc e)
202                   {
203                      // Abort the load or retry?
204
break;
205                   }
206                }
207             }
208          }
209          finally
210          {
211             // Unregister as the UCL owner to reschedule any remaining load tasks
212
if( loadLock.holds() == 1 )
213                LoadMgr3.endLoadTask(task);
214             // Notify any threads waiting to use this UCL
215
this.release();
216             this.notifyAll();
217          }
218
219          if( task.loadedClass() == null )
220          {
221             if( task.loadException() instanceof ClassNotFoundException JavaDoc )
222                throw (ClassNotFoundException JavaDoc) task.loadException();
223             else if( task.loadException() != null )
224             {
225                log.info("Unexpected error during load of:"+name, task.loadException());
226                String JavaDoc msg = "Unexpected error during load of: "+name
227                   + ", msg="+task.loadException().getMessage();
228                throw new ClassNotFoundException JavaDoc(msg);
229             }
230             // Assert that loadedClass is not null
231
else
232                throw new IllegalStateException JavaDoc("ClassLoadingTask.loadedTask is null, name: "+name);
233          }
234
235          return task.loadedClass();
236       }
237    }
238 }
239
Popular Tags