1 23 package com.sun.enterprise.util; 24 25 import java.io.*; 26 import java.util.*; 27 import java.util.zip.*; 28 29 import java.util.logging.*; 31 import com.sun.logging.*; 32 34 35 44 45 public class JarAccess { 46 static Logger _logger=LogDomains.getLogger(LogDomains.UTIL_LOGGER); 48 50 public static final String MANIFEST = "META-INF/MANIFEST.MF"; 51 static final char SEPARATOR = File.separatorChar; 52 53 private File jarName; private File dirName; private String beanName; private String [] fileNames; 58 private static LocalStringManagerImpl localStrings = 59 new LocalStringManagerImpl(JarAccess.class); 60 61 62 72 public static void create(OutputStream out, 73 File baseDir, 74 String [] ejbNames, 75 String [] files) 76 throws IOException 77 { 78 int start = 0; 79 if (ejbNames != null) { 80 start = 1; 81 } 82 JarEntrySource[] data = new JarEntrySource[files.length + start]; 83 if (ejbNames != null) { 84 data[0] = makeManifestEntry(ejbNames); 85 } 86 for (int i = 0; i<files.length; i++) { 87 data[i+start] = new JarEntrySource(entryName(files[i]), 88 new File(baseDir, files[i])); 89 } 90 create(out, data); 91 } 92 93 96 public static JarEntrySource makeManifestEntry(String [] ejbNames) { 97 StringBuffer s = new StringBuffer ("Manifest-Version: 1.0\n"); 98 s.append("\n"); 99 for ( int i=0; i<ejbNames.length; i++ ) { 100 s.append("Name: "+ejbNames[i]+"\n"); 101 s.append("Enterprise-Bean: True\n"); 102 s.append("\n"); 103 } 104 return new JarEntrySource(MANIFEST, 105 new ByteArrayInputStream(s.toString().getBytes())); 106 } 107 108 111 public static void create(OutputStream out, 112 String [] files) throws IOException { 113 ZipOutputStream zos = new ZipOutputStream(out); 114 for (int i = 0; i < files.length; i++) { 115 addEntry(zos, new JarEntrySource(new File(files[i]))); 116 } 117 zos.close(); 118 } 119 120 123 public static void create(OutputStream out, 124 JarEntrySource[] entries) throws IOException { 125 ZipOutputStream zos = new ZipOutputStream(out); 126 for (int i = 0; i < entries.length; i++) { 127 try { 128 addEntry(zos, entries[i]); 129 } catch ( Exception ex ) { 130 133 _logger.log(Level.SEVERE,"enterprise_util.excep_jaraccess_create",ex); 135 throw new IOException("Invalid JAR entry: " 137 +entries[i].getName()); 138 } 139 } 140 zos.close(); 141 } 142 143 private static String entryName(String name) { 144 name = name.replace(File.separatorChar, '/'); 145 if (name.startsWith("/")) { 146 name = name.substring(1); 147 } else if (name.startsWith("./")) { 148 name = name.substring(2); 149 } 150 return name; 151 } 152 153 156 static void addEntry(ZipOutputStream zos, 157 JarEntrySource source) throws IOException { 158 String name = source.getName(); 159 if (name.equals("") || name.equals(".")) { 160 return; 161 } 162 163 165 ZipEntry e = new ZipEntry(name); 166 167 e.setTime(source.getTime()); 168 boolean markOnly = source.isMarkOnly(); 169 170 if (markOnly) { 171 e.setMethod(ZipEntry.STORED); 172 e.setSize(0); 173 e.setCrc(0); 174 } 175 zos.putNextEntry(e); 176 if (! markOnly) { 177 byte[] buf = new byte[1024]; 178 int len = 0; 179 InputStream is = new BufferedInputStream(source.getInputStream()); 180 181 while (len != -1) { 182 try{ 183 len = is.read(buf, 0, buf.length); 184 }catch(EOFException eof){ 185 break; 186 } 187 188 if(len != -1) 189 zos.write(buf, 0, len); 190 } 191 192 is.close(); 193 } 194 zos.closeEntry(); 195 } 196 197 200 public static void extract(InputStream in, 201 String files[]) throws IOException { 202 ZipInputStream zis = new ZipInputStream(in); 203 ZipEntry e; 204 while ((e = zis.getNextEntry()) != null) { 205 if (files == null) { 206 extractFile(zis, e); 207 } else { 208 String name = e.getName().replace('/', File.separatorChar); 209 for (int i = 0; i < files.length; i++) { 210 if (name.startsWith(files[i])) { 211 extractFile(zis, e); 212 break; 213 } 214 } 215 } 216 } 217 } 218 219 222 private static void extractFile(ZipInputStream zis, ZipEntry e) 223 throws IOException 224 { 225 File f = new File(e.getName().replace('/', File.separatorChar)); 226 if (e.isDirectory()) { 227 if (!f.exists() && !f.mkdirs() || !f.isDirectory()) { 228 throw new IOException(f + ": could not create directory"); 229 } 230 } else { 231 if (f.getParent() != null) { 232 File d = new File(f.getParent()); 233 if (!d.exists() && !d.mkdirs() || !d.isDirectory()) { 234 throw new IOException(d + ": could not create directory"); 235 } 236 } 237 OutputStream os = new FileOutputStream(f); 238 byte[] b = new byte[512]; 239 int len; 240 while ((len = zis.read(b, 0, b.length)) != -1) { 241 os.write(b, 0, len); 242 } 243 zis.closeEntry(); 244 os.close(); 245 } 246 } 247 248 249 252 public static Vector extract(InputStream in, 253 String files[], 254 String directory) throws IOException { 255 Vector extractedFiles = new Vector(); 256 ZipInputStream zis = new ZipInputStream(in); 257 ZipEntry e; 258 while ((e = zis.getNextEntry()) != null) { 259 if (files == null) { 260 File extractedFile = extractFile(zis, e, directory); 261 extractedFiles.addElement(extractedFile); 262 } else { 263 String name = e.getName().replace('/', File.separatorChar); 264 for (int i = 0; i < files.length; i++) { 265 if (name.startsWith(files[i])) { 266 File extractedFile = extractFile(zis, e, directory); 267 extractedFiles.addElement(extractedFile); 268 break; 269 } 270 } 271 } 272 } 273 return extractedFiles; 274 } 275 276 277 281 private static File extractFile(ZipInputStream zis, ZipEntry e, String dir) 282 throws IOException 283 { 284 File f = new File(dir+File.separatorChar+e.getName().replace('/', File.separatorChar)); 285 if (e.isDirectory()) { 286 if (!f.exists() && !f.mkdirs() || !f.isDirectory()) { 287 throw new IOException(f + ": could not create directory"); 288 } 289 } else { 290 if (f.getParent() != null) { 291 File d = new File(f.getParent()); 292 if (!d.exists() && !d.mkdirs() || !d.isDirectory()) { 293 throw new IOException(d + ": could not create directory"); 294 } 295 } 296 OutputStream os = new FileOutputStream(f); 297 byte[] b = new byte[512]; 298 int len; 299 while ((len = zis.read(b, 0, b.length)) != -1) { 300 os.write(b, 0, len); 301 } 302 zis.closeEntry(); 303 os.close(); 304 } 305 return f; 306 } 307 308 311 private static void list(InputStream in, String files[]) 312 throws IOException 313 { 314 ZipInputStream zis = new ZipInputStream(in); 315 ZipEntry e; 316 while ((e = zis.getNextEntry()) != null) { 317 String name = e.getName().replace('/', File.separatorChar); 318 324 zis.closeEntry(); 325 if (files == null) { 326 printEntry(e); 327 } else { 328 for (int i = 0; i < files.length; i++) { 329 if (name.startsWith(files[i])) { 330 printEntry(e); 331 break; 332 } 333 } 334 } 335 } 336 } 337 338 341 private static void printEntry(ZipEntry e) throws IOException { 342 output(e.getName()); 343 } 344 345 348 private boolean parseArgs(String args[]) { 349 int l = args.length; 350 int i; 351 for (i = 0; i<l; i++) { 352 if (args[i].equals("-bean")) { 353 if (i+1 >= l) { 354 error(localStrings.getLocalString("jaraccess.bean.option", 355 "")); 356 return false; 357 } 358 beanName = args[i+1]; 359 i += 1; 360 } else if (args[i].equals("-dir")) { 361 if (i+1 >= l) { 362 error(localStrings.getLocalString("jaraccess.dir.option", 363 "")); 364 return false; 365 } 366 dirName = new File(args[i+1]); 367 i += 1; 368 } else { 369 break; 370 } 371 } 372 if (i+1 >= l) { 373 error(localStrings.getLocalString("jaraccess.num.args", "")); 374 return false; 375 } 376 jarName = new File(args[i]); 377 i += 1; 378 fileNames = new String [l-i]; 379 for (int j=0; j<l-i ; j++) { 380 fileNames[j] = args[j+i]; 381 } 382 return true; 384 } 385 386 389 private void printArgs() { 390 396 if (_logger.isLoggable(Level.FINE)) { 398 _logger.log(Level.FINE,"jarName: "+jarName); 399 _logger.log(Level.FINE,"dirName: "+dirName); 400 _logger.log(Level.FINE,"beanName: "+beanName); 401 _logger.log(Level.FINE,"fileNames: "+fileNames); 402 } 403 if (fileNames != null) { 405 for (int i=0; i<fileNames.length; i++) { 406 409 if (_logger.isLoggable(Level.FINE)) { 411 _logger.log(Level.FINE,"fileNames["+i+"]: "+fileNames[i]); 412 } 413 } 415 } 416 } 417 418 421 protected static void output(String s) { 422 425 _logger.log(Level.FINE,s); 427 } 429 430 433 protected static void error(String s) { 434 437 _logger.log(Level.SEVERE,"enterprise_util.some_error",s); 439 } 441 442 } 443 | Popular Tags |