001: /*
002: * $RCSfile: SerializableStateImpl.java,v $
003: *
004: * Copyright (c) 2005 Sun Microsystems, Inc. All rights reserved.
005: *
006: * Use is subject to license terms.
007: *
008: * $Revision: 1.1 $
009: * $Date: 2005/02/11 04:56:54 $
010: * $State: Exp $
011: */
012: package com.sun.media.jai.rmi;
013:
014: import java.awt.RenderingHints;
015: import java.io.Serializable;
016: import java.lang.reflect.Method;
017: import javax.media.jai.remote.SerializableState;
018: import javax.media.jai.remote.SerializerFactory;
019:
020: /**
021: * Framework class for adding <code>Serializer</code>s based on
022: * <code>SerializableState</code> implementations which support one or
023: * more classes or interfaces.
024: *
025: * <p> Extending classes MUST:
026: * <ol>
027: * <li> be public;</li>
028: * <li> provide a single public constructor with exactly the same signature as
029: * the protected constructor of this class;</li>
030: * <li> provide a static override of <code>getSupportedClasses()</code>;</li>
031: * <li> implement the (de)serialization methods <code>writeObject()</code>
032: * and <code>readObject()</code>; and</li>
033: * <li> add the class to <code>SerializerImpl.registerSerializers()</code> as
034: * <pre>
035: * registerSerializers(MySerializableState.class);
036: * </pre></li>
037: * </ol>
038: *
039: * @since 1.1
040: */
041: public abstract class SerializableStateImpl implements
042: SerializableState {
043: protected Class theClass;
044: protected transient Object theObject;
045:
046: /**
047: * Returns the classes supported by this SerializableState.
048: * Subclasses MUST override this method with their own STATIC method.
049: */
050: public static Class[] getSupportedClasses() {
051: throw new RuntimeException(JaiI18N
052: .getString("SerializableStateImpl0"));
053: }
054:
055: /**
056: * Whether the SerializableStateImpl permits its Serializer to
057: * serialize subclasses of the supported class(es).
058: * Subclasses SHOULD override this method to return "true" with their
059: * own STATIC method IF AND ONLY IF they support subclass serialization.
060: */
061: public static boolean permitsSubclasses() {
062: return false;
063: }
064:
065: /**
066: * Constructor. All subclasses MUST have exactly ONE constructor with
067: * the SAME signature as this constructor.
068: */
069: protected SerializableStateImpl(Class c, Object o, RenderingHints h) {
070: if (c == null || o == null) {
071: throw new IllegalArgumentException(JaiI18N
072: .getString("SerializableStateImpl1"));
073: } else {
074: boolean isInterface = c.isInterface();
075: if (isInterface && !c.isInstance(o)) {
076: throw new IllegalArgumentException(JaiI18N
077: .getString("SerializableStateImpl2"));
078: } else if (!isInterface) {
079: boolean permitsSubclasses = false;
080: try {
081: Method m = this .getClass().getMethod(
082: "permitsSubclasses", null);
083: permitsSubclasses = ((Boolean) m.invoke(null, null))
084: .booleanValue();
085: } catch (Exception e) {
086: throw new IllegalArgumentException(JaiI18N
087: .getString("SerializableStateImpl5"));
088: }
089:
090: if (!permitsSubclasses && !c.equals(o.getClass())) {
091: throw new IllegalArgumentException(JaiI18N
092: .getString("SerializableStateImpl3"));
093: } else if (permitsSubclasses
094: && !c.isAssignableFrom(o.getClass())) {
095: throw new IllegalArgumentException(JaiI18N
096: .getString("SerializableStateImpl4"));
097: }
098: }
099: }
100: theClass = c;
101: theObject = o;
102: }
103:
104: public Class getObjectClass() {
105: return theClass;
106: }
107:
108: public Object getObject() {
109: return theObject;
110: }
111:
112: protected Object getSerializableForm(Object object) {
113: if (object instanceof Serializable)
114: return object;
115: if (object != null)
116: try {
117: object = SerializerFactory.getState(object, null);
118: } catch (Exception e) {
119: object = null;
120: }
121: return object;
122: }
123:
124: protected Object getDeserializedFrom(Object object) {
125: if (object instanceof SerializableState)
126: object = ((SerializableState) object).getObject();
127: return object;
128: }
129: }
|