KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > system > JBossRMIClassLoader


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.system;
23
24 import java.net.MalformedURLException JavaDoc;
25
26 import java.rmi.server.RMIClassLoader JavaDoc;
27 import java.rmi.server.RMIClassLoaderSpi JavaDoc;
28
29 /**
30  * An implementation of RMIClassLoaderSpi to workaround the
31  * proxy ClassCastException problem in 1.4<p>
32  *
33  * <b>THIS IS A HACK!</b><p>
34  *
35  * Sun's implementation uses the caller classloader when
36  * unmarshalling proxies. This is effectively jboss.jar since
37  * that is where JRMPInvokerProxy lives. On a redeploy the
38  * new interfaces are ignored because a proxy is already cached
39  * against the classloader.<p>
40  *
41  * Another redeployment problem is that the getClassAnnotation(String)
42  * will end up using the old deployment class loader and this can result
43  * in NPEs do the class loader being destroyed.
44  *
45  * This class ignores Sun's guess at a suitable classloader and
46  * uses the thread context classloader instead.<p>
47  *
48  * It has to exist in the system classloader so I have included it
49  * in "system" for inclusion in run.jar<p>
50  *
51  * @author <a HREF="mailto:adrian.brock@happeningtimes.com">Adrian Brock</a>
52  * @author Scott.Stark@jboss.org
53  * @version $Revision: 37459 $
54  */

55 public class JBossRMIClassLoader
56    extends RMIClassLoaderSpi JavaDoc
57 {
58    // Attributes ----------------------------------------------------
59

60    /**
61     * The JVM implementation (we delegate most work to it)
62     */

63    RMIClassLoaderSpi JavaDoc delegate = RMIClassLoader.getDefaultProviderInstance();
64    
65    // Constructors --------------------------------------------------
66

67    /**
68     * Required constructor
69     */

70    public JBossRMIClassLoader()
71    {
72    }
73    
74    // RMIClassLoaderSpi Implementation ------------------------------
75

76    /**
77     * Ignore the JVM, use the thread context classloader for proxy caching
78     */

79    public Class JavaDoc loadProxyClass(String JavaDoc codebase, String JavaDoc[] interfaces, ClassLoader JavaDoc ignored)
80       throws MalformedURLException JavaDoc, ClassNotFoundException JavaDoc
81    {
82       return delegate.loadProxyClass(codebase, interfaces, Thread.currentThread().getContextClassLoader());
83    }
84
85    /**
86     * Just delegate
87     */

88    public Class JavaDoc loadClass(String JavaDoc codebase, String JavaDoc name, ClassLoader JavaDoc ignored)
89       throws MalformedURLException JavaDoc, ClassNotFoundException JavaDoc
90    {
91       return delegate.loadClass(codebase, name, Thread.currentThread().getContextClassLoader());
92    }
93
94    /**
95     * Just delegate
96     */

97    public ClassLoader JavaDoc getClassLoader(String JavaDoc codebase)
98       throws MalformedURLException JavaDoc
99    {
100       return delegate.getClassLoader(codebase);
101    }
102
103    /**
104     * Try to delegate an default to the java.rmi.server.codebase on any
105     * failure.
106     */

107    public String JavaDoc getClassAnnotation(Class JavaDoc cl)
108    {
109       String JavaDoc annotation = null;
110       try
111       {
112          annotation = delegate.getClassAnnotation(cl);
113       }
114       catch(Throwable JavaDoc t)
115       {
116          // Try the java.rmi.server.codebase property
117
annotation = System.getProperty("java.rmi.server.codebase");
118       }
119       return annotation;
120    }
121 }
122
Popular Tags