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.mashup.db.model.impl;
042:
043: import com.sun.sql.framework.jdbc.DBConnectionParameters;
044: import java.util.Map;
045: import org.netbeans.modules.mashup.db.model.FlatfileDBConnectionDefinition;
046:
047: import org.netbeans.modules.sql.framework.common.utils.TagParserUtility;
048: import org.netbeans.modules.sql.framework.model.DBConnectionDefinition;
049: import org.w3c.dom.Element;
050:
051: /**
052: * Implements FlatfileDBConnectionDefinition interface for Flatfile.
053: *
054: * @author Jonathan Giron
055: * @author Girish Patil
056: * @version $Revision$
057: */
058: public class FlatfileDBConnectionDefinitionImpl extends
059: DBConnectionParameters implements
060: FlatfileDBConnectionDefinition {
061: /** Constants used in XML tags * */
062: private static final String ATTR_DRIVER_CLASS = "driverClass";
063: private static final String ATTR_NAME = "name";
064: private static final String ATTR_PASSWORD = "password";
065: private static final String ATTR_URL = "url";
066: private static final String ATTR_USER_NAME = "userName";
067: private static final String END_QUOTE_SPACE = "\" ";
068: private static final String EQUAL_START_QUOTE = "=\"";
069: private static final String TAG_CONNECTION_DEFINITION = "connectionDefinition";
070:
071: /* JDBC driver class name for Axion */
072: private static final String AXION_DRIVER = "org.axiondb.jdbc.AxionDriver";
073:
074: /* JDBC URL */
075: private String url;
076:
077: /** Creates a new default instance of FlatfileDBConnectionDefinitionImpl. */
078: public FlatfileDBConnectionDefinitionImpl() {
079: userName = "sa";
080: password = "sa";
081: driverClass = AXION_DRIVER;
082: }
083:
084: /** Creates a new default instance of FlatfileDBConnectionDefinitionImpl. */
085: public FlatfileDBConnectionDefinitionImpl(String connName) {
086: name = connName;
087: userName = "sa";
088: password = "sa";
089: driverClass = AXION_DRIVER;
090: description = connName;
091: }
092:
093: /**
094: * Creates a new instance of FlatfileDBConnectionDefinitionImpl with the given
095: * attributes.
096: *
097: * @param connName connection name
098: * @param driverName driver name
099: * @param connUrl JDBC URL for this connection
100: * @param uname username used to establish connection
101: * @param passwd password used to establish connection
102: * @param desc description of connection
103: */
104: public FlatfileDBConnectionDefinitionImpl(String connName,
105: String driverName, String connUrl, String uname,
106: String passwd, String desc) {
107: name = connName;
108:
109: driverClass = driverName;
110: url = connUrl;
111: userName = uname;
112: password = passwd;
113: description = desc;
114: }
115:
116: /**
117: * Creates a new instance of FlatfileDBConnectionDefinitionImpl using the values in
118: * the given FlatfileDBConnectionDefinition.
119: *
120: * @param connectionDefn DBConnectionDefinition to be copied
121: */
122: public FlatfileDBConnectionDefinitionImpl(
123: DBConnectionDefinition connectionDefn) {
124: if (connectionDefn == null) {
125: throw new IllegalArgumentException(
126: "Must supply non-null DBConnectionDefinition instance for connectionDefn param.");
127: }
128:
129: if (connectionDefn instanceof FlatfileDBConnectionDefinitionImpl) {
130: copyFrom((FlatfileDBConnectionDefinitionImpl) connectionDefn);
131: }
132: }
133:
134: /**
135: * @see org.netbeans.modules.model.database.DBConnectionDefinition#getConnectionURL()
136: */
137: @Override
138: public String getConnectionURL() {
139: return url;
140: }
141:
142: @Override
143: public void setConnectionURL(String aUrl) {
144: url = aUrl;
145: }
146:
147: public String getUrl() {
148: return this .url;
149: }
150:
151: /**
152: * @see org.netbeans.modules.model.database.DBConnectionDefinition#getDBType
153: */
154: @Override
155: public String getDBType() {
156: return "Internal";
157: }
158:
159: /**
160: * Copies member values to those contained in the given DBConnectionDefinition
161: * instance. Does shallow copy of properties and flatfile collections.
162: *
163: * @param source DBConnectionDefinition whose contents are to be copied into this
164: * instance
165: */
166: public synchronized void copyFrom(DBConnectionDefinition source) {
167: if (source == null) {
168: throw new IllegalArgumentException(
169: "Must supply non-null ref for source.");
170: } else if (source == this ) {
171: return;
172: }
173:
174: this .description = source.getDescription();
175: this .name = source.getName();
176: this .driverClass = source.getDriverClass();
177: this .url = source.getConnectionURL();
178: this .userName = source.getUserName();
179: this .password = source.getPassword();
180: }
181:
182: /**
183: * Overrides default implementation.
184: *
185: * @param o Object to compare for equality against this instance.
186: * @return true if o is equivalent to this, false otherwise
187: */
188: @Override
189: public boolean equals(Object o) {
190: // Check for reflexivity.
191: if (this == o) {
192: return true;
193: } else if (!(o instanceof FlatfileDBConnectionDefinitionImpl)) {
194: return false;
195: }
196:
197: boolean response = true;
198: FlatfileDBConnectionDefinitionImpl impl = (FlatfileDBConnectionDefinitionImpl) o;
199:
200: boolean nameEqual = (name != null) ? name.equals(impl.name)
201: : (impl.name == null);
202: response &= nameEqual;
203:
204: boolean driverClassEqual = (driverClass != null) ? driverClass
205: .equals(impl.driverClass) : (impl.driverClass == null);
206: response &= driverClassEqual;
207:
208: boolean urlEqual = (url != null) ? url.equals(impl.url)
209: : (impl.url == null);
210: response &= urlEqual;
211:
212: boolean userNameEqual = (userName != null) ? userName
213: .equals(impl.userName) : (impl.userName == null);
214: response &= userNameEqual;
215:
216: boolean passwordEqual = (password != null) ? password
217: .equals(impl.password) : (impl.password == null);
218: response &= passwordEqual;
219:
220: boolean descEqual = (description != null) ? description
221: .equals(impl.description) : (impl.description == null);
222: response &= descEqual;
223:
224: return response;
225: }
226:
227: /**
228: * Overrides default implementation to compute its value based on member variables.
229: *
230: * @return computed hash code
231: */
232: @Override
233: public int hashCode() {
234: int hashCode = 0;
235:
236: hashCode += (name != null) ? name.hashCode() : 0;
237: hashCode += (driverClass != null) ? driverClass.hashCode() : 0;
238: hashCode += (url != null) ? url.hashCode() : 0;
239: hashCode += (userName != null) ? userName.hashCode() : 0;
240: hashCode += (password != null) ? password.hashCode() : 0;
241: hashCode += (description != null) ? description.hashCode() : 0;
242:
243: return hashCode;
244: }
245:
246: @Override
247: public void parseXML(Element xmlElement) {
248: Map attrs = TagParserUtility.getNodeAttributes(xmlElement);
249:
250: this .name = (String) attrs.get(ATTR_NAME);
251: this .driverClass = (String) attrs.get(ATTR_DRIVER_CLASS);
252: this .url = (String) attrs.get(ATTR_URL);
253: this .userName = (String) attrs.get(ATTR_USER_NAME);
254: this .password = (String) attrs.get(ATTR_PASSWORD);
255: }
256:
257: @Override
258: public String toXMLString(String prefix) {
259: StringBuilder sb = new StringBuilder();
260: sb.append(prefix);
261: sb.append("<");
262: sb.append(TAG_CONNECTION_DEFINITION);
263: sb.append(" ");
264: sb.append(ATTR_NAME);
265: sb.append(EQUAL_START_QUOTE);
266: sb.append(this .getName());
267: sb.append(END_QUOTE_SPACE);
268: sb.append(ATTR_DRIVER_CLASS);
269: sb.append(EQUAL_START_QUOTE);
270: sb.append(this .getDriverClass());
271: sb.append(END_QUOTE_SPACE);
272: sb.append(ATTR_URL);
273: sb.append(EQUAL_START_QUOTE);
274: sb.append(this .getUrl());
275: sb.append(END_QUOTE_SPACE);
276: sb.append(ATTR_USER_NAME);
277: sb.append(EQUAL_START_QUOTE);
278: sb.append(this .getUserName());
279: sb.append(END_QUOTE_SPACE);
280: sb.append(ATTR_PASSWORD);
281: sb.append(EQUAL_START_QUOTE);
282: sb.append(this .getPassword());
283: sb.append("\"/>\n");
284: return sb.toString();
285: }
286: }
|