001: /*
002: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
003: *
004: * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
005: *
006: * The contents of this file are subject to the terms of either the GNU
007: * General Public License Version 2 only ("GPL") or the Common
008: * Development and Distribution License("CDDL") (collectively, the
009: * "License"). You may not use this file except in compliance with the
010: * License. You can obtain a copy of the License at
011: * http://www.netbeans.org/cddl-gplv2.html
012: * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
013: * specific language governing permissions and limitations under the
014: * License. When distributing the software, include this License Header
015: * Notice in each file and include the License file at
016: * nbbuild/licenses/CDDL-GPL-2-CP. Sun designates this
017: * particular file as subject to the "Classpath" exception as provided
018: * by Sun in the GPL Version 2 section of the License file that
019: * accompanied this code. If applicable, add the following below the
020: * License Header, with the fields enclosed by brackets [] replaced by
021: * your own identifying information:
022: * "Portions Copyrighted [year] [name of copyright owner]"
023: *
024: * Contributor(s):
025: *
026: * The Original Software is NetBeans. The Initial Developer of the Original
027: * Software is Sun Microsystems, Inc. Portions Copyright 1997-2007 Sun
028: * Microsystems, Inc. All Rights Reserved.
029: *
030: * If you wish your version of this file to be governed by only the CDDL
031: * or only the GPL Version 2, indicate your decision by adding
032: * "[Contributor] elects to include this software in this distribution
033: * under the [CDDL or GPL Version 2] license." If you do not indicate a
034: * single choice of license, a recipient has the option to distribute
035: * your version of this file under either the CDDL, the GPL Version 2 or
036: * to extend the choice of license to its licensees as provided above.
037: * However, if you add GPL Version 2 code and therefore, elected the GPL
038: * Version 2 license, then the option applies only if the new code is
039: * made subject to such option by the copyright holder.
040: */
041: package org.netbeans.modules.sql.framework.model.impl;
042:
043: import org.netbeans.modules.sql.framework.common.jdbc.SQLDBConnectionDefinition;
044: import org.w3c.dom.Element;
045:
046: import com.sun.sql.framework.exception.BaseException;
047: import com.sun.sql.framework.jdbc.DBConnectionParameters;
048: import com.sun.sql.framework.utils.StringUtil;
049: import org.netbeans.modules.etl.ui.ETLEditorSupport;
050: import org.netbeans.modules.sql.framework.common.utils.DBExplorerUtil;
051: import org.netbeans.modules.sql.framework.model.DBConnectionDefinition;
052:
053: /**
054: * This class implements DBConnectionDefnition
055: *
056: * @version $Revision$
057: * @author Sudhi Seshachala
058: * @author Jonathan Giron
059: */
060: public class SQLDBConnectionDefinitionImpl extends
061: DBConnectionParameters implements Cloneable, Comparable,
062: SQLDBConnectionDefinition {
063:
064: /**
065: * SQLDBConnectionDefinitionImpl Constructor is used for the potential collection of
066: * DBConnectionDefinitions that might be parsed from a given file.
067: */
068: public SQLDBConnectionDefinitionImpl() {
069: super ();
070: }
071:
072: /**
073: * Constructs an instance of SQLDBConnectionDefinitionImpl, copying the contents of
074: * the given DBConnectionDefinition implementation.
075: *
076: * @param connectionDefn DBConnectionDefinition implementation whose contents will be
077: * copied.
078: */
079: public SQLDBConnectionDefinitionImpl(
080: DBConnectionDefinition connectionDefn) {
081: this ();
082:
083: if (connectionDefn == null) {
084: throw new IllegalArgumentException(
085: "Must supply non-null DBConnectionDefinition instance for src param.");
086: }
087:
088: if (connectionDefn instanceof SQLDBConnectionDefinition) {
089: copyFrom((SQLDBConnectionDefinition) connectionDefn);
090: } else {
091: copyFrom(connectionDefn);
092: }
093: }
094:
095: /**
096: * Constructs an instance of SQLDBConnectionDefinitionImpl using the information
097: * contained in the given XML element.
098: *
099: * @param theElement DOM element containing XML representation of this new
100: * SQLDBConnectionDefinitionImpl instance
101: * @throws BaseException if error occurs while parsing
102: */
103: public SQLDBConnectionDefinitionImpl(Element theElement)
104: throws BaseException {
105: super (theElement);
106: }
107:
108: public SQLDBConnectionDefinitionImpl(
109: SQLDBConnectionDefinition connectionDefn) {
110: this ();
111:
112: if (connectionDefn == null) {
113: throw new IllegalArgumentException(
114: "Must supply non-null DBConnectionDefinition instance for src param.");
115: }
116:
117: copyFrom(connectionDefn);
118: }
119:
120: SQLDBConnectionDefinitionImpl(String name, String dbType,
121: String driverClass, String url, String user,
122: String password, String description) {
123:
124: setName(name);
125: setDBType(dbType);
126: setDriverClass(driverClass);
127: setConnectionURL(url);
128: setUserName(user);
129: setPassword(password);
130: setDescription(description);
131: }
132:
133: @Override
134: public void setConnectionURL(String url) {
135: if (name.startsWith(DBExplorerUtil.AXION_URL_PREFIX)
136: && url.contains(ETLEditorSupport.PRJ_PATH)) {
137: String[] urlParts = DBExplorerUtil.parseConnUrl(url);
138: urlParts[1] = urlParts[1]
139: .substring(ETLEditorSupport.PRJ_PATH.length());
140: super .setConnectionURL(DBExplorerUtil.AXION_URL_PREFIX
141: + urlParts[0] + ":" + urlParts[1]);
142: } else {
143: super .setConnectionURL(url);
144: }
145: }
146:
147: @Override
148: public void setName(String name) {
149: if (name.startsWith(DBExplorerUtil.AXION_URL_PREFIX)
150: && name.contains(ETLEditorSupport.PRJ_PATH)) {
151: String[] urlParts = DBExplorerUtil.parseConnUrl(name);
152: super .setName(urlParts[0]);
153: } else {
154: if (name.length() > 60) {
155: super .setName(name.substring(name.length() - 60));
156: } else {
157: super .setName(name);
158: }
159: }
160: }
161:
162: /**
163: * Creates a clone of this SQLDBConnectionDefinitionImpl object.
164: *
165: * @return clone of this object
166: */
167: @Override
168: public Object clone() {
169: try {
170: return super .clone();
171: } catch (Exception e) {
172: throw new InternalError(e.toString());
173: }
174: }
175:
176: public Object cloneObject() {
177: return clone();
178: }
179:
180: /**
181: * Copies member values to those contained in the given DBConnectionDefinition
182: * instance. Does shallow copy of properties and flatfiles collections.
183: *
184: * @param source DBConnectionDefinition whose contents are to be copied into this
185: * instance
186: */
187: public synchronized void copyFrom(DBConnectionDefinition source) {
188: if (source == null) {
189: throw new IllegalArgumentException(
190: "Must supply non-null ref for source.");
191: } else if (source == this ) {
192: return;
193: }
194:
195: // Use accessors rather than direct assignment because they handle
196: // pathological values.
197: setDescription(source.getDescription());
198: setName(source.getName());
199: setDBType(source.getDBType());
200: setDriverClass(source.getDriverClass());
201: setConnectionURL(source.getConnectionURL());
202: setUserName(source.getUserName());
203: setPassword(source.getPassword());
204: }
205:
206: /**
207: * Copies member values to those contained in the given SQLDBConnectionDefinition
208: * instance. Does shallow copy of properties and flatfiles collections.
209: *
210: * @param source SQLDBConnectionDefinition whose contents are to be copied into this
211: * instance
212: */
213: public synchronized void copyFrom(SQLDBConnectionDefinition source) {
214: if (source == null) {
215: throw new IllegalArgumentException(
216: "Must supply non-null ref for source.");
217: } else if (source == this ) {
218: return;
219: }
220: this .copyFrom((DBConnectionDefinition) source);
221: setJNDIPath(source.getJNDIPath());
222: }
223:
224: /**
225: * Doesn't take table name into consideration.
226: *
227: * @param refObj SQLColumn to be compared.
228: * @return true if the object is identical. false if it is not.
229: */
230: @Override
231: public boolean equals(Object refObj) {
232: if (!(refObj instanceof SQLDBConnectionDefinitionImpl)) {
233: return false;
234: }
235:
236: SQLDBConnectionDefinitionImpl defn = (SQLDBConnectionDefinitionImpl) refObj;
237:
238: boolean result = (name != null) ? name.equals(defn.name)
239: : (defn.name == null);
240: result &= (dbType != null) ? dbType.equals(defn.dbType)
241: : (defn.dbType == null);
242: result &= (driverClass != null) ? this .driverClass
243: .equals(defn.driverClass) : (defn.driverClass == null);
244: result &= (jdbcUrl != null) ? jdbcUrl.equals(defn.jdbcUrl)
245: : (defn.jdbcUrl == null);
246: result &= (userName != null) ? userName.equals(defn.userName)
247: : (defn.userName == null);
248: result &= (password != null) ? this .password
249: .equals(defn.password) : (defn.password == null);
250:
251: return result;
252: }
253:
254: @Override
255: public int hashCode() {
256: return super .hashCode();
257: }
258:
259: /**
260: * Indicates whether contents of given DBConnectionDefinition implementer are
261: * identical to this SQLDBConnectionDefinitionImpl object.
262: *
263: * @param def DBConnectionDefinition implementer to compare against
264: * @return true if contents are identical; false otherwise
265: */
266: public boolean isIdentical(DBConnectionDefinition def) {
267: boolean identical = false;
268:
269: if (def != null) {
270: identical = StringUtil.isIdentical(jdbcUrl, def
271: .getConnectionURL())
272: && StringUtil.isIdentical(userName, def
273: .getUserName())
274: && StringUtil.isIdentical(password, def
275: .getPassword());
276: }
277:
278: return identical;
279: }
280:
281: /**
282: * Indicates whether contents of given DBConnectionDefinition implementer are
283: * identical to this SQLDBConnectionDefinitionImpl object.
284: *
285: * @param def DBConnectionDefinition implementer to compare against
286: * @return true if contents are identical; false otherwise
287: */
288: public boolean isIdentical(SQLDBConnectionDefinition def) {
289: boolean identical = false;
290:
291: if (def != null) {
292: identical = StringUtil.isIdentical(jdbcUrl, def
293: .getConnectionURL())
294: && StringUtil.isIdentical(userName, def
295: .getUserName())
296: && StringUtil.isIdentical(password, def
297: .getPassword())
298: && StringUtil.isIdentical(dsJndiPath, def
299: .getJNDIPath(), true);
300: }
301:
302: return identical;
303: }
304: }
|