Author: scabanovich
Date: 2007-08-06 11:54:41 -0400 (Mon, 06 Aug 2007)
New Revision: 2919
Modified:
trunk/common/plugins/org.jboss.tools.common.model/src/org/jboss/tools/common/model/filesystems/impl/JarAccess.java
trunk/common/plugins/org.jboss.tools.common.model/src/org/jboss/tools/common/model/filesystems/impl/JarFolderImpl.java
Log:
JBIDE-666
Modified:
trunk/common/plugins/org.jboss.tools.common.model/src/org/jboss/tools/common/model/filesystems/impl/JarAccess.java
===================================================================
---
trunk/common/plugins/org.jboss.tools.common.model/src/org/jboss/tools/common/model/filesystems/impl/JarAccess.java 2007-08-06
15:37:16 UTC (rev 2918)
+++
trunk/common/plugins/org.jboss.tools.common.model/src/org/jboss/tools/common/model/filesystems/impl/JarAccess.java 2007-08-06
15:54:41 UTC (rev 2919)
@@ -13,259 +13,319 @@
import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileOutputStream;
+import java.io.IOException;
import java.io.InputStream;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.HashSet;
+import java.util.Map;
+import java.util.Set;
import java.util.zip.ZipEntry;
import java.util.zip.ZipFile;
+import org.jboss.tools.common.model.plugin.ModelPlugin;
import org.jboss.tools.common.util.FileUtil;
public class JarAccess {
- private String location = null;
- private String templocation = null;
- private ZipFile jar = null;
- private HashMap<String,HashSet<String>> map = new
HashMap<String,HashSet<String>>();
- private long timeStamp = -1;
- private long size = -1;
+ private String location = null;
+ private String templocation = null;
- public JarAccess() {}
+ private ZipFile jar = null;
+ int jarLock = 0;
- public void setLocation(String location) {
- this.location = location;
- validate();
- }
-
- public String getLocation() {
- return location;
- }
+ private Map<String,HashSet<String>> map = new
HashMap<String,HashSet<String>>();
+ private Map<String,Long> fileEntries = new HashMap<String,Long>();
- public boolean isLoaded() {
- return (jar != null || loading);
- }
+ private boolean loading = false;
+ private boolean exists = false;
+ private long timeStamp = -1;
+ private long size = -1;
+
+ public JarAccess() {}
+
+ public void setLocation(String location) {
+ this.location = location;
+ validate();
+ }
- private boolean loading = false;
+ public void lockJar() {
+ jarLock++;
+ }
- public void validate() {
- if (isLoaded()) return;
+ public String getLocation() {
+ return location;
+ }
+
+ public boolean isLoaded() {
+ return (exists || loading);
+ }
+
+ public void validate() {
+ if (isLoaded()) return;
loading = true;
templocation = null;
- try {
- File f = File.createTempFile("efs_", ".jar");
- f.deleteOnExit();
- int ind = location.indexOf(":/");
- if (ind != 1 && ind != -1) {
- InputStream i = new
java.net.URL(location).openConnection().getInputStream();
- FileOutputStream o = new FileOutputStream(f);
- FileUtil.copy(i, o);
- timeStamp = -1;
- size = -1;
- } else {
- File nf = new File(location);
- FileUtil.copyFile(nf, f, true);
- timeStamp = nf.lastModified();
- size = nf.length();
- }
- templocation = f.getCanonicalPath();
- jar = new ZipFile(f);
- init();
- } catch (Exception e) {
- timeStamp = -1;
- size = -1;
- return;
- } finally {
+ try {
+ int ind = location.indexOf(":/");
+ if (ind != 1 && ind != -1) {
+ File f = File.createTempFile("efs_", ".jar");
+ f.deleteOnExit();
+ InputStream i = new java.net.URL(location).openConnection().getInputStream();
+ FileOutputStream o = new FileOutputStream(f);
+ FileUtil.copy(i, o);
+ timeStamp = -1;
+ size = -1;
+ templocation = f.getCanonicalPath();
+ } else {
+ File nf = new File(location);
+// FileUtil.copyFile(nf, f, true);
+ templocation = nf.getCanonicalPath();
+ timeStamp = nf.lastModified();
+ size = nf.length();
+ }
+ init();
+ exists = true;
+ } catch (Exception e) {
+ timeStamp = -1;
+ size = -1;
+ exists = false;
+ return;
+ } finally {
loading = false;
- }
- }
+ }
+ }
- private void init() {
- map.clear();
- map.put("", new HashSet<String>());
- Enumeration en = jar.entries();
- while(en.hasMoreElements()) {
- try {
- register(((ZipEntry)en.nextElement()).getName());
- } catch (Exception e) {
- //ignore
- }
- }
- }
+ private void init() throws Exception {
+ ZipFile jar = getZipFile();
+ map.clear();
+ fileEntries.clear();
+ map.put("", new HashSet<String>());
+ try {
+ if(jar == null) return;
+ Enumeration<?> en = jar.entries();
+ while(en.hasMoreElements()) {
+ try {
+ ZipEntry entry = (ZipEntry)en.nextElement();
+ String name = entry.getName();
+ if(name != null && !name.endsWith("/") && entry.getSize()
> 0) {
+ fileEntries.put(name, new Long(entry.getSize()));
+ }
+ register(name);
+ } catch (Exception e) {
+ ModelPlugin.getPluginLog().logError(e);
+ }
+ }
+ } finally {
+ unlockJar();
+ }
+ }
- private void register(String path) {
- String[] parsed = parse(path);
- check(parsed[0]);
- HashSet<String> set = map.get(parsed[0]);
- if(!"/".equals(parsed[1])) set.add(parsed[1]);
- }
+ private ZipFile getZipFile() throws IOException {
+ synchronized (this) {
+ lockJar();
+ if(jar != null) return jar;
+ return jar = new ZipFile(templocation);
+ }
+ }
- private String[] parse(String path) {
- String q = path;
- if(path.endsWith("/")) q = q.substring(0, path.length() - 1);
- int i = q.lastIndexOf('/');
- String root = (i < 0) ? "" : path.substring(0, i);
- String name = (i < 0) ? path : path.substring(i + 1);
- return new String[]{root, name};
- }
+ public void unlockJar() {
+ jarLock--;
+ if(jarLock > 0 || jar == null) return;
+ synchronized (this) {
+ if(jar != null && jarLock == 0) {
+ try {
+ jar.close();
+ } catch (IOException e) {
+ //ignore
+ } finally {
+ jar = null;
+ }
+ }
+ }
+ }
- private void check(String path) {
- if(map.get(path) != null) return;
- map.put(path, new HashSet<String>());
- String[] parsed = parse(path);
- check(parsed[0]);
- if("".equals(parsed[1])) return;
- HashSet<String> set = map.get(parsed[0]);
- set.add(parsed[1] + "/");
- }
+ private void register(String path) {
+ String[] parsed = parse(path);
+ check(parsed[0]);
+ HashSet<String> set = map.get(parsed[0]);
+ if (!"/".equals(parsed[1]))
+ set.add(parsed[1]);
+ }
- public String[] getChildren(String path) {
- HashSet<String> set = map.get(path);
- return (set == null) ? new String[0] : set.toArray(new String[0]);
- }
+ private String[] parse(String path) {
+ String q = path;
+ if (path.endsWith("/"))
+ q = q.substring(0, path.length() - 1);
+ int i = q.lastIndexOf('/');
+ String root = (i < 0) ? "" : path.substring(0, i);
+ String name = (i < 0) ? path : path.substring(i + 1);
+ return new String[] { root, name };
+ }
- public long getSize(String path) {
- try {
- return jar.getEntry(path).getSize();
- } catch (Exception e) {
- return 0;
- }
- }
+ private void check(String path) {
+ if (map.get(path) != null)
+ return;
+ map.put(path, new HashSet<String>());
+ String[] parsed = parse(path);
+ check(parsed[0]);
+ if ("".equals(parsed[1]))
+ return;
+ HashSet<String> set = map.get(parsed[0]);
+ set.add(parsed[1] + "/");
+ }
- public String getContent(String path) {
- int size = 1024;
- byte b[] = new byte[size];
- StringBuffer sb = new StringBuffer();
- int length = 0;
- try {
- InputStream is = jar.getInputStream(jar.getEntry(path));
- BufferedInputStream bs = new BufferedInputStream(is);
- while((length = bs.available()) > 0) {
- if(length > size) length = size;
- length = bs.read(b, 0, length);
- if(length < 0) break;
- sb.append(new String(b, 0, length));
- }
- return sb.toString();
- } catch (Exception e) {
- return "";
- }
- }
+ public String[] getChildren(String path) {
+ HashSet<String> set = map.get(path);
+ return (set == null) ? new String[0] : set.toArray(new String[0]);
+ }
- public boolean isTextEntry(String path, int length) {
- String b = getContent(path);
- b = (b == null || b.length() < length) ? b : b.substring(length);
- return FileUtil.isText(b);
- }
+ public long getSize(String path) {
+ Long s = fileEntries.get(path);
+ return s == null ? 0 : s.longValue();
+ }
- boolean hasFolder(String path) {
- return map.get(path) != null;
- }
+ public String getContent(String path) {
+ int size = 1024;
+ byte b[] = new byte[size];
+ StringBuffer sb = new StringBuffer();
+ ZipFile jar = null;
+ try {
+ jar = getZipFile();
+ } catch (Exception e) {
+ unlockJar();
+ return "";
+ }
+ int length = 0;
+ try {
+ InputStream is = jar.getInputStream(jar.getEntry(path));
+ BufferedInputStream bs = new BufferedInputStream(is);
+ while ((length = bs.available()) > 0) {
+ if (length > size)
+ length = size;
+ length = bs.read(b, 0, length);
+ if (length < 0)
+ break;
+ sb.append(new String(b, 0, length));
+ }
+ return sb.toString();
+ } catch (Exception e) {
+ ModelPlugin.getPluginLog().logError(e);
+ return "";
+ } finally {
+ unlockJar();
+ }
+ }
- boolean hasFile(String path) {
- if(path == null) return false;
- int i = path.lastIndexOf('/');
- String p = (i < 0) ? "" : path.substring(0, i);
- String n = path.substring(i + 1);
- HashSet set = (HashSet)map.get(p);
- return set != null && set.contains(n);
- }
+ public boolean isTextEntry(String path, int length) {
+ String b = getContent(path);
+ b = (b == null || b.length() < length) ? b : b.substring(length);
+ return FileUtil.isText(b);
+ }
- public LFileObject getFileObject(String alias, String relpath) {
- return new LFileObjectJarImpl(this, alias, relpath);
- }
+ boolean hasFolder(String path) {
+ return map.get(path) != null;
+ }
- public boolean isModified() {
- if(timeStamp == -1) return true;
- try {
- File f = new File(location);
- return (timeStamp != f.lastModified() || size != f.length());
- } catch (Exception e) {
- return true;
- }
- }
+ boolean hasFile(String path) {
+ if (path == null)
+ return false;
+ int i = path.lastIndexOf('/');
+ String p = (i < 0) ? "" : path.substring(0, i);
+ String n = path.substring(i + 1);
+ Set<String> set = map.get(p);
+ return set != null && set.contains(n);
+ }
- public void invalidate() {
- if(jar != null) {
- try {
- jar.close();
- } catch (Exception e) {
- //ignore
- }
- }
- jar = null;
- map.clear();
- timeStamp = -1;
- size = -1;
- }
+ public LFileObject getFileObject(String alias, String relpath) {
+ return new LFileObjectJarImpl(this, alias, relpath);
+ }
- public String getTempLocation() {
- return templocation;
- }
+ public boolean isModified() {
+ if (timeStamp == -1)
+ return true;
+ try {
+ File f = new File(location);
+ return (timeStamp != f.lastModified() || size != f.length());
+ } catch (Exception e) {
+ return true;
+ }
+ }
+ public void invalidate() {
+ exists = false;
+ map.clear();
+ timeStamp = -1;
+ size = -1;
+ }
+
+ public String getTempLocation() {
+ return templocation;
+ }
+
}
class LFileObjectJarImpl implements LFileObject {
- private JarAccess access = null;
- private String aliaspath = null;
- private String relpath = null;
+ private JarAccess access = null;
+ private String aliaspath = null;
+ private String relpath = null;
- public LFileObjectJarImpl(JarAccess access, String alias, String relpath) {
- this.access = access;
- aliaspath = relpath.length() == 0 ? alias : alias + '/' + relpath;
- this.relpath = relpath;
- }
+ public LFileObjectJarImpl(JarAccess access, String alias, String relpath) {
+ this.access = access;
+ aliaspath = relpath.length() == 0 ? alias : alias + '/' + relpath;
+ this.relpath = relpath;
+ }
- public String getName() {
- return relpath.substring(relpath.lastIndexOf('/') + 1);
- }
+ public String getName() {
+ return relpath.substring(relpath.lastIndexOf('/') + 1);
+ }
- public boolean exists() {
- return access.hasFolder(relpath) || access.hasFile(relpath);
- }
+ public boolean exists() {
+ return access.hasFolder(relpath) || access.hasFile(relpath);
+ }
- public boolean isDirectory() {
- return access.hasFolder(relpath);
- }
+ public boolean isDirectory() {
+ return access.hasFolder(relpath);
+ }
- public boolean isFile() {
- return access.hasFile(relpath);
- }
+ public boolean isFile() {
+ return access.hasFile(relpath);
+ }
- public long lastModified() {
- return 0;
- }
+ public long lastModified() {
+ return 0;
+ }
- public String getPath() {
- return aliaspath;
- }
+ public String getPath() {
+ return aliaspath;
+ }
- public boolean canWrite() {
- return false;
- }
+ public boolean canWrite() {
+ return false;
+ }
- public String read() {
- return access.getContent(relpath);
- }
+ public String read() {
+ return access.getContent(relpath);
+ }
- public void write(String s) {}
+ public void write(String s) {
+ }
- public String[] listFiles() {
- String[] r = access.getChildren(relpath);
- String rp = getPath();
- for (int i = 0; i < r.length; i++) {
- if(r[i].endsWith("/")) r[i] = r[i].substring(0, r[i].length() -
1);
- r[i] = rp + "/" + r[i];
- }
- return r;
- }
+ public String[] listFiles() {
+ String[] r = access.getChildren(relpath);
+ String rp = getPath();
+ for (int i = 0; i < r.length; i++) {
+ if (r[i].endsWith("/"))
+ r[i] = r[i].substring(0, r[i].length() - 1);
+ r[i] = rp + "/" + r[i];
+ }
+ return r;
+ }
- public boolean mkdirs() {
- return false;
- }
+ public boolean mkdirs() {
+ return false;
+ }
- public boolean delete() {
- return false;
- }
+ public boolean delete() {
+ return false;
+ }
}
-
Modified:
trunk/common/plugins/org.jboss.tools.common.model/src/org/jboss/tools/common/model/filesystems/impl/JarFolderImpl.java
===================================================================
---
trunk/common/plugins/org.jboss.tools.common.model/src/org/jboss/tools/common/model/filesystems/impl/JarFolderImpl.java 2007-08-06
15:37:16 UTC (rev 2918)
+++
trunk/common/plugins/org.jboss.tools.common.model/src/org/jboss/tools/common/model/filesystems/impl/JarFolderImpl.java 2007-08-06
15:54:41 UTC (rev 2919)
@@ -57,6 +57,7 @@
if(loaded || !isActive()) return;
JarAccess jar = getJarSystem().getJarAccess();
if(!jar.isLoaded()) return;
+ jar.lockJar();
loaded = true;
String path = getAbsolutePath();
String[] cs = jar.getChildren(path);
@@ -74,6 +75,7 @@
}
}
fire = true;
+ jar.unlockJar();
}
private void createFileObject(JarAccess jar, String path, String name) {