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