From e431e18d454a1b9fa1bf81e6fc680d3763b8cb41 Mon Sep 17 00:00:00 2001 From: hns Date: Fri, 7 Apr 2006 14:39:08 +0000 Subject: [PATCH] * Add SimpleFileRepository for adding single global JavaScript files as repositories. --- .../repository/SingleFileRepository.java | 337 ++++++++++++++++++ 1 file changed, 337 insertions(+) create mode 100644 src/helma/framework/repository/SingleFileRepository.java diff --git a/src/helma/framework/repository/SingleFileRepository.java b/src/helma/framework/repository/SingleFileRepository.java new file mode 100644 index 00000000..b0f479da --- /dev/null +++ b/src/helma/framework/repository/SingleFileRepository.java @@ -0,0 +1,337 @@ +/* + * Helma License Notice + * + * The contents of this file are subject to the Helma License + * Version 2.0 (the "License"). You may not use this file except in + * compliance with the License. A copy of the License is available at + * http://adele.helma.org/download/helma/license.txt + * + * Copyright 1998-2006 Helma Software. All Rights Reserved. + * + * $RCSfile$ + * $Author$ + * $Revision$ + * $Date$ + */ + +package helma.framework.repository; + +import java.io.IOException; +import java.io.File; +import java.util.List; +import java.util.Iterator; +import java.util.LinkedList; + +public class SingleFileRepository implements Repository { + + final Resource res; + final Repository global; + final Repository[] repositories; + + /** + * Constructs a SingleFileRepository using the given argument + * @param initArgs absolute path to the script file + */ + public SingleFileRepository(String initArgs) { + this(new File(initArgs)); + } + + /** + * Constructs a SingleFileRepository using the given argument + * @param file the script file + */ + public SingleFileRepository(File file) { + res = new FileResource(file, this); + global = new FakeGlobal(); + repositories = new Repository[] { global }; + } + + /** + * Checksum of the repository and all its content. Implementations + * should make sure + * + * @return checksum + * @throws java.io.IOException + */ + public long getChecksum() throws IOException { + return res.lastModified(); + } + + /** + * Returns the name of the repository. + * + * @return name of the repository + */ + public String getShortName() { + return res.getShortName(); + } + + /** + * Returns the name of the repository; this is a full name including all + * parent repositories. + * + * @return full name of the repository + */ + public String getName() { + return res.getName(); + } + + /** + * Get this repository's logical script root repository. + * + * @return top-level repository + * @see {isScriptRoot()} + */ + public Repository getRootRepository() { + return this; + } + + /** + * Returns this repository's parent repository. + * Returns null if this repository already is the top-level repository + * + * @return the parent repository + */ + public Repository getParentRepository() { + return null; + } + + /** + * Checks wether the repository is to be considered a top-level + * repository from a scripting point of view. For example, a zip + * file within a file repository is not a root repository from + * a physical point of view, but from the scripting point of view it is. + * + * @return true if the repository is to be considered a top-level script repository + */ + public boolean isScriptRoot() { + return false; + } + + /** + * Creates the repository if does not exist yet + * + * @throws java.io.IOException + */ + public void create() throws IOException { + // noop + } + + /** + * Checks wether the repository actually (or still) exists + * + * @return true if the repository exists + * @throws java.io.IOException + */ + public boolean exists() throws IOException { + return res.exists(); + } + + /** + * Returns this repository's direct child repositories + * + * @return direct repositories + * @throws java.io.IOException + */ + public Repository[] getRepositories() throws IOException { + return repositories; + } + + /** + * Returns all direct and indirect resources + * + * @return resources recursive + * @throws java.io.IOException + */ + public List getAllResources() throws IOException { + return global.getAllResources(); + } + + /** + * Returns all direct resources + * + * @return direct resources + * @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; + } + }; + } + + /** + * Returns a specific direct resource of the repository + * + * @param resourceName name of the child resource to return + * @return specified child resource + */ + public Resource getResource(String resourceName) { + return null; + } + + /** + * Returns the date the repository was last modified. + * + * @return last modified date + * @throws java.io.IOException + */ + public long lastModified() throws IOException { + return res.lastModified(); + } + + 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 + * + * @return checksum + * @throws java.io.IOException + */ + public long getChecksum() throws IOException { + return res.lastModified(); + } + + /** + * Returns the name of the repository. + * + * @return name of the repository + */ + public String getShortName() { + // we need to return "Global" here in order to be recognized as + // global code folder - that's the whole purpose of this class + return "Global"; + } + + /** + * Returns the name of the repository; this is a full name including all + * parent repositories. + * + * @return full name of the repository + */ + public String getName() { + return res.getName(); + } + + /** + * Get this repository's logical script root repository. + * + * @return top-level repository + * @see {isScriptRoot()} + */ + public Repository getRootRepository() { + return SingleFileRepository.this; + } + + /** + * Returns this repository's parent repository. + * Returns null if this repository already is the top-level repository + * + * @return the parent repository + */ + public Repository getParentRepository() { + return SingleFileRepository.this; + } + + /** + * Checks wether the repository is to be considered a top-level + * repository from a scripting point of view. For example, a zip + * file within a file repository is not a root repository from + * a physical point of view, but from the scripting point of view it is. + * + * @return true if the repository is to be considered a top-level script repository + */ + public boolean isScriptRoot() { + return false; + } + + /** + * Creates the repository if does not exist yet + * + * @throws java.io.IOException + */ + public void create() throws IOException { + } + + /** + * Checks wether the repository actually (or still) exists + * + * @return true if the repository exists + * @throws java.io.IOException + */ + public boolean exists() throws IOException { + return res.exists(); + } + + /** + * Returns this repository's direct child repositories + * + * @return direct repositories + * @throws java.io.IOException + */ + public Repository[] getRepositories() throws IOException { + return AbstractRepository.emptyRepositories; + } + + /** + * Returns all direct and indirect resources + * + * @return resources recursive + * @throws java.io.IOException + */ + public List getAllResources() throws IOException { + return resources; + } + + /** + * Returns all direct resources + * + * @return direct resources + * @throws java.io.IOException + */ + public Iterator getResources() throws IOException { + return resources.iterator(); + } + + /** + * Returns a specific direct resource of the repository + * + * @param resourceName name of the child resource to return + * @return specified child resource + */ + public Resource getResource(String resourceName) { + if (res.getName().equals(resourceName)) { + return res; + } + return null; + } + + /** + * Returns the date the repository was last modified. + * + * @return last modified date + * @throws java.io.IOException + */ + public long lastModified() throws IOException { + return res.lastModified(); + } + } + + +} +