001: /*
002: * JBoss, Home of Professional Open Source.
003: * Copyright 2006, Red Hat Middleware LLC, and individual contributors
004: * as indicated by the @author tags. See the copyright.txt file in the
005: * distribution for a full listing of individual contributors.
006: *
007: * This is free software; you can redistribute it and/or modify it
008: * under the terms of the GNU Lesser General Public License as
009: * published by the Free Software Foundation; either version 2.1 of
010: * the License, or (at your option) any later version.
011: *
012: * This software is distributed in the hope that it will be useful,
013: * but WITHOUT ANY WARRANTY; without even the implied warranty of
014: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
015: * Lesser General Public License for more details.
016: *
017: * You should have received a copy of the GNU Lesser General Public
018: * License along with this software; if not, write to the Free
019: * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
020: * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
021: */
022: package org.jboss.mx.server;
023:
024: import java.io.InputStream;
025: import java.io.ObjectInputStream;
026: import java.io.ObjectStreamClass;
027: import java.io.IOException;
028:
029: import java.lang.reflect.Proxy;
030:
031: /**
032: * This replaces the EjbossInputStream in the storage package.
033: * The input stream will take a class loader in its constructor and look
034: * into it to retrieve the class definitions.
035: * It is used throughout the server to deserialize parameters and objects
036: * whose definition are in a jar and not the global classpath
037: * It also has better comments than the previous version.
038: *
039: * @author <a href="rickard@dreambean.com">Rickard Oberg</a>
040: * @since Ejboss 0.9
041: */
042:
043: public class ObjectInputStreamWithClassLoader extends ObjectInputStream {
044:
045: /**
046: * The classloader to use when the default classloader cannot find
047: * the classes in the stream.
048: */
049: ClassLoader cl;
050:
051: /******************************************************************************/
052: /******************************************************************************/
053: /*
054: /* CONSTRUCTORS
055: /*
056: /******************************************************************************/
057: /******************************************************************************/
058:
059: /**
060: * Construct a new instance with the given classloader and input stream.
061: *
062: * @param ClassLoader classloader to use
063: * @param InputStream stream to read objects from
064: */
065: public ObjectInputStreamWithClassLoader(InputStream in,
066: ClassLoader cl) throws IOException {
067:
068: super (in);
069:
070: this .cl = cl;
071: }
072:
073: /******************************************************************************/
074: /******************************************************************************/
075: /*
076: /* OVERWRITING <ObjectInputStream>
077: /*
078: /******************************************************************************/
079: /******************************************************************************/
080:
081: /**
082: * Resolve the class described in the osc parameter. First, try the
083: * default classloader (implemented by the super class). If it cannot
084: * load the class, try the classloader given to this instance.
085: *
086: * @param ObjectStreamClass class description object
087: * @return the Class corresponding to class description
088: * @exception IOException if an I/O error occurs
089: * @exception ClassNotFoundException if the class cannot be found by the classloader
090: */
091: protected Class resolveClass(ObjectStreamClass osc)
092: throws IOException, ClassNotFoundException {
093:
094: return cl.loadClass(osc.getName());
095: }
096:
097: protected Class resolveProxyClass(String[] interfaces)
098: throws IOException, ClassNotFoundException {
099:
100: Class[] interfacesClass = new Class[interfaces.length];
101: for (int i = 0; i < interfaces.length; i++) {
102: interfacesClass[i] = Class
103: .forName(interfaces[i], false, cl);
104: }
105:
106: return Proxy.getProxyClass(cl, interfacesClass);
107: }
108: }
|