1 17 18 package org.sape.carbon.services.config.jar; 19 20 import java.io.File ; 21 import java.io.IOException ; 22 import java.io.InputStream ; 23 import java.io.OutputStream ; 24 import java.util.jar.JarEntry ; 25 26 import org.sape.carbon.core.config.format.ConfigurationFormatService; 27 import org.sape.carbon.core.config.node.AbstractConfigurationDocument; 28 import org.sape.carbon.core.config.node.Node; 29 import org.sape.carbon.core.config.node.NodeRemovalException; 30 import org.sape.carbon.core.exception.IllegalStateException; 31 import org.sape.carbon.core.exception.InvalidParameterException; 32 import org.sape.carbon.core.util.jar.EnhancedJarFile; 33 import org.sape.carbon.core.util.thread.ReadWriteLock; 34 35 import org.apache.commons.logging.Log; 36 import org.apache.commons.logging.LogFactory; 37 38 51 public class JarConfigurationDocument 52 extends AbstractConfigurationDocument { 53 54 57 private Log log = LogFactory.getLog(this.getClass()); 58 59 60 private File jarFile; 61 62 63 private String jarEntryName; 64 65 66 private EnhancedJarFile currentOpenJar = null; 67 68 69 private boolean jarOpenForRead; 70 71 72 private final ReadWriteLock readWriteLock; 73 74 84 public JarConfigurationDocument( 85 Node parent, 86 String name, 87 ConfigurationFormatService formatter, 88 ReadWriteLock readWriteLock, 89 File jarFile, 90 String jarEntryName) { 91 92 super(parent, name, formatter); 93 94 if (jarFile != null) { 96 this.jarFile = jarFile; 97 } else { 98 throw new InvalidParameterException( 99 this.getClass(), 100 "The [" 101 + getAbsoluteName() 102 + "] JarConfigurationDocument " 103 + "node jarFile reference cannot be null"); 104 } 105 106 if (jarEntryName != null) { 107 this.jarEntryName = jarEntryName; 108 } else { 109 throw new InvalidParameterException( 110 this.getClass(), 111 "The [" 112 + getAbsoluteName() 113 + "] JarConfigurationDocument " 114 + "node jarEntryName cannot be null"); 115 } 116 117 if (readWriteLock != null) { 118 this.readWriteLock = readWriteLock; 119 } else { 120 throw new InvalidParameterException( 121 this.getClass(), 122 "The [" 123 + getAbsoluteName() 124 + "] JarConfigurationDocument " 125 + "node readWriteLock reference cannot be null"); 126 } 127 } 128 129 132 protected InputStream openInputStream() throws IOException { 133 134 EnhancedJarFile jar = openJar(true); 135 136 JarEntry jarEntry = (JarEntry ) jar.getEntry(this.jarEntryName); 137 if (jarEntry == null) { 138 closeJar(); 139 throw new IllegalStateException ( 140 this.getClass(), 141 "JarEntry [" + this.jarEntryName 142 + "] was not found in the jar [" 143 + jarFile.getAbsolutePath() + "]"); 144 } 145 146 return jar.getInputStream(jarEntry); 147 } 148 149 152 protected void closeInputStream(InputStream in) throws IOException { 153 154 try { 157 in.close(); 158 } catch (IOException ioe) { 159 throw ioe; 160 } finally { 161 closeJar(); 162 } 163 } 164 165 168 protected OutputStream openOutputStream() throws IOException { 169 EnhancedJarFile jar = openJar(false); 170 return jar.getEntryOutputStream(this.jarEntryName); 171 } 172 173 176 protected void closeOutputStream(OutputStream out) 177 throws IOException { 178 179 try { 182 out.close(); 183 } catch (IOException ioe) { 184 throw ioe; 185 } finally { 186 closeJar(); 187 } 188 } 189 190 193 protected void destroyBackingData() throws NodeRemovalException { 194 EnhancedJarFile jar = null; 195 try { 196 197 jar = openJar(false); 198 jar.removeEntry((JarEntry ) jar.getEntry(this.jarEntryName)); 199 200 } catch (IOException ioe) { 201 throw new NodeRemovalException( 202 this.getClass(), this, ioe); 203 204 } finally { 205 if (jar != null) { 206 try { 208 closeJar(); 209 } catch (IOException ioe) { 210 if (log.isWarnEnabled()) { 212 log.warn("Could not closr jar [" 213 + currentOpenJar.getName() + "]"); 214 } 215 } 216 } 217 } 218 } 219 220 223 protected boolean backingDataExists() { 224 if (!this.jarFile.exists() || this.jarFile.isDirectory()) { 225 return false; 226 } 227 228 try { 229 EnhancedJarFile jar = openJar(true); 230 JarEntry entry = jar.getJarEntry(this.jarEntryName); 231 return (entry != null && !entry.isDirectory()); 232 233 } catch (IOException ioe) { 234 return false; 235 236 } finally { 237 try { 238 closeJar(); 239 } catch (IOException e) { 240 if (log.isWarnEnabled()) { 242 log.warn("Could not close jar [" 243 + currentOpenJar.getName() + "]"); 244 } 245 } 246 } 247 } 248 249 258 private EnhancedJarFile openJar(boolean forRead) throws IOException { 259 260 if (this.currentOpenJar != null) { 261 if (log.isWarnEnabled()) { 264 log.warn("Jar file [" + currentOpenJar.getName() 265 + "] has not been closed"); 266 } 267 } else { 268 try { 270 if (forRead) { 271 this.readWriteLock.readLock().acquire(); 272 this.jarOpenForRead = true; 273 } else { 274 this.readWriteLock.writeLock().acquire(); 275 this.jarOpenForRead = false; 276 } 277 } catch (InterruptedException ie) { 278 Thread.currentThread().interrupt(); 279 throw new IllegalStateException ( 280 this.getClass(), 281 "Caught interupt exception before acquiring jar monitor", 282 ie); 283 } 284 this.currentOpenJar = new EnhancedJarFile(this.jarFile); 286 } 287 288 return this.currentOpenJar; 289 } 290 291 297 private void closeJar() throws IOException { 298 if (this.currentOpenJar != null) { 299 300 try { 303 this.currentOpenJar.close(); 304 } catch (IOException ioe) { 305 throw ioe; 307 } finally { 308 this.currentOpenJar = null; 309 if (this.jarOpenForRead) { 310 this.readWriteLock.readLock().release(); 311 } else { 312 this.readWriteLock.writeLock().release(); 313 } 314 } 315 } 316 } 317 } 318 | Popular Tags |