Statistics
| Revision:

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 @ 1064

History | View | Annotate | Download (11 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.ArrayList;
8
import java.util.HashMap;
9
import java.util.HashSet;
10
import java.util.List;
11
import java.util.Map;
12
import java.util.Set;
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.apache.commons.lang3.StringUtils;
20
import org.gvsig.scripting.ScriptingFolder;
21
import org.gvsig.scripting.ScriptingManager;
22
import org.gvsig.scripting.ScriptingUnit;
23
import org.ini4j.Profile.Section;
24

    
25
public abstract class AbstractUnit implements ScriptingUnit, Unit {
26

    
27
    protected static final Logger logger = LoggerFactory.getLogger(AbstractUnit.class);
28

    
29
    protected DefaultScriptingManager manager;
30

    
31
    protected String id;
32
    protected String name = null;
33
    protected String description;
34
    protected String createdBy;
35
    protected String version;
36
    protected ScriptingFolder parent;
37
    protected String typename;
38
    protected Set<PropertyChangeListener> changeListener;
39
    protected boolean saved;
40
    private Map<String,String> properties;
41

    
42

    
43
    public AbstractUnit(ScriptingFolder parent, String typename, ScriptingManager manager, String id) {
44
        this.parent = parent;
45
        this.manager = (DefaultScriptingManager) manager;
46
        this.typename = typename;
47
        this.id = id;
48
        this.changeListener = null;
49
        this.properties = null;
50
    }
51

    
52
    @Override
53
    public void addPropertyChangeListener(PropertyChangeListener listener) {
54
        if( this.changeListener==null ) {
55
            this.changeListener = new HashSet<>();
56
        }
57
        this.changeListener.add(listener);
58
    }
59
    
60
    public void firePropertyChange(PropertyChangeEvent event) {
61
        if( this.changeListener==null ) {
62
            return;
63
        }
64
        for( PropertyChangeListener listener : this.changeListener ) {
65
            try {
66
                if( listener != null ) {
67
                    listener.propertyChange(event);
68
                }
69
            } catch(Exception ex) {
70
                logger.warn("Problems firing PropertyChangeListener to listener "+listener+".",ex);
71
            }
72
        }
73
    }
74
    
75
    public void firePropertyChangeListener(String name, Object oldValue, Object newValue) {
76
        PropertyChangeEvent event = new PropertyChangeEvent(this,name, oldValue, newValue);
77
        firePropertyChange(event);
78
    }
79
            
80
    @Override
81
    public String getTypeName() {
82
        return typename;
83
    }
84

    
85
    @Override
86
    public abstract void load(ScriptingFolder folder, String id);
87

    
88
    @Override
89
    public void reload() {
90
        this.load( this.getParent(), this.getId());
91
    }
92
    
93
    @Override
94
    public String toString() {
95
        if (this.getName() == null) {
96
            return "(" + this.getClass().getSimpleName() + ")";
97
        }
98
        return this.getName();
99
    }
100

    
101
    protected void setParent(ScriptingFolder parent) {
102
        this.parent = parent;
103
    }
104

    
105
    public ScriptingManager getManager() {
106
        return this.manager;
107
    }
108

    
109
    protected File getFileResource(String extension) {
110
        return new File(this.getParent().getFile(), this.id + extension).getAbsoluteFile();
111
    }
112

    
113
    @Override
114
    public String getDescription() {
115
        return this.description;
116
    }
117

    
118
    @Override
119
    public String getCreatedBy() {
120
        return this.createdBy;
121
    }
122

    
123
    @Override
124
    public String getVersion() {
125
        return this.version;
126
    }
127

    
128
    @Override
129
    public String getId() {
130
        return this.id;
131
    }
132

    
133
    @Override
134
    public String getName() {
135
        if (this.name == null) {
136
            return this.id;
137
        }
138
        return this.name;
139
    }
140

    
141
    @Override
142
    public void setDescription(String description) {
143
        firePropertyChangeListener("description", description, this.description);
144
        this.description = description;
145
    }
146

    
147
    @Override
148
    public void setCreatedBy(String createdBy) {
149
        firePropertyChangeListener("createdBy", createdBy, this.createdBy);
150
        this.createdBy = createdBy;
151
    }
152

    
153
    @Override
154
    public void setVersion(String version) {
155
        firePropertyChangeListener("version", version, this.version);
156
        this.version = version;
157
    }
158

    
159
    @Override
160
    public void setId(String id) {
161
        firePropertyChangeListener("id", id, this.id);
162
        this.id = FilenameUtils.getBaseName(id);
163
    }
164

    
165
   @Override
166
    public void setName(String name) {
167
        firePropertyChangeListener("name", name, this.name);
168
        this.name = name;
169
    }
170

    
171
    @Override
172
    public ScriptingFolder getParent() {
173
        return this.parent;
174
    }
175

    
176
    @Override
177
    public String getUserPath() {
178
        List<String> parts = new ArrayList<>();
179
        ScriptingUnit unit = this;
180
        while( unit!=null ) {
181
            parts.add(0, unit.getName());
182
            unit = unit.getParent();
183
        }
184
        return String.join("/", parts);
185
    }
186
    
187
    protected void save(Ini prefs) {
188
        prefs.put("Unit", "type", StringUtils.defaultString(this.getTypeName()));
189
        prefs.put("Unit", "name", this.getName());
190
        prefs.put("Unit", "description", StringUtils.defaultString(this.getDescription()));
191
        prefs.put("Unit", "createdBy", StringUtils.defaultString(this.getCreatedBy()));
192
        prefs.put("Unit", "version", StringUtils.defaultString(this.getVersion()));
193

    
194
        if( this.properties!=null ) {
195
            for (Map.Entry<String, String> property : properties.entrySet()) {
196
                String name = property.getKey();
197
                String value = property.getValue();
198
                prefs.put("properties", name, value);
199
            }
200
        }
201
        try {
202
            prefs.store();
203
        } catch (IOException e) {
204
            File f = prefs.getFile();
205
            String fname = (f == null) ? "(null)" : f.getAbsolutePath();
206
            String msg = "Can't save inf file '" + fname + "'.";
207
            logger.warn(msg);
208
            throw new RuntimeException(msg, e);
209
        }
210

    
211
    }
212

    
213
    protected void loadInf(Ini prefs) {
214
        String typename = getInfString(prefs, "Unit", "type", this.getTypeName());
215
        if (!this.getTypeName().equalsIgnoreCase(typename)) {
216
            File f = prefs.getFile();
217
            String fname = (f == null) ? "(null)" : f.getAbsolutePath();
218
            logger.warn("inconsistent type in inf file '" + fname + "'. Curent type '" + this.getTypeName() + "', type from inf file '" + typename + "'.");
219
        }
220
        this.setName(getInfString(prefs, "Unit", "name", this.getName()));
221
        this.setDescription(getInfString(prefs, "Unit", "description", null));
222
        this.setCreatedBy(getInfString(prefs, "Unit", "createdBy", null));
223
        this.setVersion(getInfString(prefs, "Unit", "version", null));
224
        
225
        Section sec = prefs.get("properties");
226
        if( sec != null ) {
227
            for(Map.Entry<String,String> entry : sec.entrySet() ) {
228
                this.setProperty(entry.getKey(), entry.getValue());
229
            }
230
        }
231
    }
232

    
233
    protected Object getInfValue(Ini prefs, String section, String option, Object defaultValue) {
234
        Object r = prefs.get(section, option);
235
        if (r == null) {
236
            return defaultValue;
237
        } else {
238
            return r;
239
        }
240
    }
241

    
242
    protected String getInfString(Ini prefs, String section, String option, Object defaultValue) {
243
        String s = (String) getInfValue(prefs, section, option, defaultValue);
244
        if (s != null && s.trim().length() < 1) {
245
            return null;
246
        }
247
        return s;
248
    }
249

    
250
    protected int getInfInt(Ini prefs, String section, String option, int defaultValue) {
251
        String s = (String) getInfValue(prefs, section, option, String.valueOf(defaultValue));
252
        if (s != null && s.trim().length() < 1) {
253
            return defaultValue;
254
        }
255
        return Integer.parseInt(s);
256
    }
257

    
258
    protected boolean getInfBoolean(Ini prefs, String section, String option, boolean defaultValue) {
259
        String s = (String) prefs.get(section, option);
260
        if( s == null ) {
261
            return defaultValue;
262
        }
263
        return BooleanUtils.toBoolean(s);
264
    }
265

    
266
    protected void console_println(String s) {
267
        logger.info(s);
268
    }
269

    
270
    public void create(ScriptingFolder folder, String id) {
271
        this.setParent(folder);
272
        this.setId(id);
273

    
274
        File file = new File(folder.getFile(), id + ".inf");
275
        try {
276
            file.createNewFile();
277
        } catch (IOException e) {
278
            logger.warn("Can't create inf file in '" + file.getAbsolutePath() + "'.", e);
279
        }
280
    }
281

    
282
    @Override
283
    public File getFile() {
284
        if (this.getId() == null) {
285
            return null;
286
        }
287
        if (this.getParent() == null) {
288
            return null;
289
        }
290
        if (this.getParent().getFile() == null) {
291
            return null;
292
        }
293
        return new File(getParent().getFile(), this.getId() + ".inf");
294
    }
295

    
296
    @Override
297
    public boolean equals(Object obj) {
298
        if( !(obj instanceof Unit) ) {
299
            return false;
300
        }
301
        return this.hashCode() == obj.hashCode();
302
    }
303

    
304
    @Override
305
    public int hashCode() {
306
        File f = this.getFile();
307
        if( f!=null ) {
308
            return "#$FILE$#".hashCode() + f.getAbsolutePath().hashCode();
309
        }
310
        if( this.getId()!=null ) {
311
            return "#$ID$#".hashCode() + this.getId().hashCode();
312
        }
313
        return super.hashCode();
314
    }
315
    
316
    @Override
317
    public boolean isSaved() {
318
        return this.saved;
319
    }
320

    
321
    @Override
322
    public void setSaved(boolean saved) {
323
        this.saved = saved;
324
    }
325

    
326
    @Override
327
    public void setProperty(String name, String value) {
328
        if( this.properties == null ) {
329
            this.properties = new HashMap<>();
330
        }
331
        this.properties.put(name, value);
332
    }
333

    
334
    @Override
335
    public String getProperty(String name) {
336
        if( this.properties == null ) {
337
            return null;
338
        }
339
        return this.properties.get(name);
340
    }
341
    
342
    @Override
343
    public Map<String,String> getProperties() {
344
        return this.properties;
345
    }
346

    
347
    @Override
348
    public boolean isASystemUnit() {
349
        String pathUnit;
350
        try {
351
            pathUnit = this.getParent().getFile().getCanonicalPath();
352
        } catch (IOException ex) {
353
            pathUnit = this.getParent().getFile().getAbsolutePath();
354
        }
355
        if( !pathUnit.endsWith(File.separator) ) {
356
            pathUnit = pathUnit + File.separator;
357
        }
358
        List<File> libs = this.getManager().getLibFolders();
359
        for( File lib : libs ) {
360
            String pathLib;
361
            try {
362
                pathLib = lib.getCanonicalPath();
363
            } catch (IOException ex) {
364
                pathLib = lib.getAbsolutePath();
365
            }
366
            if( !pathLib.endsWith(File.separator) ) {
367
                pathLib = pathLib + File.separator;
368
            }
369
            if( pathUnit.startsWith(pathLib) ) {
370
                return true;
371
            }
372
        }
373
        return false;
374
    }
375
    
376
}