KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > mchange > v2 > codegen > bean > IndirectingSerializableExtension


1 /*
2  * Distributed as part of c3p0 v.0.9.1
3  *
4  * Copyright (C) 2005 Machinery For Change, Inc.
5  *
6  * Author: Steve Waldman <swaldman@mchange.com>
7  *
8  * This library is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU Lesser General Public License version 2.1, as
10  * published by the Free Software Foundation.
11  *
12  * This software is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15  * GNU Lesser General Public License for more details.
16  *
17  * You should have received a copy of the GNU Lesser General Public License
18  * along with this software; see the file LICENSE. If not, write to the
19  * Free Software Foundation, Inc., 59 Temple Place, Suite 330,
20  * Boston, MA 02111-1307, USA.
21  */

22
23
24 package com.mchange.v2.codegen.bean;
25
26 import java.util.*;
27 import java.io.Serializable JavaDoc;
28 import java.io.IOException JavaDoc;
29 import com.mchange.v2.codegen.IndentedWriter;
30 import com.mchange.v2.ser.IndirectPolicy;
31
32 public class IndirectingSerializableExtension extends SerializableExtension
33 {
34     protected String JavaDoc findIndirectorExpr;
35     protected String JavaDoc indirectorClassName;
36
37     /**
38      * We expect this indirector to be a public class with a public no_arg ctor;
39      * If you need the indirector initialized somehow, you'll have to extend
40      * the class.
41      *
42      * @see #writeInitializeIndirector
43      * @see #writeExtraDeclarations
44      */

45     public IndirectingSerializableExtension( String JavaDoc indirectorClassName )
46     {
47     this.indirectorClassName = indirectorClassName;
48     this.findIndirectorExpr = "new " + indirectorClassName + "()";
49     }
50
51     protected IndirectingSerializableExtension()
52     {}
53
54     public Collection extraSpecificImports()
55     {
56     Collection col = super.extraSpecificImports();
57     col.add( indirectorClassName );
58     col.add( "com.mchange.v2.ser.IndirectlySerialized" );
59     col.add( "com.mchange.v2.ser.Indirector" );
60     col.add( "com.mchange.v2.ser.SerializableUtils" );
61     col.add( "java.io.NotSerializableException" );
62     return col;
63     }
64
65     protected IndirectPolicy indirectingPolicy( Property prop, Class JavaDoc propType )
66     {
67     if (Serializable JavaDoc.class.isAssignableFrom( propType ))
68         return IndirectPolicy.DEFINITELY_DIRECT;
69     else
70         return IndirectPolicy.INDIRECT_ON_EXCEPTION;
71     }
72
73     /**
74      * hook method... does nothing by default... override at will.
75      * The indirector will be called, uh, "indirector".
76      * You are in the middle of a method when you define this.
77      */

78     protected void writeInitializeIndirector( Property prop, Class JavaDoc propType, IndentedWriter iw ) throws IOException JavaDoc
79     {}
80
81     protected void writeExtraDeclarations(ClassInfo info, Class JavaDoc superclassType, Property[] props, Class JavaDoc[] propTypes, IndentedWriter iw)
82     throws IOException JavaDoc
83     {}
84
85     public void generate(ClassInfo info, Class JavaDoc superclassType, Property[] props, Class JavaDoc[] propTypes, IndentedWriter iw)
86     throws IOException JavaDoc
87     {
88     super.generate( info, superclassType, props, propTypes, iw);
89     writeExtraDeclarations( info, superclassType, props, propTypes, iw);
90     }
91
92     protected void writeStoreObject( Property prop, Class JavaDoc propType, IndentedWriter iw ) throws IOException JavaDoc
93     {
94     IndirectPolicy policy = indirectingPolicy( prop, propType );
95     if (policy == IndirectPolicy.DEFINITELY_INDIRECT)
96         writeIndirectStoreObject( prop, propType, iw );
97     else if (policy == IndirectPolicy.INDIRECT_ON_EXCEPTION)
98         {
99         iw.println("try");
100         iw.println("{");
101         iw.upIndent();
102         iw.println("//test serialize");
103         iw.println("SerializableUtils.toByteArray(" + 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         }
114     else if (policy == IndirectPolicy.DEFINITELY_DIRECT)
115         super.writeStoreObject( prop, propType, iw );
116     else
117         throw new InternalError JavaDoc("indirectingPolicy() overridden to return unknown policy: " + policy);
118     }
119
120     protected void writeIndirectStoreObject( Property prop, Class JavaDoc propType, IndentedWriter iw ) throws IOException JavaDoc
121     {
122     iw.println("try");
123     iw.println("{");
124     iw.upIndent();
125
126     iw.println("Indirector indirector = " + findIndirectorExpr + ';');
127     writeInitializeIndirector( prop, propType, iw );
128     iw.println("oos.writeObject( indirector.indirectForm( " + prop.getName() + " ) );");
129
130     iw.downIndent();
131     iw.println("}");
132     iw.println("catch (IOException indirectionIOException)");
133     iw.println("{ throw indirectionIOException; }");
134     iw.println("catch (Exception indirectionOtherException)");
135     iw.println("{ throw new IOException(\"Problem indirectly serializing " + prop.getName() + ": \" + indirectionOtherException.toString() ); }");
136     }
137
138     protected void writeUnstoreObject( Property prop, Class JavaDoc propType, IndentedWriter iw ) throws IOException JavaDoc
139     {
140     IndirectPolicy policy = indirectingPolicy( prop, propType );
141     if (policy == IndirectPolicy.DEFINITELY_INDIRECT || policy == IndirectPolicy.INDIRECT_ON_EXCEPTION)
142         {
143         iw.println("Object o = ois.readObject();");
144         iw.println("if (o instanceof IndirectlySerialized) o = ((IndirectlySerialized) o).getObject();");
145         iw.println("this." + prop.getName() + " = (" + prop.getSimpleTypeName() + ") o;");
146         }
147     else if (policy == IndirectPolicy.DEFINITELY_DIRECT)
148         super.writeUnstoreObject( prop, propType, iw );
149     else
150         throw new InternalError JavaDoc("indirectingPolicy() overridden to return unknown policy: " + policy);
151     }
152
153 }
154
Popular Tags