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