1 25 26 package net.yagga.util; 27 28 import java.io.*; 29 import java.util.*; 30 import java.util.jar.*; 31 import java.net.*; 32 33 72 public class MetaJarResources 73 { 74 private Hashtable htSizes=new Hashtable(); 75 private String jarFile; 76 77 private boolean executingFromJar=false; private Hashtable metaJarContent=new Hashtable(); 80 private String jarJarFile; 81 private Manifest metaManifest=null; 82 83 98 public MetaJarResources(String jFile) 99 { 100 super(); 101 this.jarFile=jFile; 102 103 try{ 104 URL u=getClass().getResource("/"+jarFile); 105 URLConnection uc=null; 107 if(u!=null) 108 uc=u.openConnection(); 109 110 if(uc instanceof JarURLConnection) 111 { 112 executingFromJar=true; 113 JarURLConnection juc = (JarURLConnection)uc; 114 jarJarFile=jarFile; 115 jarFile=juc.getJarFileURL().getFile(); 118 jarFile=URLDecoder.decode(jarFile); 123 125 initTopJarSizes(); 126 readJarInJar(); 128 } 129 else { 131 executingFromJar=false; 132 initTopJarSizes(); 133 } 134 } 135 catch(IOException ioe){ 136 Ut.error("Error getting file from file"+jarFile+":"+ioe); 137 jarFile=""; 138 } 139 } 140 141 152 public byte[] getBytes(String entryName) 153 { 154 byte b1[]=null; 155 if(executingFromJar) 157 b1=(byte [])metaJarContent.get(entryName); 158 else 159 b1=readBytesFromTopJar(entryName); 160 return b1; 161 } 162 163 168 public Manifest getManifest() 169 { 170 try{ 171 if(executingFromJar) 172 return metaManifest; 173 else 174 { 175 JarFile jf=new JarFile(jarFile); 176 return jf.getManifest(); 177 } 178 }catch(IOException ioe){ 179 Ut.error("IOE getting main class "+ioe); 180 return null; 181 } 182 } 183 184 191 public String getActualJarName(){ 192 if(executingFromJar) 193 return jarJarFile; 194 else 195 return jarFile; 196 } 197 198 211 private void readJarInJar() 212 { 213 try 214 { 215 byte[] jarE=readBytesFromTopJar(jarJarFile); 218 219 int idx=jarJarFile.lastIndexOf("/"); if(idx==-1) 222 idx=jarJarFile.lastIndexOf("\\"); if(idx>=0) 224 jarJarFile=jarJarFile.substring(idx+1); 225 226 229 String tmpDir=System.getProperty("java.io.tmpdir"); 231 232 File jarInJarFile=new File(tmpDir,jarJarFile); 233 FileOutputStream fout=new FileOutputStream(jarInJarFile.getCanonicalPath()); 234 235 for(int i=0;i<jarE.length;i++) 237 fout.write(jarE[i]); 238 fout.close(); 239 fout=null; 240 241 Hashtable sizes=new Hashtable(); 243 JarFile jf=new JarFile(jarInJarFile); 244 try 245 { 246 Enumeration e=jf.entries(); 247 while (e.hasMoreElements()) 248 { 249 JarEntry ze=(JarEntry)e.nextElement(); 250 sizes.put(ze.getName(),new Integer ((int)ze.getSize())); 252 } 253 metaManifest=jf.getManifest(); 255 jf.close(); 256 } 257 catch(IOException ioe) 258 { 259 Ut.error("Error reading sizes in '"+fout+"':"+ioe); 260 } 261 262 jarInJarFile.delete(); 265 266 ByteArrayInputStream bais=new ByteArrayInputStream(jarE); 268 JarInputStream jis=new JarInputStream(bais); 269 JarEntry je=null; 270 while ((je=jis.getNextJarEntry())!=null) 271 { 272 if (je.isDirectory()) 273 continue; 274 275 int size=(int)je.getSize(); 276 if(size==-1) 277 size=((Integer )sizes.get(je.getName())).intValue(); 278 279 byte[] b=new byte[(int)size]; 280 int rb=0; 281 int chunk=0; 282 while (((int)size - rb) > 0) 283 { 284 chunk=jis.read(b,rb,(int)size - rb); 285 if (chunk==-1) 286 break; 287 rb+=chunk; 288 } 289 metaJarContent.put(je.getName(),b); 291 292 } 294 jis.close(); 295 } 296 catch(FileNotFoundException fnfe) 297 { 298 Ut.error(fnfe.toString()); 299 } 300 catch(IOException ioe){ 301 Ut.error(ioe.toString()); 302 } 303 } 304 305 311 private void initTopJarSizes() 312 { 313 try 314 { 315 JarFile zf=new JarFile(jarFile); 316 Enumeration e=zf.entries(); 317 while (e.hasMoreElements()) 318 { 319 JarEntry ze=(JarEntry)e.nextElement(); 320 htSizes.put(ze.getName(),new Integer ((int)ze.getSize())); 321 } 322 zf.close(); 323 } 324 catch(IOException ioe){ 325 System.err.println("TOP Error reading sizes in '"+jarFile+"':"+ioe); 326 } 327 } 328 329 337 private byte[] readBytesFromTopJar(String entryName) 338 { 339 try 340 { 341 JarInputStream zis=getTopJIS(); 342 JarEntry ze=null; 343 while ((ze=zis.getNextJarEntry())!=null) 344 { 345 if (ze.isDirectory()) 346 continue; 347 348 int size=(int)ze.getSize(); 349 if (size==-1) 350 size=((Integer )htSizes.get(ze.getName())).intValue(); 351 352 if(ze.getName().equals(entryName)) 353 { 354 byte[] b=new byte[(int)size]; 356 int rb=0; 357 int chunk=0; 358 while (((int)size - rb) > 0) 359 { 360 chunk=zis.read(b,rb,(int)size - rb); 361 if (chunk==-1) 362 break; 363 rb+=chunk; 364 } 365 zis.close(); 366 return b; 367 } 368 } 369 zis.close() ; 370 } 371 catch (NullPointerException e) { 372 Ut.error("'"+jarFile+"':(done)"+e); 373 } catch (FileNotFoundException e) { 374 Ut.error("'"+jarFile+"':"+e); 375 } catch (IOException e) { 376 Ut.error("'"+jarFile+"':"+e); 377 } 378 return null; 379 } 380 381 385 private JarInputStream getTopJIS() 386 { 387 try{ 389 JarFile jf=new JarFile(jarFile); 390 if(jf!=null) 391 return new JarInputStream(new FileInputStream(jarFile)); 392 } 393 catch(Exception e) 394 { 395 Ut.error("ERROR:"+e); 396 } 397 398 try 399 { 400 InputStream is=openResource(jarFile); 402 return new JarInputStream(is); 403 } 404 catch(IOException io){ 405 Ut.error("IOE:"+io); 406 } 407 return null; 408 } 409 410 416 private InputStream openResource(String filename) 417 { 418 try 419 { 420 InputStream is=getClass().getResourceAsStream("/"+filename); 422 is.available(); return is; 424 } 425 catch (java.io.IOException e) 426 { 427 Ut.error("RM2: file not found:'"+filename+"'"); 428 System.exit(1); 429 } 430 catch(NullPointerException e2){ 431 Ut.error("RM3: file not found:'"+filename+"'"); 432 System.exit(1); 434 } 435 return null; 436 } 437 438 } 439 440 | Popular Tags |