|
1 |
package org.gvsig.tools.util;
|
|
2 |
|
|
3 |
import java.util.HashMap;
|
|
4 |
import java.util.Map;
|
|
5 |
import org.gvsig.tools.ToolsLocator;
|
|
6 |
import org.gvsig.tools.dynobject.DynStruct;
|
|
7 |
import org.gvsig.tools.persistence.PersistenceManager;
|
|
8 |
import org.gvsig.tools.persistence.Persistent;
|
|
9 |
import org.gvsig.tools.persistence.PersistentState;
|
|
10 |
import org.gvsig.tools.persistence.exception.PersistenceException;
|
|
11 |
import org.slf4j.Logger;
|
|
12 |
import org.slf4j.LoggerFactory;
|
|
13 |
|
|
14 |
/**
|
|
15 |
* This class is intended to serve as an aid to the implementation
|
|
16 |
* of PropertiesSupport.
|
|
17 |
*
|
|
18 |
* When we have a class that implements the PropertiesSupport interface,
|
|
19 |
* we will use PropertiesSupportHelper to implement it. For this, we will
|
|
20 |
* declare a property "propertiesHelper", and we will implement the interface
|
|
21 |
* methods delegating in it.
|
|
22 |
*
|
|
23 |
* <code>
|
|
24 |
* private PropertiesSupportHelper propertiesHelper = new PropertiesSupportHelper();
|
|
25 |
*
|
|
26 |
* ...
|
|
27 |
*
|
|
28 |
* @Override
|
|
29 |
* public Object getProperty(String key) {
|
|
30 |
* return this.propertiesHelper.getProperty(key);
|
|
31 |
* }
|
|
32 |
*
|
|
33 |
* @Override
|
|
34 |
* public void setProperty(String key, Object obj) {
|
|
35 |
* this.propertiesHelper.setProperty(key, obj);
|
|
36 |
* }
|
|
37 |
*
|
|
38 |
* @Override
|
|
39 |
* public Map<String,Object> getProperties() {
|
|
40 |
* return this.propertiesHelper.getProperties();
|
|
41 |
* }
|
|
42 |
* </code>
|
|
43 |
* If the class is persistent, we will declare "propertiesHelper" as:
|
|
44 |
*
|
|
45 |
* <code>
|
|
46 |
* definition.addDynFieldObject("propertiesHelper")
|
|
47 |
* .setClassOfValue(PropertiesSupportHelper.class)
|
|
48 |
* .setMandatory(false);
|
|
49 |
* </code>
|
|
50 |
*
|
|
51 |
* We will add the following code to the "loadFromState" and "saveToState":
|
|
52 |
*
|
|
53 |
* <code>
|
|
54 |
* // in loadFromState
|
|
55 |
* this.propertiesHelper = (PropertiesSupportHelper) state.get("propertiesHelper");
|
|
56 |
*
|
|
57 |
* // in saveToState
|
|
58 |
* state.set("propertiesHelper",propertiesHelper);
|
|
59 |
* </code>
|
|
60 |
*
|
|
61 |
* And we will record the persistence invoking:
|
|
62 |
*
|
|
63 |
* <code>
|
|
64 |
* new PropertiesSupportHelper.RegisterPersistence().call()
|
|
65 |
* </code>
|
|
66 |
*
|
|
67 |
* Or using <code>Caller</code>:
|
|
68 |
*
|
|
69 |
* <code>
|
|
70 |
* caller.add(new new PropertiesSupportHelper.RegisterPersistence());
|
|
71 |
* ...
|
|
72 |
* caller.call()
|
|
73 |
* </code>
|
|
74 |
*
|
|
75 |
* @author gvSIG Team
|
|
76 |
*/
|
|
77 |
|
|
78 |
public class PropertiesSupportHelper implements PropertiesSupport, Persistent {
|
|
79 |
|
|
80 |
private static final Logger logger = LoggerFactory.getLogger(PropertiesSupportHelper.class);
|
|
81 |
|
|
82 |
private Map<String, Object> properties = new HashMap<>();
|
|
83 |
|
|
84 |
@Override
|
|
85 |
public Object getProperty(String key) {
|
|
86 |
return properties.get(key);
|
|
87 |
}
|
|
88 |
|
|
89 |
@Override
|
|
90 |
public void setProperty(String key, Object val) {
|
|
91 |
properties.put(key, val);
|
|
92 |
}
|
|
93 |
|
|
94 |
@Override
|
|
95 |
public Map<String, Object> getProperties() {
|
|
96 |
return properties;
|
|
97 |
}
|
|
98 |
|
|
99 |
public void setProperties(Map<String,Object> properties) {
|
|
100 |
this.properties = new HashMap();
|
|
101 |
if (properties != null) {
|
|
102 |
this.copyFrom(properties);
|
|
103 |
}
|
|
104 |
}
|
|
105 |
|
|
106 |
private void copyFrom(Map<String, Object> properties) {
|
|
107 |
for (Map.Entry<String, Object> entry : properties.entrySet()) {
|
|
108 |
String key = entry.getKey();
|
|
109 |
if (key == null) {
|
|
110 |
continue;
|
|
111 |
}
|
|
112 |
Object value;
|
|
113 |
try {
|
|
114 |
value = entry.getValue();
|
|
115 |
} catch (Throwable th) {
|
|
116 |
if (logger.isDebugEnabled()) {
|
|
117 |
logger.warn("Can't get property " + key + ".", th);
|
|
118 |
} else {
|
|
119 |
logger.warn("Can't get property " + key + ".");
|
|
120 |
}
|
|
121 |
value = null;
|
|
122 |
}
|
|
123 |
this.properties.put(key, value);
|
|
124 |
}
|
|
125 |
}
|
|
126 |
|
|
127 |
@Override
|
|
128 |
public void saveToState(PersistentState state) throws PersistenceException {
|
|
129 |
state.set("properties", properties);
|
|
130 |
}
|
|
131 |
|
|
132 |
@Override
|
|
133 |
public void loadFromState(PersistentState state) throws PersistenceException {
|
|
134 |
this.properties = new HashMap();
|
|
135 |
Map props = (Map) state.get("properties");
|
|
136 |
if (props != null) {
|
|
137 |
copyFrom(props);
|
|
138 |
}
|
|
139 |
}
|
|
140 |
|
|
141 |
public static class RegisterPersistence implements Callable {
|
|
142 |
|
|
143 |
@Override
|
|
144 |
public Object call() {
|
|
145 |
PersistenceManager manager = ToolsLocator.getPersistenceManager();
|
|
146 |
if( manager.getDefinition("PropertiesSupportHelper")==null ) {
|
|
147 |
DynStruct definition = manager.addDefinition(
|
|
148 |
PropertiesSupportHelper.class,
|
|
149 |
"PropertiesSupportHelper",
|
|
150 |
"PropertiesSupportHelper Persistence definition",
|
|
151 |
null,
|
|
152 |
null
|
|
153 |
);
|
|
154 |
definition.addDynFieldMap("properties")
|
|
155 |
.setClassOfItems(Object.class)
|
|
156 |
.setMandatory(true);
|
|
157 |
}
|
|
158 |
return Boolean.TRUE;
|
|
159 |
}
|
|
160 |
}
|
|
161 |
|
|
162 |
}
|