1 21 22 package org.apache.derby.impl.services.reflect; 23 24 import org.apache.derby.iapi.services.sanity.SanityManager; 25 26 import org.apache.derby.iapi.services.loader.ClassFactory; 27 import org.apache.derby.iapi.services.loader.GeneratedClass; 28 import org.apache.derby.iapi.services.loader.ClassInspector; 29 30 import org.apache.derby.iapi.services.monitor.ModuleControl; 31 import org.apache.derby.iapi.services.monitor.ModuleSupportable; 32 import org.apache.derby.iapi.services.monitor.Monitor; 33 34 import org.apache.derby.iapi.error.StandardException; 35 import org.apache.derby.iapi.services.property.PropertyUtil; 36 37 import org.apache.derby.iapi.services.stream.HeaderPrintWriter; 38 import org.apache.derby.iapi.services.monitor.Monitor; 39 40 import org.apache.derby.iapi.services.compiler.*; 41 import java.lang.reflect.Modifier ; 42 import org.apache.derby.iapi.sql.compile.CodeGeneration; 43 44 import org.apache.derby.iapi.util.ByteArray; 45 import org.apache.derby.iapi.services.io.FileUtil; 46 import org.apache.derby.iapi.services.i18n.MessageService; 47 import org.apache.derby.iapi.reference.Property; 48 import org.apache.derby.iapi.reference.SQLState; 49 import org.apache.derby.iapi.reference.MessageId; 50 import org.apache.derby.iapi.reference.ClassName; 51 52 import java.util.Properties ; 53 import java.util.Hashtable ; 54 55 import java.io.ObjectStreamClass ; 56 import java.io.File ; 57 import java.io.FileOutputStream ; 58 import java.io.IOException ; 59 import java.io.Serializable ; 60 61 79 80 abstract class DatabaseClasses 81 implements ClassFactory, ModuleControl 82 { 83 86 87 private ClassInspector classInspector; 88 private JavaFactory javaFactory; 89 90 private UpdateLoader applicationLoader; 91 92 95 96 DatabaseClasses() { 97 } 98 99 102 103 public void boot(boolean create, Properties startParams) 104 throws StandardException 105 { 106 107 classInspector = new ClassInspector(this); 108 109 115 116 String classpath = null; 117 if (startParams != null) { 118 classpath = startParams.getProperty(Property.BOOT_DB_CLASSPATH); 119 } 120 121 if (classpath != null) { 122 applicationLoader = new UpdateLoader(classpath, this, true, 123 true); 124 } 125 126 javaFactory = (JavaFactory) org.apache.derby.iapi.services.monitor.Monitor.startSystemModule(org.apache.derby.iapi.reference.Module.JavaFactory); 127 } 128 129 130 131 public void stop() { 132 if (applicationLoader != null) 133 applicationLoader.close(); 134 } 135 136 139 140 150 public final GeneratedClass loadGeneratedClass(String fullyQualifiedName, ByteArray classDump) 151 throws StandardException { 152 153 154 try { 155 156 157 return loadGeneratedClassFromData(fullyQualifiedName, classDump); 158 159 } catch (LinkageError le) { 160 161 WriteClassFile(fullyQualifiedName, classDump, le); 162 163 throw StandardException.newException(SQLState.GENERATED_CLASS_LINKAGE_ERROR, 164 le, fullyQualifiedName); 165 166 } catch (VirtualMachineError vme) { 168 WriteClassFile(fullyQualifiedName, classDump, vme); 169 170 throw vme; 171 } 172 173 } 174 175 private static void WriteClassFile(String fullyQualifiedName, ByteArray bytecode, Throwable t) { 176 177 int lastDot = fullyQualifiedName.lastIndexOf((int)'.'); 179 String filename = fullyQualifiedName.substring(lastDot+1,fullyQualifiedName.length()).concat(".class"); 180 181 Object env = Monitor.getMonitor().getEnvironment(); 182 File dir = env instanceof File ? (File ) env : null; 183 184 File classFile = FileUtil.newFile(dir,filename); 185 186 HeaderPrintWriter errorStream = Monitor.getStream(); 188 189 try { 190 FileOutputStream fis = new FileOutputStream (classFile); 191 fis.write(bytecode.getArray(), 192 bytecode.getOffset(), bytecode.getLength()); 193 fis.flush(); 194 if (t!=null) { 195 errorStream.printlnWithHeader(MessageService.getTextMessage(MessageId.CM_WROTE_CLASS_FILE, fullyQualifiedName, classFile, t)); 196 } 197 fis.close(); 198 } catch (IOException e) { 199 if (SanityManager.DEBUG) 200 SanityManager.THROWASSERT("Unable to write .class file"); 201 } 202 } 203 204 public ClassInspector getClassInspector() { 205 return classInspector; 206 } 207 208 209 public final Class loadApplicationClass(String className) 210 throws ClassNotFoundException { 211 212 if (className.startsWith("org.apache.derby.")) { 213 try { 220 return Class.forName(className); 221 } catch (ClassNotFoundException cnfe) 222 { 223 } 227 } 228 229 Throwable loadError; 230 try { 231 try { 232 return loadClassNotInDatabaseJar(className); 233 } catch (ClassNotFoundException cnfe) { 234 if (applicationLoader == null) 235 throw cnfe; 236 Class c = applicationLoader.loadClass(className, true); 237 if (c == null) 238 throw cnfe; 239 return c; 240 } 241 } 242 catch (SecurityException se) 243 { 244 loadError = se; 247 } 248 catch (LinkageError le) 249 { 250 loadError = le; 253 } 254 throw new ClassNotFoundException (className + " : " + loadError.getMessage()); 255 } 256 257 abstract Class loadClassNotInDatabaseJar(String className) 258 throws ClassNotFoundException ; 259 260 public final Class loadApplicationClass(ObjectStreamClass classDescriptor) 261 throws ClassNotFoundException { 262 return loadApplicationClass(classDescriptor.getName()); 263 } 264 265 public boolean isApplicationClass(Class theClass) { 266 267 return theClass.getClassLoader() 268 instanceof JarLoader; 269 } 270 271 public void notifyModifyJar(boolean reload) throws StandardException { 272 if (applicationLoader != null) { 273 applicationLoader.modifyJar(reload); 274 } 275 } 276 277 282 public void notifyModifyClasspath(String classpath) throws StandardException { 283 284 if (applicationLoader != null) { 285 applicationLoader.modifyClasspath(classpath); 286 } 287 } 288 289 290 public int getClassLoaderVersion() { 291 if (applicationLoader != null) { 292 return applicationLoader.getClassLoaderVersion(); 293 } 294 295 return -1; 296 } 297 298 public ByteArray buildSpecificFactory(String className, String factoryName) 299 throws StandardException { 300 301 ClassBuilder cb = javaFactory.newClassBuilder(this, CodeGeneration.GENERATED_PACKAGE_PREFIX, 302 Modifier.PUBLIC | Modifier.FINAL, factoryName, "org.apache.derby.impl.services.reflect.GCInstanceFactory"); 303 304 MethodBuilder constructor = cb.newConstructorBuilder(Modifier.PUBLIC); 305 306 constructor.callSuper(); 307 constructor.methodReturn(); 308 constructor.complete(); 309 constructor = null; 310 311 MethodBuilder noArg = cb.newMethodBuilder(Modifier.PUBLIC, ClassName.GeneratedByteCode, "getNewInstance"); 312 noArg.pushNewStart(className); 313 noArg.pushNewComplete(0); 314 noArg.methodReturn(); 315 noArg.complete(); 316 noArg = null; 317 318 return cb.getClassBytecode(); 319 } 320 321 324 325 328 329 abstract LoadedGeneratedClass loadGeneratedClassFromData(String fullyQualifiedName, ByteArray classDump); 330 } 331 | Popular Tags |