01: package org.geotools.catalog.property;
02:
03: import java.io.File;
04: import java.io.FilenameFilter;
05: import java.io.IOException;
06: import java.net.MalformedURLException;
07: import java.net.URI;
08: import java.util.HashMap;
09: import java.util.Map;
10:
11: import org.geotools.catalog.Catalog;
12: import org.geotools.catalog.Service;
13: import org.geotools.catalog.ServiceFactory;
14: import org.geotools.data.property.PropertyDataStoreFactory;
15:
16: /**
17: * Creates a new property service.
18: *
19: * @author Justin Deoliveira, The Open Planning Project, jdeolive@openplans.org
20: *
21: */
22: public class PropertyServiceFactory implements ServiceFactory {
23:
24: public Service createService(Catalog catalog, URI uri, Map params) {
25: if (params.containsKey(PropertyDataStoreFactory.DIRECTORY.key)) {
26:
27: File file;
28: try {
29: file = (File) PropertyDataStoreFactory.DIRECTORY
30: .lookUp(params);
31: } catch (IOException e) {
32: //TODO: log
33: return null;
34: }
35:
36: if (file != null) {
37: PropertyDataStoreFactory factory = new PropertyDataStoreFactory();
38: return new PropertyService(catalog, params, file,
39: factory);
40: }
41: }
42:
43: return null;
44: }
45:
46: public boolean canProcess(URI uri) {
47: try {
48: File file = new File(uri.toURL().getFile());
49: return file.isDirectory() && file.canRead()
50: && containsPropertyFile(file);
51: } catch (MalformedURLException e) {
52: return false;
53: }
54: }
55:
56: public Map createParams(URI uri) {
57: if (!canProcess(uri))
58: return null;
59:
60: File file;
61: try {
62: file = new File(uri.toURL().getFile());
63: } catch (MalformedURLException e) {
64: return null;
65: }
66: HashMap map = new HashMap();
67: map.put(PropertyDataStoreFactory.DIRECTORY.key, file);
68:
69: return map;
70: }
71:
72: static boolean containsPropertyFile(File dir) {
73: if (dir.isDirectory()) {
74: dir.listFiles(new FilenameFilter() {
75:
76: public boolean accept(File dir, String name) {
77: String s = ".properties";
78: if (name.length() > s.length()) {
79: return s.equals(name.substring(s.length()));
80: }
81:
82: return false;
83: }
84:
85: });
86: }
87:
88: return false;
89: }
90: }
|