root / branches / v2_0_0_prep / libraries / libFMap_data / src / org / gvsig / fmap / data / DataManager.java @ 22129
History | View | Annotate | Download (9.26 KB)
1 |
package org.gvsig.fmap.data; |
---|---|
2 |
|
3 |
import java.util.ArrayList; |
4 |
import java.util.Iterator; |
5 |
import java.util.List; |
6 |
|
7 |
import org.gvsig.fmap.data.operation.DataStoreOperation; |
8 |
import org.gvsig.fmap.data.operation.DataStoreOperationContext; |
9 |
import org.gvsig.fmap.data.operation.DataStoreOperationException; |
10 |
import org.gvsig.fmap.data.operation.DataStoreOperationNotSupportedException; |
11 |
import org.gvsig.fmap.data.vectorial.AbstractFeatureStore; |
12 |
|
13 |
|
14 |
public class DataManager { |
15 |
private static ArrayList registers = new ArrayList(); |
16 |
|
17 |
private static DataManager manager = null; |
18 |
|
19 |
public static DataManager getManager() { |
20 |
if( manager == null ) { |
21 |
manager = new DataManager();
|
22 |
} |
23 |
return manager;
|
24 |
} |
25 |
|
26 |
public static ResourceManager getResourceManager() { |
27 |
return ResourceManager.getResourceManager();
|
28 |
} |
29 |
|
30 |
private List dataStoreOperations = new ArrayList(); |
31 |
|
32 |
public DataManager() {
|
33 |
// org.gvsig.fmap.data.datastores.vectorial.driver.shp.Register.selfRegister();
|
34 |
// org.gvsig.fmap.data.datastores.vectorial.driver.Register.selfRegister();
|
35 |
|
36 |
// org.gvsig.fmap.data.datastores.gml.Register.selfRegister();
|
37 |
} |
38 |
|
39 |
public void registerGeometryHandler(String dataStoreID, Class geometryHandlerClass) { |
40 |
|
41 |
} |
42 |
|
43 |
public void registerDataStore(String name, Class dataStoreClass, Class parametersClass) { |
44 |
RegisterInfo register=new RegisterInfo(name,dataStoreClass,parametersClass);
|
45 |
if (registers.contains(register)){
|
46 |
return;
|
47 |
} |
48 |
registers.add(register); |
49 |
} |
50 |
/**
|
51 |
* Levanta una instancia de los parametros solicitados
|
52 |
* inicializa el nombre en la instancia
|
53 |
* y la devuelve.
|
54 |
* @throws InitializeException TODO
|
55 |
**/
|
56 |
public DataStoreParameters createDataStoreParameters(String name) throws InitializeException{ |
57 |
|
58 |
RegisterInfo registerInfo = this.getRegisterByName(name);
|
59 |
if (registerInfo == null){ |
60 |
throw new InitializeException("Not registered in manager",name); |
61 |
} |
62 |
|
63 |
try {
|
64 |
return (DataStoreParameters)registerInfo.parametersClazz.newInstance();
|
65 |
} catch (InstantiationException e) { |
66 |
throw new InitializeException(name,e); |
67 |
} catch (IllegalAccessException e) { |
68 |
throw new InitializeException(name,e); |
69 |
} |
70 |
} |
71 |
/**
|
72 |
* Levanta la instancia del datasource,
|
73 |
* levanta una instancia del driver que precisa el datasource
|
74 |
* y por ultimo invoca al metodo init del datasource.
|
75 |
* @throws InitializeException
|
76 |
**/
|
77 |
public DataStore createDataStore(DataStoreParameters parameters) throws InitializeException { |
78 |
String name = parameters.getDataStoreName();
|
79 |
|
80 |
RegisterInfo registerInfo = this.getRegisterByName(name);
|
81 |
if (registerInfo == null){ |
82 |
throw new InitializeException("Not registered in manager",name); |
83 |
} |
84 |
|
85 |
try {
|
86 |
DataStore dataStore= (DataStore)registerInfo.clazz.newInstance(); |
87 |
dataStore.init(parameters); |
88 |
return dataStore;
|
89 |
} catch (InstantiationException e) { |
90 |
throw new InitializeException(name,e); |
91 |
} catch (IllegalAccessException e) { |
92 |
throw new InitializeException(name,e); |
93 |
} |
94 |
} |
95 |
|
96 |
|
97 |
/* Como conjunto de propiedades genericas a un tipo de DataStore
|
98 |
* a las que se puede acceder sin tener que crear un DataStore/Driver.
|
99 |
*
|
100 |
* Por ejemplo para "DriverDataSource.shp" podria tener el
|
101 |
* tamaƱo de identificador de campo.
|
102 |
*
|
103 |
* En "DriverDataSource.postgres" podria aportar informacion sobre
|
104 |
* las tablas disponibles o cosas asi. Hay que ver que precisa
|
105 |
* GeoDB y de donde lo puede obtener.
|
106 |
*
|
107 |
* Hay que pensarlo bien.
|
108 |
*
|
109 |
*/
|
110 |
|
111 |
public void registerDataExplorer(String name, Class dataSourceClass, Class parametersClass) { |
112 |
RegisterInfo register=new RegisterInfo(name,dataSourceClass,parametersClass);
|
113 |
if (registers.contains(register)){
|
114 |
return;
|
115 |
} |
116 |
registers.add(register); |
117 |
} |
118 |
/**
|
119 |
* Levanta una instancia de los parametros solicitados
|
120 |
* inicializa el nombre en la instancia
|
121 |
* y la devuelve.
|
122 |
* @throws InitializeException TODO
|
123 |
**/
|
124 |
public DataExplorerParameters createDataExplorerParameters(String name) throws InitializeException { |
125 |
RegisterInfo registerInfo = this.getRegisterByName(name);
|
126 |
if (registerInfo == null){ |
127 |
throw new InitializeException("Not registered in manager",name); |
128 |
} |
129 |
|
130 |
try {
|
131 |
return (DataExplorerParameters)registerInfo.parametersClazz.newInstance();
|
132 |
} catch (InstantiationException e) { |
133 |
throw new InitializeException(name,e); |
134 |
} catch (IllegalAccessException e) { |
135 |
throw new InitializeException(name,e); |
136 |
} catch (Exception e) { |
137 |
// TODO: de momento para los class en el caso de que no exista el explorer.
|
138 |
throw new InitializeException(name, e); |
139 |
} |
140 |
} |
141 |
|
142 |
public DataExplorer createDataExplorer(DataExplorerParameters parameters) throws InitializeException { |
143 |
RegisterInfo registerInfo = this.getRegisterByName(parameters.getDataExplorerName());
|
144 |
if (registerInfo == null){ |
145 |
throw new InitializeException("Not registered in manager",parameters.getDataExplorerName()); |
146 |
} |
147 |
try {
|
148 |
DataExplorer dataSource= (DataExplorer)registerInfo.clazz.newInstance(); |
149 |
dataSource.init(parameters); |
150 |
return dataSource;
|
151 |
} catch (InstantiationException e) { |
152 |
throw new InitializeException(parameters.getDataExplorerName(),e); |
153 |
} catch (IllegalAccessException e) { |
154 |
throw new InitializeException(parameters.getDataExplorerName(),e); |
155 |
} |
156 |
} |
157 |
|
158 |
private RegisterInfo getRegisterByName(String name){ |
159 |
Iterator iterator=registers.iterator();
|
160 |
while (iterator.hasNext()) {
|
161 |
RegisterInfo registerInfo = (RegisterInfo) iterator.next(); |
162 |
if (name.equals(registerInfo.name)){
|
163 |
return registerInfo;
|
164 |
} |
165 |
} |
166 |
return null; |
167 |
} |
168 |
|
169 |
|
170 |
|
171 |
public int registerDataStoreOperation(String storeName, String operationName, DataStoreOperation operation) { |
172 |
if (operationName == null){ |
173 |
throw new IllegalArgumentException("operationName cannot be null."); |
174 |
} |
175 |
if (operation == null){ |
176 |
throw new IllegalArgumentException("operation cannot be null."); |
177 |
} |
178 |
|
179 |
RegisterInfo register = this.getRegisterByName(storeName);
|
180 |
if (register == null){ |
181 |
throw new IllegalArgumentException(storeName + " not registered"); |
182 |
} |
183 |
int index = registerGeometryOperationName(operationName);
|
184 |
|
185 |
register.registerOperation(index, operation); |
186 |
|
187 |
return index;
|
188 |
} |
189 |
|
190 |
private int registerGeometryOperationName(String operationName) { |
191 |
if (operationName == null) |
192 |
throw new IllegalArgumentException("operationName cannot be null."); |
193 |
|
194 |
int index = dataStoreOperations .indexOf(operationName);
|
195 |
if (index == -1) { |
196 |
dataStoreOperations.add(operationName); |
197 |
index = dataStoreOperations.indexOf(operationName); |
198 |
} |
199 |
return index;
|
200 |
} |
201 |
|
202 |
private int getOperationIndex(String operationName) { |
203 |
return dataStoreOperations .indexOf(operationName);
|
204 |
} |
205 |
|
206 |
public Object invokeDataStoreOperation(DataStore store,int code,DataStoreOperationContext context) throws DataStoreOperationException, DataStoreOperationNotSupportedException{ |
207 |
if (code < 0 || code >= dataStoreOperations.size()){ |
208 |
throw new DataStoreOperationNotSupportedException(code,"{not registered}",store.getName()); |
209 |
} |
210 |
RegisterInfo register = this.getRegisterByName(store.getName());
|
211 |
DataStoreOperation operation = register.getOperation(code); |
212 |
if (operation == null){ |
213 |
throw new DataStoreOperationNotSupportedException(code,(String)this.dataStoreOperations.get(code),store.getName()); |
214 |
} |
215 |
return operation.invoke(store, context);
|
216 |
|
217 |
} |
218 |
|
219 |
public Object invokeDataStoreOperation(DataStore store,String operationName,DataStoreOperationContext context) throws DataStoreOperationException, DataStoreOperationNotSupportedException{ |
220 |
return this.invokeDataStoreOperation(store, this.getOperationIndex(operationName), context); |
221 |
} |
222 |
|
223 |
private class RegisterInfo{ |
224 |
private String name; |
225 |
private Class clazz; |
226 |
private Class parametersClazz; |
227 |
private List operations = new ArrayList(); |
228 |
|
229 |
public RegisterInfo(String name, Class dsc, Class pc){ |
230 |
this.name=name;
|
231 |
this.clazz=dsc;
|
232 |
this.parametersClazz=pc;
|
233 |
} |
234 |
|
235 |
public boolean equals(Object obj) { |
236 |
if (obj instanceof RegisterInfo){ |
237 |
RegisterInfo ri = (RegisterInfo)obj; |
238 |
return ri.name.equals(this.name) && |
239 |
ri.clazz.equals(this.clazz) &&
|
240 |
ri.parametersClazz.equals(this.parametersClazz);
|
241 |
} |
242 |
return super.equals(obj); |
243 |
} |
244 |
|
245 |
public void registerOperation(int index, DataStoreOperation operation){ |
246 |
|
247 |
while (index > operations.size()) {
|
248 |
operations.add(null);
|
249 |
} |
250 |
|
251 |
if (index == operations.size()) {
|
252 |
operations.add(operation); |
253 |
} else {
|
254 |
operations.set(index, operation); |
255 |
} |
256 |
} |
257 |
|
258 |
public DataStoreOperation getOperation(int index){ |
259 |
try{
|
260 |
return (DataStoreOperation) this.operations.get(index); |
261 |
}catch (IndexOutOfBoundsException e) { |
262 |
return null; |
263 |
} |
264 |
} |
265 |
|
266 |
|
267 |
} |
268 |
|
269 |
public boolean implementsDataStoreOperation( |
270 |
String storeName, String operationName) { |
271 |
return this.implementsDataStoreOperation(storeName, this.getOperationIndex(operationName)); |
272 |
} |
273 |
|
274 |
public boolean implementsDataStoreOperation( |
275 |
String storeName, int operationCode) { |
276 |
RegisterInfo register = this.getRegisterByName(storeName);
|
277 |
DataStoreOperation operation = register.getOperation(operationCode); |
278 |
if (operation == null){ |
279 |
return false; |
280 |
} |
281 |
return true; |
282 |
} |
283 |
|
284 |
public String[] getRegisters() { |
285 |
String[] reg=new String[registers.size()]; |
286 |
for (int i = 0; i < registers.size(); i++) { |
287 |
reg[i]=((RegisterInfo)registers.get(i)).name; |
288 |
} |
289 |
return reg;
|
290 |
} |
291 |
|
292 |
} |