aries-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From mahrw...@apache.org
Subject svn commit: r1104438 - in /aries/trunk/application: application-api/src/main/java/org/apache/aries/application/filesystem/ application-utils/src/main/java/org/apache/aries/application/utils/filesystem/impl/
Date Tue, 17 May 2011 17:56:35 GMT
Author: mahrwald
Date: Tue May 17 17:56:34 2011
New Revision: 1104438

URL: http://svn.apache.org/viewvc?rev=1104438&view=rev
Log:
ARIES-652: Allow more efficient batch operations on IDirectories and IFiles

Added:
    aries/trunk/application/application-api/src/main/java/org/apache/aries/application/filesystem/ICloseableDirectory.java
    aries/trunk/application/application-utils/src/main/java/org/apache/aries/application/utils/filesystem/impl/CloseableDirectory.java
    aries/trunk/application/application-utils/src/main/java/org/apache/aries/application/utils/filesystem/impl/NestedCloseableDirectory.java
    aries/trunk/application/application-utils/src/main/java/org/apache/aries/application/utils/filesystem/impl/ZipCloseableDirectory.java
Modified:
    aries/trunk/application/application-api/src/main/java/org/apache/aries/application/filesystem/IDirectory.java
    aries/trunk/application/application-utils/src/main/java/org/apache/aries/application/utils/filesystem/impl/DirectoryImpl.java
    aries/trunk/application/application-utils/src/main/java/org/apache/aries/application/utils/filesystem/impl/FileSystemImpl.java
    aries/trunk/application/application-utils/src/main/java/org/apache/aries/application/utils/filesystem/impl/NestedZipDirectory.java
    aries/trunk/application/application-utils/src/main/java/org/apache/aries/application/utils/filesystem/impl/NestedZipFile.java
    aries/trunk/application/application-utils/src/main/java/org/apache/aries/application/utils/filesystem/impl/ZipDirectory.java
    aries/trunk/application/application-utils/src/main/java/org/apache/aries/application/utils/filesystem/impl/ZipFileImpl.java

Added: aries/trunk/application/application-api/src/main/java/org/apache/aries/application/filesystem/ICloseableDirectory.java
URL: http://svn.apache.org/viewvc/aries/trunk/application/application-api/src/main/java/org/apache/aries/application/filesystem/ICloseableDirectory.java?rev=1104438&view=auto
==============================================================================
--- aries/trunk/application/application-api/src/main/java/org/apache/aries/application/filesystem/ICloseableDirectory.java
(added)
+++ aries/trunk/application/application-api/src/main/java/org/apache/aries/application/filesystem/ICloseableDirectory.java
Tue May 17 17:56:34 2011
@@ -0,0 +1,34 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIESOR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.aries.application.filesystem;
+
+import java.io.Closeable;
+
+/**
+ * Implementation of IDirectory that is more efficient for batch operations as it does not
due 
+ * automatic resource management. Instead the user has to explicitly call close to release
resources.
+ * Resources are cached for the current IDirectory archive only. Nested archives should be
converted to 
+ * {@link ICloseableDirectory} separately.
+ */
+public interface ICloseableDirectory extends IDirectory, Closeable {
+	/**
+	 * Checks whether the closeable directory has been closed
+	 */
+	boolean isClosed();
+}

Modified: aries/trunk/application/application-api/src/main/java/org/apache/aries/application/filesystem/IDirectory.java
URL: http://svn.apache.org/viewvc/aries/trunk/application/application-api/src/main/java/org/apache/aries/application/filesystem/IDirectory.java?rev=1104438&r1=1104437&r2=1104438&view=diff
==============================================================================
--- aries/trunk/application/application-api/src/main/java/org/apache/aries/application/filesystem/IDirectory.java
(original)
+++ aries/trunk/application/application-api/src/main/java/org/apache/aries/application/filesystem/IDirectory.java
Tue May 17 17:56:34 2011
@@ -23,7 +23,7 @@ import java.util.List;
 
 /**
  * A virtual directory in a file system. Widely used to present a common view of regular

- * file sytems, jar and zip files. 
+ * file systems, jar and zip files. 
  */
 public interface IDirectory extends Iterable<IFile>, IFile
 {
@@ -48,8 +48,21 @@ public interface IDirectory extends Iter
    * @return     the IFile, or null if no such file exists.
    */
   public IFile getFile(String name);
+  
   /**
    * @return true if this IDirectory is the root of the virtual file system.
    */
   public boolean isRoot();
+  
+  /**
+   * Open a more effective implementation with user regulated resource management. The implementation
will be 
+   * more efficient for batch operations. Make sure to call close when finished with the
returned IDirectory. 
+   * 
+   * IFiles and IDirectories other than the returned closeable directory
+   * will stay valid after calling the close method but will no longer perform as efficiently.
InputStreams that are
+   * open at the time of calling close may be invalidated.
+   * 
+   * @return {@link ICloseableDirectory} or null if a batch aware version of this {@link
IDirectory} is not supported
+   */
+  public ICloseableDirectory toCloseable();
 }
\ No newline at end of file

Added: aries/trunk/application/application-utils/src/main/java/org/apache/aries/application/utils/filesystem/impl/CloseableDirectory.java
URL: http://svn.apache.org/viewvc/aries/trunk/application/application-utils/src/main/java/org/apache/aries/application/utils/filesystem/impl/CloseableDirectory.java?rev=1104438&view=auto
==============================================================================
--- aries/trunk/application/application-utils/src/main/java/org/apache/aries/application/utils/filesystem/impl/CloseableDirectory.java
(added)
+++ aries/trunk/application/application-utils/src/main/java/org/apache/aries/application/utils/filesystem/impl/CloseableDirectory.java
Tue May 17 17:56:34 2011
@@ -0,0 +1,160 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIESOR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.aries.application.utils.filesystem.impl;
+
+import java.io.IOException;
+import java.io.InputStream;
+import java.net.MalformedURLException;
+import java.net.URL;
+import java.util.Iterator;
+import java.util.List;
+import java.util.concurrent.atomic.AtomicBoolean;
+
+import org.apache.aries.application.filesystem.ICloseableDirectory;
+import org.apache.aries.application.filesystem.IDirectory;
+import org.apache.aries.application.filesystem.IFile;
+
+public class CloseableDirectory implements ICloseableDirectory {
+	protected IDirectory delegate;
+	private final AtomicBoolean closed = new AtomicBoolean(false);
+
+	public CloseableDirectory(IDirectory delegate) {
+		this.delegate = delegate;
+	}
+	
+	@Override
+	public String getName() {
+		checkNotClosed();
+		return delegate.getName();
+	}
+
+	@Override
+	public boolean isDirectory() {
+		checkNotClosed();
+		return delegate.isDirectory();
+	}
+
+	@Override
+	public boolean isFile() {
+		checkNotClosed();
+		return delegate.isFile();
+	}
+
+	@Override
+	public long getLastModified() {
+		checkNotClosed();
+		return delegate.getLastModified();
+	}
+
+	@Override
+	public IFile getFile(String name) {
+		checkNotClosed();
+		return delegate.getFile(name);
+	}
+
+	@Override
+	public long getSize() {
+		checkNotClosed();
+		return delegate.getSize();
+	}
+
+	@Override
+	public IDirectory convert() {
+		checkNotClosed();
+		return delegate.convert();
+	}
+
+	@Override
+	public IDirectory convertNested() {
+		checkNotClosed();
+		return delegate.convertNested();
+	}
+
+	@Override
+	public boolean isRoot() {
+		checkNotClosed();
+		return delegate.isRoot();
+	}
+
+	@Override
+	public IDirectory getParent() {
+		checkNotClosed();
+		return delegate.getParent();
+	}
+
+	@Override
+	public IDirectory getRoot() {
+		checkNotClosed();
+		return delegate.getRoot();
+	}
+
+	@Override
+	public Iterator<IFile> iterator() {
+		checkNotClosed();
+		return delegate.iterator();
+	}
+
+	@Override
+	public List<IFile> listFiles() {
+		checkNotClosed();
+		return delegate.listFiles();
+	}
+
+	@Override
+	public List<IFile> listAllFiles() {
+		checkNotClosed();
+		return delegate.listAllFiles();
+	}
+
+	@Override
+	public ICloseableDirectory toCloseable() {
+		checkNotClosed();
+		return delegate.toCloseable();
+	}
+
+	@Override
+	public InputStream open() throws IOException, UnsupportedOperationException {
+		checkNotClosed();
+		return delegate.open();
+	}
+
+	@Override
+	public URL toURL() throws MalformedURLException {
+		checkNotClosed();
+		return delegate.toURL();
+	}
+
+	@Override
+	public final void close() throws IOException {
+		if (closed.compareAndSet(false, true)) {
+			cleanup();
+		}
+	}
+	
+	protected void cleanup() {}
+	
+	protected void checkNotClosed() {
+		if (isClosed()) throw new IllegalStateException("ICloseableDirectory is closed");
+	}
+
+	@Override
+	public boolean isClosed() {
+		return closed.get();
+	}
+}

Modified: aries/trunk/application/application-utils/src/main/java/org/apache/aries/application/utils/filesystem/impl/DirectoryImpl.java
URL: http://svn.apache.org/viewvc/aries/trunk/application/application-utils/src/main/java/org/apache/aries/application/utils/filesystem/impl/DirectoryImpl.java?rev=1104438&r1=1104437&r2=1104438&view=diff
==============================================================================
--- aries/trunk/application/application-utils/src/main/java/org/apache/aries/application/utils/filesystem/impl/DirectoryImpl.java
(original)
+++ aries/trunk/application/application-utils/src/main/java/org/apache/aries/application/utils/filesystem/impl/DirectoryImpl.java
Tue May 17 17:56:34 2011
@@ -26,6 +26,7 @@ import java.util.ArrayList;
 import java.util.Iterator;
 import java.util.List;
 
+import org.apache.aries.application.filesystem.ICloseableDirectory;
 import org.apache.aries.application.filesystem.IDirectory;
 import org.apache.aries.application.filesystem.IFile;
 
@@ -138,4 +139,9 @@ public class DirectoryImpl extends FileI
     }
     return result;
   }
+
+  @Override
+  public ICloseableDirectory toCloseable() {
+	return new CloseableDirectory(this);
+  }
 }

Modified: aries/trunk/application/application-utils/src/main/java/org/apache/aries/application/utils/filesystem/impl/FileSystemImpl.java
URL: http://svn.apache.org/viewvc/aries/trunk/application/application-utils/src/main/java/org/apache/aries/application/utils/filesystem/impl/FileSystemImpl.java?rev=1104438&r1=1104437&r2=1104438&view=diff
==============================================================================
--- aries/trunk/application/application-utils/src/main/java/org/apache/aries/application/utils/filesystem/impl/FileSystemImpl.java
(original)
+++ aries/trunk/application/application-utils/src/main/java/org/apache/aries/application/utils/filesystem/impl/FileSystemImpl.java
Tue May 17 17:56:34 2011
@@ -32,7 +32,7 @@ public class FileSystemImpl {
 				dir = new DirectoryImpl(fs, fs);
 			} else if (fs.isFile() && isValidZip(fs)) {
 				try {
-					dir = new ZipDirectory(fs, fs, parent);
+					dir = new ZipDirectory(fs, parent);
 				} catch (IOException e) {
 					_logger.error ("IOException in IDirectory.getFSRoot", e);
 				}

Added: aries/trunk/application/application-utils/src/main/java/org/apache/aries/application/utils/filesystem/impl/NestedCloseableDirectory.java
URL: http://svn.apache.org/viewvc/aries/trunk/application/application-utils/src/main/java/org/apache/aries/application/utils/filesystem/impl/NestedCloseableDirectory.java?rev=1104438&view=auto
==============================================================================
--- aries/trunk/application/application-utils/src/main/java/org/apache/aries/application/utils/filesystem/impl/NestedCloseableDirectory.java
(added)
+++ aries/trunk/application/application-utils/src/main/java/org/apache/aries/application/utils/filesystem/impl/NestedCloseableDirectory.java
Tue May 17 17:56:34 2011
@@ -0,0 +1,64 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIESOR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.aries.application.utils.filesystem.impl;
+
+import java.io.File;
+import java.io.FileOutputStream;
+import java.io.IOException;
+import java.util.zip.ZipFile;
+
+import org.apache.aries.application.filesystem.IFile;
+import org.apache.aries.application.utils.filesystem.IOUtils;
+
+public class NestedCloseableDirectory extends CloseableDirectory {
+
+	private final File tempFile;
+	private final ZipFile zip;
+	
+	public NestedCloseableDirectory(IFile archive, NestedZipDirectory delegate) throws IOException
{
+		super(delegate);
+		
+		tempFile = File.createTempFile("archiveExtract", ".jar");
+
+		FileOutputStream fos = null; 
+		try {
+			fos = new FileOutputStream(tempFile);
+			IOUtils.copy(archive.open(), fos);	
+			this.delegate = new NestedZipDirectory(delegate, this);
+			
+			zip = new ZipFile(tempFile);
+		} catch (IOException e) {
+			IOUtils.close(fos);
+			tempFile.delete();
+			throw e;
+		}
+	}
+	
+	public ZipFile getZipFile() {
+		return zip;
+	}
+
+	@Override
+	protected void cleanup() {
+		try {
+			zip.close();
+		} catch (IOException e) {}
+		tempFile.delete();
+	}
+}

Modified: aries/trunk/application/application-utils/src/main/java/org/apache/aries/application/utils/filesystem/impl/NestedZipDirectory.java
URL: http://svn.apache.org/viewvc/aries/trunk/application/application-utils/src/main/java/org/apache/aries/application/utils/filesystem/impl/NestedZipDirectory.java?rev=1104438&r1=1104437&r2=1104438&view=diff
==============================================================================
--- aries/trunk/application/application-utils/src/main/java/org/apache/aries/application/utils/filesystem/impl/NestedZipDirectory.java
(original)
+++ aries/trunk/application/application-utils/src/main/java/org/apache/aries/application/utils/filesystem/impl/NestedZipDirectory.java
Tue May 17 17:56:34 2011
@@ -1,3 +1,21 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIESOR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
 package org.apache.aries.application.utils.filesystem.impl;
 
 import java.io.IOException;
@@ -10,8 +28,10 @@ import java.util.LinkedHashMap;
 import java.util.List;
 import java.util.Map;
 import java.util.zip.ZipEntry;
+import java.util.zip.ZipFile;
 import java.util.zip.ZipInputStream;
 
+import org.apache.aries.application.filesystem.ICloseableDirectory;
 import org.apache.aries.application.filesystem.IDirectory;
 import org.apache.aries.application.filesystem.IFile;
 import org.apache.aries.application.utils.filesystem.IOUtils;
@@ -22,18 +42,22 @@ public class NestedZipDirectory extends 
 	
 	private static final Logger logger = LoggerFactory.getLogger("org.apache.aries.application.utils");
 	
-	public NestedZipDirectory(IFile archive, ZipEntry entry, NestedZipDirectory parent) {
-		super(archive, entry, parent);
+	public NestedZipDirectory(IFile archive, ZipEntry entry, NestedZipDirectory parent, NestedCloseableDirectory
cache) {
+		super(archive, entry, parent, cache);
 	}
 	
-	public NestedZipDirectory(IFile archive, String pathInZip, NestedZipDirectory parent) {
-		super(archive, pathInZip, parent);
+	public NestedZipDirectory(IFile archive, String pathInZip, NestedZipDirectory parent, NestedCloseableDirectory
cache) {
+		super(archive, pathInZip, parent, cache);
 	}
 	
 	public NestedZipDirectory(IFile archive) {
 		super(archive);
 	}
 	
+	public NestedZipDirectory(NestedZipDirectory other, NestedCloseableDirectory cache) {
+		super(other, cache);
+	}
+	
 	@Override
 	public IDirectory convert() {
 		return this;
@@ -55,36 +79,46 @@ public class NestedZipDirectory extends 
 	}
 	
 	private List<IFile> listFiles(boolean includeFilesInNestedSubdirs) {
-		ZipInputStream zis = null;
-		
-		try {
-			zis = new ZipInputStream(archive.open());
-			
-			ZipEntry entry = zis.getNextEntry();
-			
 			Map<String, ZipEntry> entriesByName = new LinkedHashMap<String, ZipEntry>();
-			while (entry != null) {
+			for (ZipEntry entry : getAllEntries()) {
 				if (ZipDirectory.isInDir(getNameInZip(), entry, includeFilesInNestedSubdirs)) {
 					entriesByName.put(entry.getName(), entry);
 				}
-				entry = zis.getNextEntry();
 			}
 
 			List<IFile> files = new ArrayList<IFile>();
 			for (ZipEntry ze : entriesByName.values()) {
 				NestedZipDirectory parent = includeFilesInNestedSubdirs ? buildParent(ze, entriesByName)
: this;
-				if (ze.isDirectory()) files.add(new NestedZipDirectory(archive, ze, parent));
-				else files.add(new NestedZipFile(archive, ze, parent));
+				if (ze.isDirectory()) files.add(new NestedZipDirectory(archive, ze, parent, cache));
+				else files.add(new NestedZipFile(archive, ze, parent, cache));
 			}
 			
 			return files;
-			
-		} catch (IOException e) {
-			logger.error("IOException reading nested ZipFile", e);
-			return Collections.emptyList();
-		} finally {
-			IOUtils.close(zis);
-		}
+	}
+	
+	private List<? extends ZipEntry> getAllEntries() {
+		if (cache != null && !!!cache.isClosed()) {
+			return Collections.list(cache.getZipFile().entries());
+		} else {
+			ZipInputStream zis = null;
+			try {
+				zis = new ZipInputStream(archive.open());
+				
+				List<ZipEntry> result = new ArrayList<ZipEntry>();
+				ZipEntry entry = zis.getNextEntry();
+				while (entry != null) {
+					result.add(entry);
+					entry = zis.getNextEntry();
+				}
+				
+				return result;				
+			} catch (IOException e) {
+				logger.error("IOException reading nested ZipFile", e);
+				return Collections.emptyList();
+			} finally {
+				IOUtils.close(zis);
+			}
+		}		
 	}
 	
 	private NestedZipDirectory buildParent(ZipEntry entry, Map<String,ZipEntry> entries)
{
@@ -101,9 +135,9 @@ public class NestedZipDirectory extends 
 				ZipEntry ze = entries.get(entryPath.toString());
 				
 				if (ze != null) {
-					result = new NestedZipDirectory(archive, ze, result);
+					result = new NestedZipDirectory(archive, ze, result, cache);
 				} else {
-					result = new NestedZipDirectory(archive, entryPath.toString(), result);
+					result = new NestedZipDirectory(archive, entryPath.toString(), result, cache);
 				}
 			}
 		}
@@ -113,34 +147,49 @@ public class NestedZipDirectory extends 
 
 	@Override
 	public IFile getFile(String name) {
-		ZipInputStream zis = null;
-		
-		try {
-			zis = new ZipInputStream(archive.open());
+		Map<String,ZipEntry> entries = new HashMap<String, ZipEntry>();
+		ZipEntry ze;
+
+		if (cache != null && !!!cache.isClosed()) {
+			ZipFile zip = cache.getZipFile();
+
+			String[] segments = name.split("/");
+			StringBuilder path = new StringBuilder();
+			for (String s : segments) {
+				path.append(s).append('/');
+				ZipEntry p = zip.getEntry(path.toString());
+				if (p != null) entries.put(path.toString(), p);
+			}
 			
-			Map<String,ZipEntry> entries = new HashMap<String, ZipEntry>();
+			ze = zip.getEntry(name);
 			
-			ZipEntry ze = zis.getNextEntry();
+		} else {
+			ZipInputStream zis = null;
 			
-			while (ze != null && !!!ze.getName().equals(name)) {
-				if (name.startsWith(ze.getName())) entries.put(ze.getName(), ze);
+			try {
+				zis = new ZipInputStream(archive.open());
 				
 				ze = zis.getNextEntry();
-			}
-			
-			if (ze != null) {
-				NestedZipDirectory parent = buildParent(ze, entries);
-				if (ze.isDirectory()) return new NestedZipDirectory(archive, ze, parent);
-				else return new NestedZipFile(archive, ze, parent);
-			} else {
+				
+				while (ze != null && !!!ze.getName().equals(name)) {
+					if (name.startsWith(ze.getName())) entries.put(ze.getName(), ze);
+					
+					ze = zis.getNextEntry();
+				}
+			} catch (IOException e) {
+				logger.error("IOException reading nested ZipFile", e);
 				return null;
+			} finally {
+				IOUtils.close(zis);
 			}
-			
-		} catch (IOException e) {
-			logger.error("IOException reading nested ZipFile", e);
+		}
+		
+		if (ze != null) {
+			NestedZipDirectory parent = buildParent(ze, entries);
+			if (ze.isDirectory()) return new NestedZipDirectory(archive, ze, parent, cache);
+			else return new NestedZipFile(archive, ze, parent, cache);
+		} else {
 			return null;
-		} finally {
-			IOUtils.close(zis);
 		}
 	}
 
@@ -165,4 +214,14 @@ public class NestedZipDirectory extends 
 		return false;
 	}
 
+	@Override
+	public ICloseableDirectory toCloseable() {
+		try {
+			return new NestedCloseableDirectory(archive, this);			
+		} catch (IOException e) {
+			logger.error("Exception while creating extracted version of nested zip file", e);
+			return null;
+		}		
+	}
+
 }

Modified: aries/trunk/application/application-utils/src/main/java/org/apache/aries/application/utils/filesystem/impl/NestedZipFile.java
URL: http://svn.apache.org/viewvc/aries/trunk/application/application-utils/src/main/java/org/apache/aries/application/utils/filesystem/impl/NestedZipFile.java?rev=1104438&r1=1104437&r2=1104438&view=diff
==============================================================================
--- aries/trunk/application/application-utils/src/main/java/org/apache/aries/application/utils/filesystem/impl/NestedZipFile.java
(original)
+++ aries/trunk/application/application-utils/src/main/java/org/apache/aries/application/utils/filesystem/impl/NestedZipFile.java
Tue May 17 17:56:34 2011
@@ -1,3 +1,21 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIESOR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
 package org.apache.aries.application.utils.filesystem.impl;
 
 import java.io.IOException;
@@ -5,6 +23,7 @@ import java.io.InputStream;
 import java.net.MalformedURLException;
 import java.net.URL;
 import java.util.zip.ZipEntry;
+import java.util.zip.ZipFile;
 import java.util.zip.ZipInputStream;
 
 import org.apache.aries.application.filesystem.IDirectory;
@@ -17,6 +36,7 @@ public class NestedZipFile implements IF
 	private final IDirectory parent;
 	protected final IFile archive;
 	private final String nameInZip;
+	protected final NestedCloseableDirectory cache;
 	
 	/**
 	 * Construct a nested zip file
@@ -24,7 +44,7 @@ public class NestedZipFile implements IF
 	 * @param entry
 	 * @param parent
 	 */
-	public NestedZipFile(IFile archive, ZipEntry entry, NestedZipDirectory parent) {
+	public NestedZipFile(IFile archive, ZipEntry entry, NestedZipDirectory parent, NestedCloseableDirectory
cache) {
 		this.archive = archive;
 		this.parent = parent;
 		this.nameInZip = entry.getName();
@@ -32,9 +52,10 @@ public class NestedZipFile implements IF
 		name = archive.getName() + "/" + (nameInZip.endsWith("/") ? nameInZip.substring(0, nameInZip.length()-1)
: nameInZip);
 		size = entry.getSize();
 		lastModified = entry.getTime();
+		this.cache = cache;
 	}
 	
-	public NestedZipFile(IFile archive, String pathInZip, NestedZipDirectory parent) {
+	public NestedZipFile(IFile archive, String pathInZip, NestedZipDirectory parent, NestedCloseableDirectory
cache) {
 		this.archive = archive;
 		this.parent = parent;
 		this.nameInZip = pathInZip;
@@ -42,6 +63,7 @@ public class NestedZipFile implements IF
 		name = archive.getName() + "/" + (nameInZip.endsWith("/") ? nameInZip.substring(0, nameInZip.length()-1)
: nameInZip);
 		size = -1;
 		lastModified = -1;
+		this.cache = cache;
 	}
 	
 	
@@ -53,6 +75,18 @@ public class NestedZipFile implements IF
 		name = archive.getName();
 		lastModified = archive.getLastModified();
 		size = archive.getSize();
+		cache = null;
+	}
+	
+	public NestedZipFile(NestedZipFile other, NestedCloseableDirectory cache) {
+		name = other.name;
+		size = other.size;
+		lastModified = other.lastModified;
+		parent = other.parent;
+		archive = other.archive;
+		nameInZip = other.nameInZip;
+		
+		this.cache = cache;
 	}
 	
 	public String getNameInZip() {
@@ -103,18 +137,26 @@ public class NestedZipFile implements IF
 
 	@Override
 	public InputStream open() throws IOException, UnsupportedOperationException {
-		final ZipInputStream zis = new ZipInputStream(archive.open());
-		
-		ZipEntry entry = zis.getNextEntry();
-		while (entry != null && !!!entry.getName().equals(nameInZip)) {
-			entry = zis.getNextEntry();
-		}
-		
-		if (entry != null) {
-			return zis;
+		if (cache != null && !!!cache.isClosed()) {
+			ZipFile zip = cache.getZipFile();
+			ZipEntry ze = zip.getEntry(nameInZip);
+			
+			if (ze != null) return zip.getInputStream(ze);
+			else return null;
 		} else {
-			zis.close();
-			return null;
+			final ZipInputStream zis = new ZipInputStream(archive.open());
+			
+			ZipEntry entry = zis.getNextEntry();
+			while (entry != null && !!!entry.getName().equals(nameInZip)) {
+				entry = zis.getNextEntry();
+			}
+			
+			if (entry != null) {
+				return zis;
+			} else {
+				zis.close();
+				return null;
+			}
 		}
 	}
 

Added: aries/trunk/application/application-utils/src/main/java/org/apache/aries/application/utils/filesystem/impl/ZipCloseableDirectory.java
URL: http://svn.apache.org/viewvc/aries/trunk/application/application-utils/src/main/java/org/apache/aries/application/utils/filesystem/impl/ZipCloseableDirectory.java?rev=1104438&view=auto
==============================================================================
--- aries/trunk/application/application-utils/src/main/java/org/apache/aries/application/utils/filesystem/impl/ZipCloseableDirectory.java
(added)
+++ aries/trunk/application/application-utils/src/main/java/org/apache/aries/application/utils/filesystem/impl/ZipCloseableDirectory.java
Tue May 17 17:56:34 2011
@@ -0,0 +1,47 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIESOR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.aries.application.utils.filesystem.impl;
+
+import java.io.File;
+import java.io.IOException;
+import java.util.zip.ZipFile;
+
+import org.apache.aries.application.utils.filesystem.IOUtils;
+
+public class ZipCloseableDirectory extends CloseableDirectory {
+	private final ZipFile zip;
+	
+	public ZipCloseableDirectory(File archive, ZipDirectory parent) throws IOException {
+		super(parent);
+
+		this.zip = new ZipFile(archive);
+		delegate = new ZipDirectory(parent, this);
+	}
+	
+	public ZipFile getZipFile() {
+		return zip;
+	}
+	
+	@Override
+	protected void cleanup() {
+		try {
+			IOUtils.close(zip);
+		} catch (IOException e) {}
+	}
+}

Modified: aries/trunk/application/application-utils/src/main/java/org/apache/aries/application/utils/filesystem/impl/ZipDirectory.java
URL: http://svn.apache.org/viewvc/aries/trunk/application/application-utils/src/main/java/org/apache/aries/application/utils/filesystem/impl/ZipDirectory.java?rev=1104438&r1=1104437&r2=1104438&view=diff
==============================================================================
--- aries/trunk/application/application-utils/src/main/java/org/apache/aries/application/utils/filesystem/impl/ZipDirectory.java
(original)
+++ aries/trunk/application/application-utils/src/main/java/org/apache/aries/application/utils/filesystem/impl/ZipDirectory.java
Tue May 17 17:56:34 2011
@@ -20,6 +20,7 @@
 package org.apache.aries.application.utils.filesystem.impl;
 
 import java.io.File;
+import java.io.IOException;
 import java.io.InputStream;
 import java.net.MalformedURLException;
 import java.util.ArrayList;
@@ -28,14 +29,20 @@ import java.util.Iterator;
 import java.util.List;
 import java.util.zip.ZipEntry;
 import java.util.zip.ZipFile;
+
+import org.apache.aries.application.filesystem.ICloseableDirectory;
 import org.apache.aries.application.filesystem.IDirectory;
 import org.apache.aries.application.filesystem.IFile;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
 
 /**
  * A directory in the zip.
  */
 public class ZipDirectory extends ZipFileImpl implements IDirectory
 {
+  private static final Logger logger = LoggerFactory.getLogger("org.apache.aries.application.utils");
+
   /** The root of the zip FS. */
   private final IDirectory root;
   private final boolean zipRoot;
@@ -47,9 +54,9 @@ public class ZipDirectory extends ZipFil
    * @param entry1 the entry in the zip representing this dir.
    * @param parent the parent directory.
    */
-  public ZipDirectory(File zip1, ZipEntry entry1, ZipDirectory parent)
+  public ZipDirectory(File zip1, ZipEntry entry1, ZipDirectory parent, ZipCloseableDirectory
cache)
   {
-    super(zip1, entry1, parent);
+    super(zip1, entry1, parent, cache);
     zipRoot = false;
     root = parent.getRoot();
   }
@@ -61,12 +68,18 @@ public class ZipDirectory extends ZipFil
    * @param parent
    * @throws MalformedURLException 
    */
-  public ZipDirectory(File file, File fs, IDirectory parent) throws MalformedURLException
+  public ZipDirectory(File fs, IDirectory parent) throws MalformedURLException
   {
-    super(file, fs, parent);
+    super(fs, parent);
     root = (parent == null) ? this : parent.getRoot();
     zipRoot = true;
   }
+  
+  public ZipDirectory(ZipDirectory other, ZipCloseableDirectory cache) {
+	  super(other, cache);
+	  root = other.root;
+	  zipRoot = other.zipRoot;
+  }
 
   @Override
   public IFile getFile(String name)
@@ -79,9 +92,9 @@ public class ZipDirectory extends ZipFil
     
     if (entryFile != null) {
       if (!!!entryFile.isDirectory()) {
-        result = new ZipFileImpl(zip, entryFile, buildParent(entryFile));
+        result = new ZipFileImpl(zip, entryFile, buildParent(entryFile), cache);
       } else {
-        result = new ZipDirectory(zip, entryFile, buildParent(entryFile));
+        result = new ZipDirectory(zip, entryFile, buildParent(entryFile), cache);
       }
     }
     return result;
@@ -116,7 +129,7 @@ public class ZipDirectory extends ZipFil
           result = this;
           break outer;
         }
-        result = new ZipDirectory(zip, dirEntry, result);
+        result = new ZipDirectory(zip, dirEntry, result, cache);
         baseBuilderCrapThingToGetRoundFindBugs.append('/');
       }
     }
@@ -152,9 +165,9 @@ public class ZipDirectory extends ZipFil
 		  if (isInDir(getNameInZip(), possibleEntry, includeFilesInNestedSubdirs)) {
 			  ZipDirectory parent = includeFilesInNestedSubdirs ? buildParent(possibleEntry) : this;
 			  if (possibleEntry.isDirectory()) {
-				  files.add(new ZipDirectory(zip, possibleEntry, parent));
+				  files.add(new ZipDirectory(zip, possibleEntry, parent, cache));
 			  } else {
-				  files.add(new ZipFileImpl(zip, possibleEntry, parent));
+				  files.add(new ZipFileImpl(zip, possibleEntry, parent, cache));
 			  }
 		  }
 
@@ -239,7 +252,7 @@ public class ZipDirectory extends ZipFil
     return super.hashCode();
   }
   
-  private ZipEntry getEntry(String entryName){
+  private ZipEntry getEntry(String entryName) {
     ZipFile z = openZipFile();
     ZipEntry entryFile = null;
     
@@ -249,4 +262,16 @@ public class ZipDirectory extends ZipFil
     }
     return entryFile;
   }
-}
\ No newline at end of file
+
+  @Override
+  public ICloseableDirectory toCloseable() {
+	  try {
+		  return new ZipCloseableDirectory(zip, this);
+	  } catch (IOException e) {
+		  logger.error("IOException opening zip file", this);
+		  return null;
+	  }
+  }
+}
+
+

Modified: aries/trunk/application/application-utils/src/main/java/org/apache/aries/application/utils/filesystem/impl/ZipFileImpl.java
URL: http://svn.apache.org/viewvc/aries/trunk/application/application-utils/src/main/java/org/apache/aries/application/utils/filesystem/impl/ZipFileImpl.java?rev=1104438&r1=1104437&r2=1104438&view=diff
==============================================================================
--- aries/trunk/application/application-utils/src/main/java/org/apache/aries/application/utils/filesystem/impl/ZipFileImpl.java
(original)
+++ aries/trunk/application/application-utils/src/main/java/org/apache/aries/application/utils/filesystem/impl/ZipFileImpl.java
Tue May 17 17:56:34 2011
@@ -57,6 +57,8 @@ public class ZipFileImpl implements IFil
   private final String url;
   /** The path of the zip archive to the VFS root */
   private final String zipPathToRoot;
+  /** The closeable directory that caches the open ZipFile */
+  protected final ZipCloseableDirectory cache;
   
   /**
    * This constructor is used to create a file entry within the zip.
@@ -65,7 +67,7 @@ public class ZipFileImpl implements IFil
    * @param entry1  the entry this IFile represents.
    * @param parent1 the parent directory.
    */
-  public ZipFileImpl(File zip1, ZipEntry entry1, ZipDirectory parent1)
+  public ZipFileImpl(File zip1, ZipEntry entry1, ZipDirectory parent1, ZipCloseableDirectory
cache)
   {
     this.zip = zip1;
     this.entry = entry1;
@@ -82,6 +84,7 @@ public class ZipFileImpl implements IFil
     url = ((ZipFileImpl)parent1).url;
     
     this.parent = parent1;
+    this.cache = cache;
   }
   
   /**
@@ -92,7 +95,7 @@ public class ZipFileImpl implements IFil
    * @param rootName the name of this zipfile relative to the IFile filesystem root
    * @throws MalformedURLException
    */
-  protected ZipFileImpl(File zip1, File fs, IDirectory parent) throws MalformedURLException
+  protected ZipFileImpl(File zip1, IDirectory parent) throws MalformedURLException
   {
     this.zip = zip1;
     this.entry = null;
@@ -107,9 +110,22 @@ public class ZipFileImpl implements IFil
     	zipPathToRoot = name+"/";
     }
     
-    lastModified = fs.lastModified();
-    size = fs.length();
-    url = fs.toURI().toURL().toExternalForm();
+    lastModified = zip1.lastModified();
+    size = zip1.length();
+    url = zip1.toURI().toURL().toExternalForm();
+    this.cache = null;
+  }
+  
+  public ZipFileImpl(ZipFileImpl other, ZipCloseableDirectory cache) {
+	  name = other.name;
+	  size = other.size;
+	  lastModified = other.lastModified;
+	  zip = other.zip;
+	  entry = other.entry;
+	  parent = other.parent;
+	  url = other.url;
+	  zipPathToRoot = other.zipPathToRoot;
+	  this.cache = cache;
   }
 
   /**
@@ -227,23 +243,32 @@ public class ZipFileImpl implements IFil
   
   ZipFile openZipFile(){
     ZipFile z = null;
-    try {
-      z = new ZipFile(zip);
-    } catch (ZipException e) {
-      logger.error ("ZipException in ZipFileImpl.openZipFile", e);
-    } catch (IOException e) {
-      logger.error ("IOException in ZipFileImpl.openZipFile", e);
+
+    if (cache != null && !!!cache.isClosed()) {
+    	z = cache.getZipFile();
+    } else {
+	    try {
+	      z = new ZipFile(zip);
+	    } catch (ZipException e) {
+	      logger.error ("ZipException in ZipFileImpl.openZipFile", e);
+	    } catch (IOException e) {
+	      logger.error ("IOException in ZipFileImpl.openZipFile", e);
+	    }
     }
     return z;
   }
   
   void closeZipFile(ZipFile z){
-    try{
-      z.close();
-    }
-    catch (IOException e) {
-      logger.error ("IOException in ZipFileImpl.closeZipFile", e);
-    }
+	  if (cache != null && cache.getZipFile() == z) {
+		  // do nothing
+	  } else {
+		  try{
+			  z.close();
+		  }
+		  catch (IOException e) {
+			  logger.error ("IOException in ZipFileImpl.closeZipFile", e);
+		  }
+	  }
   }
   
   /**



Mime
View raw message