org.vfny.geoserver.global.DataStoreInfo Maven / Gradle / Ivy
/* Copyright (c) 2001 - 2007 TOPP - www.openplans.org. All rights reserved.
* This code is licensed under the GPL 2.0 license, availible at the root
* application directory.
*/
package org.vfny.geoserver.global;
import org.geotools.data.DataStore;
import org.geotools.data.DataStoreFinder;
import org.vfny.geoserver.global.dto.DataStoreInfoDTO;
import org.vfny.geoserver.util.DataStoreUtils;
import java.io.File;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.Collections;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.NoSuchElementException;
/**
* This is the configuration iformation for one DataStore. This class can also
* generate real datastores.
*
* This class implements {@link org.geotools.catalog.Service} interface as a
* link to a catalog.
*
* @author Gabriel Rold?n
* @author dzwiers
* @author Justin Deoliveira
* @version $Id: DataStoreInfo.java 7646 2007-10-23 13:09:32Z aaime $
*/
public class DataStoreInfo extends GlobalLayerSupertype {
/** DataStoreInfo we are representing */
private DataStore dataStore = null;
/** ref to the parent class's collection */
private Data data;
private String id;
private String nameSpaceId;
private boolean enabled;
private String title;
private String _abstract;
private Map connectionParams;
/** Storage for metadata */
private Map meta;
/**
* Directory associated with this DataStore.
*
*
* This directory may be used for File based relative paths.
*
*/
File baseDir;
/**
* URL associated with this DataStore.
*
*
* This directory may be used for URL based relative paths.
*
*/
URL baseURL;
/**
* DataStoreInfo constructor.
*
*
* Stores the specified data for later use.
*
*
* @param config DataStoreInfoDTO the current configuration to use.
* @param data Data a ref to use later to look up related informtion
*/
public DataStoreInfo(DataStoreInfoDTO config, Data data) {
this.data = data;
meta = new HashMap();
connectionParams = config.getConnectionParams();
enabled = config.isEnabled();
id = config.getId();
nameSpaceId = config.getNameSpaceId();
title = config.getTitle();
_abstract = config.getAbstract();
}
/**
* toDTO purpose.
*
*
* This method is package visible only, and returns a reference to the
* GeoServerDTO. This method is unsafe, and should only be used with
* extreme caution.
*
*
* @return DataStoreInfoDTO the generated object
*/
Object toDTO() {
DataStoreInfoDTO dto = new DataStoreInfoDTO();
dto.setAbstract(_abstract);
dto.setConnectionParams(connectionParams);
dto.setEnabled(enabled);
dto.setId(id);
dto.setNameSpaceId(nameSpaceId);
dto.setTitle(title);
return dto;
}
/**
* getId purpose.
*
*
* Returns the dataStore's id.
*
*
* @return String the id.
*/
public String getId() {
return id;
}
protected Map getParams() {
Map params = new HashMap(connectionParams);
params.put("namespace", getNameSpace().getURI());
return getParams(params, data.getBaseDir().toString());
}
/**
* Get Connect params.
*
*
* This is used to smooth any relative path kind of issues for any file
* URLS. This code should be expanded to deal with any other context
* sensitve isses dataStores tend to have.
*
*
* @return DOCUMENT ME!
*
* @task REVISIT: cache these?
*/
public static Map getParams(Map m, String baseDir) {
Map params = Collections.synchronizedMap(new HashMap(m));
for (Iterator i = params.entrySet().iterator(); i.hasNext();) {
Map.Entry entry = (Map.Entry) i.next();
String key = (String) entry.getKey();
Object value = entry.getValue();
try {
//TODO: this code is a pretty big hack, using the name to
// determine if the key is a url, could be named something else
// and still be a url
if ((key != null) && key.matches(".* *url") && value instanceof String) {
String path = (String) value;
LOGGER.finer("in string url");
if (path.startsWith("file:")) {
File fixedPath = GeoserverDataDirectory.findDataFile(path);
entry.setValue(fixedPath.toURL().toExternalForm());
}
} else if (value instanceof URL && ((URL) value).getProtocol().equals("file")) {
File fixedPath = GeoserverDataDirectory.findDataFile((URL) value);
entry.setValue(fixedPath.toURL());
}
} catch (MalformedURLException ignore) {
// ignore attempt to fix relative paths
}
}
return params;
}
/**
* By now just uses DataStoreFinder to find a new instance of a
* DataStoreInfo capable of process connectionParams
. In the
* future we can see if it is better to cache or pool DataStores for
* performance, but definitely we shouldn't maintain a single
* DataStoreInfo as instance variable for synchronizing reassons
*
*
* JG: Umm we actually require a single DataStoreInfo for for locking &
* transaction support to work. DataStoreInfo is expected to be thread
* aware (that is why it has Transaction Support).
*
*
* @return DataStore
*
* @throws IllegalStateException if this DataStoreInfo is disabled by
* configuration
* @throws NoSuchElementException if no DataStoreInfo is found
*/
public synchronized DataStore getDataStore()
throws IllegalStateException, NoSuchElementException {
if (!isEnabled()) {
throw new IllegalStateException(
"this datastore is not enabled, check your configuration");
}
if (dataStore == null) {
Map m = getParams();
try {
dataStore = DataStoreUtils.getDataStore(m);
LOGGER.fine("connection established by " + toString());
} catch (Throwable ex) {
throw new IllegalStateException("can't create the datastore " + getId() + ": "
+ ex.getClass().getName() + ": " + ex.getMessage() + "\n" + ex.toString());
}
if (dataStore == null) {
// If datastore is not present, then disable it
// (although no change in config).
enabled = false;
LOGGER.fine("failed to establish connection with " + toString());
throw new NoSuchElementException("No datastore found capable of managing "
+ toString());
}
}
return dataStore;
}
/**
* getTitle purpose.
*
*
* Returns the dataStore's title.
*
*
* @return String the title.
*/
public String getTitle() {
return title;
}
/**
* getAbstract purpose.
*
*
* Returns the dataStore's abstract.
*
*
* @return String the abstract.
*/
public String getAbstract() {
return _abstract;
}
/**
* isEnabled purpose.
*
*
* Returns true when the data store is enabled.
*
*
* @return true when the data store is enabled.
*/
public boolean isEnabled() {
return enabled;
}
/**
* getNameSpace purpose.
*
*
* Returns the namespace for this datastore.
*
*
* @return NameSpaceInfo the namespace for this datastore.
*/
public NameSpaceInfo getNameSpace() {
return (NameSpaceInfo) data.getNameSpace(getNamesSpacePrefix());
}
/**
* Access namespace id
*
* @return DOCUMENT ME!
*/
public String getNamesSpacePrefix() {
return nameSpaceId;
}
/**
* Implement toString.
*
* @return String
*
* @see java.lang.Object#toString()
*/
public String toString() {
return new StringBuffer("DataStoreConfig[namespace=").append(getNameSpace().getPrefix())
.append(", enabled=")
.append(isEnabled())
.append(", abstract=")
.append(getAbstract())
.append(", connection parameters=")
.append(getParams()).append("]")
.toString();
}
/**
* Implement containsMetaData.
*
* @param key
*
* @return
*
* @see org.geotools.data.MetaData#containsMetaData(java.lang.String)
*/
public boolean containsMetaData(String key) {
return meta.containsKey(key);
}
/**
* Implement putMetaData.
*
* @param key
* @param value
*
* @see org.geotools.data.MetaData#putMetaData(java.lang.String,
* java.lang.Object)
*/
public void putMetaData(String key, Object value) {
meta.put(key, value);
}
/**
* Implement getMetaData.
*
* @param key
*
* @return
*
* @see org.geotools.data.MetaData#getMetaData(java.lang.String)
*/
public Object getMetaData(String key) {
return meta.get(key);
}
public void dispose() {
if(dataStore != null)
dataStore.dispose();
}
}