001: /*
002: * CoadunationLib: The coaduntion implementation library.
003: * Copyright (C) 2006 Rift IT Contracting
004: *
005: * This library is free software; you can redistribute it and/or
006: * modify it under the terms of the GNU Lesser General Public
007: * License as published by the Free Software Foundation; either
008: * version 2.1 of the License, or (at your option) any later version.
009: *
010: * This library is distributed in the hope that it will be useful,
011: * but WITHOUT ANY WARRANTY; without even the implied warranty of
012: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
013: * Lesser General Public License for more details.
014: *
015: * You should have received a copy of the GNU Lesser General Public
016: * License along with this library; if not, write to the Free Software
017: * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
018: *
019: * ObjectSerializer.java
020: *
021: * This class is responsible for serializing and deserializing objects.
022: */
023:
024: // package path
025: package com.rift.coad.lib.common;
026:
027: // java imports
028: import java.io.IOException;
029: import java.io.InputStream;
030: import java.io.ByteArrayInputStream;
031: import java.io.ByteArrayOutputStream;
032: import java.io.ObjectInputStream;
033: import java.io.ObjectOutputStream;
034: import java.io.ObjectStreamClass;
035:
036: /**
037: * This class is responsible for serializing and deserializing objects.
038: *
039: * @author Brett Chaldecott
040: */
041: public class ObjectSerializer {
042:
043: /**
044: * This class overrides the resolve to use the class loader on the
045: * thread to find the specified class.
046: */
047: public static class ClassLoaderObjectInputStream extends
048: ObjectInputStream {
049: /**
050: * This default constructor of the class loader object input stream.
051: *
052: * @exception IOException
053: */
054: public ClassLoaderObjectInputStream() throws IOException {
055: super ();
056: }
057:
058: /**
059: * This default constructor of the class loader object input stream.
060: *
061: * @param in The input stream for this object.
062: * @exception IOException
063: */
064: public ClassLoaderObjectInputStream(InputStream in)
065: throws IOException {
066: super (in);
067: }
068:
069: /**
070: * This method returns the class definition for the requested object.
071: *
072: * @return The class definition for the requested object.
073: * @param desc The description of the object.
074: * @exception IOException
075: * @exception ClassNotFoundException
076: */
077: protected Class resolveClass(ObjectStreamClass desc)
078: throws IOException, ClassNotFoundException {
079: // This works with arrays and classes
080: return Class.forName(desc.getName(), false, Thread
081: .currentThread().getContextClassLoader());
082: }
083:
084: }
085:
086: /**
087: * Creates a new instance of ObjectSerializer
088: */
089: private ObjectSerializer() {
090: }
091:
092: /**
093: * This method serializes the object passed to it.
094: *
095: * @return A byte array of the object.
096: * @param ref The reference to the object to serialize.
097: * @exception CommonException
098: */
099: public static byte[] serialize(Object ref) throws CommonException {
100: try {
101: if (!(ref instanceof java.io.Serializable)) {
102: throw new CommonException(
103: "This object is not serializable. "
104: + "Must implement from java.io.Serializable.");
105: }
106: ByteArrayOutputStream byteOutput = new ByteArrayOutputStream();
107: ObjectOutputStream objOutput = new ObjectOutputStream(
108: byteOutput);
109: objOutput.writeObject(ref);
110: objOutput.flush();
111: objOutput.close();
112: return byteOutput.toByteArray();
113: } catch (CommonException ex) {
114: throw ex;
115: } catch (Exception ex) {
116: throw new CommonException(
117: "Failed to serialize the object : "
118: + ex.getMessage(), ex);
119: }
120: }
121:
122: /**
123: * This method serializes the object passed to it.
124: *
125: * @return A byte array of the object.
126: * @param ref The reference to the object to serialize.
127: * @exception CommonException
128: */
129: public static Object deserialize(byte[] input)
130: throws CommonException {
131: try {
132: ByteArrayInputStream byteInput = new ByteArrayInputStream(
133: input);
134: ClassLoaderObjectInputStream objInput = new ClassLoaderObjectInputStream(
135: byteInput);
136: Object ref = objInput.readObject();
137: objInput.close();
138: return ref;
139: } catch (Exception ex) {
140: throw new CommonException(
141: "Failed to deserialize the object : "
142: + ex.getMessage(), ex);
143: }
144: }
145: }
|