001: package net.refractions.udig.catalog.memory;
002:
003: import java.io.IOException;
004: import java.io.Serializable;
005: import java.net.MalformedURLException;
006: import java.net.URL;
007: import java.util.Collections;
008: import java.util.HashMap;
009: import java.util.Map;
010:
011: import net.refractions.udig.catalog.CatalogPlugin;
012: import net.refractions.udig.catalog.IService;
013: import net.refractions.udig.catalog.ServiceExtension;
014: import net.refractions.udig.catalog.memory.internal.MemoryServiceImpl;
015:
016: import org.geotools.data.DataUtilities;
017: import org.geotools.data.memory.MemoryDataStore;
018: import org.geotools.util.MapEntry;
019:
020: public class MemoryServiceExtensionImpl implements ServiceExtension {
021: /** service creation key **/
022: public static final String KEY = "scratch"; //$NON-NLS-1$
023:
024: /** special memory service url **/
025: public final static URL URL;
026: static {
027: URL tmp;
028: try {
029: tmp = new URL("http://localhost/scratch"); //$NON-NLS-1$
030: } catch (MalformedURLException e) {
031: tmp = null;
032: e.printStackTrace();
033: }
034: URL = tmp;
035: }
036:
037: // contains a map of id->MemoryService implementation.
038: public static final Map<URL, MemoryServiceImpl> impl = Collections
039: .synchronizedMap(new HashMap<URL, MemoryServiceImpl>());
040:
041: /**
042: * This param indicates a FeatureType that needs to be created in the DataStore.
043: * <p>
044: * Since MemoryData store is volatile there needs to be a way to specify what FeatureTypes
045: * the DataStore has using the parameters.
046: * </p>
047: */
048: public static final String MEMBER_PARAM = "MEMBER_PARAM_KEY"; //$NON-NLS-1$
049:
050: private MemoryDSFactory factory;
051:
052: public MemoryServiceExtensionImpl() {
053: super ();
054: }
055:
056: /**
057: * This permits the use of a custom sub-class of an ActiveMemoryDataStore.
058: * If this is null, this extension will use the default implementation.
059: * @param factory providing custom datastore to be used or null
060: */
061: public MemoryServiceExtensionImpl(MemoryDSFactory factory) {
062: super ();
063: this .factory = factory;
064: }
065:
066: public IService createService(URL id2,
067: Map<String, Serializable> params) {
068: URL id = id2;
069: if (params.containsKey(KEY)) {
070: if (id == null) {
071: id = (URL) params.get(KEY);
072: }
073: if (!impl.containsKey(id))
074: impl.put(id, new MemoryServiceImpl(id, this .factory));
075: MemoryServiceImpl service = impl.get(id);
076: MemoryDataStore store = null;
077:
078: try {
079: store = service.resolve(MemoryDataStore.class, null);
080: } catch (IOException e) {
081: // won't happen
082: throw (RuntimeException) new RuntimeException(e
083: .getLocalizedMessage()).initCause(e);
084: }
085: for (Map.Entry<String, Serializable> entry : params
086: .entrySet()) {
087: if (entry.getKey().equals(MEMBER_PARAM)) {
088: String[] members = ((String) entry.getValue())
089: .split("_MEMBER_"); //$NON-NLS-1$
090: for (String member : members) {
091:
092: String[] spec = (member.split("_SPLIT_")); //$NON-NLS-1$
093: try {
094: store.getSchema(spec[0]);
095: } catch (Exception e) {
096: // schema does not exist create it
097: try {
098: store.createSchema(DataUtilities
099: .createType(spec[0], spec[1]));
100: } catch (Exception e2) {
101: CatalogPlugin
102: .log(
103: "Error creating type in datastore", e2); //$NON-NLS-1$
104: }
105: }
106: }
107: }
108: }
109:
110: return service;
111: }
112: return null;
113: }
114:
115: public Map<String, Serializable> createParams(URL url) {
116: if (url != null
117: && url.toExternalForm()
118: .startsWith(URL.toExternalForm())) {
119: Map<String, Serializable> map = new HashMap<String, Serializable>();
120: map.put(KEY, url);
121: return map;
122: }
123: return null;
124: }
125:
126: }
|