KickJava   Java API By Example, From Geeks To Geeks.

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


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 import java.lang.reflect.Constructor JavaDoc;
26
27 import org.jboss.logging.Logger;
28
29 import org.jboss.test.classloader.circularity.support.Support;
30 import org.jboss.mx.loading.UnifiedClassLoader;
31 import org.jboss.mx.loading.UnifiedClassLoader3;
32 import org.jboss.mx.loading.UnifiedLoaderRepository3;
33
34 /**
35  * @author Simone.Bordet@hp.com
36  * @author Scott.Stark@jboss.org
37  * @version $Revision: 58115 $
38  */

39 public class CircularityErrorTests3
40 {
41    private static Logger log = Logger.getLogger(CircularityErrorTests.class);
42
43    private Object JavaDoc lock = new Object JavaDoc();
44    private boolean sawCircularity;
45    private boolean sawClassNotFound;
46
47    public CircularityErrorTests3()
48    {
49    }
50
51    public void testClassCircularityError() throws Exception JavaDoc
52    {
53       // The scenario is this one:
54
// Thread1 asks classloader1 to load class Derived
55
// Thread2 triggers a loadClassInternal for classloader1 to load class Base
56
// Thread2 is put in sleep by the ULR since we are loading Derived
57
// Thread1 triggers a loadClassInternal for classloader1 to load class Base
58
// Thread1 throws ClassCircularityError
59

60       UnifiedLoaderRepository3 repository = new UnifiedLoaderRepository3();
61       Class JavaDoc thisClass = getClass();
62       UnifiedClassLoader thisUCL = (UnifiedClassLoader) thisClass.getClassLoader();
63       URL JavaDoc origURL = thisUCL.getOrigURL();
64       log.debug("Service origURL="+origURL);
65       URL JavaDoc url = thisClass.getProtectionDomain().getCodeSource().getLocation();
66       final UnifiedLoader ucl = new UnifiedLoader(url);
67       repository.addClassLoader(ucl);
68       log.debug("Unified ClassLoader created, url="+url);
69
70       Class JavaDoc cls = ucl.loadClass("org.jboss.test.classloader.circularity.support.Support");
71
72       Thread JavaDoc thread1 = new Thread JavaDoc(new Runnable JavaDoc()
73       {
74          public void run()
75          {
76             try
77             {
78                // Be sure thread2 is waiting
79
try {Thread.sleep(1000);}
80                catch (InterruptedException JavaDoc x) {}
81
82                try
83                {
84                   // Ask this thread to load this class with this classloader
85
log.debug("Thread " + Thread.currentThread() + " loading...");
86                   ucl.loadClass("org.jboss.test.classloader.circularity.support.Derived");
87                   log.debug("Thread " + Thread.currentThread() + " loading done !");
88                }
89                catch (ClassCircularityError JavaDoc x)
90                {
91                   log.error("Saw ClassCircularityError", x);
92                   sawCircularity = true;
93                }
94             }
95             catch (ClassNotFoundException JavaDoc x)
96             {
97                log.error("Bug in the test: ", x);
98                sawClassNotFound = true;
99             }
100          }
101       }, "CircularityError Thread");
102       thread1.start();
103
104       synchronized (lock)
105       {
106          log.debug("Thread " + Thread.currentThread() + " waiting...");
107          lock.wait();
108          log.debug("Thread " + Thread.currentThread() + " woken up !");
109       }
110
111       // Ask this thread to trigger a loadClassInternal directly; the UnifiedLoaderRepository
112
// will put this thread in sleep, but the JVM has already registered the fact that
113
// it wants to load the class, in this case class Base
114
cls.newInstance();
115
116       // The ClassCircularityError thrown should allow the call above to complete
117
if( sawCircularity )
118          throw new ClassCircularityError JavaDoc("Got ClassCircularityError, UnifiedLoaderRepository is buggy");
119       if( sawClassNotFound )
120          throw new ClassNotFoundException JavaDoc("Got ClassNotFoundException, UnifiedLoaderRepository is buggy");
121    }
122
123    public class UnifiedLoader extends UnifiedClassLoader3
124    {
125       public UnifiedLoader(URL JavaDoc url)
126       {
127          super(url);
128       }
129
130       public Class JavaDoc loadClass(String JavaDoc name) throws ClassNotFoundException JavaDoc
131       {
132          return super.loadClass(name);
133       }
134
135       public Class JavaDoc loadClassLocally(String JavaDoc name, boolean resolve) throws ClassNotFoundException JavaDoc
136       {
137          log.debug(Thread.currentThread() + " is now asked to load class: " + name);
138
139          if (name.equals("org.jboss.test.classloader.circularity.support.Derived"))
140          {
141             synchronized (lock)
142             {
143                lock.notifyAll();
144             }
145
146             // Wait to trigger ClassCircularityError
147
// Do not release the lock on the classloader
148
try
149             {
150                log.debug("Loading " + name + ", waiting...");
151                Thread.sleep(2000);
152                log.debug("Loading " + name + " end wait");
153             }
154             catch (InterruptedException JavaDoc x)
155             {
156                log.debug("Sleep was interrupted", x);
157             }
158          }
159
160          return super.loadClassLocally(name, resolve);
161       }
162    }
163
164 }
165
Popular Tags