KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > deployment > cache > FileDeploymentStore


1 /***************************************
2  * *
3  * JBoss: The OpenSource J2EE WebOS *
4  * *
5  * Distributable under LGPL license. *
6  * See terms of license at gnu.org. *
7  * *
8  ***************************************/

9
10 package org.jboss.deployment.cache;
11
12 import java.util.Map JavaDoc;
13 import java.util.HashMap JavaDoc;
14 import java.util.zip.CRC32 JavaDoc;
15
16 import java.net.URL JavaDoc;
17 import java.net.URLConnection JavaDoc;
18 import java.net.MalformedURLException JavaDoc;
19
20 import java.io.File JavaDoc;
21 import java.io.FileNotFoundException JavaDoc;
22 import java.io.IOException JavaDoc;
23
24 import java.io.InputStream JavaDoc;
25 import java.io.OutputStream JavaDoc;
26 import java.io.BufferedInputStream JavaDoc;
27 import java.io.BufferedOutputStream JavaDoc;
28 import java.io.ObjectInputStream JavaDoc;
29 import java.io.ObjectOutputStream JavaDoc;
30 import java.io.FileInputStream JavaDoc;
31 import java.io.FileOutputStream JavaDoc;
32
33 import org.jboss.system.ServiceMBeanSupport;
34 import org.jboss.system.ConfigurationException;
35 import org.jboss.system.server.ServerConfigLocator;
36
37 import org.jboss.util.NullArgumentException;
38 import org.jboss.util.NestedRuntimeException;
39 import org.jboss.util.stream.Streams;
40
41 /**
42  * A local file based {@link DeploymentStore}.
43  *
44  * @jmx:mbean extends="org.jboss.deployment.cache.DeploymentStoreMBean"
45  *
46  * @todo Validate the urlMap
47  *
48  * @version <tt>$Revision: 1.5 $</tt>
49  * @author <a HREF="mailto:jason@planet57.com">Jason Dillon</a>
50  */

51 public class FileDeploymentStore
52    extends ServiceMBeanSupport
53    implements DeploymentStore, FileDeploymentStoreMBean
54 {
55    /** The local directory where cache data will be stored. */
56    protected File JavaDoc dir;
57
58    /** The file where the mapping is located. */
59    protected File JavaDoc mapFile;
60
61    /** The URL to local file mapping. */
62    protected Map JavaDoc urlMap;
63
64    /**
65     * Set the local directory where cache data will be stored.
66     *
67     * @param dir The local directory where cache data will be stored.
68     *
69     * @throws IOException File not found, not a directory, can't write...
70     *
71     * @jmx:managed-attribute
72     */

73    public void setDirectory(File JavaDoc dir) throws IOException JavaDoc
74    {
75       if (dir == null)
76          throw new NullArgumentException("dir");
77
78       if (!dir.isAbsolute()) {
79          File JavaDoc serverHome = serverHome = ServerConfigLocator.locate().getServerHomeDir();
80          dir = new File JavaDoc(serverHome, dir.getPath());
81       }
82
83       if (!dir.exists()) {
84          if (!dir.mkdirs()) {
85             throw new IOException JavaDoc("Failed to create directory: " + dir);
86          }
87       }
88
89       if (!dir.isDirectory()) {
90          throw new FileNotFoundException JavaDoc("Given file reference is not a directory: " + dir);
91       }
92
93       if (!dir.canWrite()) {
94          throw new IOException JavaDoc("Can not write to directory: " + dir);
95       }
96
97       if (!dir.canRead()) {
98          throw new IOException JavaDoc("Can not read directory: " + dir);
99       }
100
101       // should be ok...
102

103       this.dir = dir.getCanonicalFile();
104
105       log.debug("Using directory for cache storage: " + dir);
106
107       // the map file to use
108
this.mapFile = new File JavaDoc(dir, "state-map.ser");
109    }
110
111    /**
112     * Returns the local directory where cache data is stored.
113     *
114     * @return The local directory where cache data is stored.
115     *
116     * @jmx:managed-attribute
117     */

118    public File JavaDoc getDirectory() {
119       return this.dir;
120    }
121
122    /**
123     * Set the name of the local directory where cache data will be stored.
124     *
125     * <p>Invokes {@link #setDirectory}.
126     *
127     * @param dirname The name of the local directory where cache data will be stored.
128     *
129     * @throws IOException File not found, not a directory, can't write...
130     *
131     * @jmx:managed-attribute
132     */

133    public void setDirectoryName(String JavaDoc dirname) throws IOException JavaDoc
134    {
135       if (dirname == null)
136          throw new NullArgumentException("dirname");
137
138       setDirectory(new File JavaDoc(dirname));
139    }
140
141    /**
142     * Get the name of the local directory where cache data is stored.
143     *
144     * @return The name of the local directory where cache data is stored.
145     *
146     * @jmx:managed-attribute
147     */

148    public String JavaDoc getDirectoryName() {
149       return dir.getAbsolutePath();
150    }
151
152
153    /////////////////////////////////////////////////////////////////////////
154
// DeploymentStore //
155
/////////////////////////////////////////////////////////////////////////
156

157    protected URL JavaDoc getURLFromFile(final File JavaDoc file)
158    {
159       try {
160          return file.toURL();
161       }
162       catch (Exception JavaDoc e) {
163          // should never happen
164
throw new NestedRuntimeException(e);
165       }
166    }
167
168    public URL JavaDoc get(final URL JavaDoc url)
169    {
170       File JavaDoc file = (File JavaDoc)urlMap.get(url);
171       if (file == null) return null;
172
173       return getURLFromFile(file);
174    }
175
176    public URL JavaDoc put(final URL JavaDoc url) throws Exception JavaDoc
177    {
178       URL JavaDoc localURL = get(url);
179       File JavaDoc file;
180
181       if (localURL == null) {
182          // make a short unique filename for the given url which
183
// will always be generated for this specific url
184
CRC32 JavaDoc checksum = new CRC32 JavaDoc();
185          checksum.update(url.toString().getBytes());
186       
187          String JavaDoc prefix = Long.toString(checksum.getValue(), Character.MAX_RADIX);
188          String JavaDoc filename = url.getFile();
189          filename = filename.substring(filename.lastIndexOf("/") + 1, filename.length());
190       
191          file = new File JavaDoc(dir, prefix + "-" + filename);
192          urlMap.put(url, file);
193          writeMap();
194       }
195       else {
196          // no need to regen the filename, we have it already
197
file = (File JavaDoc)urlMap.get(url);
198       }
199
200       // copy the data from the url to the local file
201
copyURL(url, file);
202
203       // return the local file url
204
return getURLFromFile(file);
205    }
206
207    /**
208     * Copy the data at the given source URL to the given file.
209     */

210    protected void copyURL(final URL JavaDoc source, final File JavaDoc dest) throws IOException JavaDoc
211    {
212       InputStream JavaDoc is = new BufferedInputStream JavaDoc(source.openConnection().getInputStream());
213       OutputStream JavaDoc os = new BufferedOutputStream JavaDoc(new FileOutputStream JavaDoc(dest));
214
215       try {
216          Streams.copy(is, os);
217          os.flush();
218       }
219       finally {
220          os.close();
221          is.close();
222       }
223    }
224       
225    /**
226     * Read the url map from serialized state.
227     */

228    protected Map JavaDoc readMap() throws ClassNotFoundException JavaDoc, IOException JavaDoc
229    {
230       if (mapFile.exists()) {
231          Map JavaDoc map;
232
233          InputStream JavaDoc is = new BufferedInputStream JavaDoc(new FileInputStream JavaDoc(mapFile));
234          ObjectInputStream JavaDoc ois = new ObjectInputStream JavaDoc(is);
235
236          try {
237             map = (Map JavaDoc)ois.readObject();
238          }
239          finally {
240             ois.close();
241          }
242
243          return map;
244       }
245       else {
246          log.debug("Map file not found, creating new map");
247          return new HashMap JavaDoc();
248       }
249    }
250
251    /**
252     * Write the url map to serialized state.
253     */

254    protected void writeMap() throws IOException JavaDoc
255    {
256       OutputStream JavaDoc os = new BufferedOutputStream JavaDoc(new FileOutputStream JavaDoc(mapFile));
257       ObjectOutputStream JavaDoc oos = new ObjectOutputStream JavaDoc(os);
258
259       try {
260          oos.writeObject(urlMap);
261          oos.flush();
262       }
263       finally {
264          oos.close();
265       }
266    }
267
268
269    /////////////////////////////////////////////////////////////////////////
270
// Service/ServiceMBeanSupport //
271
/////////////////////////////////////////////////////////////////////////
272

273    /**
274     * Setup the url map.
275     */

276    protected void createService() throws Exception JavaDoc
277    {
278       if (dir == null)
279          throw new ConfigurationException("Missing attribute 'Directory'");
280
281       // read the map if there is one
282
urlMap = readMap();
283    }
284 }
285
Popular Tags