001: package com.quadcap.sql.io;
002:
003: /* Copyright 1999 - 2003 Quadcap Software. All rights reserved.
004: *
005: * This software is distributed under the Quadcap Free Software License.
006: * This software may be used or modified for any purpose, personal or
007: * commercial. Open Source redistributions are permitted. Commercial
008: * redistribution of larger works derived from, or works which bundle
009: * this software requires a "Commercial Redistribution License"; see
010: * http://www.quadcap.com/purchase.
011: *
012: * Redistributions qualify as "Open Source" under one of the following terms:
013: *
014: * Redistributions are made at no charge beyond the reasonable cost of
015: * materials and delivery.
016: *
017: * Redistributions are accompanied by a copy of the Source Code or by an
018: * irrevocable offer to provide a copy of the Source Code for up to three
019: * years at the cost of materials and delivery. Such redistributions
020: * must allow further use, modification, and redistribution of the Source
021: * Code under substantially the same terms as this license.
022: *
023: * Redistributions of source code must retain the copyright notices as they
024: * appear in each source code file, these license terms, and the
025: * disclaimer/limitation of liability set forth as paragraph 6 below.
026: *
027: * Redistributions in binary form must reproduce this Copyright Notice,
028: * these license terms, and the disclaimer/limitation of liability set
029: * forth as paragraph 6 below, in the documentation and/or other materials
030: * provided with the distribution.
031: *
032: * The Software is provided on an "AS IS" basis. No warranty is
033: * provided that the Software is free of defects, or fit for a
034: * particular purpose.
035: *
036: * Limitation of Liability. Quadcap Software shall not be liable
037: * for any damages suffered by the Licensee or any third party resulting
038: * from use of the Software.
039: */
040:
041: import java.io.Externalizable;
042: import java.io.InvalidClassException;
043: import java.io.IOException;
044: import java.io.ObjectInput;
045: import java.io.ObjectOutput;
046:
047: import java.util.Hashtable;
048: import java.util.Vector;
049:
050: import com.quadcap.util.Debug;
051:
052: /**
053: * Adaptor class for objects that are themselves externalizable or
054: * which have externalize adaptors.
055: *
056: * @author Stan Bailes
057: */
058: public class Extern {
059: static Extern[] classes = new Extern[256];
060: static Hashtable classNames = new Hashtable();
061:
062: static void add(String name, int code, ExternalizeProxy proxy) {
063: add(name, code, proxy, null);
064: }
065:
066: public static void add(String name, int code,
067: ExternalizeProxy proxy, Object obj) {
068: Extern ext = new Extern(name, code, proxy);
069: if (obj instanceof Externable) {
070: // Need to check that this
071: // object really implements the 'getExtern' call, and
072: // isn't using an inherited version...
073: Class cl = obj.getClass();
074: try {
075: if (cl.getDeclaredMethod("getExtern", new Class[0]) == null) {
076: throw new RuntimeException("Not Externable: "
077: + cl.getName());
078: }
079: } catch (NoSuchMethodException e) {
080: throw new RuntimeException("Not Externable: "
081: + cl.getName());
082: }
083: ((Externable) obj).setExtern(ext);
084: }
085: classes[code] = ext;
086: classNames.put(name, ext);
087: }
088:
089: static void add(String name, int code) {
090: ExternalizeProxy p = null;
091: Object obj = null;
092: try {
093: Class c = Class.forName(name);
094: obj = c.newInstance();
095: if (obj instanceof ExternalizeProxy) {
096: p = (ExternalizeProxy) obj;
097: }
098: } catch (Throwable t) {
099: Debug.println(t.toString());
100: }
101: add(name, code, p, obj);
102: }
103:
104: static {
105: // null 0
106: add("com.quadcap.sql.Column", 1);
107: add("com.quadcap.sql.DatabaseRoot", 2);
108: add("com.quadcap.sql.file.LogEntry", 3);
109: add("com.quadcap.sql.types.TypeBinary", 4);
110: add("com.quadcap.sql.AddTable", 5);
111: add("com.quadcap.sql.Table", 6);
112: add("com.quadcap.sql.types.TypeInt", 7);
113: add("com.quadcap.sql.types.TypeVarChar", 8);
114: add("com.quadcap.sql.types.ValueType", 9);
115: add("com.quadcap.sql.QuantifiedCompare", 10);
116: add("com.quadcap.sql.PrimaryKeyConstraint", 11);
117: add("com.quadcap.sql.types.TypeBlob", 12);
118: add("com.quadcap.sql.InsertRow", 13);
119: add("com.quadcap.sql.AddIndexEntry", 14);
120: add("com.quadcap.sql.types.ValueInteger", 15);
121: add("com.quadcap.sql.types.ValueString", 16);
122: add("com.quadcap.sql.NotNullConstraint", 17);
123: add("com.quadcap.sql.types.TypeDecimal", 18);
124: add("com.quadcap.sql.UniqueConstraint", 19);
125: add("com.quadcap.sql.types.ValueNull", 20);
126: add("com.quadcap.sql.CheckConstraint", 21);
127: add("com.quadcap.sql.BinaryExpression", 22);
128: add("com.quadcap.sql.NameExpression", 23);
129: add("com.quadcap.sql.ValueExpression", 24);
130: add("com.quadcap.sql.DefaultTableConstraint", 25);
131: add("com.quadcap.sql.types.ValueBlob", 26);
132: add("com.quadcap.sql.View", 27);
133: add("com.quadcap.sql.StmtCreateView", 28);
134: add("com.quadcap.sql.SelectExpression", 29);
135: add("com.quadcap.sql.SelectFromTable", 30);
136: add("com.quadcap.sql.InsertBlob", 31);
137: add("com.quadcap.sql.DeleteIndexEntry", 32);
138: add("com.quadcap.sql.ExportedKeyConstraint", 33);
139: add("com.quadcap.sql.DeleteRow", 34);
140: add("com.quadcap.sql.types.ValueScaledInteger", 35);
141: add("com.quadcap.sql.types.TypeReal", 36);
142: add("com.quadcap.sql.types.ValueDouble", 37);
143: add("com.quadcap.sql.types.TypeVarBinary", 38);
144: add("com.quadcap.sql.types.ValueOctets", 39);
145: add("com.quadcap.sql.types.TypeChar", 40);
146: add("com.quadcap.sql.types.TypeSmallInt", 41);
147: add("com.quadcap.sql.DropTable", 42);
148: add("com.quadcap.sql.SelectItem", 43);
149: add("com.quadcap.sql.UnaryExpression", 44);
150: add("com.quadcap.sql.AutoNumberConstraint", 45);
151: add("com.quadcap.sql.InExpression", 46);
152: add("com.quadcap.sql.TernaryExpression", 47);
153: add("com.quadcap.sql.StmtNull", 48);
154: add("com.quadcap.sql.AggregateExpression", 49);
155: add("com.quadcap.sql.VectorExpression", 50);
156: add("com.quadcap.sql.types.TypeTime", 51);
157: add("com.quadcap.sql.types.TypeTimestamp", 52);
158: add("com.quadcap.sql.types.TypeDate", 53);
159: add("com.quadcap.sql.types.ValueDate", 54);
160: add("com.quadcap.sql.types.ValueTime", 55);
161: add("com.quadcap.sql.types.ValueTimestamp", 56);
162: add("com.quadcap.sql.types.ValueInterval", 57);
163: add("com.quadcap.sql.types.TypeInterval", 58);
164: add("com.quadcap.sql.types.ValueShort", 59);
165: add("com.quadcap.sql.types.ValuePattern", 60);
166: add("com.quadcap.sql.UpdateRow", 61);
167: add("com.quadcap.sql.types.ValueBoolean", 62);
168: add("com.quadcap.sql.ImportedKeyConstraint", 63);
169: add("com.quadcap.sql.StmtRenameTable", 64);
170: add("com.quadcap.sql.RefcountBlob", 65);
171: add("com.quadcap.sql.AutoNumberStep", 66);
172: add("com.quadcap.sql.types.ValueLong", 67);
173: add("com.quadcap.sql.DeleteConstraint", 68);
174: add("com.quadcap.sql.AddConstraint", 69);
175: add("com.quadcap.sql.NonUniqueIndexConstraint", 70);
176: add("com.quadcap.sql.types.TypeBigInt", 71);
177: add("com.quadcap.sql.types.TypeTinyInt", 72);
178: add("com.quadcap.sql.types.ValueFloat", 73);
179: add("com.quadcap.sql.types.ValueByte", 74);
180: add("com.quadcap.sql.types.TypeBoolean", 75);
181: add("com.quadcap.sql.types.TypeAny", 76);
182: add("com.quadcap.sql.JoinedTable", 77);
183: add("com.quadcap.sql.AddColumn", 78);
184: add("com.quadcap.sql.FunctionExpression", 79);
185: add("com.quadcap.sql.AlterColumn", 80);
186: add("com.quadcap.sql.types.TypeClob", 81);
187: add("com.quadcap.sql.types.ValueClob", 82);
188: add("com.quadcap.sql.types.ValueUnknown", 83);
189: add("com.quadcap.sql.SelectFromItem", 84);
190: add("com.quadcap.sql.MergeExpression", 85);
191: add("com.quadcap.sql.types.ValueDefault", 86);
192: add("com.quadcap.sql.DropColumn", 87);
193:
194: add("java.math.BigDecimal", 251, new ExternProxyBigDecimal());
195: add("java.lang.Integer", 252, new ExternProxyInteger());
196: add("java.lang.String", 253, new ExternProxyString());
197: add("java.util.Vector", 254, new ExternProxyVector());
198: }
199:
200: public static Extern get(int code) {
201: try {
202: return classes[code];
203: } catch (Throwable t) {
204: return null;
205: }
206: }
207:
208: //#ifdef DEBUG
209: public String toString() {
210: return "[" + code + "]: " + className;
211: }
212:
213: //#endif
214:
215: static Extern get(String className) {
216: return (Extern) classNames.get(className);
217: }
218:
219: int code;
220: String className;
221: ExternalizeProxy proxy;
222: Class eclass;
223:
224: public Extern(String className, int code, ExternalizeProxy proxy) {
225: this .className = className;
226: this .code = code;
227: this .proxy = proxy;
228: if (proxy == null) {
229: try {
230: eclass = Class.forName(className);
231: } catch (Exception e) {
232: Debug.print(e);
233: throw new RuntimeException(e.toString());
234: }
235: }
236: }
237:
238: public Object readObject(ObjectInput in) throws IOException,
239: ClassNotFoundException {
240: if (proxy != null) {
241: return proxy.readObject(in);
242: } else {
243: try {
244: Object obj = eclass.newInstance();
245: ((Externalizable) obj).readExternal(in);
246: return obj;
247: } catch (IllegalAccessException e) {
248: throw new InvalidClassException(className,
249: "IllegalAccessException");
250: } catch (InstantiationException e) {
251: throw new InvalidClassException(className,
252: "InstantiationException");
253: }
254: }
255: }
256:
257: public void writeObject(ObjectOutput out, Object obj)
258: throws IOException {
259: // XXX It turns out the com.quadcap.sql.file.Log knows about the
260: // XXX code because it wants to 'patch' already logged entries.
261: // XXX see Log.undo()
262: out.write(code);
263: if (proxy != null) {
264: proxy.writeObject(out, obj);
265: } else {
266: ((Externalizable) obj).writeExternal(out);
267: }
268: }
269: }
|