KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sslexplorer > boot > LocalRepository


1 package com.sslexplorer.boot;
2
3 import java.io.File JavaDoc;
4 import java.io.FileFilter JavaDoc;
5 import java.io.FileInputStream JavaDoc;
6 import java.io.FileNotFoundException JavaDoc;
7 import java.io.FileOutputStream JavaDoc;
8 import java.io.FilenameFilter JavaDoc;
9 import java.io.IOException JavaDoc;
10 import java.io.InputStream JavaDoc;
11 import java.io.OutputStream JavaDoc;
12 import java.util.HashMap JavaDoc;
13
14 import org.apache.commons.logging.Log;
15 import org.apache.commons.logging.LogFactory;
16
17 /**
18  * Implementation of a {@link com.sslexplorer.boot.Repository} that uses
19  * local files for storage. This implementation would <b>not</b> not
20  * suitable for multiple server setups.
21  *
22  * @author Lee David Painter <a HREF="mailto: lee@3sp.com">&lt;lee@3sp.com&gt;</a>
23  */

24 public class LocalRepository implements Repository {
25
26     static Log log = LogFactory.getLog(LocalRepository.class);
27     
28     // Private instance variables
29

30     private File JavaDoc basedir;
31     private HashMap JavaDoc stores = new HashMap JavaDoc();
32     
33     /**
34      * Constructor
35      *
36      * @throws IOException on any error
37      */

38     public LocalRepository() throws IOException JavaDoc {
39         
40         /**
41          * Setup the repository path
42          */

43         if(System.getProperty("sslexplorer.localRepositoryPath")!=null) {
44             basedir = new File JavaDoc(System.getProperty("sslexplorer.localRepositoryPath"));
45             basedir.mkdirs();
46         }
47         
48         if(basedir!=null && !basedir.exists()) {
49             log.error("Local repository could not be created [" + basedir.getAbsolutePath() + "]");
50             basedir = null;
51         }
52         
53         if(basedir==null) {
54             basedir = new File JavaDoc(ContextHolder.getContext().getConfDirectory(), "repository");
55             basedir.mkdirs();
56         }
57
58         if(!basedir.exists()) {
59             throw new IOException JavaDoc("Default local repository could not be created [" + basedir.getAbsolutePath() + "]");
60         }
61         
62         /**
63          * Load the available stores in this repository
64          */

65         File JavaDoc[] files = basedir.listFiles(new FileFilter JavaDoc() {
66            public boolean accept(File JavaDoc file) {
67                return file.isDirectory();
68            }
69         });
70         
71         for(int i=0;i<files.length;i++) {
72             stores.put(files[i].getName(), new LocalRepositoryStore(files[i]));
73         }
74     }
75
76
77     /* (non-Javadoc)
78      * @see com.sslexplorer.boot.Repository#getStore(java.lang.String)
79      */

80     public RepositoryStore getStore(String JavaDoc storeName) {
81
82         if(!stores.containsKey(storeName)) {
83             File JavaDoc f = new File JavaDoc(basedir, storeName);
84             f.mkdirs();
85             stores.put(storeName, new LocalRepositoryStore(f));
86         }
87         
88         return (RepositoryStore) stores.get(storeName);
89     }
90     
91     // Supporting classes
92

93     class LocalRepositoryStore implements RepositoryStore {
94
95         File JavaDoc basedir;
96         
97         LocalRepositoryStore(File JavaDoc basedir) {
98             this.basedir = basedir;
99         }
100         
101         public InputStream JavaDoc getEntryInputStream(String JavaDoc name) throws IOException JavaDoc {
102         
103             File JavaDoc f = new File JavaDoc(basedir, name);
104             
105             if(!f.exists())
106                 throw new FileNotFoundException JavaDoc(name + " is not a valid " + basedir.getName() + " entry");
107             
108             return new FileInputStream JavaDoc(f);
109         }
110
111         public OutputStream JavaDoc getEntryOutputStream(String JavaDoc name) throws IOException JavaDoc {
112             
113             File JavaDoc f = new File JavaDoc(basedir, name);
114             
115             return new FileOutputStream JavaDoc(f);
116         }
117
118         public void removeEntry(String JavaDoc name) throws IOException JavaDoc {
119             
120             File JavaDoc f = new File JavaDoc(basedir, name);
121             
122             if(f.exists()) {
123                if(!f.delete())
124                    throw new IOException JavaDoc(name + " could not be deleted from the " + basedir.getName() + " store");
125             }
126             
127         }
128         
129         public boolean hasEntry(String JavaDoc name) {
130             return new File JavaDoc(basedir, name).exists();
131         }
132
133         public String JavaDoc[] listEntries() {
134            return basedir.list(new FilenameFilter JavaDoc() {
135             public boolean accept(File JavaDoc f, String JavaDoc name) {
136                 return !new File JavaDoc(f, name).isDirectory();
137             }
138            });
139         }
140         
141     }
142
143
144 }
145
Popular Tags