001: /**
002: * JOnAS: Java(TM) Open Application Server
003: * Copyright (C) 1999-2005 Bull S.A.
004: * Contact: jonas-team@objectweb.org
005: *
006: * This library is free software; you can redistribute it and/or
007: * modify it under the terms of the GNU Lesser General Public
008: * License as published by the Free Software Foundation; either
009: * version 2.1 of the License, or any later version.
010: *
011: * This library is distributed in the hope that it will be useful,
012: * but WITHOUT ANY WARRANTY; without even the implied warranty of
013: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
014: * Lesser General Public License for more details.
015: *
016: * You should have received a copy of the GNU Lesser General Public
017: * License along with this library; if not, write to the Free Software
018: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
019: * USA
020: *
021: * --------------------------------------------------------------------------
022: * $Id: RemoteClassLoaderSpi.java 7557 2005-10-21 11:56:51Z coqp $
023: * --------------------------------------------------------------------------
024: */package org.objectweb.jonas.server;
025:
026: import java.net.MalformedURLException;
027: import java.net.URLClassLoader;
028: import java.rmi.server.RMIClassLoader;
029: import java.rmi.server.RMIClassLoaderSpi;
030:
031: import org.objectweb.carol.util.configuration.CarolDefaultValues;
032:
033: /**
034: * Class <code>RemoteClassLoaderSpi</code> is the CAROL JRMP CLass Loader SPI
035: * for serialization performances.
036: * @author Guillaume Riviere (Guillaume.Riviere@inrialpes.fr)
037: */
038: public class RemoteClassLoaderSpi extends RMIClassLoaderSpi {
039:
040: /**
041: * Carol was already initialized
042: */
043: private static boolean carolIsInitialized = false;
044:
045: /**
046: * Local call optimization is set
047: */
048: private static boolean carolIsOptimized = false;
049:
050: /**
051: * Current provider
052: */
053: private final RMIClassLoaderSpi defaultProvider = RMIClassLoader
054: .getDefaultProviderInstance();
055:
056: /**
057: * Loads a class from a codebase URL path, optionally using the supplied
058: * loader.
059: * @param codebase the list of URLs (separated by spaces) to load the class
060: * from, or <code>null</code>
061: * @param name the name of the class to load
062: * @param defaultLoader additional contextual class loader to use, or
063: * <code>null</code>
064: * @return the <code>Class</code> object representing the loaded class
065: * @throws MalformedURLException if <code>codebase</code> is non-<code>null</code>
066: * and contains an invalid URL, or if <code>codebase</code> is
067: * <code>null</code> and the system property
068: * <code>java.rmi.server.codebase</code> contains an invalid URL
069: * @throws ClassNotFoundException if a definition for the class could not be
070: * found at the specified location
071: */
072: public Class loadClass(String codebase, String name,
073: ClassLoader defaultLoader) throws MalformedURLException,
074: ClassNotFoundException {
075: return defaultProvider.loadClass(codebase, name, defaultLoader);
076: }
077:
078: /**
079: * Loads a dynamic proxy class (see {@link java.lang.reflect.Proxy} that
080: * implements a set of interfaces with the given names from a codebase URL
081: * path, optionally using the supplied loader.
082: * @param codebase the list of URLs (space-separated) to load classes from,
083: * or <code>null</code>
084: * @param interfaces the names of the interfaces for the proxy class to
085: * implement
086: * @return a dynamic proxy class that implements the named interfaces
087: * @param defaultLoader additional contextual class loader to use, or
088: * <code>null</code>
089: * @throws MalformedURLException if <code>codebase</code> is non-<code>null</code>
090: * and contains an invalid URL, or if <code>codebase</code> is
091: * <code>null</code> and the system property
092: * <code>java.rmi.server.codebase</code> contains an invalid URL
093: * @throws ClassNotFoundException if a definition for one of the named
094: * interfaces could not be found at the specified location, or if
095: * creation of the dynamic proxy class failed (such as if
096: * {@link java.lang.reflect.Proxy#getProxyClass(ClassLoader,Class[])}
097: * would throw an <code>IllegalArgumentException</code> for the
098: * given interface list)
099: */
100: public Class loadProxyClass(String codebase, String[] interfaces,
101: ClassLoader defaultLoader) throws MalformedURLException,
102: ClassNotFoundException {
103: return defaultProvider.loadProxyClass(codebase, interfaces,
104: defaultLoader);
105: }
106:
107: /**
108: * Returns a class loader that loads classes from the given codebase URL
109: * path.
110: * @param codebase the list of URLs (space-separated) from which the
111: * returned class loader will load classes from, or <code>null</code>
112: * @return a class loader that loads classes from the given codebase URL
113: * path
114: * @throws MalformedURLException if <code>codebase</code> is non-<code>null</code>
115: * and contains an invalid URL, or if <code>codebase</code> is
116: * <code>null</code> and the system property
117: * <code>java.rmi.server.codebase</code> contains an invalid URL
118: */
119: public ClassLoader getClassLoader(String codebase)
120: throws MalformedURLException {
121: return defaultProvider.getClassLoader(codebase);
122: }
123:
124: /**
125: * Returns the annotation string (representing a location for the class
126: * definition) that RMI will use to annotate the class descriptor when
127: * marshalling objects of the given class.<br>
128: * By default, remove rmi annotations of JClassLoader class. Between two
129: * JOnAS, commons classes are the same, don't need t have a bigger
130: * annotation. When local call is set, always disable annotation.
131: * @param cl the class to obtain the annotation for
132: * @return a string to be used to annotate the given class when it gets
133: * marshalled, or <code>null</code>
134: */
135: public String getClassAnnotation(Class cl) {
136: ClassLoader loader = cl.getClassLoader();
137:
138: // Init values
139: if (!carolIsInitialized) {
140: String sValue = System.getProperty(
141: CarolDefaultValues.LOCALREG_JRMP_PROPERTY, "init");
142: if (!sValue.equals("init")) {
143: carolIsOptimized = new Boolean(sValue).booleanValue();
144: carolIsInitialized = true;
145: }
146:
147: }
148:
149: if (loader instanceof JClassLoader) {
150: return null;
151: } else if ((loader instanceof URLClassLoader)
152: && (carolIsOptimized)) {
153: return null;
154: } else {
155: return defaultProvider.getClassAnnotation(cl);
156: }
157: }
158: }
|