Changeset 9141

Show
Ignore:
Timestamp:
07/02/08 15:09:24 (5 months ago)
Author:
hannes
Message:

Clean up Repository/Resource framework:

  • Fix naming: "name" becomes "path", and "shortname" becomes "name".
  • Remove methods from Reposiotory we ain't gonna use.
  • Factor out common Resource methods to AbstractResource
Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • helma-ng/trunk/src/org/helma/javascript/ModuleScope.java

    r9107 r9141  
    3131    Resource resource; 
    3232    Repository repository; 
     33    String name; 
    3334    long checksum; 
    3435    private static final long serialVersionUID = -2409425841990094897L; 
     
    3839        this.resource = resource; 
    3940        this.repository = repository; 
     41        this.name = moduleName; 
    4042        setParentScope(null); 
    4143        setPrototype(prototype); 
    4244        defineProperty("__name__", moduleName, DONTENUM); 
    43         defineProperty("__path__", repository.getName(), DONTENUM); 
     45        defineProperty("__path__", repository.getPath(), DONTENUM); 
    4446    } 
    4547 
     
    5456    public void setChecksum(long checksum) { 
    5557        this.checksum = checksum; 
     58    } 
     59 
     60    public String getName() { 
     61        return name; 
    5662    } 
    5763 
  • helma-ng/trunk/src/org/helma/javascript/ReloadableScript.java

    r9107 r9141  
    8888            try { 
    8989                exception = null; 
    90                 script = cx.compileReader(resource.getReader(), resource.getName(), 1, null); 
     90                script = cx.compileReader(resource.getReader(), resource.getPath(), 1, null); 
    9191            } catch (Exception x) { 
    9292                exception = x; 
     
    124124            for (Resource res: resources) { 
    125125                if (res.getName().endsWith(".js")) { 
    126                     scripts.add(cx.compileReader(res.getReader(), res.getName(), 1, null)); 
     126                    scripts.add(cx.compileReader(res.getReader(), res.getPath(), 1, null)); 
    127127                } 
    128128           } 
  • helma-ng/trunk/src/org/helma/repository/AbstractRepository.java

    r8986 r9141  
    2020 
    2121import java.io.File; 
    22 import java.io.IOException; 
    2322import java.util.*; 
    2423 
     
    4140 
    4241    /** 
    43      * Holds mounted repositories 
    44      */ 
    45     Map<String,Repository> mounted = new HashMap<String,Repository>(); 
    46  
    47     /** 
    4842     * Holds direct resources 
    4943     */ 
     
    5347     * Cached name for faster access 
    5448     */ 
    55     String name
     49    String path
    5650 
    5751    /** 
    5852     * Cached short name for faster access 
    5953     */ 
    60     String shortName; 
     54    String name; 
    6155 
    6256    /* 
     
    8579     * Get the full name that identifies this repository globally 
    8680     */ 
    87     public String getName() { 
    88         return name
     81    public String getPath() { 
     82        return path
    8983    } 
    9084 
     
    9387     * parent repository 
    9488     */ 
    95     public String getShortName() { 
    96         return shortName; 
    97     } 
    98  
    99     /** 
    100      * Get this repository's logical script root repository. 
    101      * 
    102      *@see {isScriptRoot()} 
    103      */ 
    104     public Repository getRootRepository() { 
    105         if (parent == null || isScriptRoot()) { 
    106             return this; 
    107         } else { 
    108             return parent.getRootRepository(); 
    109         } 
    110     } 
    111  
    112     /** 
    113      * Mount a child repository under the given path name. 
    114      * 
    115      * @param pathname the path name 
    116      * @param child    the child element 
    117      */ 
    118     public void mountRepository(Repository child, String pathname) { 
    119         if (!child.equals(mounted.get(pathname))) 
    120             mounted.put(pathname, child); 
     89    public String getName() { 
     90        return name; 
    12191    } 
    12292 
     
    12999        String[] subs = StringUtils.split(path, separator); 
    130100        if (subs.length == 1) { 
    131             Resource res = resources.get(subs[0]); 
     101            Resource resource = resources.get(subs[0]); 
     102            if (resource != null) { 
     103                return resource; 
     104            } 
    132105            // if resource does not exist, create it 
    133             if (res == null) { 
    134                 res = createResource(subs[0]); 
    135                 resources.put(subs[0], res); 
    136             } 
    137             return res; 
     106            return createResource(subs[0]); 
    138107        } 
    139108        Repository repository = this; 
     
    160129            System.err.println("Warning: resource path doesn not exist: " + path); 
    161130        } 
    162         try { 
    163             return repository.getAllResources(); 
    164         } catch (IOException iox) { 
    165             return Collections.EMPTY_LIST; 
    166         } 
     131        return repository.getAllResources(); 
    167132    } 
    168133 
     
    194159     * contained in sub-reposotories. 
    195160     */ 
    196     public synchronized List<Resource> getAllResources() throws IOException
     161    public synchronized List<Resource> getAllResources()
    197162        update(); 
    198163        ArrayList<Resource> allResources = new ArrayList<Resource>(); 
     
    211176     */ 
    212177    public String toString() { 
    213         return getName(); 
     178        return getPath(); 
    214179    } 
    215180 
  • helma-ng/trunk/src/org/helma/repository/FileRepository.java

    r8960 r9141  
    7272 
    7373        if (parent == null) { 
    74             name = shortName = directory.getAbsolutePath(); 
     74            path = name = directory.getAbsolutePath(); 
    7575        } else { 
    7676            this.parent = parent; 
    77             shortName = directory.getName(); 
    78             name = directory.getAbsolutePath(); 
    79         } 
    80     } 
    81  
     77            name = directory.getName(); 
     78            path = directory.getAbsolutePath(); 
     79        } 
     80    } 
     81 
     82    /** 
     83     * Check whether the repository exists. 
     84     * @return true if the repository exists. 
     85     */ 
    8286    public boolean exists() { 
    8387        return directory.exists() && directory.isDirectory(); 
    84     } 
    85  
    86     public void create() { 
    87         if (!directory.exists() || !directory.isDirectory()) { 
    88             directory.mkdirs(); 
    89         } 
    90     } 
    91  
    92     /** 
    93      * Checks wether the repository is to be considered a top-level 
    94      * repository from a scripting point of view. For example, a zip 
    95      * file within a file repository is not a root repository from 
    96      * a physical point of view, but from the scripting point of view it is. 
    97      * 
    98      * @return true if the repository is to be considered a top-level script repository 
    99      */ 
    100     public boolean isScriptRoot() { 
    101         return parent == null; 
    10288    } 
    10389 
     
    10995     */ 
    11096    public Repository getChildRepository(String name) { 
    111         if (mounted.containsKey(name)) { 
    112             return mounted.get(name); 
    113         } 
    11497        return new FileRepository(new File(directory, name), this); 
    11598    } 
     
    185168                    // a file resource 
    186169                    FileResource resource = new FileResource(file, this); 
    187                     newResources.put(resource.getShortName(), resource); 
     170                    newResources.put(resource.getName(), resource); 
    188171                } 
    189172            } 
     
    218201 
    219202    public String toString() { 
    220         return new StringBuffer("FileRepository[").append(name).append("]").toString(); 
     203        return new StringBuffer("FileRepository[").append(path).append("]").toString(); 
    221204    } 
    222205} 
  • helma-ng/trunk/src/org/helma/repository/FileResource.java

    r8994 r9141  
    2525    File file; 
    2626    Repository repository; 
     27    String path; 
    2728    String name; 
    28     String shortName; 
    2929    String baseName; 
    3030 
     
    4444        this.repository = repository == null ? 
    4545                new FileRepository(file.getParentFile()) : repository; 
    46         name = file.getPath(); 
    47         shortName = file.getName(); 
     46        path = file.getPath(); 
     47        name = file.getName(); 
    4848        // base name is short name with extension cut off 
    49         int lastDot = shortName.lastIndexOf("."); 
    50         baseName = (lastDot == -1) ? shortName : shortName.substring(0, lastDot); 
     49        int lastDot = name.lastIndexOf("."); 
     50        baseName = (lastDot == -1) ? name : name.substring(0, lastDot); 
     51    } 
     52 
     53    public String getPath() { 
     54        return path; 
    5155    } 
    5256 
    5357    public String getName() { 
    5458        return name; 
    55     } 
    56  
    57     public String getShortName() { 
    58         return shortName; 
    5959    } 
    6060 
     
    122122 
    123123    public int hashCode() { 
    124         return 17 + name.hashCode(); 
     124        return 17 + path.hashCode(); 
    125125    } 
    126126 
    127127    public boolean equals(Object obj) { 
    128         return obj instanceof FileResource && name.equals(((FileResource)obj).name); 
     128        return obj instanceof FileResource && path.equals(((FileResource)obj).path); 
    129129    } 
    130130 
    131131    public String toString() { 
    132         return getName(); 
     132        return getPath(); 
    133133    } 
    134134} 
  • helma-ng/trunk/src/org/helma/repository/Repository.java

    r8960 r9141  
    7777     * @throws IOException an I/O error occurred 
    7878     */ 
    79     public List<Resource> getAllResources() throws IOException
     79    public List<Resource> getAllResources()
    8080 
    8181    /** 
     
    9595 
    9696    /** 
    97      * Creates the repository if does not exist yet 
    98      * 
    99      * @throws IOException  an I/O error occurred 
    100      */ 
    101     public void create() throws IOException; 
    102  
    103     /** 
    104      * Checks wether the repository is to be considered a top-level 
    105      * repository from a scripting point of view. For example, a zip 
    106      * file within a file repository is not a root repository from 
    107      * a physical point of view, but from the scripting point of view it is. 
    108      * 
    109      * @return true if the repository is to be considered a top-level script repository 
    110      */ 
    111     public boolean isScriptRoot(); 
    112  
    113     /** 
    11497     * Returns this repository's parent repository. 
    11598     * Returns null if this repository already is the top-level repository 
     
    127110 
    128111    /** 
    129      * Get this repository's logical script root repository. 
    130      * 
    131      * @see {isScriptRoot()} 
    132      * @return top-level repository 
    133      */ 
    134     public Repository getRootRepository(); 
    135  
    136     /** 
    137      * Mount a child repository under the given path name. 
    138      * 
    139      * @param pathname the path name 
    140      * @param child the child element 
    141      */ 
    142     public void mountRepository(Repository child, String pathname); 
    143  
    144     /** 
    145112     * Returns the name of the repository; this is a full name including all 
    146113     * parent repositories. 
     
    148115     * @return full name of the repository 
    149116     */ 
    150     public String getName(); 
     117    public String getPath(); 
    151118 
    152119    /** 
     
    155122     * @return name of the repository 
    156123     */ 
    157     public String getShortName(); 
     124    public String getName(); 
    158125 
    159126} 
  • helma-ng/trunk/src/org/helma/repository/Resource.java

    r8835 r9141  
    1717package org.helma.repository; 
    1818 
     19import java.io.IOException; 
    1920import java.io.InputStream; 
    20 import java.io.IOException; 
    2121import java.io.Reader; 
     22import java.net.MalformedURLException; 
    2223import java.net.URL; 
    2324 
     
    3233     * @return last modified date 
    3334     */ 
    34     public long lastModified()
     35    public long lastModified() throws IOException
    3536 
    3637    /** 
     
    7475 
    7576    /** 
    76      * Returns the name of the resource; does not include the name of the 
    77      * repository the resource was fetched from 
    78      * @return name of the resource 
     77     * Returns the path of the resource. 
     78     * @return path of the resource 
     79     */ 
     80    public String getPath(); 
     81 
     82    /** 
     83     * Returns the short name of the resource. 
     84     * @return short name of the resource 
    7985     */ 
    8086    public String getName(); 
    81  
    82     /** 
    83      * Returns the short name of the resource which is its name exclusive file 
    84      * ending if it exists 
    85      * @return short name of the resource 
    86      */ 
    87     public String getShortName(); 
    8887 
    8988    /** 
     
    9998     * @return url to the resource 
    10099     */ 
    101     public URL getUrl() throws UnsupportedOperationException
     100    public URL getUrl() throws UnsupportedOperationException, MalformedURLException
    102101 
    103102    /** 
  • helma-ng/trunk/src/org/helma/repository/ResourceTracker.java

    r8835 r9141  
    2828    long lastModified; 
    2929 
    30     public ResourceTracker(Resource resource)
     30    public ResourceTracker(Resource resource) throws IOException
    3131        this.resource = resource; 
    3232        markClean(); 
     
    3737    } 
    3838 
    39     public void markClean()
     39    public void markClean() throws IOException
    4040        lastModified = resource.lastModified(); 
    4141    } 
  • helma-ng/trunk/src/org/helma/repository/ZipRepository.java

    r8986 r9141  
    8181 
    8282        if (zipentry == null) { 
    83             name = shortName = file.getName(); 
     83            path = name = file.getName(); 
    8484            depth = 0; 
    8585            entryPath = ""; 
     
    8787            String[] pathArray = StringUtils.split(zipentry.getName(), separator); 
    8888            depth = pathArray.length; 
    89             shortName = pathArray[depth - 1]; 
     89            name = pathArray[depth - 1]; 
    9090            entryPath = zipentry.getName(); 
    91             name = new StringBuffer(parent.getName()) 
    92                                    .append('/').append(shortName).toString(); 
     91            path = new StringBuffer(parent.getPath()) 
     92                                   .append('/').append(name).toString(); 
    9393        } 
    9494    } 
     
    126126                    } 
    127127                    String[] entrypath = StringUtils.split(eName, separator); 
    128                     if (depth > 0 && !shortName.equals(entrypath[depth-1])) { 
     128                    if (depth > 0 && !name.equals(entrypath[depth-1])) { 
    129129                        // catch case where our name is Foo and other's is FooBar 
    130130                        continue; 
     
    136136                        // create a new child resource 
    137137                        ZipResource resource = new ZipResource(entry.getName(), this); 
    138                         newResources.put(resource.getShortName(), resource); 
     138                        newResources.put(resource.getName(), resource); 
    139139                    } else if (entrypath.length > depth) { 
    140140                        // create a new child repository 
     
    213213    } 
    214214 
    215     public void create() { 
    216         // we do not create zip files as it makes no sense 
    217         throw new UnsupportedOperationException("create() not implemented for ZipRepository"); 
    218     } 
    219  
    220     /** 
    221      * Checks wether the repository is to be considered a top-level 
    222      * repository from a scripting point of view. For example, a zip 
    223      * file within a file repository is not a root repository from 
    224      * a physical point of view, but from the scripting point of view it is. 
    225      * 
    226      * @return true if the repository is to be considered a top-level script repository 
    227      */ 
    228     public boolean isScriptRoot() { 
    229         return depth == 0; 
    230     } 
    231  
    232215    /** 
    233216     * Get a child repository with the given name 
     
    237220     */ 
    238221    public Repository getChildRepository(String name) { 
    239         if (mounted.containsKey(name)) { 
    240             return mounted.get(name); 
    241         } 
    242222        return new ZipRepository(file, this, new ZipEntry(entryPath + "/" + name)); 
    243223    } 
     
    248228 
    249229    public int hashCode() { 
    250         return 17 + (37 * file.hashCode()) + (37 * name.hashCode()); 
     230        return 17 + (37 * file.hashCode()) + (37 * path.hashCode()); 
    251231    } 
    252232 
     
    257237 
    258238        ZipRepository rep = (ZipRepository) obj; 
    259         return (file.equals(rep.file) && name.equals(rep.name)); 
     239        return (file.equals(rep.file) && path.equals(rep.path)); 
    260240    } 
    261241 
    262242    public String toString() { 
    263         return new StringBuffer("ZipRepository[").append(name).append("]").toString(); 
     243        return new StringBuffer("ZipRepository[").append(path).append("]").toString(); 
    264244    } 
    265245 
  • helma-ng/trunk/src/org/helma/repository/ZipResource.java

    r8835 r9141  
    2626    private String entryName; 
    2727    private ZipRepository repository; 
     28    private String path; 
    2829    private String name; 
    29     private String shortName; 
    3030    private String baseName; 
    3131 
     
    3636        int lastSlash = entryName.lastIndexOf('/'); 
    3737 
    38         shortName = entryName.substring(lastSlash + 1); 
    39         name = new StringBuffer(repository.getName()).append('/') 
    40                 .append(shortName).toString(); 
     38        name = entryName.substring(lastSlash + 1); 
     39        path = new StringBuffer(repository.getPath()).append('/') 
     40                .append(name).toString(); 
    4141 
    4242        // base name is short name with extension cut off 
    43         int lastDot = shortName.lastIndexOf("."); 
    44         baseName = (lastDot == -1) ? shortName : shortName.substring(0, lastDot); 
     43        int lastDot = name.lastIndexOf("."); 
     44        baseName = (lastDot == -1) ? name : name.substring(0, lastDot); 
    4545    } 
    4646 
     
    131131    } 
    132132 
     133    public String getPath() { 
     134        return path; 
     135    } 
     136 
    133137    public String getName() { 
    134138        return name; 
    135     } 
    136  
    137     public String getShortName() { 
    138         return shortName; 
    139139    } 
    140140 
     
    170170 
    171171    public int hashCode() { 
    172         return 17 + name.hashCode(); 
     172        return 17 + path.hashCode(); 
    173173    } 
    174174 
    175175    public boolean equals(Object obj) { 
    176         return obj instanceof ZipResource && name.equals(((ZipResource) obj).name); 
     176        return obj instanceof ZipResource && path.equals(((ZipResource) obj).path); 
    177177    } 
    178178 
    179179    public String toString() { 
    180         return getName(); 
     180        return getPath(); 
    181181    } 
    182182}