Quadcap Embeddable Database

com/quadcap/sql/io/Extern.java

Go to the documentation of this file.
00001 package com.quadcap.sql.io; 00002 00003 /* Copyright 1999 - 2003 Quadcap Software. All rights reserved. 00004 * 00005 * This software is distributed under the Quadcap Free Software License. 00006 * This software may be used or modified for any purpose, personal or 00007 * commercial. Open Source redistributions are permitted. Commercial 00008 * redistribution of larger works derived from, or works which bundle 00009 * this software requires a "Commercial Redistribution License"; see 00010 * http://www.quadcap.com/purchase. 00011 * 00012 * Redistributions qualify as "Open Source" under one of the following terms: 00013 * 00014 * Redistributions are made at no charge beyond the reasonable cost of 00015 * materials and delivery. 00016 * 00017 * Redistributions are accompanied by a copy of the Source Code or by an 00018 * irrevocable offer to provide a copy of the Source Code for up to three 00019 * years at the cost of materials and delivery. Such redistributions 00020 * must allow further use, modification, and redistribution of the Source 00021 * Code under substantially the same terms as this license. 00022 * 00023 * Redistributions of source code must retain the copyright notices as they 00024 * appear in each source code file, these license terms, and the 00025 * disclaimer/limitation of liability set forth as paragraph 6 below. 00026 * 00027 * Redistributions in binary form must reproduce this Copyright Notice, 00028 * these license terms, and the disclaimer/limitation of liability set 00029 * forth as paragraph 6 below, in the documentation and/or other materials 00030 * provided with the distribution. 00031 * 00032 * The Software is provided on an "AS IS" basis. No warranty is 00033 * provided that the Software is free of defects, or fit for a 00034 * particular purpose. 00035 * 00036 * Limitation of Liability. Quadcap Software shall not be liable 00037 * for any damages suffered by the Licensee or any third party resulting 00038 * from use of the Software. 00039 */ 00040 00041 import java.io.Externalizable; 00042 import java.io.InvalidClassException; 00043 import java.io.IOException; 00044 import java.io.ObjectInput; 00045 import java.io.ObjectOutput; 00046 00047 import java.util.Hashtable; 00048 import java.util.Vector; 00049 00050 import com.quadcap.util.Debug; 00051 00052 /** 00053 * Adaptor class for objects that are themselves externalizable or 00054 * which have externalize adaptors. 00055 * 00056 * @author Stan Bailes 00057 */ 00058 public class Extern { 00059 static Extern[] classes = new Extern[256]; 00060 static Hashtable classNames = new Hashtable(); 00061 00062 static void add(String name, int code, 00063 ExternalizeProxy proxy) { 00064 add(name, code, proxy, null); 00065 } 00066 00067 public static void add(String name, int code, 00068 ExternalizeProxy proxy, Object obj) 00069 { 00070 Extern ext = new Extern(name, code, proxy); 00071 if (obj instanceof Externable) { 00072 // Need to check that this 00073 // object really implements the 'getExtern' call, and 00074 // isn't using an inherited version... 00075 Class cl = obj.getClass(); 00076 try { 00077 if (cl.getDeclaredMethod("getExtern", new Class[0]) == null) { 00078 throw new RuntimeException("Not Externable: " + 00079 cl.getName()); 00080 } 00081 } catch (NoSuchMethodException e) { 00082 throw new RuntimeException("Not Externable: " + 00083 cl.getName()); 00084 } 00085 ((Externable)obj).setExtern(ext); 00086 } 00087 classes[code] = ext; 00088 classNames.put(name, ext); 00089 } 00090 00091 static void add(String name, int code) { 00092 ExternalizeProxy p = null; 00093 Object obj = null; 00094 try { 00095 Class c = Class.forName(name); 00096 obj = c.newInstance(); 00097 if (obj instanceof ExternalizeProxy) { 00098 p = (ExternalizeProxy)obj; 00099 } 00100 } catch (Throwable t) { 00101 Debug.println(t.toString()); 00102 } 00103 add(name, code, p, obj); 00104 } 00105 00106 static { 00107 // null 0 00108 add("com.quadcap.sql.Column", 1); 00109 add("com.quadcap.sql.DatabaseRoot", 2); 00110 add("com.quadcap.sql.file.LogEntry", 3); 00111 add("com.quadcap.sql.types.TypeBinary", 4); 00112 add("com.quadcap.sql.AddTable", 5); 00113 add("com.quadcap.sql.Table", 6); 00114 add("com.quadcap.sql.types.TypeInt", 7); 00115 add("com.quadcap.sql.types.TypeVarChar", 8); 00116 add("com.quadcap.sql.types.ValueType", 9); 00117 add("com.quadcap.sql.QuantifiedCompare", 10); 00118 add("com.quadcap.sql.PrimaryKeyConstraint", 11); 00119 add("com.quadcap.sql.types.TypeBlob", 12); 00120 add("com.quadcap.sql.InsertRow", 13); 00121 add("com.quadcap.sql.AddIndexEntry", 14); 00122 add("com.quadcap.sql.types.ValueInteger", 15); 00123 add("com.quadcap.sql.types.ValueString", 16); 00124 add("com.quadcap.sql.NotNullConstraint", 17); 00125 add("com.quadcap.sql.types.TypeDecimal", 18); 00126 add("com.quadcap.sql.UniqueConstraint", 19); 00127 add("com.quadcap.sql.types.ValueNull", 20); 00128 add("com.quadcap.sql.CheckConstraint", 21); 00129 add("com.quadcap.sql.BinaryExpression", 22); 00130 add("com.quadcap.sql.NameExpression", 23); 00131 add("com.quadcap.sql.ValueExpression", 24); 00132 add("com.quadcap.sql.DefaultTableConstraint", 25); 00133 add("com.quadcap.sql.types.ValueBlob", 26); 00134 add("com.quadcap.sql.View", 27); 00135 add("com.quadcap.sql.StmtCreateView", 28); 00136 add("com.quadcap.sql.SelectExpression", 29); 00137 add("com.quadcap.sql.SelectFromTable", 30); 00138 add("com.quadcap.sql.InsertBlob", 31); 00139 add("com.quadcap.sql.DeleteIndexEntry", 32); 00140 add("com.quadcap.sql.ExportedKeyConstraint", 33); 00141 add("com.quadcap.sql.DeleteRow", 34); 00142 add("com.quadcap.sql.types.ValueScaledInteger", 35); 00143 add("com.quadcap.sql.types.TypeReal", 36); 00144 add("com.quadcap.sql.types.ValueDouble", 37); 00145 add("com.quadcap.sql.types.TypeVarBinary", 38); 00146 add("com.quadcap.sql.types.ValueOctets", 39); 00147 add("com.quadcap.sql.types.TypeChar", 40); 00148 add("com.quadcap.sql.types.TypeSmallInt", 41); 00149 add("com.quadcap.sql.DropTable", 42); 00150 add("com.quadcap.sql.SelectItem", 43); 00151 add("com.quadcap.sql.UnaryExpression", 44); 00152 add("com.quadcap.sql.AutoNumberConstraint", 45); 00153 add("com.quadcap.sql.InExpression", 46); 00154 add("com.quadcap.sql.TernaryExpression", 47); 00155 add("com.quadcap.sql.StmtNull", 48); 00156 add("com.quadcap.sql.AggregateExpression", 49); 00157 add("com.quadcap.sql.VectorExpression", 50); 00158 add("com.quadcap.sql.types.TypeTime", 51); 00159 add("com.quadcap.sql.types.TypeTimestamp", 52); 00160 add("com.quadcap.sql.types.TypeDate", 53); 00161 add("com.quadcap.sql.types.ValueDate", 54); 00162 add("com.quadcap.sql.types.ValueTime", 55); 00163 add("com.quadcap.sql.types.ValueTimestamp", 56); 00164 add("com.quadcap.sql.types.ValueInterval", 57); 00165 add("com.quadcap.sql.types.TypeInterval", 58); 00166 add("com.quadcap.sql.types.ValueShort", 59); 00167 add("com.quadcap.sql.types.ValuePattern", 60); 00168 add("com.quadcap.sql.UpdateRow", 61); 00169 add("com.quadcap.sql.types.ValueBoolean", 62); 00170 add("com.quadcap.sql.ImportedKeyConstraint", 63); 00171 add("com.quadcap.sql.StmtRenameTable", 64); 00172 add("com.quadcap.sql.RefcountBlob", 65); 00173 add("com.quadcap.sql.AutoNumberStep", 66); 00174 add("com.quadcap.sql.types.ValueLong", 67); 00175 add("com.quadcap.sql.DeleteConstraint", 68); 00176 add("com.quadcap.sql.AddConstraint", 69); 00177 add("com.quadcap.sql.NonUniqueIndexConstraint", 70); 00178 add("com.quadcap.sql.types.TypeBigInt", 71); 00179 add("com.quadcap.sql.types.TypeTinyInt", 72); 00180 add("com.quadcap.sql.types.ValueFloat", 73); 00181 add("com.quadcap.sql.types.ValueByte", 74); 00182 add("com.quadcap.sql.types.TypeBoolean", 75); 00183 add("com.quadcap.sql.types.TypeAny", 76); 00184 add("com.quadcap.sql.JoinedTable", 77); 00185 add("com.quadcap.sql.AddColumn", 78); 00186 add("com.quadcap.sql.FunctionExpression", 79); 00187 add("com.quadcap.sql.AlterColumn", 80); 00188 add("com.quadcap.sql.types.TypeClob", 81); 00189 add("com.quadcap.sql.types.ValueClob", 82); 00190 add("com.quadcap.sql.types.ValueUnknown", 83); 00191 add("com.quadcap.sql.SelectFromItem", 84); 00192 add("com.quadcap.sql.MergeExpression", 85); 00193 add("com.quadcap.sql.types.ValueDefault", 86); 00194 add("com.quadcap.sql.DropColumn", 87); 00195 00196 add("java.math.BigDecimal", 251, 00197 new ExternProxyBigDecimal()); 00198 add("java.lang.Integer", 252, 00199 new ExternProxyInteger()); 00200 add("java.lang.String", 253, 00201 new ExternProxyString()); 00202 add("java.util.Vector", 254, 00203 new ExternProxyVector()); 00204 } 00205 00206 public static Extern get(int code) { 00207 try { 00208 return classes[code]; 00209 } catch (Throwable t) { 00210 return null; 00211 } 00212 } 00213 00214 //#ifdef DEBUG 00215 public String toString() { return "[" + code + "]: " + className; } 00216 //#endif 00217 00218 static Extern get(String className) { 00219 return (Extern)classNames.get(className); 00220 } 00221 00222 int code; 00223 String className; 00224 ExternalizeProxy proxy; 00225 Class eclass; 00226 00227 public Extern(String className, int code, ExternalizeProxy proxy) { 00228 this.className = className; 00229 this.code = code; 00230 this.proxy = proxy; 00231 if (proxy == null) { 00232 try { 00233 eclass = Class.forName(className); 00234 } catch (Exception e) { 00235 Debug.print(e); 00236 throw new RuntimeException(e.toString()); 00237 } 00238 } 00239 } 00240 00241 public Object readObject(ObjectInput in) 00242 throws IOException, ClassNotFoundException 00243 { 00244 if (proxy != null) { 00245 return proxy.readObject(in); 00246 } else { 00247 try { 00248 Object obj = eclass.newInstance(); 00249 ((Externalizable)obj).readExternal(in); 00250 return obj; 00251 } catch (IllegalAccessException e) { 00252 throw new InvalidClassException(className, 00253 "IllegalAccessException"); 00254 } catch (InstantiationException e) { 00255 throw new InvalidClassException(className, 00256 "InstantiationException"); 00257 } 00258 } 00259 } 00260 00261 public void writeObject(ObjectOutput out, Object obj) 00262 throws IOException 00263 { 00264 // XXX It turns out the com.quadcap.sql.file.Log knows about the 00265 // XXX code because it wants to 'patch' already logged entries. 00266 // XXX see Log.undo() 00267 out.write(code); 00268 if (proxy != null) { 00269 proxy.writeObject(out, obj); 00270 } else { 00271 ((Externalizable)obj).writeExternal(out); 00272 } 00273 } 00274 }