gvsig-scripting / org.gvsig.scripting / trunk / org.gvsig.scripting / org.gvsig.scripting.lib / org.gvsig.scripting.lib.impl / src / main / java / org / gvsig / scripting / impl / AbstractUnit.java @ 1084
History | View | Annotate | Download (11.1 KB)
1 |
package org.gvsig.scripting.impl; |
---|---|
2 |
|
3 |
import java.beans.PropertyChangeEvent; |
4 |
import java.beans.PropertyChangeListener; |
5 |
import java.io.File; |
6 |
import java.io.IOException; |
7 |
import java.net.URI; |
8 |
import java.util.ArrayList; |
9 |
import java.util.HashMap; |
10 |
import java.util.HashSet; |
11 |
import java.util.List; |
12 |
import java.util.Map; |
13 |
import java.util.Set; |
14 |
|
15 |
import org.ini4j.Ini; |
16 |
import org.slf4j.Logger; |
17 |
import org.slf4j.LoggerFactory; |
18 |
import org.apache.commons.io.FilenameUtils; |
19 |
import org.apache.commons.lang3.BooleanUtils; |
20 |
import org.apache.commons.lang3.StringUtils; |
21 |
import org.gvsig.scripting.ScriptingFolder; |
22 |
import org.gvsig.scripting.ScriptingManager; |
23 |
import org.gvsig.scripting.ScriptingUnit; |
24 |
import org.ini4j.Profile.Section; |
25 |
|
26 |
public abstract class AbstractUnit implements ScriptingUnit, Unit { |
27 |
|
28 |
protected static final Logger logger = LoggerFactory.getLogger(AbstractUnit.class); |
29 |
|
30 |
protected DefaultScriptingManager manager;
|
31 |
|
32 |
protected String id; |
33 |
protected String name = null; |
34 |
protected String description; |
35 |
protected String createdBy; |
36 |
protected String version; |
37 |
protected ScriptingFolder parent;
|
38 |
protected String typename; |
39 |
protected Set<PropertyChangeListener> changeListener; |
40 |
protected boolean saved; |
41 |
private Map<String,String> properties; |
42 |
|
43 |
|
44 |
public AbstractUnit(ScriptingFolder parent, String typename, ScriptingManager manager, String id) { |
45 |
this.parent = parent;
|
46 |
this.manager = (DefaultScriptingManager) manager;
|
47 |
this.typename = typename;
|
48 |
this.id = id;
|
49 |
this.changeListener = null; |
50 |
this.properties = null; |
51 |
} |
52 |
|
53 |
@Override
|
54 |
public void addPropertyChangeListener(PropertyChangeListener listener) { |
55 |
if( this.changeListener==null ) { |
56 |
this.changeListener = new HashSet<>(); |
57 |
} |
58 |
this.changeListener.add(listener);
|
59 |
} |
60 |
|
61 |
public void firePropertyChange(PropertyChangeEvent event) { |
62 |
if( this.changeListener==null ) { |
63 |
return;
|
64 |
} |
65 |
for( PropertyChangeListener listener : this.changeListener ) { |
66 |
try {
|
67 |
if( listener != null ) { |
68 |
listener.propertyChange(event); |
69 |
} |
70 |
} catch(Exception ex) { |
71 |
logger.warn("Problems firing PropertyChangeListener to listener "+listener+".",ex); |
72 |
} |
73 |
} |
74 |
} |
75 |
|
76 |
public void firePropertyChangeListener(String name, Object oldValue, Object newValue) { |
77 |
PropertyChangeEvent event = new PropertyChangeEvent(this,name, oldValue, newValue); |
78 |
firePropertyChange(event); |
79 |
} |
80 |
|
81 |
@Override
|
82 |
public String getTypeName() { |
83 |
return typename;
|
84 |
} |
85 |
|
86 |
@Override
|
87 |
public abstract void load(ScriptingFolder folder, String id); |
88 |
|
89 |
@Override
|
90 |
public void reload() { |
91 |
this.load( this.getParent(), this.getId()); |
92 |
} |
93 |
|
94 |
@Override
|
95 |
public String toString() { |
96 |
if (this.getName() == null) { |
97 |
return "(" + this.getClass().getSimpleName() + ")"; |
98 |
} |
99 |
return this.getName(); |
100 |
} |
101 |
|
102 |
protected void setParent(ScriptingFolder parent) { |
103 |
this.parent = parent;
|
104 |
} |
105 |
|
106 |
public ScriptingManager getManager() {
|
107 |
return this.manager; |
108 |
} |
109 |
|
110 |
@Override
|
111 |
public File getFileResource(String extension) { |
112 |
return new File(this.getParent().getFile(), this.id + extension).getAbsoluteFile(); |
113 |
} |
114 |
|
115 |
@Override
|
116 |
public String getDescription() { |
117 |
return this.description; |
118 |
} |
119 |
|
120 |
@Override
|
121 |
public String getCreatedBy() { |
122 |
return this.createdBy; |
123 |
} |
124 |
|
125 |
@Override
|
126 |
public String getVersion() { |
127 |
return this.version; |
128 |
} |
129 |
|
130 |
@Override
|
131 |
public String getId() { |
132 |
return this.id; |
133 |
} |
134 |
|
135 |
@Override
|
136 |
public String getName() { |
137 |
if (this.name == null) { |
138 |
return this.id; |
139 |
} |
140 |
return this.name; |
141 |
} |
142 |
|
143 |
@Override
|
144 |
public void setDescription(String description) { |
145 |
firePropertyChangeListener("description", description, this.description); |
146 |
this.description = description;
|
147 |
} |
148 |
|
149 |
@Override
|
150 |
public void setCreatedBy(String createdBy) { |
151 |
firePropertyChangeListener("createdBy", createdBy, this.createdBy); |
152 |
this.createdBy = createdBy;
|
153 |
} |
154 |
|
155 |
@Override
|
156 |
public void setVersion(String version) { |
157 |
firePropertyChangeListener("version", version, this.version); |
158 |
this.version = version;
|
159 |
} |
160 |
|
161 |
@Override
|
162 |
public void setId(String id) { |
163 |
firePropertyChangeListener("id", id, this.id); |
164 |
this.id = FilenameUtils.getBaseName(id);
|
165 |
} |
166 |
|
167 |
@Override
|
168 |
public void setName(String name) { |
169 |
firePropertyChangeListener("name", name, this.name); |
170 |
this.name = name;
|
171 |
} |
172 |
|
173 |
@Override
|
174 |
public ScriptingFolder getParent() {
|
175 |
return this.parent; |
176 |
} |
177 |
|
178 |
@Override
|
179 |
public String getUserPath() { |
180 |
List<String> parts = new ArrayList<>(); |
181 |
ScriptingUnit unit = this;
|
182 |
while( unit!=null ) { |
183 |
parts.add(0, unit.getName());
|
184 |
unit = unit.getParent(); |
185 |
} |
186 |
return String.join("/", parts); |
187 |
} |
188 |
|
189 |
protected void save(Ini prefs) { |
190 |
prefs.put("Unit", "type", StringUtils.defaultString(this.getTypeName())); |
191 |
prefs.put("Unit", "name", this.getName()); |
192 |
prefs.put("Unit", "description", StringUtils.defaultString(this.getDescription())); |
193 |
prefs.put("Unit", "createdBy", StringUtils.defaultString(this.getCreatedBy())); |
194 |
prefs.put("Unit", "version", StringUtils.defaultString(this.getVersion())); |
195 |
|
196 |
if( this.properties!=null ) { |
197 |
for (Map.Entry<String, String> property : properties.entrySet()) { |
198 |
String name = property.getKey();
|
199 |
String value = property.getValue();
|
200 |
prefs.put("properties", name, value);
|
201 |
} |
202 |
} |
203 |
try {
|
204 |
prefs.store(); |
205 |
} catch (IOException e) { |
206 |
File f = prefs.getFile();
|
207 |
String fname = (f == null) ? "(null)" : f.getAbsolutePath(); |
208 |
String msg = "Can't save inf file '" + fname + "'."; |
209 |
logger.warn(msg); |
210 |
throw new RuntimeException(msg, e); |
211 |
} |
212 |
|
213 |
} |
214 |
|
215 |
protected void loadInf(Ini prefs) { |
216 |
String typename = getInfString(prefs, "Unit", "type", this.getTypeName()); |
217 |
if (!this.getTypeName().equalsIgnoreCase(typename)) { |
218 |
File f = prefs.getFile();
|
219 |
String fname = (f == null) ? "(null)" : f.getAbsolutePath(); |
220 |
logger.warn("inconsistent type in inf file '" + fname + "'. Curent type '" + this.getTypeName() + "', type from inf file '" + typename + "'."); |
221 |
} |
222 |
this.setName(getInfString(prefs, "Unit", "name", this.getName())); |
223 |
this.setDescription(getInfString(prefs, "Unit", "description", null)); |
224 |
this.setCreatedBy(getInfString(prefs, "Unit", "createdBy", null)); |
225 |
this.setVersion(getInfString(prefs, "Unit", "version", null)); |
226 |
|
227 |
Section sec = prefs.get("properties");
|
228 |
if( sec != null ) { |
229 |
for(Map.Entry<String,String> entry : sec.entrySet() ) { |
230 |
this.setProperty(entry.getKey(), entry.getValue());
|
231 |
} |
232 |
} |
233 |
} |
234 |
|
235 |
protected Object getInfValue(Ini prefs, String section, String option, Object defaultValue) { |
236 |
Object r = prefs.get(section, option);
|
237 |
if (r == null) { |
238 |
return defaultValue;
|
239 |
} else {
|
240 |
return r;
|
241 |
} |
242 |
} |
243 |
|
244 |
protected String getInfString(Ini prefs, String section, String option, Object defaultValue) { |
245 |
String s = (String) getInfValue(prefs, section, option, defaultValue); |
246 |
if (s != null && s.trim().length() < 1) { |
247 |
return null; |
248 |
} |
249 |
return s;
|
250 |
} |
251 |
|
252 |
protected int getInfInt(Ini prefs, String section, String option, int defaultValue) { |
253 |
String s = (String) getInfValue(prefs, section, option, String.valueOf(defaultValue)); |
254 |
if (s != null && s.trim().length() < 1) { |
255 |
return defaultValue;
|
256 |
} |
257 |
return Integer.parseInt(s); |
258 |
} |
259 |
|
260 |
protected boolean getInfBoolean(Ini prefs, String section, String option, boolean defaultValue) { |
261 |
String s = (String) prefs.get(section, option); |
262 |
if( s == null ) { |
263 |
return defaultValue;
|
264 |
} |
265 |
return BooleanUtils.toBoolean(s);
|
266 |
} |
267 |
|
268 |
protected void console_println(String s) { |
269 |
logger.info(s); |
270 |
} |
271 |
|
272 |
public void create(ScriptingFolder folder, String id) { |
273 |
this.setParent(folder);
|
274 |
this.setId(id);
|
275 |
|
276 |
File file = new File(folder.getFile(), id + ".inf"); |
277 |
try {
|
278 |
file.createNewFile(); |
279 |
} catch (IOException e) { |
280 |
logger.warn("Can't create inf file in '" + file.getAbsolutePath() + "'.", e); |
281 |
} |
282 |
} |
283 |
|
284 |
@Override
|
285 |
public File getFile() { |
286 |
if (this.getId() == null) { |
287 |
return null; |
288 |
} |
289 |
if (this.getParent() == null) { |
290 |
return null; |
291 |
} |
292 |
if (this.getParent().getFile() == null) { |
293 |
return null; |
294 |
} |
295 |
return new File(getParent().getFile(), this.getId() + ".inf"); |
296 |
} |
297 |
|
298 |
public URI getURI() { |
299 |
File f = this.getFile(); |
300 |
return f.toURI();
|
301 |
} |
302 |
|
303 |
@Override
|
304 |
public boolean equals(Object obj) { |
305 |
if( !(obj instanceof Unit) ) { |
306 |
return false; |
307 |
} |
308 |
return this.hashCode() == obj.hashCode(); |
309 |
} |
310 |
|
311 |
@Override
|
312 |
public int hashCode() { |
313 |
File f = this.getFile(); |
314 |
if( f!=null ) { |
315 |
return "#$FILE$#".hashCode() + f.getAbsolutePath().hashCode(); |
316 |
} |
317 |
if( this.getId()!=null ) { |
318 |
return "#$ID$#".hashCode() + this.getId().hashCode(); |
319 |
} |
320 |
return super.hashCode(); |
321 |
} |
322 |
|
323 |
@Override
|
324 |
public boolean isSaved() { |
325 |
return this.saved; |
326 |
} |
327 |
|
328 |
@Override
|
329 |
public void setSaved(boolean saved) { |
330 |
this.saved = saved;
|
331 |
} |
332 |
|
333 |
@Override
|
334 |
public void setProperty(String name, String value) { |
335 |
if( this.properties == null ) { |
336 |
this.properties = new HashMap<>(); |
337 |
} |
338 |
this.properties.put(name, value);
|
339 |
} |
340 |
|
341 |
@Override
|
342 |
public String getProperty(String name) { |
343 |
if( this.properties == null ) { |
344 |
return null; |
345 |
} |
346 |
return this.properties.get(name); |
347 |
} |
348 |
|
349 |
@Override
|
350 |
public Map<String,String> getProperties() { |
351 |
return this.properties; |
352 |
} |
353 |
|
354 |
@Override
|
355 |
public boolean isASystemUnit() { |
356 |
String pathUnit;
|
357 |
try {
|
358 |
pathUnit = this.getParent().getFile().getCanonicalPath();
|
359 |
} catch (IOException ex) { |
360 |
pathUnit = this.getParent().getFile().getAbsolutePath();
|
361 |
} |
362 |
if( !pathUnit.endsWith(File.separator) ) { |
363 |
pathUnit = pathUnit + File.separator;
|
364 |
} |
365 |
List<File> libs = this.getManager().getLibFolders(); |
366 |
for( File lib : libs ) { |
367 |
String pathLib;
|
368 |
try {
|
369 |
pathLib = lib.getCanonicalPath(); |
370 |
} catch (IOException ex) { |
371 |
pathLib = lib.getAbsolutePath(); |
372 |
} |
373 |
if( !pathLib.endsWith(File.separator) ) { |
374 |
pathLib = pathLib + File.separator;
|
375 |
} |
376 |
if( pathUnit.startsWith(pathLib) ) {
|
377 |
return true; |
378 |
} |
379 |
} |
380 |
return false; |
381 |
} |
382 |
|
383 |
} |