KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > quadcap > sql > io > Extern


1 package com.quadcap.sql.io;
2
3 /* Copyright 1999 - 2003 Quadcap Software. All rights reserved.
4  *
5  * This software is distributed under the Quadcap Free Software License.
6  * This software may be used or modified for any purpose, personal or
7  * commercial. Open Source redistributions are permitted. Commercial
8  * redistribution of larger works derived from, or works which bundle
9  * this software requires a "Commercial Redistribution License"; see
10  * http://www.quadcap.com/purchase.
11  *
12  * Redistributions qualify as "Open Source" under one of the following terms:
13  *
14  * Redistributions are made at no charge beyond the reasonable cost of
15  * materials and delivery.
16  *
17  * Redistributions are accompanied by a copy of the Source Code or by an
18  * irrevocable offer to provide a copy of the Source Code for up to three
19  * years at the cost of materials and delivery. Such redistributions
20  * must allow further use, modification, and redistribution of the Source
21  * Code under substantially the same terms as this license.
22  *
23  * Redistributions of source code must retain the copyright notices as they
24  * appear in each source code file, these license terms, and the
25  * disclaimer/limitation of liability set forth as paragraph 6 below.
26  *
27  * Redistributions in binary form must reproduce this Copyright Notice,
28  * these license terms, and the disclaimer/limitation of liability set
29  * forth as paragraph 6 below, in the documentation and/or other materials
30  * provided with the distribution.
31  *
32  * The Software is provided on an "AS IS" basis. No warranty is
33  * provided that the Software is free of defects, or fit for a
34  * particular purpose.
35  *
36  * Limitation of Liability. Quadcap Software shall not be liable
37  * for any damages suffered by the Licensee or any third party resulting
38  * from use of the Software.
39  */

40
41 import java.io.Externalizable JavaDoc;
42 import java.io.InvalidClassException JavaDoc;
43 import java.io.IOException JavaDoc;
44 import java.io.ObjectInput JavaDoc;
45 import java.io.ObjectOutput JavaDoc;
46
47 import java.util.Hashtable JavaDoc;
48 import java.util.Vector JavaDoc;
49
50 import com.quadcap.util.Debug;
51
52 /**
53  * Adaptor class for objects that are themselves externalizable or
54  * which have externalize adaptors.
55  *
56  * @author Stan Bailes
57  */

58 public class Extern {
59     static Extern[] classes = new Extern[256];
60     static Hashtable JavaDoc classNames = new Hashtable JavaDoc();
61
62     static void add(String JavaDoc name, int code,
63             ExternalizeProxy proxy) {
64         add(name, code, proxy, null);
65     }
66
67     public static void add(String JavaDoc name, int code,
68                            ExternalizeProxy proxy, Object JavaDoc obj)
69     {
70     Extern ext = new Extern(name, code, proxy);
71         if (obj instanceof Externable) {
72             // Need to check that this
73
// object really implements the 'getExtern' call, and
74
// isn't using an inherited version...
75
Class JavaDoc cl = obj.getClass();
76             try {
77                 if (cl.getDeclaredMethod("getExtern", new Class JavaDoc[0]) == null) {
78                     throw new RuntimeException JavaDoc("Not Externable: " +
79                                                cl.getName());
80                 }
81             } catch (NoSuchMethodException JavaDoc e) {
82                 throw new RuntimeException JavaDoc("Not Externable: " +
83                                            cl.getName());
84             }
85             ((Externable)obj).setExtern(ext);
86         }
87     classes[code] = ext;
88     classNames.put(name, ext);
89     }
90
91     static void add(String JavaDoc name, int code) {
92     ExternalizeProxy p = null;
93         Object JavaDoc obj = null;
94     try {
95         Class JavaDoc c = Class.forName(name);
96         obj = c.newInstance();
97         if (obj instanceof ExternalizeProxy) {
98         p = (ExternalizeProxy)obj;
99         }
100     } catch (Throwable JavaDoc t) {
101         Debug.println(t.toString());
102     }
103     add(name, code, p, obj);
104     }
105
106     static {
107     // null 0
108
add("com.quadcap.sql.Column", 1);
109     add("com.quadcap.sql.DatabaseRoot", 2);
110     add("com.quadcap.sql.file.LogEntry", 3);
111     add("com.quadcap.sql.types.TypeBinary", 4);
112     add("com.quadcap.sql.AddTable", 5);
113     add("com.quadcap.sql.Table", 6);
114     add("com.quadcap.sql.types.TypeInt", 7);
115     add("com.quadcap.sql.types.TypeVarChar", 8);
116     add("com.quadcap.sql.types.ValueType", 9);
117     add("com.quadcap.sql.QuantifiedCompare", 10);
118     add("com.quadcap.sql.PrimaryKeyConstraint", 11);
119     add("com.quadcap.sql.types.TypeBlob", 12);
120     add("com.quadcap.sql.InsertRow", 13);
121     add("com.quadcap.sql.AddIndexEntry", 14);
122     add("com.quadcap.sql.types.ValueInteger", 15);
123     add("com.quadcap.sql.types.ValueString", 16);
124     add("com.quadcap.sql.NotNullConstraint", 17);
125     add("com.quadcap.sql.types.TypeDecimal", 18);
126     add("com.quadcap.sql.UniqueConstraint", 19);
127     add("com.quadcap.sql.types.ValueNull", 20);
128     add("com.quadcap.sql.CheckConstraint", 21);
129     add("com.quadcap.sql.BinaryExpression", 22);
130     add("com.quadcap.sql.NameExpression", 23);
131     add("com.quadcap.sql.ValueExpression", 24);
132     add("com.quadcap.sql.DefaultTableConstraint", 25);
133     add("com.quadcap.sql.types.ValueBlob", 26);
134     add("com.quadcap.sql.View", 27);
135     add("com.quadcap.sql.StmtCreateView", 28);
136     add("com.quadcap.sql.SelectExpression", 29);
137     add("com.quadcap.sql.SelectFromTable", 30);
138     add("com.quadcap.sql.InsertBlob", 31);
139     add("com.quadcap.sql.DeleteIndexEntry", 32);
140     add("com.quadcap.sql.ExportedKeyConstraint", 33);
141     add("com.quadcap.sql.DeleteRow", 34);
142     add("com.quadcap.sql.types.ValueScaledInteger", 35);
143     add("com.quadcap.sql.types.TypeReal", 36);
144     add("com.quadcap.sql.types.ValueDouble", 37);
145     add("com.quadcap.sql.types.TypeVarBinary", 38);
146     add("com.quadcap.sql.types.ValueOctets", 39);
147     add("com.quadcap.sql.types.TypeChar", 40);
148     add("com.quadcap.sql.types.TypeSmallInt", 41);
149     add("com.quadcap.sql.DropTable", 42);
150     add("com.quadcap.sql.SelectItem", 43);
151     add("com.quadcap.sql.UnaryExpression", 44);
152     add("com.quadcap.sql.AutoNumberConstraint", 45);
153     add("com.quadcap.sql.InExpression", 46);
154     add("com.quadcap.sql.TernaryExpression", 47);
155     add("com.quadcap.sql.StmtNull", 48);
156     add("com.quadcap.sql.AggregateExpression", 49);
157     add("com.quadcap.sql.VectorExpression", 50);
158     add("com.quadcap.sql.types.TypeTime", 51);
159     add("com.quadcap.sql.types.TypeTimestamp", 52);
160     add("com.quadcap.sql.types.TypeDate", 53);
161     add("com.quadcap.sql.types.ValueDate", 54);
162     add("com.quadcap.sql.types.ValueTime", 55);
163     add("com.quadcap.sql.types.ValueTimestamp", 56);
164     add("com.quadcap.sql.types.ValueInterval", 57);
165     add("com.quadcap.sql.types.TypeInterval", 58);
166     add("com.quadcap.sql.types.ValueShort", 59);
167     add("com.quadcap.sql.types.ValuePattern", 60);
168     add("com.quadcap.sql.UpdateRow", 61);
169     add("com.quadcap.sql.types.ValueBoolean", 62);
170     add("com.quadcap.sql.ImportedKeyConstraint", 63);
171     add("com.quadcap.sql.StmtRenameTable", 64);
172     add("com.quadcap.sql.RefcountBlob", 65);
173     add("com.quadcap.sql.AutoNumberStep", 66);
174     add("com.quadcap.sql.types.ValueLong", 67);
175     add("com.quadcap.sql.DeleteConstraint", 68);
176     add("com.quadcap.sql.AddConstraint", 69);
177     add("com.quadcap.sql.NonUniqueIndexConstraint", 70);
178     add("com.quadcap.sql.types.TypeBigInt", 71);
179     add("com.quadcap.sql.types.TypeTinyInt", 72);
180     add("com.quadcap.sql.types.ValueFloat", 73);
181     add("com.quadcap.sql.types.ValueByte", 74);
182     add("com.quadcap.sql.types.TypeBoolean", 75);
183     add("com.quadcap.sql.types.TypeAny", 76);
184     add("com.quadcap.sql.JoinedTable", 77);
185     add("com.quadcap.sql.AddColumn", 78);
186     add("com.quadcap.sql.FunctionExpression", 79);
187     add("com.quadcap.sql.AlterColumn", 80);
188     add("com.quadcap.sql.types.TypeClob", 81);
189     add("com.quadcap.sql.types.ValueClob", 82);
190     add("com.quadcap.sql.types.ValueUnknown", 83);
191     add("com.quadcap.sql.SelectFromItem", 84);
192     add("com.quadcap.sql.MergeExpression", 85);
193     add("com.quadcap.sql.types.ValueDefault", 86);
194     add("com.quadcap.sql.DropColumn", 87);
195
196     add("java.math.BigDecimal", 251,
197         new ExternProxyBigDecimal());
198     add("java.lang.Integer", 252,
199         new ExternProxyInteger());
200     add("java.lang.String", 253,
201         new ExternProxyString());
202     add("java.util.Vector", 254,
203         new ExternProxyVector());
204     }
205
206     public static Extern get(int code) {
207     try {
208         return classes[code];
209     } catch (Throwable JavaDoc t) {
210         return null;
211     }
212     }
213
214     //#ifdef DEBUG
215
public String JavaDoc toString() { return "[" + code + "]: " + className; }
216     //#endif
217

218     static Extern get(String JavaDoc className) {
219     return (Extern)classNames.get(className);
220     }
221
222     int code;
223     String JavaDoc className;
224     ExternalizeProxy proxy;
225     Class JavaDoc eclass;
226     
227     public Extern(String JavaDoc className, int code, ExternalizeProxy proxy) {
228     this.className = className;
229     this.code = code;
230     this.proxy = proxy;
231     if (proxy == null) {
232         try {
233         eclass = Class.forName(className);
234         } catch (Exception JavaDoc e) {
235         Debug.print(e);
236         throw new RuntimeException JavaDoc(e.toString());
237         }
238     }
239     }
240     
241     public Object JavaDoc readObject(ObjectInput JavaDoc in)
242     throws IOException JavaDoc, ClassNotFoundException JavaDoc
243     {
244     if (proxy != null) {
245         return proxy.readObject(in);
246     } else {
247         try {
248         Object JavaDoc obj = eclass.newInstance();
249         ((Externalizable JavaDoc)obj).readExternal(in);
250         return obj;
251         } catch (IllegalAccessException JavaDoc e) {
252         throw new InvalidClassException JavaDoc(className,
253                         "IllegalAccessException");
254         } catch (InstantiationException JavaDoc e) {
255         throw new InvalidClassException JavaDoc(className,
256                         "InstantiationException");
257         }
258     }
259     }
260
261     public void writeObject(ObjectOutput JavaDoc out, Object JavaDoc obj)
262     throws IOException JavaDoc
263     {
264     // XXX It turns out the com.quadcap.sql.file.Log knows about the
265
// XXX code because it wants to 'patch' already logged entries.
266
// XXX see Log.undo()
267
out.write(code);
268     if (proxy != null) {
269         proxy.writeObject(out, obj);
270     } else {
271         ((Externalizable JavaDoc)obj).writeExternal(out);
272     }
273     }
274 }
275
Popular Tags