KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > easybeans > container > archive > EZBDirectoryArchiveImpl


1 /**
2  * EasyBeans
3  * Copyright (C) 2006 Bull S.A.S.
4  * Contact: easybeans@objectweb.org
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2.1 of the License, or any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with this library; if not, write to the Free Software
18  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
19  * USA
20  *
21  * --------------------------------------------------------------------------
22  * $Id: EZBDirectoryArchiveImpl.java 1026 2006-08-04 14:54:10Z benoitf $
23  * --------------------------------------------------------------------------
24  */

25
26 package org.objectweb.easybeans.container.archive;
27
28 import static org.objectweb.easybeans.util.url.URLUtils.fileToURL2;
29
30 import java.io.File JavaDoc;
31 import java.net.URL JavaDoc;
32 import java.util.ArrayList JavaDoc;
33 import java.util.Iterator JavaDoc;
34 import java.util.List JavaDoc;
35
36 import org.objectweb.easybeans.api.EZBArchive;
37 import org.objectweb.easybeans.api.EZBArchiveException;
38 import org.objectweb.easybeans.util.url.URLUtilsException;
39 /**
40  * Creates wrapper around directory.
41  * @author Florent Benoit
42  */

43 public class EZBDirectoryArchiveImpl implements EZBArchive {
44
45     /**
46      * Internal resource used as archive.
47      */

48     private File JavaDoc directory = null;
49
50     /**
51      * Creates new instance of an EZBArchive for a directory.
52      * @param directory the given directory.
53      */

54     protected EZBDirectoryArchiveImpl(final File JavaDoc directory) {
55         this.directory = directory;
56     }
57
58     /**
59      * @return a description of this archive. This name could be used in logger info.
60      */

61     public String JavaDoc getName() {
62         return directory.getPath();
63     }
64
65     /**
66      * Encode resource name to be used on Unix/Windows systems.
67      * @param resourceName the name to encode.
68      * @return the encoded name.
69      */

70     private String JavaDoc encode(final String JavaDoc resourceName) {
71         String JavaDoc[] tokens = resourceName.split("/");
72         StringBuilder JavaDoc sb = new StringBuilder JavaDoc();
73         for (String JavaDoc token : tokens) {
74             if (sb.length() > 0) {
75                 sb.append(File.separator);
76             }
77             sb.append(token);
78         }
79         return sb.toString();
80     }
81
82
83     /**
84      * Close the underlying Resource.
85      * @return Returns <code>true</code> if the close was
86      * succesful, <code>false</code> otherwise.
87      */

88     public boolean close() {
89         // Nothing to close
90
return true;
91     }
92
93     /**
94      * @param resourceName The resource name to be looked up.
95      * @return Returns the resource URL if the resource
96      * has been found. null otherwise.
97      * @throws EZBArchiveException if method fails.
98      */

99     public URL JavaDoc getResource(final String JavaDoc resourceName) throws EZBArchiveException {
100         URL JavaDoc resourceURL = null;
101         // lookup the directory on filesystem
102
File JavaDoc f = new File JavaDoc(directory, encode(resourceName));
103         try {
104             if (f.exists()) {
105                 resourceURL = fileToURL2(f);
106             }
107         } catch (URLUtilsException e) {
108             throw new EZBArchiveException("Invalid url", e);
109         }
110         return resourceURL;
111     }
112
113     /**
114      * @return Returns an Iterator of Resource's URL.
115      * @throws EZBArchiveException if method fails.
116      */

117     public Iterator JavaDoc<URL JavaDoc> getResources() throws EZBArchiveException {
118         List JavaDoc<URL JavaDoc> listResources = new ArrayList JavaDoc<URL JavaDoc>();
119         // Get all files and subdirectories
120
addFiles(directory, listResources);
121         return listResources.iterator();
122     }
123
124     /**
125      * Methods that loop on directories to find the children (files).
126      * @param file the given directory/file.
127      * @param listResources the list on which to add new files.
128      */

129     private void addFiles(final File JavaDoc file, final List JavaDoc<URL JavaDoc> listResources) {
130         if (!file.exists()) {
131             return;
132         }
133
134         if (file.isDirectory()) {
135             // directory
136
File JavaDoc[] files = file.listFiles();
137             for (File JavaDoc f : files) {
138                 // loop
139
addFiles(f, listResources);
140             }
141         } else {
142             // single file
143
try {
144                 listResources.add(fileToURL2(file));
145             } catch (URLUtilsException e) {
146                 throw new IllegalStateException JavaDoc("Invalid url", e);
147             }
148         }
149     }
150
151
152     /**
153      * @param resourceName The resource name to be looked up.
154      * @return Returns an Iterator of matching resources.
155      * @throws EZBArchiveException if method fails.
156      */

157     public Iterator JavaDoc<URL JavaDoc> getResources(final String JavaDoc resourceName) throws EZBArchiveException{
158         List JavaDoc<URL JavaDoc> listResources = new ArrayList JavaDoc<URL JavaDoc>();
159         File JavaDoc f = new File JavaDoc(directory, encode(resourceName));
160         if (f.exists()) {
161             try {
162                 listResources.add(fileToURL2(f));
163             } catch (URLUtilsException e) {
164                 throw new EZBArchiveException("Invalid url", e);
165             }
166         }
167
168         return listResources.iterator();
169
170     }
171
172     /**
173      * @return Returns the resource URL.
174      * @throws EZBArchiveException if method fails.
175      */

176     public URL JavaDoc getURL() throws EZBArchiveException {
177         try {
178             return fileToURL2(directory);
179         } catch (URLUtilsException e) {
180             throw new IllegalStateException JavaDoc("Invalid url", e);
181         }
182     }
183
184     /**
185      * Is that the given object is equals to our instance.
186      * @param o the object to compare.
187      * @return true if equals, else false.
188      */

189     @Override JavaDoc
190     public boolean equals(final Object JavaDoc o) {
191         if (!(o instanceof EZBDirectoryArchiveImpl)) {
192             return false;
193         }
194         EZBDirectoryArchiveImpl other = (EZBDirectoryArchiveImpl) o;
195         return this.directory.equals(other.directory);
196     }
197
198     /**
199      * Gets hashcode for this object.
200      * @return hash code.
201      */

202     @Override JavaDoc
203     public int hashCode() {
204         return directory.hashCode();
205     }
206
207     /**
208      * @return string representation
209      */

210     @Override JavaDoc
211     public String JavaDoc toString() {
212         return getName();
213     }
214 }
215
Popular Tags