svn-gvsig-desktop / trunk / org.gvsig.desktop / org.gvsig.desktop.compat.cdc / org.gvsig.fmap.dal / org.gvsig.fmap.dal.db / org.gvsig.fmap.dal.db.jdbc / src / main / java / org / gvsig / fmap / dal / store / jdbc2 / spi / JDBCServerExplorerBase.java @ 46277
History | View | Annotate | Download (39.2 KB)
1 |
/**
|
---|---|
2 |
* gvSIG. Desktop Geographic Information System.
|
3 |
*
|
4 |
* Copyright (C) 2007-2020 gvSIG Association.
|
5 |
*
|
6 |
* This program is free software; you can redistribute it and/or
|
7 |
* modify it under the terms of the GNU General Public License
|
8 |
* as published by the Free Software Foundation; either version 3
|
9 |
* of the License, or (at your option) any later version.
|
10 |
*
|
11 |
* This program is distributed in the hope that it will be useful,
|
12 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
13 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
14 |
* GNU General Public License for more details.
|
15 |
*
|
16 |
* You should have received a copy of the GNU General Public License
|
17 |
* along with this program; if not, write to the Free Software
|
18 |
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
|
19 |
* MA 02110-1301, USA.
|
20 |
*
|
21 |
* For any additional information, do not hesitate to contact us
|
22 |
* at info AT gvsig.com, or visit our website www.gvsig.com.
|
23 |
*/
|
24 |
package org.gvsig.fmap.dal.store.jdbc2.spi; |
25 |
|
26 |
import org.gvsig.tools.resourcesstorage.CompoundResourcesStorage; |
27 |
import java.io.File; |
28 |
import java.util.ArrayList; |
29 |
import java.util.Arrays; |
30 |
import java.util.Collections; |
31 |
import java.util.HashMap; |
32 |
import java.util.List; |
33 |
import java.util.Map; |
34 |
import javax.json.JsonObject; |
35 |
import javax.json.JsonString; |
36 |
import javax.json.JsonValue; |
37 |
import org.apache.commons.codec.binary.Hex; |
38 |
import org.apache.commons.collections.map.LRUMap; |
39 |
import org.apache.commons.lang3.BooleanUtils; |
40 |
import org.apache.commons.lang3.ObjectUtils; |
41 |
import org.apache.commons.lang3.StringUtils; |
42 |
import org.apache.commons.lang3.tuple.ImmutablePair; |
43 |
import org.apache.commons.lang3.tuple.Pair; |
44 |
import org.gvsig.expressionevaluator.ExpressionBuilder; |
45 |
import org.gvsig.expressionevaluator.ExpressionUtils; |
46 |
import org.gvsig.fmap.dal.DALLocator; |
47 |
import org.gvsig.fmap.dal.DataManager; |
48 |
import org.gvsig.fmap.dal.DataStore; |
49 |
import org.gvsig.fmap.dal.DataStoreParameters; |
50 |
import org.gvsig.fmap.dal.DatabaseWorkspaceManager; |
51 |
import static org.gvsig.fmap.dal.DatabaseWorkspaceManager.FIELD_CONFIGURATION_NAME; |
52 |
import static org.gvsig.fmap.dal.DatabaseWorkspaceManager.FIELD_CONFIGURATION_VALUE; |
53 |
import static org.gvsig.fmap.dal.DatabaseWorkspaceManager.TABLE_CONFIGURATION_NAME; |
54 |
import static org.gvsig.fmap.dal.DatabaseWorkspaceManager.TABLE_RESOURCES_NAME; |
55 |
import org.gvsig.fmap.dal.NewDataStoreParameters; |
56 |
import org.gvsig.fmap.dal.SQLBuilder; |
57 |
import org.gvsig.fmap.dal.SQLBuilder.Privilege; |
58 |
import org.gvsig.fmap.dal.exception.CloseException; |
59 |
import org.gvsig.fmap.dal.exception.DataException; |
60 |
import org.gvsig.fmap.dal.exception.InitializeException; |
61 |
import org.gvsig.fmap.dal.exception.OpenException; |
62 |
import org.gvsig.fmap.dal.exception.RemoveException; |
63 |
import org.gvsig.fmap.dal.feature.EditableFeature; |
64 |
import org.gvsig.fmap.dal.feature.EditableFeatureType; |
65 |
import org.gvsig.fmap.dal.feature.Feature; |
66 |
import org.gvsig.fmap.dal.feature.FeatureStore; |
67 |
import org.gvsig.fmap.dal.feature.FeatureType; |
68 |
import org.gvsig.fmap.dal.feature.NewFeatureStoreParameters; |
69 |
import org.gvsig.fmap.dal.resource.spi.ResourceProvider; |
70 |
import org.gvsig.fmap.dal.serverexplorer.db.spi.AbstractDBServerExplorer; |
71 |
import org.gvsig.fmap.dal.serverexplorer.filesystem.FilesystemStoreParameters; |
72 |
import org.gvsig.fmap.dal.spi.DataManagerProviderServices; |
73 |
import org.gvsig.fmap.dal.spi.DataServerExplorerProviderServices; |
74 |
import org.gvsig.fmap.dal.store.jdbc.JDBCConnectionParameters; |
75 |
import org.gvsig.fmap.dal.store.jdbc.JDBCNewStoreParameters; |
76 |
import org.gvsig.fmap.dal.store.jdbc.JDBCServerExplorerParameters; |
77 |
import org.gvsig.fmap.dal.store.jdbc.JDBCStoreParameters; |
78 |
import org.gvsig.fmap.dal.store.jdbc.exception.JDBCExecuteSQLException; |
79 |
import org.gvsig.fmap.dal.store.jdbc2.JDBCHelper; |
80 |
import org.gvsig.fmap.dal.store.jdbc2.JDBCServerExplorer; |
81 |
import org.gvsig.fmap.dal.store.jdbc2.OperationsFactory; |
82 |
import org.gvsig.fmap.dal.store.jdbc2.spi.operations.CanCreateTablesOperation; |
83 |
import org.gvsig.fmap.dal.store.jdbc2.spi.operations.CreateTableOperation; |
84 |
import org.gvsig.fmap.dal.store.jdbc2.spi.operations.DropTableOperation; |
85 |
import org.gvsig.fmap.dal.store.jdbc2.spi.operations.ExecuteOperation; |
86 |
import org.gvsig.fmap.dal.store.jdbc2.spi.operations.FetchFeatureTypeOperation; |
87 |
import org.gvsig.fmap.dal.store.jdbc2.spi.operations.ListTablesOperation; |
88 |
import org.gvsig.fmap.dal.store.jdbc2.spi.operations.UpdateTableStatisticsOperation; |
89 |
import org.gvsig.json.Json; |
90 |
import org.gvsig.json.JsonObjectBuilder; |
91 |
import org.gvsig.tools.dispose.DisposeUtils; |
92 |
import org.gvsig.tools.exception.BaseException; |
93 |
import org.gvsig.tools.resourcesstorage.EmptyResourcesStorage; |
94 |
import org.gvsig.tools.resourcesstorage.ResourcesStorage; |
95 |
import org.gvsig.tools.util.CachedValue; |
96 |
import org.slf4j.Logger; |
97 |
import org.slf4j.LoggerFactory; |
98 |
|
99 |
@SuppressWarnings("UseSpecificCatch") |
100 |
public class JDBCServerExplorerBase extends AbstractDBServerExplorer implements JDBCServerExplorer { |
101 |
|
102 |
private static final Logger LOG = LoggerFactory.getLogger(JDBCServerExplorerBase.class); |
103 |
private static final String CONFIG_NAME_CUSTOM_RESOURCES = "CUSTOM_RESOURCES"; |
104 |
|
105 |
protected JDBCHelper helper = null; |
106 |
|
107 |
private Boolean canAdd; |
108 |
|
109 |
private static final Map<String, CachedValue<List<JDBCStoreParameters>>> CACHED_TABLES = Collections.synchronizedMap(new LRUMap(10)); |
110 |
private static final Map<String, CachedValue<CustomResourcesConfig>> CACHED_CUSTOM_RESOURCES_CONFIG = Collections.synchronizedMap(new LRUMap(10)); |
111 |
|
112 |
private static class CustomResourcesConfig { |
113 |
|
114 |
private static final String CUSTOM_RESOURCES_CACHETIME = "CacheTime"; |
115 |
private static final String CUSTOM_RESOURCES_MAPPING = "Mapping"; |
116 |
private static final String CUSTOM_RESOURCES_READONLY = "ResourcesReadonly"; |
117 |
|
118 |
|
119 |
private int cacheTime; |
120 |
private Map<String, String> mapping; |
121 |
private Map<String,Boolean> resourcesReadonly; |
122 |
|
123 |
public CustomResourcesConfig(String jsonConfig) { |
124 |
this.cacheTime = 30*60*1000; // 30min |
125 |
this.mapping = new HashMap<>(); |
126 |
this.resourcesReadonly = new HashMap<>(); |
127 |
|
128 |
if (StringUtils.isNotBlank(jsonConfig)) {
|
129 |
try {
|
130 |
JsonObject config = Json.createObject(jsonConfig); |
131 |
this.cacheTime = config.getInt(CUSTOM_RESOURCES_CACHETIME, this.cacheTime); |
132 |
if (config.containsKey(CUSTOM_RESOURCES_MAPPING)) {
|
133 |
JsonObject m = config.getJsonObject(CUSTOM_RESOURCES_MAPPING); |
134 |
for (Map.Entry<String, JsonValue> entry : m.entrySet()) { |
135 |
String key = entry.getKey();
|
136 |
JsonValue value = entry.getValue(); |
137 |
if (value instanceof JsonString) { |
138 |
this.mapping.put(key, ((JsonString) value).getString());
|
139 |
} |
140 |
} |
141 |
} |
142 |
if (config.containsKey(CUSTOM_RESOURCES_READONLY)) {
|
143 |
JsonObject resreadonly = config.getJsonObject(CUSTOM_RESOURCES_READONLY); |
144 |
for (Map.Entry<String, JsonValue> entry : resreadonly.entrySet()) { |
145 |
String key = entry.getKey();
|
146 |
JsonValue value = entry.getValue(); |
147 |
if (value == JsonValue.TRUE) {
|
148 |
this.resourcesReadonly.put(key, true); |
149 |
} else if (value == JsonValue.FALSE) { |
150 |
this.resourcesReadonly.put(key, false); |
151 |
} |
152 |
} |
153 |
} |
154 |
} catch (Exception ex) { |
155 |
LOG.debug("Can't parse json from "+CONFIG_NAME_CUSTOM_RESOURCES+" variable", ex); |
156 |
// Do nothing.
|
157 |
} |
158 |
} |
159 |
} |
160 |
|
161 |
public String toJsonString() { |
162 |
JsonObjectBuilder builder = Json.createObjectBuilder(); |
163 |
builder.add(CUSTOM_RESOURCES_CACHETIME, this.cacheTime);
|
164 |
JsonObjectBuilder m = Json.createObjectBuilder(); |
165 |
for (Map.Entry<String, String> entry : this.mapping.entrySet()) { |
166 |
String key = entry.getKey();
|
167 |
String value = entry.getValue();
|
168 |
m.add(key, value); |
169 |
} |
170 |
builder.add(CUSTOM_RESOURCES_MAPPING, m); |
171 |
JsonObjectBuilder resreadonly = Json.createObjectBuilder(); |
172 |
for (Map.Entry<String, Boolean> entry : this.resourcesReadonly.entrySet()) { |
173 |
String key = entry.getKey();
|
174 |
Boolean readonly = entry.getValue();
|
175 |
resreadonly.add(key,readonly); |
176 |
} |
177 |
builder.add(CUSTOM_RESOURCES_READONLY, resreadonly); |
178 |
return builder.build().toString();
|
179 |
} |
180 |
|
181 |
public int getCacheExpireTimeInMillis() { |
182 |
return cacheTime;
|
183 |
} |
184 |
|
185 |
public String getResourcesTablename(String tablename) { |
186 |
String resourceTablename = mapping.get(tablename);
|
187 |
// LOG.info("Resource of table "+tablename+" = "+resourceTablename);
|
188 |
return resourceTablename;
|
189 |
} |
190 |
|
191 |
public void addResourceMapping(String tablename, String resourcesTablename) { |
192 |
this.mapping.put(tablename, resourcesTablename);
|
193 |
} |
194 |
|
195 |
private boolean isInternalTable(String storeName) { |
196 |
if (DatabaseWorkspaceManager.isInternalTable(storeName)) {
|
197 |
return true; |
198 |
} |
199 |
for (String resourcesTablename : this.mapping.values()) { |
200 |
if( StringUtils.equals(storeName, resourcesTablename) ) {
|
201 |
return true; |
202 |
} |
203 |
} |
204 |
return false; |
205 |
} |
206 |
|
207 |
public void setResourcesReadOnly(String resourcesTableName, boolean readonly) { |
208 |
this.resourcesReadonly.put(resourcesTableName,readonly);
|
209 |
} |
210 |
|
211 |
public boolean isResourcesReadOnly(String resourcesTableName) { |
212 |
return this.resourcesReadonly.getOrDefault(resourcesTableName,false); |
213 |
} |
214 |
} |
215 |
|
216 |
private static class CachedCustomResourcesConfig extends CachedValue<CustomResourcesConfig> { |
217 |
|
218 |
private final JDBCStoreParameters openParameters; |
219 |
|
220 |
private CachedCustomResourcesConfig(JDBCStoreParameters openParameters) {
|
221 |
this.openParameters = openParameters;
|
222 |
this.setExpireTime(30*60*1000); // 30min |
223 |
} |
224 |
|
225 |
@Override
|
226 |
protected void reload() { |
227 |
String jsonConfig = getConfigValue(this.openParameters, CONFIG_NAME_CUSTOM_RESOURCES); |
228 |
CustomResourcesConfig config = new CustomResourcesConfig(jsonConfig);
|
229 |
this.setExpireTime(config.getCacheExpireTimeInMillis());
|
230 |
this.setValue(config);
|
231 |
} |
232 |
|
233 |
} |
234 |
|
235 |
private static class CachedTablesValue extends CachedValue<List<JDBCStoreParameters>> { |
236 |
|
237 |
private final int mode; |
238 |
private final JDBCServerExplorerParameters serverParameters; |
239 |
private final boolean informationTables; |
240 |
private JDBCHelper helper;
|
241 |
private final int tablesOrViews; |
242 |
|
243 |
public CachedTablesValue(JDBCHelper helper, int mode, JDBCServerExplorerParameters serverParameters, boolean informationTables, int tablesOrViews) { |
244 |
this.mode = mode;
|
245 |
this.serverParameters = serverParameters;
|
246 |
this.informationTables = informationTables;
|
247 |
this.helper = helper;
|
248 |
this.tablesOrViews = tablesOrViews;
|
249 |
} |
250 |
|
251 |
public CachedTablesValue(JDBCHelper helper, int mode, JDBCServerExplorerParameters serverParameters, boolean informationTables, long expireTime, int tablesOrViews) { |
252 |
this.mode = mode;
|
253 |
this.setExpireTime(expireTime);
|
254 |
this.serverParameters = serverParameters;
|
255 |
this.informationTables = informationTables;
|
256 |
this.helper = helper;
|
257 |
this.tablesOrViews = tablesOrViews;
|
258 |
} |
259 |
|
260 |
@Override
|
261 |
protected void reload() { |
262 |
List<JDBCStoreParameters> tables = null; |
263 |
if(helper == null){ |
264 |
this.setValue(tables);
|
265 |
return;
|
266 |
} |
267 |
OperationsFactory operations = helper.getOperations(); |
268 |
if (operations == null) { |
269 |
this.setValue(null); |
270 |
LOG.debug("Sets tables to null to force reload tables from new ServerExplorar.");
|
271 |
return;
|
272 |
} |
273 |
try {
|
274 |
ListTablesOperation listTables = operations.createListTables( |
275 |
this.mode, serverParameters, informationTables, tablesOrViews
|
276 |
); |
277 |
tables = (List<JDBCStoreParameters>) listTables.perform();
|
278 |
} catch (Exception ex) { |
279 |
LOG.debug("Can't reload cached list of tables.", ex);
|
280 |
} |
281 |
this.setValue(tables);
|
282 |
} |
283 |
|
284 |
public JDBCHelper getHelper(){
|
285 |
return this.helper; |
286 |
} |
287 |
|
288 |
public void dispose() { |
289 |
this.helper = null; |
290 |
} |
291 |
} |
292 |
|
293 |
public JDBCServerExplorerBase(
|
294 |
JDBCServerExplorerParameters parameters, |
295 |
DataServerExplorerProviderServices services, |
296 |
JDBCHelper helper |
297 |
) throws InitializeException {
|
298 |
super(parameters, services);
|
299 |
this.helper = helper;
|
300 |
} |
301 |
|
302 |
@Override
|
303 |
public String getProviderName() { |
304 |
return this.getHelper().getProviderName(); |
305 |
} |
306 |
|
307 |
@Override
|
308 |
public String getStoreName() { |
309 |
return this.getHelper().getProviderName(); |
310 |
} |
311 |
|
312 |
protected DataManagerProviderServices getManager() {
|
313 |
return (DataManagerProviderServices) DALLocator.getDataManager();
|
314 |
} |
315 |
|
316 |
@Override
|
317 |
public JDBCServerExplorerParameters getParameters() {
|
318 |
return (JDBCServerExplorerParameters) super.getParameters(); |
319 |
} |
320 |
|
321 |
@Override
|
322 |
public boolean closeResourceRequested(ResourceProvider resource) { |
323 |
this.getHelper().getResulSetControler().pack();
|
324 |
return true; |
325 |
} |
326 |
|
327 |
@Override
|
328 |
public void resourceChanged(ResourceProvider resource) { |
329 |
// Nothing to do
|
330 |
} |
331 |
|
332 |
protected JDBCHelper getHelper() {
|
333 |
return helper;
|
334 |
} |
335 |
|
336 |
protected OperationsFactory getOperations() {
|
337 |
return this.getHelper().getOperations(); |
338 |
} |
339 |
|
340 |
@Override
|
341 |
public DataStore open(DataStoreParameters params) throws DataException { |
342 |
checkIsMine(params); |
343 |
DataStore store = super.open(params);
|
344 |
return store;
|
345 |
} |
346 |
|
347 |
@Override
|
348 |
public DataStore open(String tableName) throws DataException { |
349 |
JDBCStoreParameters params = this.get(tableName);
|
350 |
DataStore store = super.open(params);
|
351 |
return store;
|
352 |
} |
353 |
|
354 |
@Override
|
355 |
public List list(int mode) throws DataException { |
356 |
return list(mode, SHOW_TABLES_AND_VIEWS);
|
357 |
} |
358 |
|
359 |
@Override
|
360 |
public List list(int mode, int tablesOrViews) throws DataException { |
361 |
boolean informationTables = BooleanUtils.isTrue(
|
362 |
this.getParameters().getShowInformationDBTables()
|
363 |
); |
364 |
|
365 |
JDBCServerExplorerParameters serverParams = this.getParameters();
|
366 |
|
367 |
String key = buildKeyForCachedTables(mode, serverParams, informationTables, tablesOrViews);
|
368 |
CachedValue<List<JDBCStoreParameters>> tablesValue = CACHED_TABLES.get(key);
|
369 |
List<JDBCStoreParameters> tables = null; |
370 |
if (tablesValue != null) { |
371 |
tables = tablesValue.get(); |
372 |
} |
373 |
if (tables != null) { |
374 |
return tables;
|
375 |
} |
376 |
tablesValue = new CachedTablesValue(this.helper, mode, serverParams, informationTables, 60000, tablesOrViews); //60" |
377 |
CACHED_TABLES.put(key, tablesValue); |
378 |
tables = tablesValue.get(); |
379 |
return tables;
|
380 |
} |
381 |
|
382 |
public String buildKeyForCachedTables(int mode, JDBCServerExplorerParameters params, boolean informationTables, int tablesOrViews) { |
383 |
JDBCServerExplorerParameters clonedParams = (JDBCServerExplorerParameters) params.getCopy(); |
384 |
clonedParams.setSchema(null); //ListTableOperation no usa el schema |
385 |
|
386 |
StringBuilder builder = new StringBuilder(); |
387 |
builder.append(String.valueOf(mode));
|
388 |
builder.append(Hex.encodeHex(clonedParams.toByteArray())); |
389 |
builder.append(String.valueOf(informationTables));
|
390 |
builder.append(String.valueOf(tablesOrViews));
|
391 |
String key = builder.toString();
|
392 |
return key;
|
393 |
} |
394 |
|
395 |
@Override
|
396 |
public void remove(DataStoreParameters theParams) throws RemoveException { |
397 |
JDBCStoreParameters params = (JDBCStoreParameters) theParams; |
398 |
DropTableOperation removeTable = this.getOperations().createDropTable(
|
399 |
this.getOperations().createTableReference(params)
|
400 |
); |
401 |
if ((Boolean) removeTable.perform()) { |
402 |
this.dropCachedTables();
|
403 |
} |
404 |
} |
405 |
|
406 |
@Override
|
407 |
public JDBCStoreParameters getOpenParameters() throws DataException { |
408 |
JDBCStoreParameters params = this.helper.createOpenStoreParameters(this.getParameters()); |
409 |
return params;
|
410 |
} |
411 |
|
412 |
@Override
|
413 |
public NewDataStoreParameters getAddParameters(String storeName) |
414 |
throws DataException {
|
415 |
JDBCNewStoreParameters params = this.getAddParameters();
|
416 |
params.setTable(storeName); |
417 |
return params;
|
418 |
} |
419 |
|
420 |
@Override
|
421 |
public JDBCNewStoreParameters getAddParameters() throws DataException { |
422 |
JDBCServerExplorerParameters parameters = getParameters(); |
423 |
JDBCNewStoreParameters params = this.helper.createNewStoreParameters();
|
424 |
params.setHost(parameters.getHost()); |
425 |
params.setPort(parameters.getPort()); |
426 |
params.setDBName(parameters.getDBName()); |
427 |
params.setUser(parameters.getUser()); |
428 |
params.setPassword(parameters.getPassword()); |
429 |
params.setCatalog(parameters.getCatalog()); |
430 |
params.setSchema(parameters.getSchema()); |
431 |
params.setJDBCDriverClassName(parameters.getJDBCDriverClassName()); |
432 |
params.setUrl(parameters.getUrl()); |
433 |
if (parameters instanceof FilesystemStoreParameters) { |
434 |
File f = ((FilesystemStoreParameters) parameters).getFile();
|
435 |
((FilesystemStoreParameters) params).setFile(f); |
436 |
} |
437 |
|
438 |
params.setDefaultFeatureType(this.getServerExplorerProviderServices()
|
439 |
.createNewFeatureType()); |
440 |
|
441 |
return params;
|
442 |
} |
443 |
|
444 |
protected void checkIsMine(DataStoreParameters dsp) { |
445 |
if (!(dsp instanceof JDBCConnectionParameters)) { |
446 |
throw new IllegalArgumentException( |
447 |
"not instance of FilesystemStoreParameters");
|
448 |
} |
449 |
JDBCServerExplorerParameters parameters = getParameters(); |
450 |
|
451 |
JDBCConnectionParameters pgp = (JDBCConnectionParameters) dsp; |
452 |
if (!StringUtils.equals(pgp.getHost(), parameters.getHost())) {
|
453 |
throw new IllegalArgumentException("wrong explorer: Host (mine: " |
454 |
+ parameters.getHost() + " other:" + pgp.getHost() + ")"); |
455 |
} |
456 |
if (!ObjectUtils.equals(pgp.getPort(), parameters.getPort())) {
|
457 |
throw new IllegalArgumentException("wrong explorer: Port (mine: " |
458 |
+ parameters.getPort() + " other:" + pgp.getPort() + ")"); |
459 |
} |
460 |
if (!StringUtils.equals(pgp.getDBName(), parameters.getDBName())) {
|
461 |
throw new IllegalArgumentException("wrong explorer: DBName (mine: " |
462 |
+ parameters.getDBName() + " other:" + pgp.getDBName()
|
463 |
+ ")");
|
464 |
} |
465 |
if (!StringUtils.isEmpty(parameters.getCatalog())) {
|
466 |
if (!StringUtils.equals(pgp.getCatalog(), parameters.getCatalog())) {
|
467 |
throw new IllegalArgumentException( |
468 |
"wrong explorer: Catalog (mine: "
|
469 |
+ parameters.getCatalog() + " other:"
|
470 |
+ pgp.getCatalog() + ")");
|
471 |
} |
472 |
} |
473 |
if (!StringUtils.isEmpty(parameters.getSchema())) {
|
474 |
if (!StringUtils.equals(pgp.getSchema(), parameters.getSchema())) {
|
475 |
throw new IllegalArgumentException( |
476 |
"wrong explorer: Schema (mine: "
|
477 |
+ parameters.getSchema() + " other:"
|
478 |
+ pgp.getSchema() + ")");
|
479 |
} |
480 |
} |
481 |
} |
482 |
|
483 |
@Override
|
484 |
public void open() throws OpenException { |
485 |
|
486 |
} |
487 |
|
488 |
@Override
|
489 |
public void close() throws CloseException { |
490 |
|
491 |
} |
492 |
|
493 |
@Override
|
494 |
protected void doDispose() throws BaseException { |
495 |
synchronized (CACHED_TABLES) {
|
496 |
List<String> toRemove = new ArrayList<>(); |
497 |
for (Map.Entry<String, CachedValue<List<JDBCStoreParameters>>> entry : CACHED_TABLES.entrySet()) { |
498 |
CachedTablesValue value = (CachedTablesValue) entry.getValue(); |
499 |
if (value.getHelper() == this.helper) { |
500 |
toRemove.add(entry.getKey()); |
501 |
value.dispose(); |
502 |
} |
503 |
} |
504 |
for (String key : toRemove) { |
505 |
CACHED_TABLES.remove(key); |
506 |
} |
507 |
helper.dispose(); |
508 |
helper = null;
|
509 |
} |
510 |
} |
511 |
|
512 |
@Override
|
513 |
public boolean canAdd() { |
514 |
if (this.canAdd == null) { |
515 |
CanCreateTablesOperation canAdd_ = this.getOperations().createCanCreateTables();
|
516 |
this.canAdd = (Boolean) canAdd_.perform(); |
517 |
} |
518 |
return this.canAdd; |
519 |
} |
520 |
|
521 |
@Override
|
522 |
public FeatureType getFeatureType(DataStoreParameters theParams)
|
523 |
throws DataException {
|
524 |
|
525 |
JDBCStoreParameters params = (JDBCStoreParameters) theParams; |
526 |
|
527 |
checkIsMine(params); |
528 |
|
529 |
EditableFeatureType fetureType |
530 |
= this.getServerExplorerProviderServices().createNewFeatureType();
|
531 |
|
532 |
List<String> primaryKeys = null; |
533 |
if (params.getPkFields() != null) { |
534 |
primaryKeys = Arrays.asList(params.getPkFields());
|
535 |
} |
536 |
|
537 |
FetchFeatureTypeOperation fetch = this.getOperations().createFetchFeatureType(
|
538 |
fetureType, |
539 |
this.getOperations().createTableReference(params),
|
540 |
primaryKeys, |
541 |
params.getDefaultGeometryField(), |
542 |
params.getCRS() |
543 |
); |
544 |
fetch.perform(); |
545 |
return fetureType;
|
546 |
} |
547 |
|
548 |
@Override
|
549 |
public boolean add(String providerName, NewDataStoreParameters theParams, boolean overwrite) |
550 |
throws DataException {
|
551 |
|
552 |
List<Pair<String, Privilege>> userAndPrivileges = new ArrayList<>(); |
553 |
JDBCNewStoreParameters params = (JDBCNewStoreParameters) theParams; |
554 |
if (!StringUtils.isEmpty(params.getAllRole())) {
|
555 |
userAndPrivileges.add( |
556 |
new ImmutablePair<>(params.getAllRole(), Privilege.ALL)
|
557 |
); |
558 |
} |
559 |
if (!StringUtils.isEmpty(params.getDeleteRole())) {
|
560 |
userAndPrivileges.add( |
561 |
new ImmutablePair<>(params.getDeleteRole(), Privilege.DELETE)
|
562 |
); |
563 |
} |
564 |
if (!StringUtils.isEmpty(params.getInsertRole())) {
|
565 |
userAndPrivileges.add( |
566 |
new ImmutablePair<>(params.getInsertRole(), Privilege.INSERT)
|
567 |
); |
568 |
} |
569 |
if (!StringUtils.isEmpty(params.getReferenceRole())) {
|
570 |
userAndPrivileges.add( |
571 |
new ImmutablePair<>(params.getReferenceRole(), Privilege.REFERENCE)
|
572 |
); |
573 |
} |
574 |
if (!StringUtils.isEmpty(params.getSelectRole())) {
|
575 |
userAndPrivileges.add( |
576 |
new ImmutablePair<>(params.getSelectRole(), Privilege.SELECT)
|
577 |
); |
578 |
} |
579 |
if (!StringUtils.isEmpty(params.getTriggerRole())) {
|
580 |
userAndPrivileges.add( |
581 |
new ImmutablePair<>(params.getTriggerRole(), Privilege.TRIGGER)
|
582 |
); |
583 |
} |
584 |
if (!StringUtils.isEmpty(params.getTruncateRole())) {
|
585 |
userAndPrivileges.add( |
586 |
new ImmutablePair<>(params.getTruncateRole(), Privilege.TRUNCATE)
|
587 |
); |
588 |
} |
589 |
if (!StringUtils.isEmpty(params.getUpdateRole())) {
|
590 |
userAndPrivileges.add( |
591 |
new ImmutablePair<>(params.getUpdateRole(), Privilege.UPDATE)
|
592 |
); |
593 |
} |
594 |
List<String> additionalSQLs = new ArrayList<>(); |
595 |
if (!StringUtils.isEmpty(params.getPostCreatingStatement())) {
|
596 |
additionalSQLs.add(params.getPostCreatingStatement()); |
597 |
} |
598 |
CreateTableOperation createTable = this.getOperations().createTable(
|
599 |
this.getOperations().createTableReference(params),
|
600 |
params.getDefaultFeatureType(), |
601 |
userAndPrivileges, |
602 |
additionalSQLs |
603 |
); |
604 |
|
605 |
boolean isOk = (boolean) createTable.perform(); |
606 |
if (!isOk) {
|
607 |
return false; |
608 |
} |
609 |
|
610 |
// We collect the featureType of the operation because
|
611 |
// the provider has been able to make changes to it
|
612 |
params.setDefaultFeatureType(createTable.getType()); |
613 |
|
614 |
if (theParams instanceof NewFeatureStoreParameters) { |
615 |
DataManager dataManager = DALLocator.getDataManager(); |
616 |
ResourcesStorage resources = this.getResourcesStorage(theParams);
|
617 |
dataManager.writeDALResource(resources, ((NewFeatureStoreParameters) theParams).getDefaultFeatureType()); |
618 |
} |
619 |
this.dropCachedTables();
|
620 |
return true; |
621 |
} |
622 |
|
623 |
private void dropCachedTables() { |
624 |
boolean informationTables = BooleanUtils.isTrue(
|
625 |
this.getParameters().getShowInformationDBTables()
|
626 |
); |
627 |
synchronized(CACHED_TABLES){
|
628 |
CACHED_TABLES.remove(buildKeyForCachedTables(MODE_ALL, this.getParameters(), informationTables, SHOW_TABLES_AND_VIEWS));
|
629 |
CACHED_TABLES.remove(buildKeyForCachedTables(MODE_FEATURE, this.getParameters(), informationTables, SHOW_TABLES_AND_VIEWS));
|
630 |
CACHED_TABLES.remove(buildKeyForCachedTables(MODE_GEOMETRY, this.getParameters(), informationTables, SHOW_TABLES_AND_VIEWS));
|
631 |
CACHED_TABLES.remove(buildKeyForCachedTables(MODE_ALL, this.getParameters(), informationTables, SHOW_TABLES));
|
632 |
CACHED_TABLES.remove(buildKeyForCachedTables(MODE_FEATURE, this.getParameters(), informationTables, SHOW_TABLES));
|
633 |
CACHED_TABLES.remove(buildKeyForCachedTables(MODE_GEOMETRY, this.getParameters(), informationTables, SHOW_TABLES));
|
634 |
CACHED_TABLES.remove(buildKeyForCachedTables(MODE_ALL, this.getParameters(), informationTables, SHOW_VIEWS));
|
635 |
CACHED_TABLES.remove(buildKeyForCachedTables(MODE_FEATURE, this.getParameters(), informationTables, SHOW_VIEWS));
|
636 |
CACHED_TABLES.remove(buildKeyForCachedTables(MODE_GEOMETRY, this.getParameters(), informationTables, SHOW_VIEWS));
|
637 |
} |
638 |
} |
639 |
|
640 |
@Override
|
641 |
public List getDataStoreProviderNames() { |
642 |
List x = new ArrayList(1); |
643 |
x.add(this.getProviderName());
|
644 |
return x;
|
645 |
} |
646 |
|
647 |
@Override
|
648 |
public void updateTableStatistics(String database, String schema, String table) throws JDBCExecuteSQLException { |
649 |
UpdateTableStatisticsOperation updateStatistics = this.getOperations().createUpdateTableStatistics(
|
650 |
this.getOperations().createTableReference(database, schema, table, null) |
651 |
); |
652 |
updateStatistics.perform(); |
653 |
} |
654 |
|
655 |
@Override
|
656 |
public Object execute(String sql) { |
657 |
ExecuteOperation execute = this.getOperations().createExecute(sql);
|
658 |
return execute.perform();
|
659 |
} |
660 |
|
661 |
@Override
|
662 |
public JDBCStoreParameters get(String name) throws DataException { |
663 |
JDBCStoreParameters params = this.getOpenParameters();
|
664 |
params.setTable(name); |
665 |
return params;
|
666 |
} |
667 |
|
668 |
@Override
|
669 |
public SQLBuilder createSQLBuilder() {
|
670 |
JDBCSQLBuilderBase builder = this.getHelper().createSQLBuilder();
|
671 |
return builder;
|
672 |
} |
673 |
|
674 |
private CustomResourcesConfig getCustomResourcesConfig() {
|
675 |
JDBCServerExplorerParameters serverParams = this.getParameters();
|
676 |
String key = buildKeyForCachedTables(MODE_ALL, serverParams, false, SHOW_TABLES_AND_VIEWS); //??? |
677 |
CachedValue<CustomResourcesConfig> cachedConfig = CACHED_CUSTOM_RESOURCES_CONFIG.get(key); |
678 |
if (cachedConfig != null) { |
679 |
CustomResourcesConfig config = cachedConfig.get(); |
680 |
// LOG.info("Return custom resource from CACHE.");
|
681 |
return config;
|
682 |
} |
683 |
JDBCStoreParameters params = this.helper.createOpenStoreParameters(this.getParameters()); |
684 |
cachedConfig = new CachedCustomResourcesConfig(params);
|
685 |
CACHED_CUSTOM_RESOURCES_CONFIG.put(key, cachedConfig); |
686 |
return cachedConfig.get();
|
687 |
} |
688 |
|
689 |
private void refreshCustomResourcesConfig() { |
690 |
JDBCServerExplorerParameters serverParams = this.getParameters();
|
691 |
String key = buildKeyForCachedTables(MODE_ALL, serverParams, false, SHOW_TABLES_AND_VIEWS); //??? |
692 |
CachedValue<CustomResourcesConfig> cachedConfig = CACHED_CUSTOM_RESOURCES_CONFIG.get(key); |
693 |
if (cachedConfig != null) { |
694 |
cachedConfig.expired(); |
695 |
} |
696 |
} |
697 |
|
698 |
private ResourcesStorage getResourcesStorage(DataStoreParameters parameters, String storeName) { |
699 |
if (DatabaseWorkspaceManager.isInternalTable(storeName)) {
|
700 |
EmptyResourcesStorage resourcesStorage = new EmptyResourcesStorage();
|
701 |
return resourcesStorage;
|
702 |
} |
703 |
String resourcesTablename = null; |
704 |
try {
|
705 |
ResourcesStorage alternateResourcesStorage = null;
|
706 |
DataManager dataManager = DALLocator.getDataManager(); |
707 |
DatabaseWorkspaceManager workspace = dataManager.getDatabaseWorkspace(parameters); |
708 |
if (workspace != null) { |
709 |
alternateResourcesStorage = workspace.getAlternativeResourcesStorage(storeName); |
710 |
} |
711 |
|
712 |
ResourcesStorage defaultResourcesStorage = null;
|
713 |
ResourcesStorage customResourcesStorage = null;
|
714 |
|
715 |
// TODO: Habria que ver de localizar los parametros sin tener que hacer un list.
|
716 |
resourcesTablename = TABLE_RESOURCES_NAME; |
717 |
List<JDBCStoreParameters> tables = this.list(); |
718 |
for (JDBCStoreParameters params : tables) {
|
719 |
String theTableName = params.getTable();
|
720 |
if (StringUtils.equals(theTableName, resourcesTablename)) {
|
721 |
defaultResourcesStorage = new JDBCResourcesStorage(
|
722 |
this.helper,
|
723 |
alternateResourcesStorage, |
724 |
params, |
725 |
storeName |
726 |
); |
727 |
break;
|
728 |
} |
729 |
} |
730 |
CustomResourcesConfig config = getCustomResourcesConfig(); |
731 |
if( config!=null ) { |
732 |
if( config.isInternalTable(storeName) ) {
|
733 |
defaultResourcesStorage = new EmptyResourcesStorage();
|
734 |
} else {
|
735 |
resourcesTablename = config.getResourcesTablename(storeName); |
736 |
if( StringUtils.isNotBlank(resourcesTablename) ) {
|
737 |
JDBCStoreParameters params = this.getOpenParameters();
|
738 |
params.setTable(resourcesTablename); |
739 |
customResourcesStorage = new JDBCResourcesStorage(
|
740 |
this.helper,
|
741 |
alternateResourcesStorage, |
742 |
params, |
743 |
storeName, |
744 |
config.isResourcesReadOnly(resourcesTablename) |
745 |
); |
746 |
} |
747 |
} |
748 |
} |
749 |
return new CompoundResourcesStorage(defaultResourcesStorage, customResourcesStorage); |
750 |
} catch (Throwable ex) { |
751 |
LOG.warn("Can't retrieve reources storage for table '" + storeName + "' in '" + this.getParameters().getUrl() + " ("+resourcesTablename+").", ex); |
752 |
} |
753 |
EmptyResourcesStorage theResourcesStorage = new EmptyResourcesStorage();
|
754 |
return theResourcesStorage;
|
755 |
} |
756 |
|
757 |
@Override
|
758 |
public ResourcesStorage getResourcesStorage() {
|
759 |
JDBCStoreParameters params; |
760 |
try {
|
761 |
params = this.getOpenParameters();
|
762 |
params.setTable(DatabaseWorkspaceManager.TABLE_RESOURCES_NAME); |
763 |
JDBCResourcesStorage theResourcesStorage = new JDBCResourcesStorage(
|
764 |
this.helper,
|
765 |
null,
|
766 |
params, |
767 |
"$ServerExplorer"
|
768 |
); |
769 |
return theResourcesStorage;
|
770 |
} catch (DataException ex) {
|
771 |
return null; |
772 |
} |
773 |
} |
774 |
|
775 |
@Override
|
776 |
public ResourcesStorage getResourcesStorage(DataStoreParameters parameters) {
|
777 |
if (parameters == null) { |
778 |
throw new IllegalArgumentException("null is a valid value for parameters."); |
779 |
} |
780 |
String tableName;
|
781 |
if (parameters instanceof JDBCNewStoreParameters) { |
782 |
tableName = ((JDBCNewStoreParameters) parameters).getTable(); |
783 |
} else if (parameters instanceof JDBCStoreParameters) { |
784 |
tableName = ((JDBCStoreParameters) parameters).getTable(); |
785 |
} else {
|
786 |
throw new IllegalArgumentException("Required a JDBCStoreParameters or JDBCNewStoreParameters parameters, received " + parameters.getClass().getName() + "."); |
787 |
} |
788 |
return this.getResourcesStorage(parameters, tableName); |
789 |
} |
790 |
|
791 |
@Override
|
792 |
public ResourcesStorage getResourcesStorage(DataStore dataStore) {
|
793 |
return this.getResourcesStorage( |
794 |
(JDBCStoreParameters) dataStore.getParameters(), |
795 |
dataStore.getName() |
796 |
); |
797 |
} |
798 |
|
799 |
@Override
|
800 |
public boolean exists(DataStoreParameters parameters) throws DataException { |
801 |
JDBCStoreParameters params = (JDBCStoreParameters) parameters; |
802 |
JDBCSQLBuilderBase builder = this.getHelper().createSQLBuilder();
|
803 |
SQLBuilder.TableNameBuilder searchTable = builder.createTableNameBuilder() |
804 |
.database(params.getDBName()) |
805 |
.schema(params.getSchema()) |
806 |
.name(params.getTable()); |
807 |
SQLBuilder.TableNameBuilder table = builder.createTableNameBuilder(); |
808 |
|
809 |
List<JDBCStoreParameters> l = this.list(); |
810 |
for (JDBCStoreParameters current : l) {
|
811 |
table.database(current.getDBName()) |
812 |
.schema(current.getSchema()) |
813 |
.name(current.getTable()); |
814 |
if (table.equals(searchTable)) {
|
815 |
return true; |
816 |
} |
817 |
} |
818 |
return false; |
819 |
} |
820 |
|
821 |
@Override
|
822 |
public void setCustomResources(String tableName, String resourcesTableName) { |
823 |
this.setCustomResources(tableName, resourcesTableName, false); |
824 |
} |
825 |
|
826 |
@Override
|
827 |
public void setCustomResources(String tableName, String resourcesTableName, boolean readonly) { |
828 |
CustomResourcesConfig config = getCustomResourcesConfig(); |
829 |
if( config==null ) { |
830 |
throw new RuntimeException("Can't retrieve alternative resources configuration"); |
831 |
} |
832 |
config.addResourceMapping(tableName, resourcesTableName); |
833 |
config.setResourcesReadOnly(resourcesTableName,readonly); |
834 |
JDBCStoreParameters params = this.helper.createOpenStoreParameters(this.getParameters()); |
835 |
if(!setConfigValue(params, CONFIG_NAME_CUSTOM_RESOURCES, config.toJsonString())) {
|
836 |
throw new RuntimeException("Can't save custom resources configuration"); |
837 |
} |
838 |
refreshCustomResourcesConfig(); |
839 |
|
840 |
|
841 |
if (StringUtils.isNotBlank(resourcesTableName)) {
|
842 |
try {
|
843 |
params = this.getOpenParameters();
|
844 |
params.setTable(resourcesTableName); |
845 |
JDBCResourcesStorage resourcesStorage = new JDBCResourcesStorage(
|
846 |
this.helper,
|
847 |
null,
|
848 |
params, |
849 |
tableName, |
850 |
config.isResourcesReadOnly(resourcesTableName) |
851 |
); |
852 |
resourcesStorage.clearCache(); |
853 |
} catch (Exception ex) { |
854 |
LOG.debug("Can't remove local cache for table "+tableName, ex);
|
855 |
} |
856 |
} |
857 |
|
858 |
|
859 |
} |
860 |
|
861 |
private static boolean setConfigValue(JDBCStoreParameters params, String name, String value) { |
862 |
FeatureStore store = null;
|
863 |
try {
|
864 |
DataManager dataManager = DALLocator.getDataManager(); |
865 |
params.setTable(TABLE_CONFIGURATION_NAME); |
866 |
store = (FeatureStore) dataManager.openStore( |
867 |
params.getProviderName(), |
868 |
params |
869 |
); |
870 |
} catch (Exception ex) { |
871 |
LOG.trace("Can't read configuration value '"+name+"'", ex); |
872 |
// Do noting
|
873 |
// } finally {
|
874 |
// DisposeUtils.disposeQuietly(store);
|
875 |
} |
876 |
if( store == null ) { |
877 |
return false; |
878 |
} |
879 |
try {
|
880 |
store.edit(); |
881 |
ExpressionBuilder builder = ExpressionUtils.createExpressionBuilder(); |
882 |
String filter = builder.eq(
|
883 |
builder.column(FIELD_CONFIGURATION_NAME), |
884 |
builder.constant(name) |
885 |
).toString(); |
886 |
Feature feature = store.findFirst(filter); |
887 |
EditableFeature efeature; |
888 |
if (feature == null) { |
889 |
efeature = store.createNewFeature(); |
890 |
efeature.set(FIELD_CONFIGURATION_NAME, name); |
891 |
efeature.set(FIELD_CONFIGURATION_VALUE, value); |
892 |
store.insert(efeature); |
893 |
} else {
|
894 |
efeature = feature.getEditable(); |
895 |
efeature.set(FIELD_CONFIGURATION_VALUE, value); |
896 |
store.update(efeature); |
897 |
} |
898 |
store.finishEditing(); |
899 |
return true; |
900 |
} catch (Exception ex) { |
901 |
LOG.debug("Can't write configuration value for '"+name+"'", ex); |
902 |
return false; |
903 |
} finally {
|
904 |
DisposeUtils.disposeQuietly(store); |
905 |
} |
906 |
} |
907 |
|
908 |
private static String getConfigValue(JDBCStoreParameters params, String name) { |
909 |
FeatureStore store = null;
|
910 |
try {
|
911 |
DataManager dataManager = DALLocator.getDataManager(); |
912 |
params.setTable(TABLE_CONFIGURATION_NAME); |
913 |
store = (FeatureStore) dataManager.openStore( |
914 |
params.getProviderName(), |
915 |
params |
916 |
); |
917 |
} catch (Exception ex) { |
918 |
LOG.trace("Can't read configuration value '"+name+"'", ex); |
919 |
// Do noting
|
920 |
// } finally {
|
921 |
// DisposeUtils.disposeQuietly(store);
|
922 |
} |
923 |
if( store == null ) { |
924 |
return null; |
925 |
} |
926 |
try {
|
927 |
ExpressionBuilder builder = ExpressionUtils.createExpressionBuilder(); |
928 |
String filter = builder.eq(
|
929 |
builder.column(FIELD_CONFIGURATION_NAME), |
930 |
builder.constant(name) |
931 |
).toString(); |
932 |
Feature feature = store.findFirst(filter); |
933 |
if( feature == null ) { |
934 |
return null; |
935 |
} |
936 |
String value = feature.getString(FIELD_CONFIGURATION_VALUE);
|
937 |
return value;
|
938 |
|
939 |
} catch (Exception ex) { |
940 |
LOG.debug("Can't read configuration value '"+name+"'", ex); |
941 |
return null; |
942 |
} finally {
|
943 |
DisposeUtils.disposeQuietly(store); |
944 |
} |
945 |
} |
946 |
|
947 |
@Override
|
948 |
public boolean exists() { |
949 |
try {
|
950 |
JDBCServerExplorerParameters serverParameters = this.getParameters();
|
951 |
OperationsFactory operations = helper.getOperations(); |
952 |
ListTablesOperation listTables = operations.createListTables( |
953 |
MODE_ALL, serverParameters, false, SHOW_TABLES
|
954 |
); |
955 |
List<JDBCStoreParameters> tables = (List<JDBCStoreParameters>) listTables.perform(); |
956 |
return true; |
957 |
} catch(Throwable th) { |
958 |
return false; |
959 |
} |
960 |
} |
961 |
|
962 |
@Override
|
963 |
public String getConnectionProviderStatus() { |
964 |
return this.helper.getConnectionProviderStatus(); |
965 |
} |
966 |
|
967 |
|
968 |
} |