001 /**
002 *
003 * Copyright 2004 Protique Ltd
004 *
005 * Licensed under the Apache License, Version 2.0 (the "License");
006 * you may not use this file except in compliance with the License.
007 * You may obtain a copy of the License at
008 *
009 * http://www.apache.org/licenses/LICENSE-2.0
010 *
011 * Unless required by applicable law or agreed to in writing, software
012 * distributed under the License is distributed on an "AS IS" BASIS,
013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014 * See the License for the specific language governing permissions and
015 * limitations under the License.
016 *
017 **/
018 package org.activemq.util;
019
020 import java.io.ByteArrayInputStream;
021 import java.io.ByteArrayOutputStream;
022 import java.io.IOException;
023 import java.io.InputStream;
024 import java.io.ObjectInputStream;
025 import java.io.ObjectOutputStream;
026 import java.io.ObjectStreamClass;
027 import java.io.OutputStream;
028 import java.lang.reflect.Proxy;
029
030 /**
031 * @version $Revision: 1.1.1.1 $
032 */
033 public class SerializationHelper {
034
035 static final private ClassLoader ACTIVEMQ_CLASSLOADER = SerializationHelper.class.getClassLoader();
036
037 /**
038 * Serialize an Object to a ByteArray
039 * @param object
040 * @return a byte[] array
041 * @throws IOException
042 */
043 public static byte[] writeObject(Object object) throws IOException {
044 ByteArrayOutputStream buffer = new ByteArrayOutputStream();
045 ObjectOutputStream out = new ObjectOutputStream(buffer);
046 out.writeObject(object);
047 out.close();
048 return buffer.toByteArray();
049 }
050
051 /**
052 * Read an Object from a byte array
053 * @param data
054 * @return
055 * @throws IOException
056 * @throws ClassNotFoundException
057 */
058 public static Object readObject(byte[] data) throws IOException, ClassNotFoundException {
059 ByteArrayInputStream buffer = new ByteArrayInputStream(data);
060 ObjectInputStreamExt in = new ObjectInputStreamExt(buffer);
061 Object answer = in.readObject();
062 in.close();
063 return answer;
064 }
065
066 /**
067 * read an object from an InputStream
068 * @param in
069 * @return
070 * @throws IOException
071 * @throws ClassNotFoundException
072 */
073 public static Object readObject(InputStream in) throws IOException, ClassNotFoundException{
074 ObjectInputStreamExt objIn = new ObjectInputStreamExt(in);
075 Object result = objIn.readObject();
076 return result;
077
078 }
079
080 /**
081 * Write an Object to an OutputStream
082 * @param out
083 * @param obj
084 * @throws IOException
085 */
086 public static void writeObject(OutputStream out, Object obj) throws IOException{
087 ObjectOutputStream objOut = new ObjectOutputStream(out);
088 objOut.writeObject(obj);
089 objOut.flush();
090 }
091
092 static public class ObjectInputStreamExt extends ObjectInputStream {
093
094 public ObjectInputStreamExt(InputStream in) throws IOException {
095 super(in);
096 }
097
098 protected Class resolveClass(ObjectStreamClass classDesc) throws IOException, ClassNotFoundException {
099 ClassLoader cl = Thread.currentThread().getContextClassLoader();
100 return load(classDesc.getName(), cl);
101 }
102
103 protected Class resolveProxyClass(String[] interfaces) throws IOException, ClassNotFoundException {
104 ClassLoader cl = Thread.currentThread().getContextClassLoader();
105 Class[] cinterfaces = new Class[interfaces.length];
106 for (int i = 0; i < interfaces.length; i++)
107 cinterfaces[i] = load(interfaces[i], cl);
108
109 try {
110 return Proxy.getProxyClass(cinterfaces[0].getClassLoader(), cinterfaces);
111 } catch (IllegalArgumentException e) {
112 throw new ClassNotFoundException(null, e);
113 }
114 }
115
116 private Class load(String className, ClassLoader cl) throws ClassNotFoundException {
117 try {
118 return ClassLoading.loadClass(className, cl);
119 } catch ( ClassNotFoundException e ) {
120 return ClassLoading.loadClass(className, ACTIVEMQ_CLASSLOADER);
121 }
122 }
123
124 }
125
126 }