svn-gvsig-desktop / branches / v2_0_0_prep / libraries / libFMap_dalfile / src / org / gvsig / fmap / dal / serverexplorer / filesystem / FilesystemExplorer.java @ 24887
History | View | Annotate | Download (8.69 KB)
1 |
package org.gvsig.fmap.dal.serverexplorer.filesystem; |
---|---|
2 |
|
3 |
import java.io.File; |
4 |
import java.io.FileFilter; |
5 |
import java.util.ArrayList; |
6 |
import java.util.Iterator; |
7 |
import java.util.List; |
8 |
import java.util.Map; |
9 |
|
10 |
import org.gvsig.fmap.dal.DALLocator; |
11 |
import org.gvsig.fmap.dal.DataServerExplorerParameters; |
12 |
import org.gvsig.fmap.dal.DataStoreParameters; |
13 |
import org.gvsig.fmap.dal.NewDataStoreParameters; |
14 |
import org.gvsig.fmap.dal.exception.DataException; |
15 |
import org.gvsig.fmap.dal.exception.FileNotFoundException; |
16 |
import org.gvsig.fmap.dal.exception.InitializeException; |
17 |
import org.gvsig.fmap.dal.exception.ProviderNotRegisteredException; |
18 |
import org.gvsig.fmap.dal.exception.RemoveException; |
19 |
import org.gvsig.fmap.dal.spi.DataManagerProviderServices; |
20 |
import org.gvsig.fmap.dal.spi.DataServerExplorerProvider; |
21 |
import org.gvsig.fmap.dal.spi.DataServerExplorerProviderServices; |
22 |
import org.gvsig.fmap.dal.spi.DataStoreProvider; |
23 |
import org.gvsig.tools.ToolsLocator; |
24 |
import org.gvsig.tools.extensionpoint.ExtensionBuilder; |
25 |
import org.gvsig.tools.extensionpoint.ExtensionPoint.Extension; |
26 |
|
27 |
|
28 |
public class FilesystemExplorer implements DataServerExplorerProvider { |
29 |
// FIXME: , IPersistence{
|
30 |
|
31 |
public static final String NAME = "FilesystemExplorer"; |
32 |
|
33 |
final private static String FILE_FILTER_EPSNAME = "FilesystemExplorerFilters"; |
34 |
final private static String FILE_FILTER_EPSDESCRIPTION = "Registro de filtros asociados al explorador del sistema de ficheros."; |
35 |
|
36 |
FilesystemExplorerParameters parameters; |
37 |
private File root; |
38 |
|
39 |
private File current; |
40 |
|
41 |
private DataServerExplorerProviderServices providerServices;
|
42 |
|
43 |
|
44 |
public static class FilterWraper implements ExtensionBuilder { |
45 |
|
46 |
private FileFilter filter; |
47 |
|
48 |
public FilterWraper(FileFilter filter) { |
49 |
this.filter = filter;
|
50 |
} |
51 |
|
52 |
public Object create() { |
53 |
return filter;
|
54 |
} |
55 |
|
56 |
public Object create(Object[] args) { |
57 |
return filter;
|
58 |
} |
59 |
|
60 |
public Object create(Map args) { |
61 |
return filter;
|
62 |
} |
63 |
|
64 |
} |
65 |
|
66 |
public static class FilterWraperIterator implements Iterator { |
67 |
private Iterator it; |
68 |
|
69 |
public FilterWraperIterator(Iterator it) { |
70 |
this.it = it;
|
71 |
} |
72 |
|
73 |
public boolean hasNext() { |
74 |
return this.it.hasNext(); |
75 |
} |
76 |
|
77 |
public Object next() { |
78 |
return ((Extension) this.it.next()).getBuilder().create(); |
79 |
} |
80 |
|
81 |
public void remove() { |
82 |
throw new UnsupportedOperationException(); |
83 |
} |
84 |
|
85 |
} |
86 |
|
87 |
public static void registerFilter(FilesystemFileFilter filter) { |
88 |
FilterWraper filterWraper = new FilterWraper(filter);
|
89 |
|
90 |
ToolsLocator.getExtensionPointManager().add(FILE_FILTER_EPSNAME, |
91 |
FILE_FILTER_EPSDESCRIPTION).append( |
92 |
filter.getDataStoreProviderName(), filter.getDescription(), |
93 |
filterWraper); |
94 |
} |
95 |
|
96 |
public static Iterator getFilters() { |
97 |
Iterator it = new FilterWraperIterator(ToolsLocator |
98 |
.getExtensionPointManager().get(FILE_FILTER_EPSNAME).iterator()); |
99 |
return it;
|
100 |
} |
101 |
|
102 |
public FilesystemExplorer(FilesystemExplorerParameters parameters)
|
103 |
throws InitializeException {
|
104 |
this.parameters = parameters;
|
105 |
this.root = new File(this.parameters.getRoot()); |
106 |
if (this.parameters.getInitialpath() == null) { |
107 |
this.current = new File(this.parameters.getRoot()); |
108 |
}else{
|
109 |
this.current = new File(this.parameters.getInitialpath()); |
110 |
} |
111 |
} |
112 |
|
113 |
public String getType() { |
114 |
return NAME;
|
115 |
} |
116 |
|
117 |
public DataServerExplorerParameters getParameters() {
|
118 |
return parameters;
|
119 |
} |
120 |
|
121 |
public void dispose() throws DataException { |
122 |
this.parameters = null; |
123 |
this.root = null; |
124 |
} |
125 |
|
126 |
public List list() throws DataException { |
127 |
if (!this.current.exists()) { |
128 |
//TODO crear excepcion de Data??
|
129 |
new org.gvsig.fmap.dal.exception.FileNotFoundException(this.current); |
130 |
} |
131 |
|
132 |
if (!this.current.isDirectory()) { |
133 |
new IllegalArgumentException(this.getName() |
134 |
+ ": Path not a directory '"
|
135 |
+ this.current + "'"); |
136 |
} |
137 |
// DataManager dsm=DataManager.getManager();
|
138 |
|
139 |
String files[] = this.current.list(); |
140 |
int i;
|
141 |
File theFile;
|
142 |
ArrayList list = new ArrayList(); |
143 |
DataStoreParameters dsp = null;
|
144 |
|
145 |
for (i = 0; i < files.length; i++) { |
146 |
theFile = new File(this.root, files[i]); |
147 |
dsp = this.getParametersFor(theFile);
|
148 |
if (dsp != null) { |
149 |
list.add(dsp); |
150 |
} |
151 |
} |
152 |
return list;
|
153 |
} |
154 |
|
155 |
public void setCurrentPath(File path) throws FileNotFoundException { |
156 |
// FIXME Comprobar si es un directorio existente
|
157 |
if (!path.exists()) {
|
158 |
throw new FileNotFoundException(path); |
159 |
} |
160 |
if (!path.isDirectory()) {
|
161 |
throw new IllegalArgumentException(path.getPath() |
162 |
+ " is not a directory");
|
163 |
} |
164 |
if (!isFromRoot(path)) {
|
165 |
throw new IllegalArgumentException(path.getPath() |
166 |
+ " is not from root");
|
167 |
|
168 |
} |
169 |
|
170 |
this.current = path;
|
171 |
} |
172 |
|
173 |
public File getCurrentPath() { |
174 |
return this.current; |
175 |
} |
176 |
|
177 |
public File getRoot() { |
178 |
return this.root; |
179 |
} |
180 |
|
181 |
public void remove(DataStoreParameters dsp) throws RemoveException { |
182 |
String providerName = dsp.getDataStoreName();
|
183 |
try {
|
184 |
this.checkIsMine(dsp);
|
185 |
DataStoreProvider provider = this.getManager().createStoreProvider(
|
186 |
providerName); |
187 |
|
188 |
provider.remove(dsp); |
189 |
} catch (DataException e) {
|
190 |
throw new RemoveException(this.getName(), e); |
191 |
} |
192 |
} |
193 |
|
194 |
public boolean add(NewDataStoreParameters ndsp, boolean overwrite) |
195 |
throws DataException {
|
196 |
String providerName = ndsp.getDataStoreName();
|
197 |
this.checkIsMine(ndsp);
|
198 |
try {
|
199 |
DataStoreProvider provider = this.getManager().createStoreProvider(
|
200 |
providerName); |
201 |
|
202 |
provider.create(ndsp, overwrite); |
203 |
return true; // TODO deber?a devolver un booleano el provider o esto ser un void |
204 |
} catch (DataException e) {
|
205 |
throw new RemoveException(this.getName(), e); |
206 |
} |
207 |
} |
208 |
|
209 |
public boolean canAdd() { |
210 |
return this.root.canWrite(); |
211 |
} |
212 |
|
213 |
public String getName() { |
214 |
return this.FILE_FILTER_EPSNAME; |
215 |
} |
216 |
|
217 |
public NewDataStoreParameters getAddParameters(String storeName) |
218 |
throws DataException {
|
219 |
DataStoreProvider provider = this.getProvider(storeName);
|
220 |
if (provider == null) { |
221 |
throw new IllegalArgumentException( |
222 |
"Not registered in this explorer"); // FIXME |
223 |
} |
224 |
|
225 |
NewDataStoreParameters nParams = provider.getCreateParameters(); |
226 |
// nParams.setAttribute("path", this.getCurrentPath().getPath());
|
227 |
return nParams;
|
228 |
} |
229 |
|
230 |
public boolean canAdd(String storeName) |
231 |
throws DataException {
|
232 |
DataStoreProvider provider = this.getProvider(storeName);
|
233 |
if (provider == null) { |
234 |
throw new IllegalArgumentException( |
235 |
"Not registered in this explorer"); // FIXME |
236 |
} |
237 |
|
238 |
return provider.canCreate();
|
239 |
|
240 |
} |
241 |
|
242 |
// ==========================================
|
243 |
|
244 |
private DataStoreProvider getProvider(String storeName) |
245 |
throws InitializeException, ProviderNotRegisteredException {
|
246 |
Iterator filters = getFilters();
|
247 |
while (filters.hasNext()) {
|
248 |
FilesystemFileFilter filter = (FilesystemFileFilter) filters.next(); |
249 |
if (filter.getDataStoreProviderName().equals(storeName)) {
|
250 |
return this.getManager().createStoreProvider(storeName); |
251 |
} |
252 |
} |
253 |
return null; |
254 |
} |
255 |
|
256 |
private DataManagerProviderServices getManager() {
|
257 |
return (DataManagerProviderServices) DALLocator.getDataManager();
|
258 |
} |
259 |
|
260 |
public DataStoreParameters getParametersFor(File file) |
261 |
throws DataException {
|
262 |
if (!file.exists()) {
|
263 |
return null; |
264 |
} |
265 |
if (!file.isFile()) {
|
266 |
return null; |
267 |
} |
268 |
if (!file.canRead()) {
|
269 |
return null; |
270 |
} |
271 |
if (file.isHidden()) { // XXX ??? |
272 |
return null; |
273 |
} |
274 |
Iterator filters = getFilters();
|
275 |
while (filters.hasNext()) {
|
276 |
FilesystemFileFilter filter = (FilesystemFileFilter) filters.next(); |
277 |
if (filter.accept(file)) {
|
278 |
DataStoreParameters params = this.getManager()
|
279 |
.createStoreParameters( |
280 |
filter.getDataStoreProviderName()); |
281 |
((FilesystemStoreParameters) params).setFile(file); |
282 |
return params;
|
283 |
} |
284 |
} |
285 |
return null; |
286 |
} |
287 |
|
288 |
private void checkIsMine(DataStoreParameters dsp) |
289 |
throws IllegalArgumentException, DataException { |
290 |
if (!(dsp instanceof FilesystemExplorer)) { |
291 |
new IllegalArgumentException("not instance of FilesystemExplorer"); |
292 |
} |
293 |
Iterator filters = getFilters();
|
294 |
File file = ((FilesystemStoreParameters) dsp).getFile();
|
295 |
if (!this.isFromRoot(file)) { |
296 |
throw new IllegalArgumentException("worng explorer"); |
297 |
} |
298 |
FilesystemFileFilter filter; |
299 |
while (filters.hasNext()) {
|
300 |
filter = (FilesystemFileFilter) filters.next(); |
301 |
if (dsp.getDataStoreName().equals(filter.getDataStoreProviderName())) {
|
302 |
return;
|
303 |
} |
304 |
} |
305 |
throw new IllegalArgumentException("worng explorer"); |
306 |
} |
307 |
|
308 |
private boolean isFromRoot(File file) { |
309 |
return file.getAbsolutePath().startsWith(this.root.getAbsolutePath()); |
310 |
} |
311 |
|
312 |
public void initialize( |
313 |
DataServerExplorerProviderServices dataServerExplorerProviderServices) { |
314 |
this.providerServices = dataServerExplorerProviderServices;
|
315 |
|
316 |
} |
317 |
|
318 |
|
319 |
} |