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.visualweb.dataconnectivity.sql;
042:
043: import javax.naming.NamingException;
044:
045: /**
046: * This class represents an alias for a DataSource. It just points to another
047: * datasource. Calls to getter methods delegate to the referenced data source.
048: * Calls to setter metnods throw an UnsupportedOperationException.
049: * Other methods such as test() and getSchemas(), also delegate to the
050: * referenced data source.
051: *
052: * @author jfbrown
053: */
054: public class DesignTimeDataSourceAlias extends DesignTimeDataSource {
055:
056: private DesignTimeDataSourceAlias() {
057: }
058:
059: public DesignTimeDataSourceAlias(String newAlias) {
060: try {
061: dshelper = new DesignTimeDataSourceHelper();
062: } catch (NamingException ne) {
063: // should never be here - bad stuff will happen.
064: throw new RuntimeException(
065: "DesignTimeDataSourceAlias: InitalConxtext coding error."); //NOI18N
066: }
067: if (newAlias != null && !"".equals(newAlias)) { //NOI18N
068: alias = newAlias;
069: }
070: }
071:
072: DesignTimeDataSourceHelper dshelper;
073:
074: private String alias = null;
075:
076: public void setAlias(String newAlias) {
077: if (newAlias != null && newAlias.equals(alias)) {
078: return;
079: }
080: this .alias = newAlias;
081: try {
082: save();
083: } catch (NamingException ne) {
084: // should never be here.
085: RuntimeException ree = new RuntimeException(ne
086: .getLocalizedMessage());
087: ree.fillInStackTrace();
088: throw ree;
089: }
090: }
091:
092: public String getAlias() {
093: return alias;
094: }
095:
096: public boolean isValidAlias() {
097: // TODO
098: return (getReferencedDataSource() != null);
099: }
100:
101: /**
102: * lookup the referenced datasource in the naming context.
103: * If not found, return null. Callers to this method
104: * will have to handle a "null" return value as appropriate.
105: */
106: public DesignTimeDataSource getReferencedDataSource() {
107:
108: DesignTimeDataSource dts = null;
109: try {
110: dts = dshelper.getDataSource(alias);
111: } catch (NamingException ne) {
112: // TODO: maybe log something, but this isn't necessarily
113: // an error if the user deleted the referenced data
114: // source.
115: }
116: return dts;
117: }
118:
119: /**
120: * utility method for constructing the RuntimeException when setter methods
121: * are called.
122: */
123: private RuntimeException unsupportedMethodCall(String methodName) {
124: String retVal = "Unsupported method " + methodName
125: + " on Data Source Alias."; //NOI18N
126: return new UnsupportedOperationException(retVal);
127: }
128:
129: public void setLogWriter(java.io.PrintWriter out)
130: throws java.sql.SQLException {
131: throw unsupportedMethodCall("setLogWriter()"); //NOI18N
132: }
133:
134: public void setSchemasInitialized(boolean schemasInitialized) {
135:
136: throw unsupportedMethodCall("setSchemasInitialized()"); //NOI18N
137: }
138:
139: public void setValidationQuery(String validationQuery) {
140:
141: throw unsupportedMethodCall("setValidationQuery()"); //NOI18N
142: }
143:
144: public void setValidationTable(String validationTable) {
145:
146: throw unsupportedMethodCall("setValidationTable()"); //NOI18N
147: }
148:
149: public void setUsername(String username) {
150:
151: throw unsupportedMethodCall("setUsername()"); //NOI18N
152: }
153:
154: public void setUrl(String url) {
155:
156: throw unsupportedMethodCall("setUrl()"); //NOI18N
157: }
158:
159: public void addSchema(String schema) {
160:
161: throw unsupportedMethodCall("addSchema()"); //NOI18N
162: }
163:
164: public void removeSchema(String schema) {
165:
166: throw unsupportedMethodCall("removeSchema()"); //NOI18N
167: }
168:
169: public void setDriverClassName(String driverClassName) {
170:
171: throw unsupportedMethodCall("setDriverClassName()"); //NOI18N
172: }
173:
174: public void setPassword(String password) {
175:
176: throw unsupportedMethodCall("setPassword()"); //NOI18N
177: }
178:
179: public void setLoginTimeout(int seconds)
180: throws java.sql.SQLException {
181:
182: throw unsupportedMethodCall("setLoginTimeout()"); //NOI18N
183: }
184:
185: public void setSchemas(java.util.Collection schemas) {
186:
187: throw unsupportedMethodCall("setSchemas()"); //NOI18N
188: }
189:
190: /**
191: * used for persisting this instance.
192: */
193: public String getTag(String key, int level, int tabWidth) {
194:
195: return getSpaces(level, tabWidth) + "<object name=\""
196: + escapeXML(key)
197: + "\" class=\""
198: + getClass().getName()
199: + "\">\n" //NOI18N
200:
201: + getSpaces(level + 1, tabWidth)
202: + "<arg class=\"java.lang.String\"" // NOI18N
203: + ((getAlias() == null) ? "" : " value=\""
204: + escapeXML(getAlias()) + "\"") // NOI18N
205: + "/>\n" // NOI18N
206:
207: + getSpaces(level, tabWidth) + "</object>\n"; // NOI18N
208: }
209:
210: public String toString() {
211: return "Alias for " + getAlias() + ""; //NOI18N
212: }
213:
214: public boolean test() {
215: DesignTimeDataSource dts = getReferencedDataSource();
216: if (dts == null) {
217: return false;
218: }
219: return dts.test();
220: }
221:
222: public java.sql.SQLException getTestException() {
223: DesignTimeDataSource dts = getReferencedDataSource();
224: if (dts == null) {
225: return getReferenceNotFoundException();
226: }
227:
228: return dts.getTestException();
229: }
230:
231: public int getTestRowsReturned() {
232: DesignTimeDataSource dts = getReferencedDataSource();
233: if (dts == null) {
234: return DesignTimeDataSource.SQL_NOT_RUN;
235: }
236:
237: return dts.getTestRowsReturned();
238: }
239:
240: public boolean getTestConnectionSucceeded() {
241: DesignTimeDataSource dts = getReferencedDataSource();
242: if (dts == null) {
243: return false;
244: }
245: return dts.getTestConnectionSucceeded();
246: }
247:
248: public boolean getSchemasInitialized() {
249:
250: DesignTimeDataSource dts = getReferencedDataSource();
251: if (dts == null) {
252: return false;
253: }
254: return dts.getSchemasInitialized();
255: }
256:
257: /**
258: * gets schemas selected for this datasource, an empty set means all schemas
259: */
260: public String[] getSchemas() {
261:
262: DesignTimeDataSource dts = getReferencedDataSource();
263: if (dts == null) {
264: return null;
265: }
266:
267: return dts.getSchemas();
268: }
269:
270: public String getPassword() {
271:
272: DesignTimeDataSource dts = getReferencedDataSource();
273: if (dts == null) {
274: return null;
275: }
276:
277: return dts.getPassword();
278: }
279:
280: public int getLoginTimeout() throws java.sql.SQLException {
281:
282: DesignTimeDataSource dts = getReferencedDataSource();
283: if (dts == null) {
284: return 0;
285: }
286:
287: return dts.getLoginTimeout();
288: }
289:
290: public java.io.PrintWriter getLogWriter()
291: throws java.sql.SQLException {
292:
293: DesignTimeDataSource dts = getReferencedDataSource();
294: if (dts == null) {
295: return null;
296: }
297:
298: return dts.getLogWriter();
299: }
300:
301: public String getDriverClassName() {
302:
303: DesignTimeDataSource dts = getReferencedDataSource();
304: if (dts == null) {
305: return null;
306: }
307:
308: return dts.getDriverClassName();
309: }
310:
311: public java.sql.Connection getConnection(String username,
312: String password) throws java.sql.SQLException {
313:
314: DesignTimeDataSource dts = getReferencedDataSource();
315: if (dts == null) {
316: throw getReferenceNotFoundException();
317: }
318: return dts.getConnection(username, password);
319: }
320:
321: public java.sql.Connection getConnection()
322: throws java.sql.SQLException {
323:
324: DesignTimeDataSource dts = getReferencedDataSource();
325: if (dts == null) {
326: throw getReferenceNotFoundException();
327: }
328: return dts.getConnection();
329: }
330:
331: public void clearSchemas() {
332: DesignTimeDataSource dts = getReferencedDataSource();
333: if (dts == null) {
334: return;
335: }
336: dts.clearSchemas();
337: }
338:
339: public String getUrl() {
340:
341: DesignTimeDataSource dts = getReferencedDataSource();
342: if (dts == null) {
343: return null;
344: }
345:
346: return dts.getUrl();
347: }
348:
349: public String getUsername() {
350:
351: DesignTimeDataSource dts = getReferencedDataSource();
352: if (dts == null) {
353: return null;
354: }
355:
356: return dts.getUsername();
357: }
358:
359: public String getValidationQuery() {
360:
361: DesignTimeDataSource dts = getReferencedDataSource();
362: if (dts == null) {
363: return null;
364: }
365:
366: return dts.getValidationQuery();
367: }
368:
369: public String getValidationTable() {
370:
371: DesignTimeDataSource dts = getReferencedDataSource();
372: if (dts == null) {
373: return null;
374: }
375:
376: return dts.getValidationTable();
377: }
378:
379: public void initSchemas() throws java.sql.SQLException,
380: javax.naming.NamingException {
381:
382: DesignTimeDataSource dts = getReferencedDataSource();
383: if (dts == null) {
384: throw getReferenceNotFoundException();
385: }
386:
387: dts.initSchemas();
388: }
389:
390: /***
391: * generate an SQLException that the referenced data source
392: * was not found.
393: */
394: private java.sql.SQLException getReferenceNotFoundException() {
395: String msg = java.text.MessageFormat.format(rb
396: .getString("REFERENCE_NOT_FOUND"),
397: new Object[] { getAlias() });
398: return new java.sql.SQLException(msg);
399:
400: }
401:
402: }
|