From 20ea453c35740694c5926cdb8909819a07183cba Mon Sep 17 00:00:00 2001 From: hns Date: Tue, 7 Dec 2004 14:39:55 +0000 Subject: [PATCH] * Include application name in source names. * Make sure files from zip files are removed from prototypes and prototypes are updated properly when they are no longer present. --- src/helma/framework/core/TypeManager.java | 16 ++++- src/helma/framework/core/ZippedAppFile.java | 72 +++++++++++++++------ src/helma/scripting/ActionFile.java | 5 +- src/helma/scripting/FunctionFile.java | 9 ++- src/helma/scripting/Template.java | 5 +- 5 files changed, 77 insertions(+), 30 deletions(-) diff --git a/src/helma/framework/core/TypeManager.java b/src/helma/framework/core/TypeManager.java index ccc5099c..965ea7ed 100644 --- a/src/helma/framework/core/TypeManager.java +++ b/src/helma/framework/core/TypeManager.java @@ -416,10 +416,11 @@ public final class TypeManager { String filename = file.getName(); int dot = filename.lastIndexOf("."); String tmpname = filename.substring(0, dot); + String srcName = getSourceName(file); if (filename.endsWith(templateExtension)) { try { - Template t = new Template(file, tmpname, proto); + Template t = new Template(file, tmpname, srcName, proto); proto.addTemplate(t); } catch (Throwable x) { @@ -427,7 +428,7 @@ public final class TypeManager { } } else if (filename.endsWith(scriptExtension)) { try { - FunctionFile ff = new FunctionFile(file, proto); + FunctionFile ff = new FunctionFile(file, srcName, proto); proto.addFunctionFile(ff); } catch (Throwable x) { @@ -435,7 +436,7 @@ public final class TypeManager { } } else if (filename.endsWith(actionExtension)) { try { - ActionFile af = new ActionFile(file, tmpname, proto); + ActionFile af = new ActionFile(file, tmpname, srcName, proto); proto.addActionFile(af); } catch (Throwable x) { @@ -474,4 +475,13 @@ public final class TypeManager { } // end of synchronized (proto) } + + private String getSourceName(File file) { + StringBuffer b = new StringBuffer(app.getName()); + b.append(":"); + b.append(file.getParentFile().getName()); + b.append("/"); + b.append(file.getName()); + return b.toString(); + } } diff --git a/src/helma/framework/core/ZippedAppFile.java b/src/helma/framework/core/ZippedAppFile.java index e887943b..b768393f 100644 --- a/src/helma/framework/core/ZippedAppFile.java +++ b/src/helma/framework/core/ZippedAppFile.java @@ -31,7 +31,10 @@ public class ZippedAppFile implements Updatable { Application app; File file; long lastmod; + // Set of updatables provided by this zip file Set updatables; + // Set of prototypes this zip files provides updatables for + Set prototypes; /** * Creates a new ZippedAppFile object. @@ -42,8 +45,8 @@ public class ZippedAppFile implements Updatable { public ZippedAppFile(File file, Application app) { this.app = app; this.file = file; - - // System.err.println ("CREATING ZIP FILE "+this); + updatables = new HashSet(); + prototypes = new HashSet(); } /** @@ -57,18 +60,18 @@ public class ZippedAppFile implements Updatable { /** * */ - public void update() { + public synchronized void update() { if (!file.exists()) { remove(); } else { ZipFile zip = null; + Set newUpdatables = new HashSet(); + Set newPrototypes = new HashSet(); try { lastmod = file.lastModified(); - // System.err.println ("UPDATING ZIP FILE "+this); zip = new ZipFile(file); - updatables = new HashSet(); for (Enumeration en = zip.entries(); en.hasMoreElements();) { ZipEntry entry = (ZipEntry) en.nextElement(); @@ -98,26 +101,26 @@ public class ZippedAppFile implements Updatable { if (fname.endsWith(".hac")) { String name = fname.substring(0, fname.lastIndexOf(".")); - String sourceName = file.getName() + "/" + ename; + String srcName = getSourceName(ename); String content = getZipEntryContent(zip, entry); // System.err.println ("["+content+"]"); - ActionFile act = new ActionFile(content, name, sourceName, + ActionFile act = new ActionFile(content, name, srcName, proto); proto.addActionFile(act); - updatables.add(act); + newUpdatables.add(act); } else if (fname.endsWith(".hsp")) { String name = fname.substring(0, fname.lastIndexOf(".")); - String sourceName = file.getName() + "/" + ename; + String srcName = getSourceName(ename); String content = getZipEntryContent(zip, entry); // System.err.println ("["+content+"]"); - Template tmp = new Template(content, name, sourceName, proto); + Template tmp = new Template(content, name, srcName, proto); proto.addTemplate(tmp); - updatables.add(tmp); + newUpdatables.add(tmp); } else if (fname.endsWith(".skin")) { String name = fname.substring(0, fname.lastIndexOf(".")); @@ -127,17 +130,17 @@ public class ZippedAppFile implements Updatable { SkinFile skin = new SkinFile(content, name, proto); proto.addSkinFile(skin); - updatables.add(skin); + newUpdatables.add(skin); } else if (fname.endsWith(".js")) { - String sourceName = file.getName() + "/" + ename; + String srcName = getSourceName(ename); String content = getZipEntryContent(zip, entry); // System.err.println ("["+content+"]"); - FunctionFile ff = new FunctionFile(content, sourceName, proto); + FunctionFile ff = new FunctionFile(content, srcName, proto); proto.addFunctionFile(ff); - updatables.add(ff); + newUpdatables.add(ff); } else if ("type.properties".equalsIgnoreCase(fname)) { DbMapping dbmap = proto.getDbMapping(); @@ -147,7 +150,7 @@ public class ZippedAppFile implements Updatable { } // mark prototype as updated - proto.markUpdated(); + newPrototypes.add(proto); } } } catch (Throwable x) { @@ -156,6 +159,20 @@ public class ZippedAppFile implements Updatable { x.printStackTrace(); } } finally { + // remove updatables that have gone + updatables.removeAll(newUpdatables); + for (Iterator it = updatables.iterator(); it.hasNext();) { + ((Updatable) it.next()).remove(); + } + updatables = newUpdatables; + + // mark both old and new prototypes as updated + prototypes.addAll(newPrototypes); + for (Iterator it = prototypes.iterator(); it.hasNext();) { + ((Prototype) it.next()).markUpdated(); + } + prototypes = newPrototypes; + try { zip.close(); } catch (Exception ignore) { @@ -168,14 +185,17 @@ public class ZippedAppFile implements Updatable { * */ public void remove() { - if (updatables != null) { - for (Iterator it = updatables.iterator(); it.hasNext();) - ((Updatable) it.next()).remove(); + // remove updatables from prototypes + for (Iterator it = updatables.iterator(); it.hasNext();) { + ((Updatable) it.next()).remove(); + } + // mark affected prototypes as updated + for (Iterator it = prototypes.iterator(); it.hasNext();) { + ((Prototype) it.next()).markUpdated(); } + // remove self from type manager app.typemgr.removeZipFile(file.getName()); - - // System.err.println ("REMOVING ZIP FILE "+this); } /** @@ -207,4 +227,14 @@ public class ZippedAppFile implements Updatable { public String toString() { return file.getName(); } + + + private String getSourceName(String entry) { + StringBuffer b = new StringBuffer(app.getName()); + b.append(":"); + b.append(file.getName()); + b.append("/"); + b.append(entry); + return b.toString(); + } } diff --git a/src/helma/scripting/ActionFile.java b/src/helma/scripting/ActionFile.java index 0204a086..f0cd7929 100644 --- a/src/helma/scripting/ActionFile.java +++ b/src/helma/scripting/ActionFile.java @@ -39,12 +39,13 @@ public class ActionFile implements Updatable { * * @param file ... * @param name ... + * @param sourceName ... * @param proto ... */ - public ActionFile(File file, String name, Prototype proto) { + public ActionFile(File file, String name, String sourceName, Prototype proto) { this.prototype = proto; this.name = name; - this.sourceName = file.getParentFile().getName() + "/" + file.getName(); + this.sourceName = sourceName; this.file = file; this.lastmod = file.lastModified(); this.content = null; diff --git a/src/helma/scripting/FunctionFile.java b/src/helma/scripting/FunctionFile.java index 94738e7f..fe22fef7 100644 --- a/src/helma/scripting/FunctionFile.java +++ b/src/helma/scripting/FunctionFile.java @@ -34,11 +34,12 @@ public class FunctionFile implements Updatable { * Creates a new FunctionFile object. * * @param file ... + * @param sourceName ... * @param proto ... */ - public FunctionFile(File file, Prototype proto) { + public FunctionFile(File file, String sourceName, Prototype proto) { this.prototype = proto; - this.sourceName = file.getParentFile().getName() + "/" + file.getName(); + this.sourceName = sourceName; this.file = file; update(); } @@ -47,6 +48,10 @@ public class FunctionFile implements Updatable { * Create a function file without a file, passing the code directly. This is used for * files contained in zipped applications. The whole update mechanism is bypassed * by immediately parsing the code. + * + * @param body ... + * @param sourceName ... + * @param proto ... */ public FunctionFile(String body, String sourceName, Prototype proto) { this.prototype = proto; diff --git a/src/helma/scripting/Template.java b/src/helma/scripting/Template.java index c786eede..f85acc82 100644 --- a/src/helma/scripting/Template.java +++ b/src/helma/scripting/Template.java @@ -37,10 +37,11 @@ public class Template extends ActionFile { * * @param file ... * @param name ... + * @param sourceName * @param proto ... */ - public Template(File file, String name, Prototype proto) { - super(file, name, proto); + public Template(File file, String name, String sourceName, Prototype proto) { + super(file, name, sourceName, proto); } /**