svn-gvsig-desktop / branches / v2_0_0_prep / libraries / libFMap_data / src / org / gvsig / data / DataManager.java @ 21670
History | View | Annotate | Download (8.89 KB)
1 |
package org.gvsig.data; |
---|---|
2 |
|
3 |
import java.util.ArrayList; |
4 |
import java.util.Iterator; |
5 |
import java.util.List; |
6 |
|
7 |
import org.gvsig.data.operation.DataStoreOperation; |
8 |
import org.gvsig.data.operation.DataStoreOperationContext; |
9 |
import org.gvsig.data.operation.DataStoreOperationException; |
10 |
import org.gvsig.data.operation.DataStoreOperationNotSupportedException; |
11 |
import org.gvsig.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.data.datastores.vectorial.driver.shp.Register.selfRegister();
|
34 |
// org.gvsig.data.datastores.vectorial.driver.Register.selfRegister();
|
35 |
|
36 |
// org.gvsig.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 |
} |
137 |
} |
138 |
|
139 |
public DataExplorer createDataExplorer(DataExplorerParameters parameters) throws InitializeException { |
140 |
RegisterInfo registerInfo = this.getRegisterByName(parameters.getDataExplorerName());
|
141 |
if (registerInfo == null){ |
142 |
throw new InitializeException("Not registered in manager",parameters.getDataExplorerName()); |
143 |
} |
144 |
try {
|
145 |
DataExplorer dataSource= (DataExplorer)registerInfo.clazz.newInstance(); |
146 |
dataSource.init(parameters); |
147 |
return dataSource;
|
148 |
} catch (InstantiationException e) { |
149 |
throw new InitializeException(parameters.getDataExplorerName(),e); |
150 |
} catch (IllegalAccessException e) { |
151 |
throw new InitializeException(parameters.getDataExplorerName(),e); |
152 |
} |
153 |
} |
154 |
|
155 |
private RegisterInfo getRegisterByName(String name){ |
156 |
Iterator iterator=registers.iterator();
|
157 |
while (iterator.hasNext()) {
|
158 |
RegisterInfo registerInfo = (RegisterInfo) iterator.next(); |
159 |
if (name.equals(registerInfo.name)){
|
160 |
return registerInfo;
|
161 |
} |
162 |
} |
163 |
return null; |
164 |
} |
165 |
|
166 |
|
167 |
|
168 |
public int registerDataStoreOperation(String storeName, String operationName, DataStoreOperation operation) { |
169 |
if (operationName == null){ |
170 |
throw new IllegalArgumentException("operationName cannot be null."); |
171 |
} |
172 |
if (operation == null){ |
173 |
throw new IllegalArgumentException("operation cannot be null."); |
174 |
} |
175 |
|
176 |
RegisterInfo register = this.getRegisterByName(storeName);
|
177 |
if (register == null){ |
178 |
throw new IllegalArgumentException(storeName + " not registered"); |
179 |
} |
180 |
int index = registerGeometryOperationName(operationName);
|
181 |
|
182 |
register.registerOperation(index, operation); |
183 |
|
184 |
return index;
|
185 |
} |
186 |
|
187 |
private int registerGeometryOperationName(String operationName) { |
188 |
if (operationName == null) |
189 |
throw new IllegalArgumentException("operationName cannot be null."); |
190 |
|
191 |
int index = dataStoreOperations .indexOf(operationName);
|
192 |
if (index == -1) { |
193 |
dataStoreOperations.add(operationName); |
194 |
index = dataStoreOperations.indexOf(operationName); |
195 |
} |
196 |
return index;
|
197 |
} |
198 |
|
199 |
private int getOperationIndex(String operationName) { |
200 |
return dataStoreOperations .indexOf(operationName);
|
201 |
} |
202 |
|
203 |
public Object invokeDataStoreOperation(DataStore store,int code,DataStoreOperationContext context) throws DataStoreOperationException, DataStoreOperationNotSupportedException{ |
204 |
if (code < 0 || code >= dataStoreOperations.size()){ |
205 |
throw new DataStoreOperationNotSupportedException(code,"{not registered}",store.getName()); |
206 |
} |
207 |
RegisterInfo register = this.getRegisterByName(store.getName());
|
208 |
DataStoreOperation operation = register.getOperation(code); |
209 |
if (operation == null){ |
210 |
throw new DataStoreOperationNotSupportedException(code,(String)this.dataStoreOperations.get(code),store.getName()); |
211 |
} |
212 |
return operation.invoke(store, context);
|
213 |
|
214 |
} |
215 |
|
216 |
public Object invokeDataStoreOperation(DataStore store,String operationName,DataStoreOperationContext context) throws DataStoreOperationException, DataStoreOperationNotSupportedException{ |
217 |
return this.invokeDataStoreOperation(store, this.getOperationIndex(operationName), context); |
218 |
} |
219 |
|
220 |
private class RegisterInfo{ |
221 |
private String name; |
222 |
private Class clazz; |
223 |
private Class parametersClazz; |
224 |
private List operations = new ArrayList(); |
225 |
|
226 |
public RegisterInfo(String name, Class dsc, Class pc){ |
227 |
this.name=name;
|
228 |
this.clazz=dsc;
|
229 |
this.parametersClazz=pc;
|
230 |
} |
231 |
|
232 |
public boolean equals(Object obj) { |
233 |
if (obj instanceof RegisterInfo){ |
234 |
RegisterInfo ri = (RegisterInfo)obj; |
235 |
return ri.name.equals(this.name) && |
236 |
ri.clazz.equals(this.clazz) &&
|
237 |
ri.parametersClazz.equals(this.parametersClazz);
|
238 |
} |
239 |
return super.equals(obj); |
240 |
} |
241 |
|
242 |
public void registerOperation(int index, DataStoreOperation operation){ |
243 |
|
244 |
while (index > operations.size()) {
|
245 |
operations.add(null);
|
246 |
} |
247 |
|
248 |
if (index == operations.size()) {
|
249 |
operations.add(operation); |
250 |
} else {
|
251 |
operations.set(index, operation); |
252 |
} |
253 |
} |
254 |
|
255 |
public DataStoreOperation getOperation(int index){ |
256 |
try{
|
257 |
return (DataStoreOperation) this.operations.get(index); |
258 |
}catch (IndexOutOfBoundsException e) { |
259 |
return null; |
260 |
} |
261 |
} |
262 |
|
263 |
|
264 |
} |
265 |
|
266 |
public boolean implementsDataStoreOperation( |
267 |
String storeName, String operationName) { |
268 |
return this.implementsDataStoreOperation(storeName, this.getOperationIndex(operationName)); |
269 |
} |
270 |
|
271 |
public boolean implementsDataStoreOperation( |
272 |
String storeName, int operationCode) { |
273 |
RegisterInfo register = this.getRegisterByName(storeName);
|
274 |
DataStoreOperation operation = register.getOperation(operationCode); |
275 |
if (operation == null){ |
276 |
return false; |
277 |
} |
278 |
return true; |
279 |
} |
280 |
|
281 |
} |