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

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
    public String getUserPath() {
177
        List<String> parts = new ArrayList<>();
178
        ScriptingUnit unit = this;
179
        while( unit!=null ) {
180
            parts.add(0, unit.getName());
181
            unit = unit.getParent();
182
        }
183
        return String.join("/", parts);
184
    }
185
    
186
    protected void save(Ini prefs) {
187
        prefs.put("Unit", "type", StringUtils.defaultString(this.getTypeName()));
188
        prefs.put("Unit", "name", this.getName());
189
        prefs.put("Unit", "description", StringUtils.defaultString(this.getDescription()));
190
        prefs.put("Unit", "createdBy", StringUtils.defaultString(this.getCreatedBy()));
191
        prefs.put("Unit", "version", StringUtils.defaultString(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
            String msg = "Can't save inf file '" + fname + "'.";
206
            logger.warn(msg);
207
            throw new RuntimeException(msg, e);
208
        }
209

    
210
    }
211

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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