KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > jonas > server > RemoteClassLoaderSpi


1 /**
2  * JOnAS: Java(TM) Open Application Server
3  * Copyright (C) 1999-2005 Bull S.A.
4  * Contact: jonas-team@objectweb.org
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2.1 of the License, or any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with this library; if not, write to the Free Software
18  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
19  * USA
20  *
21  * --------------------------------------------------------------------------
22  * $Id: RemoteClassLoaderSpi.java,v 1.7 2005/04/19 14:14:34 benoitf Exp $
23  * --------------------------------------------------------------------------
24  */

25
26 package org.objectweb.jonas.server;
27
28 import java.net.MalformedURLException JavaDoc;
29 import java.net.URLClassLoader JavaDoc;
30 import java.rmi.server.RMIClassLoader JavaDoc;
31 import java.rmi.server.RMIClassLoaderSpi JavaDoc;
32
33 import org.objectweb.carol.util.configuration.CarolDefaultValues;
34
35 /**
36  * Class <code>RemoteClassLoaderSpi</code> is the CAROL JRMP CLass Loader SPI
37  * for serialization performances.
38  * @author Guillaume Riviere (Guillaume.Riviere@inrialpes.fr)
39  */

40 public class RemoteClassLoaderSpi extends RMIClassLoaderSpi JavaDoc {
41
42     /**
43      * Carol was already initialized
44      */

45     private static boolean carolIsInitialized = false;
46
47     /**
48      * Local call optimization is set
49      */

50     private static boolean carolIsOptimized = false;
51
52     /**
53      * Current provider
54      */

55     private final RMIClassLoaderSpi JavaDoc defaultProvider = RMIClassLoader.getDefaultProviderInstance();
56
57     /**
58      * Loads a class from a codebase URL path, optionally using the supplied
59      * loader.
60      * @param codebase the list of URLs (separated by spaces) to load the class
61      * from, or <code>null</code>
62      * @param name the name of the class to load
63      * @param defaultLoader additional contextual class loader to use, or
64      * <code>null</code>
65      * @return the <code>Class</code> object representing the loaded class
66      * @throws MalformedURLException if <code>codebase</code> is non-<code>null</code>
67      * and contains an invalid URL, or if <code>codebase</code> is
68      * <code>null</code> and the system property
69      * <code>java.rmi.server.codebase</code> contains an invalid URL
70      * @throws ClassNotFoundException if a definition for the class could not be
71      * found at the specified location
72      */

73     public Class JavaDoc loadClass(String JavaDoc codebase, String JavaDoc name, ClassLoader JavaDoc defaultLoader) throws MalformedURLException JavaDoc,
74             ClassNotFoundException JavaDoc {
75         return defaultProvider.loadClass(codebase, name, defaultLoader);
76     }
77
78     /**
79      * Loads a dynamic proxy class (see {@link java.lang.reflect.Proxy} that
80      * implements a set of interfaces with the given names from a codebase URL
81      * path, optionally using the supplied loader.
82      * @param codebase the list of URLs (space-separated) to load classes from,
83      * or <code>null</code>
84      * @param interfaces the names of the interfaces for the proxy class to
85      * implement
86      * @return a dynamic proxy class that implements the named interfaces
87      * @param defaultLoader additional contextual class loader to use, or
88      * <code>null</code>
89      * @throws MalformedURLException if <code>codebase</code> is non-<code>null</code>
90      * and contains an invalid URL, or if <code>codebase</code> is
91      * <code>null</code> and the system property
92      * <code>java.rmi.server.codebase</code> contains an invalid URL
93      * @throws ClassNotFoundException if a definition for one of the named
94      * interfaces could not be found at the specified location, or if
95      * creation of the dynamic proxy class failed (such as if
96      * {@link java.lang.reflect.Proxy#getProxyClass(ClassLoader,Class[])}
97      * would throw an <code>IllegalArgumentException</code> for the
98      * given interface list)
99      */

100     public Class JavaDoc loadProxyClass(String JavaDoc codebase, String JavaDoc[] interfaces, ClassLoader JavaDoc defaultLoader)
101             throws MalformedURLException JavaDoc, ClassNotFoundException JavaDoc {
102         return defaultProvider.loadProxyClass(codebase, interfaces, defaultLoader);
103     }
104
105     /**
106      * Returns a class loader that loads classes from the given codebase URL
107      * path.
108      * @param codebase the list of URLs (space-separated) from which the
109      * returned class loader will load classes from, or <code>null</code>
110      * @return a class loader that loads classes from the given codebase URL
111      * path
112      * @throws MalformedURLException if <code>codebase</code> is non-<code>null</code>
113      * and contains an invalid URL, or if <code>codebase</code> is
114      * <code>null</code> and the system property
115      * <code>java.rmi.server.codebase</code> contains an invalid URL
116      */

117     public ClassLoader JavaDoc getClassLoader(String JavaDoc codebase) throws MalformedURLException JavaDoc {
118         return defaultProvider.getClassLoader(codebase);
119     }
120
121     /**
122      * Returns the annotation string (representing a location for the class
123      * definition) that RMI will use to annotate the class descriptor when
124      * marshalling objects of the given class.<br>
125      * By default, remove rmi annotations of JClassLoader class. Between two
126      * JOnAS, commons classes are the same, don't need t have a bigger
127      * annotation. When local call is set, always disable annotation.
128      * @param cl the class to obtain the annotation for
129      * @return a string to be used to annotate the given class when it gets
130      * marshalled, or <code>null</code>
131      */

132     public String JavaDoc getClassAnnotation(Class JavaDoc cl) {
133         ClassLoader JavaDoc loader = cl.getClassLoader();
134
135         // Init values
136
if (!carolIsInitialized) {
137             String JavaDoc sValue = System.getProperty(CarolDefaultValues.LOCAL_JRMP_PROPERTY, "init");
138             if (!sValue.equals("init")) {
139                 carolIsOptimized = new Boolean JavaDoc(sValue).booleanValue();
140                 carolIsInitialized = true;
141             }
142
143         }
144
145         if (loader instanceof JClassLoader) {
146             return null;
147         } else if ((loader instanceof URLClassLoader JavaDoc) && (carolIsOptimized)) {
148             return null;
149         } else {
150             return defaultProvider.getClassAnnotation(cl);
151         }
152     }
153 }
154
Popular Tags