1 21 22 package org.apache.derby.impl.services.reflect; 23 24 import org.apache.derby.impl.sql.execute.JarUtil; 25 import org.apache.derby.iapi.services.stream.HeaderPrintWriter; 26 import org.apache.derby.iapi.error.StandardException; 27 28 import java.io.File ; 29 import java.io.InputStream ; 30 import java.io.IOException ; 31 32 import java.util.zip.ZipFile ; 33 import java.util.zip.ZipInputStream ; 34 import java.util.zip.ZipEntry ; 35 36 37 import org.apache.derby.iapi.services.io.LimitInputStream; 38 import org.apache.derby.iapi.util.IdUtil; 39 40 import org.apache.derby.iapi.reference.MessageId; 41 import org.apache.derby.iapi.services.i18n.MessageService; 42 43 44 public class JarLoader extends ClassLoader { 45 46 private static final JarFile jarFileFactory; 47 48 static { 49 50 jarFileFactory = new JarFileJava2(); 52 } 53 54 private UpdateLoader updateLoader; 55 private JarFile jf; 56 private HeaderPrintWriter vs; 57 58 JarLoader(UpdateLoader updateLoader, String [] name, HeaderPrintWriter vs) { 59 60 this.updateLoader = updateLoader; 61 this.jf = jarFileFactory.newJarFile(name); 62 this.vs = vs; 63 } 64 65 void initialize() { 68 69 Object zipData = load(); 70 71 try { 72 73 if (zipData instanceof File ) { 74 jf.initialize((File ) zipData); 75 return; 76 } 77 78 if (zipData instanceof InputStream ) { 79 jf.isStream = true; 80 try { 81 ((InputStream ) zipData).close(); 82 } catch (IOException ioe) { 83 } 84 return; 85 } 86 } catch (IOException ioe) { 87 if (vs != null) 88 vs.println(MessageService.getTextMessage(MessageId.CM_LOAD_JAR_EXCEPTION, getJarName(), ioe)); 89 } 90 91 setInvalid(false); 93 } 94 95 100 public Class loadClass(String className, boolean resolve) 101 throws ClassNotFoundException { 102 103 try { 107 return Class.forName(className); 108 } catch (ClassNotFoundException cnfe) { 109 110 if (updateLoader == null) 111 throw new ClassNotFoundException (MessageService.getTextMessage(MessageId.CM_STALE_LOADER, className)); 112 113 Class c = updateLoader.loadClass(className, resolve); 114 if (c == null) 115 throw cnfe; 116 return c; 117 } 118 } 119 120 123 public InputStream getResourceAsStream(String name) { 124 if (updateLoader == null) 125 return null; 126 return updateLoader.getResourceAsStream(name); 127 } 128 129 132 final String getJarName() { 133 return jf.getJarName(); 134 } 135 136 Class loadClassData(String className, String jvmClassName, boolean resolve) { 137 138 if (updateLoader == null) 139 return null; 140 141 try { 142 if (jf.isZip()) 143 return loadClassDataFromJar(className, jvmClassName, resolve); 144 145 if (jf.isStream) { 146 return loadClassData((InputStream ) load(), 148 className, jvmClassName, resolve); 149 } 150 151 return null; 152 } catch (IOException ioe) { 153 if (vs != null) 154 vs.println(MessageService.getTextMessage(MessageId.CM_CLASS_LOAD_EXCEPTION, className, getJarName(), ioe)); 155 return null; 156 } 157 } 158 159 162 InputStream getStream(String name) { 163 164 if (updateLoader == null) 165 return null; 166 167 if (jf.isZip()) 168 return getRawStream(jf.getZip(), name); 169 170 if (jf.isStream) { 171 return getRawStream((InputStream ) load(), name); 172 } 173 return null; 174 } 175 176 177 180 181 182 private Class loadClassDataFromJar(String className, String jvmClassName, boolean resolve) 183 throws IOException { 184 185 ZipEntry ze = jf.getEntry(jvmClassName); 186 if (ze == null) 187 return null; 188 189 InputStream in = jf.getZip().getInputStream(ze); 190 191 try { 192 return loadClassData(ze, in, className, resolve); 193 } finally { 194 in.close(); 195 } 196 } 197 198 private Class loadClassData( 199 InputStream in, String className, String jvmClassName, boolean resolve) 200 throws IOException { 201 202 ZipInputStream zipIn = jf.getZipOnStream(in); 203 204 for (;;) { 205 206 ZipEntry ze = jf.getNextEntry(zipIn); 207 if (ze == null) { 208 zipIn.close(); 209 return null; 210 } 211 212 if (ze.getName().equals(jvmClassName)) { 213 Class c = loadClassData(ze, zipIn, className, resolve); 214 zipIn.close(); 215 return c; 216 } 217 } 218 219 } 220 221 private Class loadClassData(ZipEntry ze, InputStream in, 222 String className, boolean resolve) throws IOException { 223 224 byte[] data = jf.readData(ze, in, className); 225 226 Object [] signers = jf.getSigners(className, ze); 227 228 synchronized (updateLoader) { 229 Class c = updateLoader.checkLoaded(className, resolve); 232 if (c == null) { 233 c = defineClass(className, data, 0, data.length); 234 if (signers != null) { 235 setSigners(c, signers); 236 } 237 if (resolve) 238 resolveClass(c); 239 } 240 return c; 241 242 } 243 } 244 245 Class checkLoaded(String className, boolean resolve) { 246 if (updateLoader == null) 247 return null; 248 249 Class c = findLoadedClass(className); 250 if ((c != null) && resolve) 251 resolveClass(c); 252 return c; 253 } 254 255 private Object load() { 256 257 String [] dbJarName = jf.name; 258 259 String schemaName = dbJarName[IdUtil.DBCP_SCHEMA_NAME]; 260 String sqlName = dbJarName[IdUtil.DBCP_SQL_JAR_NAME]; 261 262 try { 264 return updateLoader.getJarReader().readJarFile(schemaName, sqlName); 265 } catch (StandardException se) { 266 if (vs != null) 267 vs.println(MessageService.getTextMessage(MessageId.CM_LOAD_JAR_EXCEPTION, jf.getJarName(), se)); 268 return null; 269 } 270 271 } 272 273 JarFile setInvalid(boolean newJarFile) { 274 275 jf.setInvalid(); 276 updateLoader = null; 277 return newJarFile ? jarFileFactory.newJarFile(jf.name) : null; 278 } 279 280 283 284 290 private InputStream getRawStream(ZipFile zip, String name) { 291 292 try { 293 ZipEntry ze = zip.getEntry(name); 294 if (ze == null) 295 return null; 296 297 return zip.getInputStream(ze); 298 } catch (IOException ioe) { 299 return null; 300 } 301 } 302 303 309 private InputStream getRawStream(InputStream in, String name) { 310 311 ZipInputStream zipIn = null; 312 try { 313 zipIn = new ZipInputStream (in); 314 315 ZipEntry ze; 316 while ((ze = jf.getNextEntry(zipIn)) != null) { 317 318 if (ze.getName().equals(name)) { 319 LimitInputStream lis = new LimitInputStream(zipIn); 320 lis.setLimit((int) ze.getSize()); 321 return lis; 322 } 323 } 324 325 zipIn.close(); 326 327 } catch (IOException ioe) { 328 if (zipIn != null) { 329 try { 330 zipIn.close(); 331 } catch (IOException ioe2) { 332 } 333 } 334 } 335 return null; 336 } 337 } 338 | Popular Tags |