001: package com.vividsolutions.jump.workbench.datastore;
002:
003: import java.util.ArrayList;
004: import java.util.Arrays;
005: import java.util.HashMap;
006: import java.util.Iterator;
007: import java.util.List;
008: import java.util.Map;
009:
010: import com.vividsolutions.jump.datastore.DataStoreConnection;
011: import com.vividsolutions.jump.datastore.DataStoreDriver;
012: import com.vividsolutions.jump.parameter.ParameterList;
013: import com.vividsolutions.jump.parameter.ParameterListSchema;
014: import com.vividsolutions.jump.util.Block;
015: import com.vividsolutions.jump.util.CollectionUtil;
016: import com.vividsolutions.jump.util.SimpleStringEncrypter;
017: import com.vividsolutions.jump.util.StringUtil;
018:
019: /**
020: * Contains a ParameterList and its associated DataStoreDriver.
021: */
022: public class ConnectionDescriptor {
023: private static String getBasicClassName(String fullClassName) {
024: int dotPos = fullClassName.lastIndexOf('.');
025: String name = fullClassName;
026: if (dotPos > 0) {
027: name = fullClassName.substring(dotPos + 1);
028: }
029: return name;
030: }
031:
032: private static String getDataStoreDriverClassName(
033: String fullClassName) {
034: String className = getBasicClassName(fullClassName);
035: int dsdSuffixIndex = className.indexOf("DataStoreDriver");
036: if (dsdSuffixIndex < 1)
037: return className;
038: return className.substring(0, dsdSuffixIndex);
039: }
040:
041: // the display name of the connection
042: private String name = null;
043: private ParameterList parameterList;
044: private String dataStoreDriverClassName;
045:
046: public ConnectionDescriptor() {
047: }
048:
049: public ConnectionDescriptor(Class dataStoreDriverClass,
050: ParameterList parameterList) {
051: this (null, dataStoreDriverClass, parameterList);
052: }
053:
054: public ConnectionDescriptor(String name,
055: Class dataStoreDriverClass, ParameterList parameterList) {
056: this .name = name;
057: setDataStoreDriverClassName(dataStoreDriverClass.getName());
058: setParameterList(parameterList);
059: }
060:
061: public void setName(String name) {
062: this .name = name;
063: }
064:
065: public String getName() {
066: return name;
067: }
068:
069: public DataStoreConnection createConnection(DataStoreDriver driver)
070: throws Exception {
071: return driver.createConnection(parameterList);
072: }
073:
074: public int hashCode() {
075: // Implement #hashCode so that ConnectionDescriptor works
076: // as a HashMap key. But just set it to 0 for now, to
077: // avoid the work of creating code to generate a proper hash.
078: // This will unfortunately force a linear scan of the keys whenever
079: // HashMap#get is used; however, this will not be a big problem, as
080: // I don't expect there to be many keys in HashMaps of
081: // ConnectionDescriptors [Jon Aquino 2005-03-07]
082: return 0;
083: }
084:
085: public boolean equals(Object other) {
086: return equals((ConnectionDescriptor) other);
087: }
088:
089: private boolean equals(ConnectionDescriptor other) {
090: if (!(other instanceof ConnectionDescriptor)) {
091: // This case includes null. [Jon Aquino 2005-03-16]
092: return false;
093: }
094: return getDataStoreDriverClassName().equals(
095: other.getDataStoreDriverClassName())
096: && getParameterListWithoutPassword().equals(
097: other.getParameterListWithoutPassword());
098: }
099:
100: public ParameterList getParameterList() {
101: return parameterList;
102: }
103:
104: public String toString() {
105: if (name != null) {
106: return name + " (" + getParametersString() + ")";
107: }
108: return getParametersString();
109: }
110:
111: public String getParametersString() {
112: return getDataStoreDriverClassName(dataStoreDriverClassName)
113: + ":"
114: + StringUtil
115: .toCommaDelimitedString(
116: CollectionUtil
117: .select(
118: parameterValues(getParameterListWithoutPassword()),
119: new Block() {
120: public Object yield(
121: Object name) {
122: // Don't include null
123: // parameters e.g.
124: // passwords [Jon Aquino
125: // 2005-03-15]
126: return Boolean
127: .valueOf(name != null);
128: }
129: })).replaceAll(", ",
130: ":");
131: }
132:
133: private List parameterValues(ParameterList parameterList) {
134: List parameterValues = new ArrayList();
135: for (Iterator i = Arrays.asList(
136: parameterList.getSchema().getNames()).iterator(); i
137: .hasNext();) {
138: String name = (String) i.next();
139: parameterValues.add(parameterList.getParameter(name));
140: }
141: return parameterValues;
142: }
143:
144: public ParameterList getParameterListWithoutPassword() {
145: ParameterList parameterListWithoutPassword = new ParameterList(
146: parameterList);
147: if (passwordParameterName(parameterList.getSchema()) != null) {
148: parameterListWithoutPassword.setParameter(
149: passwordParameterName(parameterList.getSchema()),
150: null);
151: }
152: return parameterListWithoutPassword;
153: }
154:
155: public void setParameterListWithObfuscatedPassword(
156: PersistentParameterList parameterListWithObfuscatedPassword) {
157: ParameterList parameterList = new ParameterList(
158: parameterListWithObfuscatedPassword);
159: if (passwordParameterName(parameterList.getSchema()) != null) {
160: parameterList
161: .setParameter(
162: passwordParameterName(parameterList
163: .getSchema()),
164: unobfuscate(parameterList
165: .getParameterString(passwordParameterName(parameterList
166: .getSchema()))));
167: }
168: setParameterList(parameterList);
169: }
170:
171: public PersistentParameterList getParameterListWithObfuscatedPassword() {
172: ParameterList parameterListWithObfuscatedPassword = new ParameterList(
173: parameterList);
174: if (passwordParameterName(parameterList.getSchema()) != null) {
175: parameterListWithObfuscatedPassword
176: .setParameter(
177: passwordParameterName(parameterList
178: .getSchema()),
179: obfuscate(parameterList
180: .getParameterString(passwordParameterName(parameterList
181: .getSchema()))));
182: }
183: return new PersistentParameterList(
184: parameterListWithObfuscatedPassword);
185: }
186:
187: private String obfuscate(String s) {
188: return s != null ? new SimpleStringEncrypter().encrypt(s)
189: : null;
190: }
191:
192: private String unobfuscate(String s) {
193: return s != null ? new SimpleStringEncrypter().decrypt(s)
194: : null;
195: }
196:
197: public void setDataStoreDriverClassName(
198: String dataStoreDriverClassName) {
199: this .dataStoreDriverClassName = dataStoreDriverClassName;
200: }
201:
202: public String getDataStoreDriverClassName() {
203: return dataStoreDriverClassName;
204: }
205:
206: public void setParameterList(ParameterList parameterList) {
207: this .parameterList = parameterList;
208: }
209:
210: public static String passwordParameterName(
211: ParameterListSchema schema) {
212: for (Iterator i = Arrays.asList(schema.getNames()).iterator(); i
213: .hasNext();) {
214: String name = (String) i.next();
215: if (name.equalsIgnoreCase("password")) {
216: return name;
217: }
218: }
219: return null;
220: }
221:
222: public static class PersistentParameterList extends ParameterList {
223: public PersistentParameterList() {
224: super (new ParameterListSchema(new String[] {},
225: new Class[] {}));
226: }
227:
228: public PersistentParameterList(ParameterList parameterList) {
229: this ();
230: setSchema(new PersistentParameterListSchema(parameterList
231: .getSchema()));
232: setNameToValueMap(nameToValueMap(parameterList));
233: }
234:
235: private static Map nameToValueMap(ParameterList parameterList) {
236: Map nameToValueMap = new HashMap();
237: for (Iterator i = Arrays.asList(
238: parameterList.getSchema().getNames()).iterator(); i
239: .hasNext();) {
240: String name = (String) i.next();
241: nameToValueMap.put(name, parameterList
242: .getParameter(name));
243: }
244: return nameToValueMap;
245: }
246:
247: public Map getNameToValueMap() {
248: return nameToValueMap(this );
249: }
250:
251: public void setNameToValueMap(Map nameToValueMap) {
252: for (Iterator i = nameToValueMap.keySet().iterator(); i
253: .hasNext();) {
254: String name = (String) i.next();
255: setParameter(name, nameToValueMap.get(name));
256: }
257: }
258:
259: public void setSchema(PersistentParameterListSchema schema) {
260: initialize(schema);
261: }
262: }
263:
264: public static class PersistentParameterListSchema extends
265: ParameterListSchema {
266: public PersistentParameterListSchema() {
267: super (new String[] {}, new Class[] {});
268: }
269:
270: public PersistentParameterListSchema(ParameterListSchema schema) {
271: this ();
272: initialize(schema.getNames(), schema.getClasses());
273: }
274:
275: public List getPersistentNames() {
276: return Arrays.asList(getNames());
277: }
278:
279: public void addPersistentName(String name) {
280: String[] newNames = new String[getNames().length + 1];
281: System.arraycopy(getNames(), 0, newNames, 0,
282: getNames().length);
283: newNames[getNames().length] = name;
284: initialize(newNames, getClasses());
285: }
286:
287: public List getPersistentClasses() {
288: return Arrays.asList(getClasses());
289: }
290:
291: public void addPersistentClass(Class c) {
292: Class[] newClasses = new Class[getClasses().length + 1];
293: System.arraycopy(getClasses(), 0, newClasses, 0,
294: getClasses().length);
295: newClasses[getClasses().length] = c;
296: initialize(getNames(), newClasses);
297: }
298: }
299: }
|