All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.vfny.geoserver.global.DataStoreInfo Maven / Gradle / Ivy

The newest version!
/* 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 java.io.File;
import java.io.IOException;
import java.io.Serializable;
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;

import org.geoserver.catalog.Catalog;
import org.geoserver.catalog.NamespaceInfo;
import org.geotools.data.DataStore;
import org.vfny.geoserver.global.dto.DataStoreInfoDTO;


/**
 * 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 9165 2008-06-06 20:41:13Z jdeolive $ * * @deprecated use {@link org.geoserver.catalog.DataStoreInfo} */ 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; org.geoserver.catalog.DataStoreInfo dataStore; Catalog catalog; ///** // * 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(); //} public DataStoreInfo(org.geoserver.catalog.DataStoreInfo dataStore, Catalog catalog ) { this.dataStore = dataStore; this.catalog = catalog; } public void load(DataStoreInfoDTO dto) { dataStore.getConnectionParameters().clear(); dataStore.getConnectionParameters().putAll( dto.getConnectionParams() ); dataStore.setEnabled( dto.isEnabled() ); dataStore.setName(dto.getId() ); dataStore.setWorkspace( catalog.getWorkspaceByName( dto.getNameSpaceId() )); dataStore.setDescription( dto.getTitle() ); } /** * 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(getAbstract()); dto.setConnectionParams(getParams()); dto.setEnabled(isEnabled()); dto.setId(getId()); dto.setNameSpaceId(getNamesSpacePrefix()); dto.setTitle(getTitle()); return dto; //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 dataStore.getName(); //return id; } protected Map getParams() { Map params = new HashMap(dataStore.getConnectionParameters()); params.put("namespace", getNameSpace().getURI()); return params; //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"); } try { return dataStore.getDataStore(null); } catch (IOException e) { throw new IllegalStateException( e ); } //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); // } // // 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 dataStore.getDescription(); //return title; } /** * getAbstract purpose. * *

* Returns the dataStore's abstract. *

* * @return String the abstract. */ public String getAbstract() { return dataStore.getDescription(); //return _abstract; } /** * isEnabled purpose. * *

* Returns true when the data store is enabled. *

* * @return true when the data store is enabled. */ public boolean isEnabled() { return dataStore.isEnabled(); //return enabled; } /** * getNameSpace purpose. * *

* Returns the namespace for this datastore. *

* * @return NameSpaceInfo the namespace for this datastore. */ public NameSpaceInfo getNameSpace() { NamespaceInfo ns =catalog.getNamespaceByPrefix( dataStore.getWorkspace().getName() ); return new NameSpaceInfo( ns, catalog ); //return (NameSpaceInfo) data.getNameSpace(getNamesSpacePrefix()); } /** * Access namespace id * * @return DOCUMENT ME! */ public String getNamesSpacePrefix() { return dataStore.getWorkspace().getName(); //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 dataStore.getMetadata().get( key ) != null; //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) { dataStore.getMetadata().put( key, (Serializable) 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 dataStore.getMetadata().get( key ); //return meta.get(key); } public void dispose() { DataStore ds = getDataStore(); if ( ds != null ) { ds.dispose(); } //if(dataStore != null) // dataStore.dispose(); } }




© 2015 - 2024 Weber Informatics LLC | Privacy Policy