001: /*
002: * $RCSfile: SerializerImpl.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:55 $
010: * $State: Exp $
011: */
012: package com.sun.media.jai.rmi;
013:
014: import java.awt.RenderingHints;
015: import java.lang.reflect.Constructor;
016: import java.lang.reflect.Method;
017: import javax.media.jai.remote.RemoteImagingException;
018: import javax.media.jai.remote.SerializableState;
019: import javax.media.jai.remote.Serializer;
020: import javax.media.jai.remote.SerializerFactory;
021: import javax.media.jai.util.ImagingException;
022: import javax.media.jai.util.ImagingListener;
023: import com.sun.media.jai.util.ImageUtil;
024:
025: /**
026: * Framework class for automatically creating <code>Serializer</code>s
027: * for <code>SerializableStateImpl</code> subclasses. Each subclass of
028: * <code>SerializableStateImpl</code> should add a statement like
029: * <pre>
030: * registerSerializers(MySerializableState.class);
031: * </pre>
032: * to the no-argument version of <code>registerSerializers()</code>.
033: * This latter method is invoked by the static initializer of
034: * <code>SerializerFactory</code>.
035: *
036: * @since 1.1
037: */
038: public final class SerializerImpl implements Serializer {
039: private Class theClass;
040: private boolean areSubclassesPermitted;
041: private Constructor ctor;
042:
043: /**
044: * Registers all known <code>Serializer</code>s with the
045: * <code>SerializerFactory</code>.
046: */
047: public static final void registerSerializers() {
048: registerSerializers(ColorModelState.class);
049: registerSerializers(DataBufferState.class);
050: registerSerializers(HashSetState.class);
051: registerSerializers(HashtableState.class);
052: registerSerializers(RasterState.class);
053: registerSerializers(RenderedImageState.class);
054: registerSerializers(RenderContextState.class);
055: registerSerializers(RenderingHintsState.class);
056: registerSerializers(RenderingKeyState.class);
057: registerSerializers(SampleModelState.class);
058: registerSerializers(VectorState.class);
059: registerSerializers(ShapeState.class);
060: }
061:
062: private static void registerSerializers(Class ssi) {
063: if (ssi == null) {
064: throw new IllegalArgumentException(JaiI18N
065: .getString("Generic0"));
066: }
067:
068: if (!SerializableStateImpl.class.isAssignableFrom(ssi)) {
069: throw new IllegalArgumentException(JaiI18N
070: .getString("SerializerImpl0"));
071: }
072:
073: ImagingListener listener = ImageUtil
074: .getImagingListener((RenderingHints) null);
075: Class[] classes = null;
076: try {
077: Method m1 = ssi.getMethod("getSupportedClasses", null);
078: classes = (Class[]) m1.invoke(null, null);
079: } catch (java.lang.NoSuchMethodException e) {
080: String message = JaiI18N.getString("SerializerImpl1");
081: listener.errorOccurred(message, new RemoteImagingException(
082: message, e), SerializerImpl.class, false);
083: } catch (java.lang.IllegalAccessException e) {
084: String message = JaiI18N.getString("SerializerImpl1");
085: listener.errorOccurred(message, new RemoteImagingException(
086: message, e), SerializerImpl.class, false);
087: } catch (java.lang.reflect.InvocationTargetException e) {
088: String message = JaiI18N.getString("SerializerImpl1");
089: listener.errorOccurred(message, new RemoteImagingException(
090: message, e), SerializerImpl.class, false);
091: }
092:
093: boolean supportsSubclasses = false;
094: try {
095: Method m2 = ssi.getMethod("permitsSubclasses", null);
096: Boolean b = (Boolean) m2.invoke(null, null);
097: supportsSubclasses = b.booleanValue();
098: } catch (java.lang.NoSuchMethodException e) {
099: String message = JaiI18N.getString("SerializerImpl4");
100: listener.errorOccurred(message, new RemoteImagingException(
101: message, e), SerializerImpl.class, false);
102: } catch (java.lang.IllegalAccessException e) {
103: String message = JaiI18N.getString("SerializerImpl4");
104: listener.errorOccurred(message, new RemoteImagingException(
105: message, e), SerializerImpl.class, false);
106: } catch (java.lang.reflect.InvocationTargetException e) {
107: String message = JaiI18N.getString("SerializerImpl4");
108: listener.errorOccurred(message, new RemoteImagingException(
109: message, e), SerializerImpl.class, false);
110: }
111:
112: int numClasses = classes.length;
113: for (int i = 0; i < numClasses; i++) {
114: Serializer s = new SerializerImpl(ssi, classes[i],
115: supportsSubclasses);
116: SerializerFactory.registerSerializer(s);
117: }
118: }
119:
120: /**
121: * Constructs a <code>SerializerImpl</code>. The parameter <code>c</code>
122: * is saved by reference. The parameter <code>c</code> is used to
123: * determine the standard <code>SerializableStateImpl</code> constructor
124: * which is saved by reference. The supplied parameters are not checked
125: * as this class should never be instantiated except from within
126: * <code>registerSerializers(Class)</code>.
127: */
128: protected SerializerImpl(Class ssi, // SerializableStateImpl subclass
129: Class c, boolean areSubclassesPermitted) {
130: theClass = c;
131: this .areSubclassesPermitted = areSubclassesPermitted;
132:
133: try {
134: Class[] paramTypes = new Class[] { Class.class,
135: Object.class, RenderingHints.class };
136: ctor = ssi.getConstructor(paramTypes);
137: } catch (java.lang.NoSuchMethodException e) {
138: String message = theClass.getName() + ": "
139: + JaiI18N.getString("SerializerImpl2");
140: sendExceptionToListener(message,
141: new RemoteImagingException(message, e));
142: }
143: }
144:
145: /**
146: * Creates a <code>SerializableState</code> using the
147: * <code>SerializableStateImpl</code> subclass constructor obtained
148: * by reflection.
149: */
150: public SerializableState getState(Object o, RenderingHints h) {
151: Object state = null;
152: try {
153: state = ctor.newInstance(new Object[] { theClass, o, h });
154: } catch (InstantiationException e) {
155: String message = theClass.getName() + ": "
156: + JaiI18N.getString("SerializerImpl3");
157: sendExceptionToListener(message,
158: new RemoteImagingException(message, e));
159: } catch (IllegalAccessException e) {
160: String message = theClass.getName() + ": "
161: + JaiI18N.getString("SerializerImpl3");
162: sendExceptionToListener(message,
163: new RemoteImagingException(message, e));
164: } catch (java.lang.reflect.InvocationTargetException e) {
165: String message = theClass.getName() + ": "
166: + JaiI18N.getString("SerializerImpl3");
167: sendExceptionToListener(message,
168: new RemoteImagingException(message, e));
169: }
170:
171: return (SerializableState) state;
172: }
173:
174: public Class getSupportedClass() {
175: return theClass;
176: }
177:
178: public boolean permitsSubclasses() {
179: return areSubclassesPermitted;
180: }
181:
182: private void sendExceptionToListener(String message, Exception e) {
183: ImagingListener listener = ImageUtil
184: .getImagingListener((RenderingHints) null);
185: listener.errorOccurred(message,
186: new ImagingException(message, e), this , false);
187: }
188: }
|