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 | 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 | 42879 | jjdelcerro | import org.gvsig.fmap.dal.DataServerExplorer; |
20 | 42511 | jjdelcerro | import org.gvsig.fmap.dal.feature.FeatureAttributeDescriptor; |
21 | import org.gvsig.fmap.dal.feature.FeatureStore; |
||
22 | import org.gvsig.fmap.dal.feature.FeatureType; |
||
23 | 42775 | jjdelcerro | import org.gvsig.fmap.dal.feature.FeatureTypeDefinitionsManager; |
24 | 42511 | jjdelcerro | import org.gvsig.tools.ToolsLocator; |
25 | 42879 | jjdelcerro | import org.gvsig.tools.dispose.DisposeUtils; |
26 | 42511 | jjdelcerro | 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 | 42775 | jjdelcerro | public class DefaultFeatureTypeDefinitionsManager implements FeatureTypeDefinitionsManager { |
32 | 42511 | jjdelcerro | |
33 | 42775 | jjdelcerro | private static final Logger logger = LoggerFactory.getLogger(DefaultFeatureTypeDefinitionsManager.class); |
34 | 42511 | jjdelcerro | |
35 | 42775 | jjdelcerro | private File definitionsFolder = null; |
36 | private final Map<String, DynClass> dynClasses; |
||
37 | 42511 | jjdelcerro | |
38 | 42775 | jjdelcerro | public DefaultFeatureTypeDefinitionsManager() {
|
39 | this.dynClasses = new HashMap<>(); |
||
40 | 42511 | jjdelcerro | } |
41 | |||
42 | 42775 | jjdelcerro | private File getDefinitionsFolder() throws IOException { |
43 | 42511 | jjdelcerro | if (this.definitionsFolder == null) { |
44 | PluginsManager pluginManager = PluginsLocator.getManager(); |
||
45 | PluginServices plugin = pluginManager.getPlugin(CreateDefaultFormDefinitionExtension.class); |
||
46 | File homeFolder = plugin.getPluginHomeFolder();
|
||
47 | 42775 | jjdelcerro | File definitionsFolder = new File(homeFolder, "schemas"); |
48 | 42511 | jjdelcerro | if (!definitionsFolder.exists()) {
|
49 | FileUtils.forceMkdir(definitionsFolder); |
||
50 | } |
||
51 | this.definitionsFolder = definitionsFolder;
|
||
52 | } |
||
53 | return this.definitionsFolder; |
||
54 | } |
||
55 | |||
56 | 42775 | jjdelcerro | private long getCRC(FeatureType type) { |
57 | 42511 | jjdelcerro | 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 | 42775 | jjdelcerro | private String getKey(FeatureStore store, FeatureType featureType) { |
71 | return store.getName() + "_" + Long.toHexString(getCRC(featureType)); |
||
72 | 42511 | jjdelcerro | } |
73 | |||
74 | 42775 | jjdelcerro | private File getDefinitionFile(String key) { |
75 | File folder;
|
||
76 | 42511 | jjdelcerro | try {
|
77 | 42775 | jjdelcerro | folder = getDefinitionsFolder(); |
78 | } catch (IOException ex) { |
||
79 | 42511 | jjdelcerro | return null; |
80 | } |
||
81 | 42775 | jjdelcerro | File f = new File(folder, key + ".xml"); |
82 | if (f.exists()) {
|
||
83 | return f;
|
||
84 | 42511 | jjdelcerro | } |
85 | 42775 | jjdelcerro | Properties prop = new Properties(); |
86 | FileInputStream fin = null; |
||
87 | 42511 | jjdelcerro | try {
|
88 | 42775 | jjdelcerro | fin = new FileInputStream(new File(folder, "index.properties")); |
89 | prop.load(fin); |
||
90 | } catch (IOException ex) { |
||
91 | 42511 | jjdelcerro | return null; |
92 | 42775 | jjdelcerro | } finally {
|
93 | IOUtils.closeQuietly(fin); |
||
94 | 42511 | jjdelcerro | } |
95 | 42775 | jjdelcerro | String s = prop.getProperty(key, null); |
96 | if (s == null) { |
||
97 | 42511 | jjdelcerro | return null; |
98 | } |
||
99 | 42775 | jjdelcerro | f = new File(folder, s); |
100 | if (f.exists()) {
|
||
101 | return f;
|
||
102 | 42511 | jjdelcerro | } |
103 | 42792 | jbadia | f = new File(s); |
104 | if (f.exists()) {
|
||
105 | return f;
|
||
106 | } |
||
107 | 42775 | jjdelcerro | return null; |
108 | 42511 | jjdelcerro | } |
109 | |||
110 | @Override
|
||
111 | 42775 | jjdelcerro | 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 | 42879 | jjdelcerro | 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 | 42775 | jjdelcerro | } |
132 | DynObjectManager dynObjectManager = ToolsLocator.getDynObjectManager(); |
||
133 | 42511 | jjdelcerro | try {
|
134 | 42775 | jjdelcerro | String xml = FileUtils.readFileToString(definitionFile);
|
135 | 42879 | jjdelcerro | xml = xml.replaceAll("[$][{]CWD[}]",definitionFile.getParentFile().getAbsolutePath());
|
136 | 42792 | jbadia | |
137 | 42775 | jjdelcerro | 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 | 42511 | jjdelcerro | } |
146 | 42775 | jjdelcerro | |
147 | dynClass = this.dynClasses.get(key);
|
||
148 | if (dynClass != null) { |
||
149 | return dynClass;
|
||
150 | } |
||
151 | return featureType;
|
||
152 | 42511 | jjdelcerro | } |
153 | |||
154 | @Override
|
||
155 | 42775 | jjdelcerro | public boolean contains(FeatureStore store, FeatureType featureType) { |
156 | String key = this.getKey(store, featureType); |
||
157 | return this.dynClasses.containsKey(key); |
||
158 | 42511 | jjdelcerro | } |
159 | |||
160 | @Override
|
||
161 | 42775 | jjdelcerro | public void add(FeatureStore store, FeatureType featureType, DynClass dynClass) { |
162 | 42511 | jjdelcerro | try {
|
163 | 42775 | jjdelcerro | 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 | 42511 | jjdelcerro | } catch (IOException ex) { |
170 | 42775 | jjdelcerro | throw new RuntimeException(ex); |
171 | 42511 | jjdelcerro | } |
172 | } |
||
173 | |||
174 | @Override
|
||
175 | 42775 | jjdelcerro | public void remove(FeatureStore store, FeatureType featureType) { |
176 | String key = this.getKey(store, featureType); |
||
177 | this.dynClasses.remove(key);
|
||
178 | 42511 | jjdelcerro | } |
179 | 42775 | jjdelcerro | |
180 | public void addModel(File model) { |
||
181 | 42792 | jbadia | 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 | 42511 | jjdelcerro | } |
219 | |||
220 | } |