KickJava   Java API By Example, From Geeks To Geeks.

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


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.LoaderRepository;
31 import org.jboss.mx.loading.UnifiedClassLoader;
32 import org.jboss.mx.loading.UnifiedClassLoader3;
33 import org.jboss.mx.loading.UnifiedLoaderRepository3;
34
35 /**
36  * @author Simone.Bordet@hp.com
37  * @author Scott.Stark@jboss.org
38  * @version $Revision: 58115 $
39  */

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

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