1 17 18 package org.sape.carbon.core.util.jar; 19 20 import java.io.File ; 21 import java.io.IOException ; 22 import java.io.InputStream ; 23 import java.util.ArrayList ; 24 import java.util.Enumeration ; 25 import java.util.List ; 26 import java.util.StringTokenizer ; 27 import java.util.jar.JarEntry ; 28 import java.util.jar.JarFile ; 29 import java.util.jar.Manifest ; 30 import java.util.zip.ZipEntry ; 31 32 import org.sape.carbon.core.exception.ExceptionUtility; 33 import org.sape.carbon.core.exception.InvalidParameterException; 34 35 import org.apache.commons.logging.Log; 36 import org.apache.commons.logging.LogFactory; 37 38 51 public class EnhancedJarFile { 52 public static final String JAR_DELIMETER = "/"; 53 54 57 private Log log = LogFactory.getLog(this.getClass()); 58 59 private JarFile jar; 60 61 64 public EnhancedJarFile(String name) throws IOException { 65 if (log.isTraceEnabled()) { 66 log.trace("Openning Jar [" + name + "] " + 67 ExceptionUtility.captureStackTrace(new Exception ())); 68 } 69 this.jar = new JarFile (name); 70 } 71 72 75 public EnhancedJarFile(String name, boolean verify) throws IOException { 76 if (log.isTraceEnabled()) { 77 log.trace("Openning Jar [" + name + "] " + 78 ExceptionUtility.captureStackTrace(new Exception ())); 79 } 80 this.jar = new JarFile (name, verify); 81 } 82 83 86 public EnhancedJarFile(File file) throws IOException { 87 if (log.isTraceEnabled()) { 88 log.trace("Openning Jar [" + file.getAbsolutePath() + 89 "] " + ExceptionUtility.captureStackTrace(new Exception ())); 90 } 91 this.jar = new JarFile (file); 92 } 93 94 97 public EnhancedJarFile(File file, boolean verify) throws IOException { 98 if (log.isTraceEnabled()) { 99 log.trace("Openning Jar [" + file.getAbsolutePath() + 100 "] " + ExceptionUtility.captureStackTrace(new Exception ())); 101 } 102 this.jar = new JarFile (file, verify); 103 } 104 105 108 public EnhancedJarFile(File file, boolean verify, int mode) 109 throws IOException { 110 if (log.isTraceEnabled()) { 111 log.trace("Openning Jar [" + file.getAbsolutePath() + 112 "] " + ExceptionUtility.captureStackTrace(new Exception ())); 113 } 114 this.jar = new JarFile (file, verify, mode); 115 } 116 117 127 public List listSubEntries(String entryName) { 128 129 if (entryName == null) { 130 throw new InvalidParameterException( 131 this.getClass(), 132 "Entry name cannot be null"); 133 } 134 135 136 Enumeration entries = jar.entries(); 137 List subEntries = new ArrayList (); 138 139 while(entries.hasMoreElements()) { 140 JarEntry nextEntry = (JarEntry ) entries.nextElement(); 141 142 if (nextEntry.getName().startsWith(entryName)) { 143 146 StringTokenizer tokenizer = new StringTokenizer ( 149 nextEntry.getName().substring(entryName.length()), 150 EnhancedJarFile.JAR_DELIMETER); 151 152 if (tokenizer.countTokens() == 1) { 153 subEntries.add(nextEntry); 155 } 156 } 157 } 158 159 return subEntries; 160 } 161 162 170 public JarEntryOutputStream getEntryOutputStream(String entryName) { 171 return new JarEntryOutputStream(this, entryName); 172 } 173 174 182 public void removeEntry(JarEntry entry) throws IOException { 183 if (entry != null && getEntry(entry.getName()) != null) { 185 JarEntryOutputStream outputStream = 186 new JarEntryOutputStream(this, entry.getName()); 187 188 outputStream.close(); 189 } 190 } 191 192 195 public Enumeration entries() { 196 return this.jar.entries(); 197 } 198 199 202 public ZipEntry getEntry(String arg0) { 203 return this.jar.getEntry(arg0); 204 } 205 206 209 public InputStream getInputStream(ZipEntry arg0) throws IOException { 210 return this.jar.getInputStream(arg0); 211 } 212 213 216 public JarEntry getJarEntry(String arg0) { 217 return this.jar.getJarEntry(arg0); 218 } 219 220 223 public Manifest getManifest() throws IOException { 224 return this.jar.getManifest(); 225 } 226 227 230 public void close() throws IOException { 231 if (log.isTraceEnabled()) { 232 log.trace("Closing Jar [" + this.jar.getName() + 233 "] " + ExceptionUtility.captureStackTrace(new Exception ())); 234 } 235 this.jar.close(); 236 } 237 238 241 public String getName() { 242 return this.jar.getName(); 243 } 244 245 248 public int size() { 249 return this.jar.size(); 250 } 251 252 262 void swapJars(File newJarFile) throws IOException { 263 File oldJarFile = new File (getName()); 264 this.jar.close(); 265 oldJarFile.delete(); 266 if (newJarFile.renameTo(oldJarFile)) { 267 this.jar = new JarFile (oldJarFile); 268 } else { 269 throw new IOException (); 270 } 271 } 272 } 273 | Popular Tags |