001: /*
002: * The contents of this file are subject to the terms of the Common Development
003: * and Distribution License (the License). You may not use this file except in
004: * compliance with the License.
005: *
006: * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
007: * or http://www.netbeans.org/cddl.txt.
008: *
009: * When distributing Covered Code, include this CDDL Header Notice in each file
010: * and include the License file at http://www.netbeans.org/cddl.txt.
011: * If applicable, add the following below the CDDL Header, with the fields
012: * enclosed by brackets [] replaced by your own identifying information:
013: * "Portions Copyrighted [year] [name of copyright owner]"
014: *
015: * The Original Software is NetBeans. The Initial Developer of the Original
016: * Software is Sun Microsystems, Inc. Portions Copyright 1997-2007 Sun
017: * Microsystems, Inc. All Rights Reserved.
018: */
019:
020: /*
021: * Sun Public License Notice
022: *
023: * The contents of this file are subject to the Sun Public License
024: * Version 1.0 (the "License"). You may not use this file except in
025: * compliance with the License. A copy of the License is available at
026: * http://www.sun.com/
027: *
028: * The Original Code is NetBeans. The Initial Developer of the Original
029: * Code is Sun Microsystems, Inc. Portions Copyright 1997-2004 Sun
030: * Microsystems, Inc. All Rights Reserved.
031: */
032:
033: package org.netbeans.modules.sql.project.dbmodel;
034:
035: import java.io.File;
036:
037: import java.util.Arrays;
038: import java.util.Collection;
039: import java.util.Collections;
040: import java.util.HashMap;
041: import java.util.List;
042:
043: /**
044: * Encapsulates information regarding known JDBC driver types for use in establishing
045: * database metadata connections.
046: *
047: * @author Jonathan Giron
048: * @version
049: */
050: public class DriverTypes {
051:
052: // Master list of driver TypeInfo instances.
053: private static final TypeInfo[] TYPES = new TypeInfo[] {
054: new TypeInfo("Oracle", "oracle.jdbc.OracleDriver",
055: "jdbc:oracle:thin:@<host>:<port>:<sid>"),
056: /*
057: new TypeInfo("Oracle OCI",
058: "oracle.driver.jdbc.OracleDriver",
059: "jdbc:oracle:oci8:@<host>:<port>:<sid>"),
060: */
061: new TypeInfo("SeeBeyond (DataDirect)",
062: "com.SeeBeyond.oracle.jdbc.oracle.OracleDriver",
063: "jdbc:SeeBeyond:oracle://<host>:<port>;SID=<sid>"),
064: /* new TypeInfo("User-defined",
065: "<fully-qualified driver classname>",
066: "jdbc:<url parameters>")
067: */
068: new TypeInfo("OracleOCI", "oracle.jdbc.OracleDriver",
069: "jdbc:oracle:oci8:@<tns>##setTNSEntry#<tns>"),
070:
071: };
072:
073: // Map of display names to TypeInfo instances
074: private static HashMap typeMap;
075:
076: static {
077: typeMap = new HashMap();
078: for (int i = 0; i < TYPES.length; i++) {
079: TypeInfo info = TYPES[i];
080: typeMap.put(info.getDisplayName(), info);
081: }
082: if (TYPES.length != typeMap.keySet().size()) {
083: throw new IllegalStateException(
084: "Count of display names does not match count of driver TypeInfo instances!"); // NOI18N
085: }
086: }
087:
088: /** Creates a new instance of DriverTypes */
089: public DriverTypes() {
090: }
091:
092: /**
093: * Gets Collection of driver display names.
094: *
095: * @return Collection of display names
096: */
097: public static Collection getDisplayNames() {
098: return typeMap.keySet();
099: }
100:
101: /**
102: * Gets collection of known TypeInfo instances, in the form of an unmodifiable List.
103: *
104: * @return List of TypeInfo instances.
105: */
106: public static List getTypeInfoList() {
107: return (TYPES != null) ? Collections.unmodifiableList(Arrays
108: .asList(TYPES)) : Collections.EMPTY_LIST;
109: }
110:
111: /**
112: * Gets TypeInfo instance associated with the given display name.
113: *
114: * @param displayName display name to use as key in retrieving TypeInfo instance
115: * @return TypeInfo associated with displayName, if it exists; null otherwise
116: */
117: public static TypeInfo getTypeInfo(String displayName) {
118: Object obj = typeMap.get(displayName);
119: return (obj != null) ? (TypeInfo) obj : null;
120: }
121:
122: /**
123: * Constructs a JDBC URL given the specified connection parameters.
124: *
125: * @param driverName Driver display name
126: * @param hostname Database hostname
127: * @param port Database port
128: * @param sid Database SID
129: * @param username Database login ID
130: * @param password Database login password
131: *
132: * @return Applicable JDBC URL composited from the supplied connection
133: * information, or null if the specified driver class name is not
134: * recognized or supported
135: */
136: public static final String makeUrl(String driverName,
137: String hostname, String port, String sid, String username,
138: String password) {
139:
140: String url = null;
141: TypeInfo info = getTypeInfo(driverName);
142:
143: if (info != null) {
144: StringBuffer template = new StringBuffer(info
145: .getURLTemplate());
146: String var = getSubstVar(template);
147: while (var != null) {
148: if (var.equalsIgnoreCase("host")) {
149: replaceSubstVar(template, var, hostname);
150: } else if (var.equalsIgnoreCase("port")) {
151: replaceSubstVar(template, var, port);
152: } else if (var.equalsIgnoreCase("sid")) {
153: replaceSubstVar(template, var, sid);
154: } else if (var.equalsIgnoreCase("username")) {
155: replaceSubstVar(template, var, username);
156: } else if (var.equalsIgnoreCase("password")) {
157: replaceSubstVar(template, var, password);
158: }
159: var = getSubstVar(template);
160: }
161: url = template.toString();
162: }
163: return url;
164: }
165:
166: /**
167: * Constructs a JDBC URL given the specified connection parameters.
168: *
169: * @param driverName Driver display name
170: * @param hostname Database hostname
171: * @param port Database port
172: * @param sid Database SID
173: * @param username Database login ID
174: * @param password Database login password
175: *
176: * @return Applicable JDBC URL composited from the supplied connection
177: * information, or null if the specified driver class name is not
178: * recognized or supported
179: */
180: public static final String makeUrl(String driverName,
181: String hostname, String port, String sid, String username,
182: String password, String tns) {
183:
184: String url = null;
185: TypeInfo info = getTypeInfo(driverName);
186:
187: if (info != null) {
188: StringBuffer template = new StringBuffer(info
189: .getURLTemplate());
190: String var = getSubstVar(template);
191: while (var != null) {
192: if (var.equalsIgnoreCase("host")) {
193: replaceSubstVar(template, var, hostname);
194: } else if (var.equalsIgnoreCase("port")) {
195: replaceSubstVar(template, var, port);
196: } else if (var.equalsIgnoreCase("sid")) {
197: replaceSubstVar(template, var, sid);
198: } else if (var.equalsIgnoreCase("username")) {
199: replaceSubstVar(template, var, username);
200: } else if (var.equalsIgnoreCase("password")) {
201: replaceSubstVar(template, var, password);
202: } else if (var.equalsIgnoreCase("tns")) {
203: replaceSubstVar(template, var, tns);
204: }
205: var = getSubstVar(template);
206: }
207: url = template.toString();
208: }
209: return url;
210: }
211:
212: /**
213: * Get the first encountered substitution string in the buffer.
214: * A string bounded on its left and right edges with the characters
215: * '<' and '>', including these characters, is a substitution string.
216: *
217: * @param template Container of data
218: *
219: * @return If a substitution string is found, the string is returned;
220: * otherwise, null is returned
221: */
222: private static final String getSubstVar(StringBuffer template) {
223: String substVar = null;
224: int start = template.indexOf("<");
225: int end = template.indexOf(">");
226: if (start != -1 && end != -1 && start < (end + 1)) {
227: substVar = template.substring(start + 1, end);
228: }
229: return substVar;
230: }
231:
232: /**
233: * Replace the first occurence of '<' + pattern + '>' in the buffer,
234: * with the replacement string,
235: *
236: * @param buffer Container of data
237: * @param pattern Substring to find
238: * @param replacement String to replace the pattern
239: */
240: private static final void replaceSubstVar(StringBuffer buffer,
241: String pattern, String replacement) {
242:
243: String pat = "<" + pattern + ">";
244: int position = buffer.indexOf(pat);
245: if (position != -1) {
246: buffer.delete(position, position + pat.length());
247: buffer.insert(position, replacement);
248: }
249: }
250:
251: /**
252: * Lightweight container class to associate a JDBC driver display name with its
253: * fully-qualified Java classname and a standard JDBC URL template.
254: *
255: * @author Jonathan Giron
256: * @version
257: */
258: public static class TypeInfo {
259: private String displayName;
260: private String className;
261: private String urlTemplate;
262: private File jarFile;
263:
264: /**
265: * Creates new instance of TypeInfo with the given display name, driver classname,
266: * and URL template.
267: *
268: * @param displayStr user-friendly description of driver type
269: * @param driverClass fully-qualified classname of driver
270: * @param urlStr String representing URL template, in the form
271: * "jdbc:{JDBC vendor type}:{vendor-specific parameters enclosed by angle brackets}"
272: */
273: public TypeInfo(String displayStr, String driverClass,
274: String urlStr) {
275: if (displayStr == null || displayStr.trim().length() == 0) {
276: throw new IllegalArgumentException(
277: "Must supply non-empty String instance for display argument."); //NOI18N
278: }
279:
280: if (driverClass == null || driverClass.trim().length() == 0) {
281: throw new IllegalArgumentException(
282: "Must supply non-empty String instance for driverClass argument."); //NOI18N
283: }
284:
285: if (urlStr == null || urlStr.trim().length() == 0) {
286: throw new IllegalArgumentException(
287: "Must supply non-empty String instance for urlString argument."); //NOI18N
288: }
289:
290: displayName = displayStr;
291: className = driverClass;
292: urlTemplate = urlStr;
293: }
294:
295: /**
296: * Gets display name for this driver type.
297: *
298: * @return driver display name
299: */
300: public String getDisplayName() {
301: return displayName;
302: }
303:
304: /**
305: * Gets JDBC driver classname for this driver type.
306: *
307: * @return fully-qualified JDBC driver classname
308: */
309: public String getDriverClassName() {
310: return className;
311: }
312:
313: /**
314: * Gets URL template representing required information for accessing a
315: * database of this driver type.
316: *
317: * @return URL template
318: */
319: public String getURLTemplate() {
320: return urlTemplate;
321: }
322:
323: /**
324: * Gets optional File instance indicating path to JAR file containing the associated
325: * JDBC driver.
326: *
327: * @return File instance pointing to location of driver JAR file, or null if no
328: * path information is known.
329: */
330: public File getDriverJarFile() {
331: return jarFile;
332: }
333:
334: /**
335: * Sets optional File instance indicating path to JAR file containing the associated
336: * JDBC driver. A null argument indicates that the location of the JAR file within the
337: * local filesystem is unknown.
338: *
339: * @param newLocation File instance representing location of driver JAR file within the
340: * local filesystem, or null if location is unknown
341: */
342: public void setDriverJarFile(File newLocation) {
343: jarFile = newLocation;
344: }
345:
346: /**
347: * Overrides parent implementation to return driver display name.
348: *
349: * @return display name of JDBC driver
350: */
351: public String toString() {
352: return displayName;
353: }
354: }
355: }
|