001: /**
002: * ===========================================
003: * JFreeReport : a free Java reporting library
004: * ===========================================
005: *
006: * Project Info: http://reporting.pentaho.org/
007: *
008: * (C) Copyright 2001-2007, by Object Refinery Ltd, Pentaho Corporation and Contributors.
009: *
010: * This library is free software; you can redistribute it and/or modify it under the terms
011: * of the GNU Lesser General Public License as published by the Free Software Foundation;
012: * either version 2.1 of the License, or (at your option) any later version.
013: *
014: * This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
015: * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
016: * See the GNU Lesser General Public License for more details.
017: *
018: * You should have received a copy of the GNU Lesser General Public License along with this
019: * library; if not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330,
020: * Boston, MA 02111-1307, USA.
021: *
022: * [Java is a trademark or registered trademark of Sun Microsystems, Inc.
023: * in the United States and other countries.]
024: *
025: * ------------
026: * SerializerHelper.java
027: * ------------
028: * (C) Copyright 2001-2007, by Object Refinery Ltd, Pentaho Corporation and Contributors.
029: */package org.jfree.report.util;
030:
031: import java.io.IOException;
032: import java.io.NotSerializableException;
033: import java.io.ObjectInputStream;
034: import java.io.ObjectOutputStream;
035: import java.io.Serializable;
036: import java.util.HashMap;
037: import java.util.Iterator;
038:
039: import org.jfree.report.util.serializers.BasicStrokeSerializer;
040: import org.jfree.report.util.serializers.ColorSerializer;
041: import org.jfree.report.util.serializers.Dimension2DSerializer;
042: import org.jfree.report.util.serializers.Ellipse2DSerializer;
043: import org.jfree.report.util.serializers.GeneralPathSerializer;
044: import org.jfree.report.util.serializers.Line2DSerializer;
045: import org.jfree.report.util.serializers.PageFormatSerializer;
046: import org.jfree.report.util.serializers.Point2DSerializer;
047: import org.jfree.report.util.serializers.Rectangle2DSerializer;
048: import org.jfree.util.ClassComparator;
049:
050: /**
051: * The SerializeHelper is used to make implementing custom serialization handlers easier.
052: * Handlers for certain object types need to be added to this helper before this
053: * implementation is usable.
054: *
055: * @author Thomas Morgner
056: * @deprecated Use JCommon-Serializer directly. This class will be removed.
057: */
058: public class SerializerHelper {
059: /**
060: * The singleton instance of the serialize helper.
061: */
062: private static SerializerHelper singleton;
063:
064: /**
065: * Returns or creates a new SerializerHelper. When a new instance is created by this
066: * method, all known SerializeMethods are registered.
067: *
068: * @return the SerializerHelper singleton instance.
069: */
070: public static synchronized SerializerHelper getInstance() {
071: if (singleton == null) {
072: singleton = new SerializerHelper();
073: singleton.registerMethod(new BasicStrokeSerializer());
074: singleton.registerMethod(new ColorSerializer());
075: singleton.registerMethod(new Dimension2DSerializer());
076: singleton.registerMethod(new Ellipse2DSerializer());
077: singleton.registerMethod(new Line2DSerializer());
078: singleton.registerMethod(new Point2DSerializer());
079: singleton.registerMethod(new Rectangle2DSerializer());
080: //singleton.registerMethod(new BandLayoutManagerSerializer());
081: singleton.registerMethod(new PageFormatSerializer());
082: singleton.registerMethod(new GeneralPathSerializer());
083: }
084: return singleton;
085: }
086:
087: /**
088: * This method can be used to replace the singleton instance of this helper.
089: *
090: * @param helper the new instance of the serialize helper.
091: */
092: protected static void setInstance(final SerializerHelper helper) {
093: singleton = helper;
094: }
095:
096: /**
097: * A collection of the serializer methods.
098: */
099: private final HashMap methods;
100:
101: /**
102: * A class comparator for searching the super class of an certain class.
103: */
104: private final ClassComparator comparator;
105:
106: /**
107: * Creates a new SerializerHelper.
108: */
109: protected SerializerHelper() {
110: this .comparator = new ClassComparator();
111: this .methods = new HashMap();
112: }
113:
114: /**
115: * Registers a new SerializeMethod with this SerializerHelper.
116: *
117: * @param method the method that should be registered.
118: */
119: public void registerMethod(final SerializeMethod method) {
120: this .methods.put(method.getObjectClass(), method);
121: }
122:
123: /**
124: * Deregisters a new SerializeMethod with this SerializerHelper.
125: *
126: * @param method the method that should be deregistered.
127: */
128: public void unregisterMethod(final SerializeMethod method) {
129: this .methods.remove(method.getObjectClass());
130: }
131:
132: /**
133: * Returns the collection of all registered serialize methods.
134: *
135: * @return a collection of the registered serialize methods.
136: */
137: protected HashMap getMethods() {
138: return methods;
139: }
140:
141: /**
142: * Returns the class comparator instance used to find correct super classes.
143: *
144: * @return the class comparator.
145: */
146: protected ClassComparator getComparator() {
147: return comparator;
148: }
149:
150: /**
151: * Looks up the SerializeMethod for the given class or null if there is no
152: * SerializeMethod for the given class.
153: *
154: * @param c the class for which we want to lookup a serialize method.
155: * @return the method or null, if there is no registered method for the class.
156: */
157: protected SerializeMethod getSerializer(final Class c) {
158: final SerializeMethod sm = (SerializeMethod) methods.get(c);
159: if (sm != null) {
160: return sm;
161: }
162: return getSuperClassObjectDescription(c, null);
163: }
164:
165: /**
166: * Looks up the SerializeMethod for the given class or null if there is no
167: * SerializeMethod for the given class. This method searches all superclasses.
168: *
169: * @param d the class for which we want to lookup a serialize method.
170: * @param knownSuperClass the known super class, if any or null.
171: * @return the method or null, if there is no registered method for the class.
172: */
173: protected SerializeMethod getSuperClassObjectDescription(
174: final Class d, SerializeMethod knownSuperClass) {
175: final Iterator keys = methods.keySet().iterator();
176: while (keys.hasNext()) {
177: final Class keyClass = (Class) keys.next();
178: if (keyClass.isAssignableFrom(d)) {
179: final SerializeMethod od = (SerializeMethod) methods
180: .get(keyClass);
181: if (knownSuperClass == null) {
182: knownSuperClass = od;
183: } else {
184: if (comparator.isComparable(knownSuperClass
185: .getObjectClass(), od.getObjectClass())) {
186: if (comparator.compare(knownSuperClass
187: .getObjectClass(), od.getObjectClass()) < 0) {
188: knownSuperClass = od;
189: }
190: }
191: }
192: }
193: }
194: return knownSuperClass;
195: }
196:
197: /**
198: * Writes a serializable object description to the given object output stream. This
199: * method selects the best serialize helper method for the given object.
200: *
201: * @param o the to be serialized object.
202: * @param out the outputstream that should receive the object.
203: * @throws IOException if an I/O error occured.
204: */
205: public void writeObject(final Object o, final ObjectOutputStream out)
206: throws IOException {
207: if (o == null) {
208: out.writeByte(0);
209: return;
210: }
211: if (o instanceof Serializable) {
212: out.writeByte(1);
213: out.writeObject(o);
214: return;
215: }
216:
217: final SerializeMethod m = getSerializer(o.getClass());
218: if (m == null) {
219: throw new NotSerializableException(o.getClass().getName());
220: }
221: out.writeByte(2);
222: out.writeObject(m.getObjectClass());
223: m.writeObject(o, out);
224: }
225:
226: /**
227: * Reads the object from the object input stream. This object selects the best
228: * serializer to read the object.
229: * <p/>
230: * Make sure, that you use the same configuration (library and class versions,
231: * registered methods in the SerializerHelper) for reading as you used for writing.
232: *
233: * @param in the object input stream from where to read the serialized data.
234: * @return the generated object.
235: *
236: * @throws IOException if reading the stream failed.
237: * @throws ClassNotFoundException if serialized object class cannot be found.
238: */
239: public Object readObject(final ObjectInputStream in)
240: throws IOException, ClassNotFoundException {
241: final int type = in.readByte();
242: if (type == 0) {
243: return null;
244: }
245: if (type == 1) {
246: return in.readObject();
247: }
248: final Class c = (Class) in.readObject();
249: final SerializeMethod m = getSerializer(c);
250: if (m == null) {
251: throw new NotSerializableException(c.getName());
252: }
253: return m.readObject(in);
254: }
255: }
|