From d47c1f16436d6ab73080e7bb4748918898c2b9c3 Mon Sep 17 00:00:00 2001 From: hns Date: Wed, 12 Apr 2006 14:55:04 +0000 Subject: [PATCH] * Allow non-script resources to be wrapped in SingleFileRepositories and use fake Global subrepository only for script resources. * Implement equals(), hashCode() and toString() in SingleFileRepository --- src/helma/framework/core/ApplicationBean.java | 6 +- .../repository/SingleFileRepository.java | 73 +++++++++++++------ 2 files changed, 51 insertions(+), 28 deletions(-) diff --git a/src/helma/framework/core/ApplicationBean.java b/src/helma/framework/core/ApplicationBean.java index a34f843b..e4e545c2 100644 --- a/src/helma/framework/core/ApplicationBean.java +++ b/src/helma/framework/core/ApplicationBean.java @@ -153,12 +153,10 @@ public class ApplicationBean implements Serializable { if (file.isDirectory()) { rep = new FileRepository(file); } else if (file.isFile()) { - if (file.getName().endsWith(".js")) { - rep = new SingleFileRepository(file); - } else if (file.getName().endsWith(".zip")) { + if (file.getName().endsWith(".zip")) { rep = new ZipRepository(file); } else { - throw new RuntimeException("Unrecognized file type in addRepository: " + obj); + rep = new SingleFileRepository(file); } } else { throw new RuntimeException("Unrecognized file type in addRepository: " + obj); diff --git a/src/helma/framework/repository/SingleFileRepository.java b/src/helma/framework/repository/SingleFileRepository.java index b0f479da..6c79fba9 100644 --- a/src/helma/framework/repository/SingleFileRepository.java +++ b/src/helma/framework/repository/SingleFileRepository.java @@ -25,8 +25,10 @@ import java.util.LinkedList; public class SingleFileRepository implements Repository { final Resource res; - final Repository global; final Repository[] repositories; + final LinkedList resources = new LinkedList(); + final LinkedList allResources = new LinkedList(); + final boolean isScriptFile; /** * Constructs a SingleFileRepository using the given argument @@ -42,8 +44,14 @@ public class SingleFileRepository implements Repository { */ public SingleFileRepository(File file) { res = new FileResource(file, this); - global = new FakeGlobal(); - repositories = new Repository[] { global }; + allResources.add(res); + isScriptFile = file.getName().endsWith(".js"); + if (isScriptFile) { + repositories = new Repository[] { new FakeGlobal() }; + } else { + repositories = AbstractRepository.emptyRepositories; + resources.add(res); + } } /** @@ -144,7 +152,7 @@ public class SingleFileRepository implements Repository { * @throws java.io.IOException */ public List getAllResources() throws IOException { - return global.getAllResources(); + return resources; } /** @@ -154,17 +162,7 @@ public class SingleFileRepository implements Repository { * @throws java.io.IOException */ public Iterator getResources() throws IOException { - return new Iterator() { - public boolean hasNext() { - return false; - } - - public void remove() {} - - public Object next() { - return null; - } - }; + return resources.iterator(); } /** @@ -174,6 +172,9 @@ public class SingleFileRepository implements Repository { * @return specified child resource */ public Resource getResource(String resourceName) { + if (!isScriptFile && res.getName().equals(resourceName)) { + return res; + } return null; } @@ -187,15 +188,39 @@ public class SingleFileRepository implements Repository { return res.lastModified(); } + /** + * Return our single resource. + * @return the wrapped resource + */ + protected Resource getResource() { + return res; + } + + /** + * Indicates whether some other object is "equal to" this one. + */ + public boolean equals(Object obj) { + return (obj instanceof SingleFileRepository && + res.equals(((SingleFileRepository) obj).res)); + } + + /** + * Returns a hash code value for the object. + */ + public int hashCode() { + return res.hashCode(); + } + + /** + * Returns a string representation of the object. + */ + public String toString() { + return new StringBuffer("SingleFileRepository[") + .append(res.getName()).append("]").toString(); + } + class FakeGlobal implements Repository { - final List resources; - - FakeGlobal() { - resources = new LinkedList(); - resources.add(res); - } - /** * Checksum of the repository and all its content. Implementations * should make sure @@ -295,7 +320,7 @@ public class SingleFileRepository implements Repository { * @throws java.io.IOException */ public List getAllResources() throws IOException { - return resources; + return allResources; } /** @@ -305,7 +330,7 @@ public class SingleFileRepository implements Repository { * @throws java.io.IOException */ public Iterator getResources() throws IOException { - return resources.iterator(); + return allResources.iterator(); } /**