svn-gvsig-desktop / trunk / org.gvsig.desktop / org.gvsig.desktop.plugin / org.gvsig.app / org.gvsig.app.mainplugin / src / main / java / org / gvsig / app / extension / DefaultFeatureTypeDefinitionsManager.java @ 42879
History | View | Annotate | Download (7.81 KB)
1 |
package org.gvsig.app.extension; |
---|---|
2 |
|
3 |
import java.io.File; |
4 |
import java.io.FileInputStream; |
5 |
import java.io.FileOutputStream; |
6 |
import java.io.IOException; |
7 |
import java.io.InputStream; |
8 |
import java.nio.charset.Charset; |
9 |
import java.util.HashMap; |
10 |
import java.util.Map; |
11 |
import java.util.Properties; |
12 |
import java.util.zip.CRC32; |
13 |
|
14 |
import org.apache.commons.io.FileUtils; |
15 |
import org.apache.commons.io.IOUtils; |
16 |
import org.gvsig.andami.PluginServices; |
17 |
import org.gvsig.andami.PluginsLocator; |
18 |
import org.gvsig.andami.PluginsManager; |
19 |
import org.gvsig.fmap.dal.DataServerExplorer; |
20 |
import org.gvsig.fmap.dal.feature.FeatureAttributeDescriptor; |
21 |
import org.gvsig.fmap.dal.feature.FeatureStore; |
22 |
import org.gvsig.fmap.dal.feature.FeatureType; |
23 |
import org.gvsig.fmap.dal.feature.FeatureTypeDefinitionsManager; |
24 |
import org.gvsig.tools.ToolsLocator; |
25 |
import org.gvsig.tools.dispose.DisposeUtils; |
26 |
import org.gvsig.tools.dynobject.DynClass; |
27 |
import org.gvsig.tools.dynobject.DynObjectManager; |
28 |
import org.slf4j.Logger; |
29 |
import org.slf4j.LoggerFactory; |
30 |
|
31 |
public class DefaultFeatureTypeDefinitionsManager implements FeatureTypeDefinitionsManager { |
32 |
|
33 |
private static final Logger logger = LoggerFactory.getLogger(DefaultFeatureTypeDefinitionsManager.class); |
34 |
|
35 |
private File definitionsFolder = null; |
36 |
private final Map<String, DynClass> dynClasses; |
37 |
|
38 |
public DefaultFeatureTypeDefinitionsManager() {
|
39 |
this.dynClasses = new HashMap<>(); |
40 |
} |
41 |
|
42 |
private File getDefinitionsFolder() throws IOException { |
43 |
if (this.definitionsFolder == null) { |
44 |
PluginsManager pluginManager = PluginsLocator.getManager(); |
45 |
PluginServices plugin = pluginManager.getPlugin(CreateDefaultFormDefinitionExtension.class); |
46 |
File homeFolder = plugin.getPluginHomeFolder();
|
47 |
File definitionsFolder = new File(homeFolder, "schemas"); |
48 |
if (!definitionsFolder.exists()) {
|
49 |
FileUtils.forceMkdir(definitionsFolder); |
50 |
} |
51 |
this.definitionsFolder = definitionsFolder;
|
52 |
} |
53 |
return this.definitionsFolder; |
54 |
} |
55 |
|
56 |
private long getCRC(FeatureType type) { |
57 |
StringBuilder buffer = new StringBuilder(); |
58 |
for (int i = 0; i < type.size(); i++) { |
59 |
FeatureAttributeDescriptor x = type.getAttributeDescriptor(i); |
60 |
buffer.append(x.getName()); |
61 |
buffer.append(x.getDataTypeName()); |
62 |
buffer.append(x.getSize()); |
63 |
} |
64 |
CRC32 crc = new CRC32(); |
65 |
byte[] data = buffer.toString().getBytes(); |
66 |
crc.update(data); |
67 |
return crc.getValue();
|
68 |
} |
69 |
|
70 |
private String getKey(FeatureStore store, FeatureType featureType) { |
71 |
return store.getName() + "_" + Long.toHexString(getCRC(featureType)); |
72 |
} |
73 |
|
74 |
private File getDefinitionFile(String key) { |
75 |
File folder;
|
76 |
try {
|
77 |
folder = getDefinitionsFolder(); |
78 |
} catch (IOException ex) { |
79 |
return null; |
80 |
} |
81 |
File f = new File(folder, key + ".xml"); |
82 |
if (f.exists()) {
|
83 |
return f;
|
84 |
} |
85 |
Properties prop = new Properties(); |
86 |
FileInputStream fin = null; |
87 |
try {
|
88 |
fin = new FileInputStream(new File(folder, "index.properties")); |
89 |
prop.load(fin); |
90 |
} catch (IOException ex) { |
91 |
return null; |
92 |
} finally {
|
93 |
IOUtils.closeQuietly(fin); |
94 |
} |
95 |
String s = prop.getProperty(key, null); |
96 |
if (s == null) { |
97 |
return null; |
98 |
} |
99 |
f = new File(folder, s); |
100 |
if (f.exists()) {
|
101 |
return f;
|
102 |
} |
103 |
f = new File(s); |
104 |
if (f.exists()) {
|
105 |
return f;
|
106 |
} |
107 |
return null; |
108 |
} |
109 |
|
110 |
@Override
|
111 |
public DynClass get(FeatureStore store, FeatureType featureType) {
|
112 |
String key = this.getKey(store, featureType); |
113 |
DynClass dynClass = this.dynClasses.get(key);
|
114 |
if (dynClass != null) { |
115 |
return dynClass;
|
116 |
} |
117 |
File definitionFile = getDefinitionFile(key);
|
118 |
if (definitionFile == null) { |
119 |
DataServerExplorer explorer = null;
|
120 |
try {
|
121 |
explorer = store.getExplorer(); |
122 |
definitionFile = explorer.getResourcePath(store, key); |
123 |
} catch(Exception ex) { |
124 |
// Do nothing, leave definitionFile to null
|
125 |
} finally {
|
126 |
DisposeUtils.disposeQuietly(explorer); |
127 |
} |
128 |
if( definitionFile == null ) { |
129 |
return featureType;
|
130 |
} |
131 |
} |
132 |
DynObjectManager dynObjectManager = ToolsLocator.getDynObjectManager(); |
133 |
try {
|
134 |
String xml = FileUtils.readFileToString(definitionFile);
|
135 |
xml = xml.replaceAll("[$][{]CWD[}]",definitionFile.getParentFile().getAbsolutePath());
|
136 |
|
137 |
InputStream is = IOUtils.toInputStream(xml, Charset.forName("UTF-8")); |
138 |
Map<String, DynClass> dynClasses = dynObjectManager.importDynClassDefinitions(is, this.getClass().getClassLoader()); |
139 |
for (DynClass aDynClass : dynClasses.values()) {
|
140 |
this.dynClasses.put(aDynClass.getName(), aDynClass);
|
141 |
} |
142 |
} catch (Exception ex) { |
143 |
logger.warn("Can't parse xml definition.", ex);
|
144 |
return null; |
145 |
} |
146 |
|
147 |
dynClass = this.dynClasses.get(key);
|
148 |
if (dynClass != null) { |
149 |
return dynClass;
|
150 |
} |
151 |
return featureType;
|
152 |
} |
153 |
|
154 |
@Override
|
155 |
public boolean contains(FeatureStore store, FeatureType featureType) { |
156 |
String key = this.getKey(store, featureType); |
157 |
return this.dynClasses.containsKey(key); |
158 |
} |
159 |
|
160 |
@Override
|
161 |
public void add(FeatureStore store, FeatureType featureType, DynClass dynClass) { |
162 |
try {
|
163 |
String key = this.getKey(store, featureType); |
164 |
File f = new File(getDefinitionsFolder(), key + ".xml"); |
165 |
DynObjectManager dynObjectManager = ToolsLocator.getDynObjectManager(); |
166 |
String xml = dynObjectManager.exportSimpleDynClassDefinitions(dynClass);
|
167 |
FileUtils.write(f, xml); |
168 |
this.dynClasses.put(key, dynClass);
|
169 |
} catch (IOException ex) { |
170 |
throw new RuntimeException(ex); |
171 |
} |
172 |
} |
173 |
|
174 |
@Override
|
175 |
public void remove(FeatureStore store, FeatureType featureType) { |
176 |
String key = this.getKey(store, featureType); |
177 |
this.dynClasses.remove(key);
|
178 |
} |
179 |
|
180 |
public void addModel(File model) { |
181 |
DynObjectManager dynObjectManager = ToolsLocator.getDynObjectManager(); |
182 |
try {
|
183 |
String xml = FileUtils.readFileToString(model);
|
184 |
InputStream is = IOUtils.toInputStream(xml, Charset.forName("UTF-8")); |
185 |
Map<String, DynClass> dynClasses = dynObjectManager.importDynClassDefinitions(is, this.getClass().getClassLoader()); |
186 |
|
187 |
File folder;
|
188 |
try {
|
189 |
folder = getDefinitionsFolder(); |
190 |
} catch (IOException ex) { |
191 |
return ;
|
192 |
} |
193 |
Properties prop = new Properties(); |
194 |
FileInputStream fin = null; |
195 |
try {
|
196 |
fin = new FileInputStream(new File(folder, "index.properties")); |
197 |
prop.load(fin); |
198 |
} catch (IOException ex) { |
199 |
} finally {
|
200 |
IOUtils.closeQuietly(fin); |
201 |
} |
202 |
for (DynClass aDynClass : dynClasses.values()) {
|
203 |
String className = aDynClass.getName();
|
204 |
prop.setProperty(className, model.getAbsolutePath()); |
205 |
} |
206 |
FileOutputStream fout = null; |
207 |
try {
|
208 |
fout = new FileOutputStream(new File(folder, "index.properties")); |
209 |
prop.store(fout, "");
|
210 |
} catch (IOException ex) { |
211 |
} finally {
|
212 |
IOUtils.closeQuietly(fout); |
213 |
} |
214 |
|
215 |
} catch (Exception ex) { |
216 |
logger.warn("Can't parse xml definition.", ex);
|
217 |
} |
218 |
} |
219 |
|
220 |
} |