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