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