001: /*
002: * Distributed as part of c3p0 v.0.9.1.2
003: *
004: * Copyright (C) 2005 Machinery For Change, Inc.
005: *
006: * Author: Steve Waldman <swaldman@mchange.com>
007: *
008: * This library is free software; you can redistribute it and/or modify
009: * it under the terms of the GNU Lesser General Public License version 2.1, as
010: * published by the Free Software Foundation.
011: *
012: * This software is distributed in the hope that it will be useful,
013: * but WITHOUT ANY WARRANTY; without even the implied warranty of
014: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
015: * GNU Lesser General Public License for more details.
016: *
017: * You should have received a copy of the GNU Lesser General Public License
018: * along with this software; see the file LICENSE. If not, write to the
019: * Free Software Foundation, Inc., 59 Temple Place, Suite 330,
020: * Boston, MA 02111-1307, USA.
021: */
022:
023: package com.mchange.v2.codegen.bean;
024:
025: import java.util.*;
026: import java.io.Serializable;
027: import java.io.IOException;
028: import com.mchange.v2.codegen.IndentedWriter;
029: import com.mchange.v2.ser.IndirectPolicy;
030:
031: public class IndirectingSerializableExtension extends
032: SerializableExtension {
033: protected String findIndirectorExpr;
034: protected String indirectorClassName;
035:
036: /**
037: * We expect this indirector to be a public class with a public no_arg ctor;
038: * If you need the indirector initialized somehow, you'll have to extend
039: * the class.
040: *
041: * @see #writeInitializeIndirector
042: * @see #writeExtraDeclarations
043: */
044: public IndirectingSerializableExtension(String indirectorClassName) {
045: this .indirectorClassName = indirectorClassName;
046: this .findIndirectorExpr = "new " + indirectorClassName + "()";
047: }
048:
049: protected IndirectingSerializableExtension() {
050: }
051:
052: public Collection extraSpecificImports() {
053: Collection col = super .extraSpecificImports();
054: col.add(indirectorClassName);
055: col.add("com.mchange.v2.ser.IndirectlySerialized");
056: col.add("com.mchange.v2.ser.Indirector");
057: col.add("com.mchange.v2.ser.SerializableUtils");
058: col.add("java.io.NotSerializableException");
059: return col;
060: }
061:
062: protected IndirectPolicy indirectingPolicy(Property prop,
063: Class propType) {
064: if (Serializable.class.isAssignableFrom(propType))
065: return IndirectPolicy.DEFINITELY_DIRECT;
066: else
067: return IndirectPolicy.INDIRECT_ON_EXCEPTION;
068: }
069:
070: /**
071: * hook method... does nothing by default... override at will.
072: * The indirector will be called, uh, "indirector".
073: * You are in the middle of a method when you define this.
074: */
075: protected void writeInitializeIndirector(Property prop,
076: Class propType, IndentedWriter iw) throws IOException {
077: }
078:
079: protected void writeExtraDeclarations(ClassInfo info,
080: Class super classType, Property[] props, Class[] propTypes,
081: IndentedWriter iw) throws IOException {
082: }
083:
084: public void generate(ClassInfo info, Class super classType,
085: Property[] props, Class[] propTypes, IndentedWriter iw)
086: throws IOException {
087: super .generate(info, super classType, props, propTypes, iw);
088: writeExtraDeclarations(info, super classType, props, propTypes,
089: iw);
090: }
091:
092: protected void writeStoreObject(Property prop, Class propType,
093: IndentedWriter iw) throws IOException {
094: IndirectPolicy policy = indirectingPolicy(prop, propType);
095: if (policy == IndirectPolicy.DEFINITELY_INDIRECT)
096: writeIndirectStoreObject(prop, propType, iw);
097: else if (policy == IndirectPolicy.INDIRECT_ON_EXCEPTION) {
098: iw.println("try");
099: iw.println("{");
100: iw.upIndent();
101: iw.println("//test serialize");
102: iw.println("SerializableUtils.toByteArray("
103: + prop.getName() + ");");
104: super .writeStoreObject(prop, propType, iw);
105: iw.downIndent();
106: iw.println("}");
107: iw.println("catch (NotSerializableException nse)");
108: iw.println("{");
109: iw.upIndent();
110: writeIndirectStoreObject(prop, propType, iw);
111: iw.downIndent();
112: iw.println("}");
113: } else if (policy == IndirectPolicy.DEFINITELY_DIRECT)
114: super .writeStoreObject(prop, propType, iw);
115: else
116: throw new InternalError(
117: "indirectingPolicy() overridden to return unknown policy: "
118: + policy);
119: }
120:
121: protected void writeIndirectStoreObject(Property prop,
122: Class propType, IndentedWriter iw) throws IOException {
123: iw.println("try");
124: iw.println("{");
125: iw.upIndent();
126:
127: iw.println("Indirector indirector = " + findIndirectorExpr
128: + ';');
129: writeInitializeIndirector(prop, propType, iw);
130: iw.println("oos.writeObject( indirector.indirectForm( "
131: + prop.getName() + " ) );");
132:
133: iw.downIndent();
134: iw.println("}");
135: iw.println("catch (IOException indirectionIOException)");
136: iw.println("{ throw indirectionIOException; }");
137: iw.println("catch (Exception indirectionOtherException)");
138: iw
139: .println("{ throw new IOException(\"Problem indirectly serializing "
140: + prop.getName()
141: + ": \" + indirectionOtherException.toString() ); }");
142: }
143:
144: protected void writeUnstoreObject(Property prop, Class propType,
145: IndentedWriter iw) throws IOException {
146: IndirectPolicy policy = indirectingPolicy(prop, propType);
147: if (policy == IndirectPolicy.DEFINITELY_INDIRECT
148: || policy == IndirectPolicy.INDIRECT_ON_EXCEPTION) {
149: iw.println("Object o = ois.readObject();");
150: iw
151: .println("if (o instanceof IndirectlySerialized) o = ((IndirectlySerialized) o).getObject();");
152: iw.println("this." + prop.getName() + " = ("
153: + prop.getSimpleTypeName() + ") o;");
154: } else if (policy == IndirectPolicy.DEFINITELY_DIRECT)
155: super .writeUnstoreObject(prop, propType, iw);
156: else
157: throw new InternalError(
158: "indirectingPolicy() overridden to return unknown policy: "
159: + policy);
160: }
161:
162: }
|