KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > derby > jdbc > ClientDataSource40


1 /*
2  
3    Derby - Class org.apache.derby.jdbc.ClientDataSource40
4  
5    Licensed to the Apache Software Foundation (ASF) under one or more
6    contributor license agreements. See the NOTICE file distributed with
7    this work for additional information regarding copyright ownership.
8    The ASF licenses this file to You under the Apache License, Version 2.0
9    (the "License"); you may not use this file except in compliance with
10    the License. You may obtain a copy of the License at
11  
12       http://www.apache.org/licenses/LICENSE-2.0
13  
14    Unless required by applicable law or agreed to in writing, software
15    distributed under the License is distributed on an "AS IS" BASIS,
16    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17    See the License for the specific language governing permissions and
18    limitations under the License.
19  
20  */

21
22 package org.apache.derby.jdbc;
23
24 import java.sql.SQLException JavaDoc;
25 import javax.sql.DataSource JavaDoc;
26 import org.apache.derby.client.am.ClientMessageId;
27 import org.apache.derby.client.am.SqlException;
28 import org.apache.derby.shared.common.reference.SQLState;
29
30 /**
31  * ClientDataSource40 is a simple data source implementation
32  * that can be used for establishing connections in a
33  * non-pooling, non-distributed environment.
34  * The class ClientConnectionPoolDataSource40 can be used in a connection pooling environment,
35  * and the class ClientXADataSource40 can be used in a distributed, and pooling
36  * environment. Use these DataSources if your application runs under
37  * JDBC4.0. Use the corresponding ClientDataSource, ClientConnectionPoolDataSource, and
38  * ClientXADataSource classes if
39  * your application runs in the following environments:
40  * <p/>
41  * <UL>
42  * <LI> JDBC 3.0 - Java 2 - JDK 1.4, J2SE 5.0
43  * <LI> JDBC 2.0 - Java 2 - JDK 1.2,1.3
44  * </UL>
45  *
46  * <p>The example below registers a DNC data source object with a JNDI naming service.
47  * <pre>
48  * org.apache.derby.client.ClientDataSource40 dataSource = new org.apache.derby.client.ClientDataSource40 ();
49  * dataSource.setServerName ("my_derby_database_server");
50  * dataSource.setDatabaseName ("my_derby_database_name");
51  * javax.naming.Context context = new javax.naming.InitialContext();
52  * context.bind ("jdbc/my_datasource_name", dataSource);
53  * </pre>
54  * The first line of code in the example creates a data source object.
55  * The next two lines initialize the data source's
56  * properties. Then a Java object that references the initial JNDI naming
57  * context is created by calling the
58  * InitialContext() constructor, which is provided by JNDI.
59  * System properties (not shown) are used to tell JNDI the
60  * service provider to use. The JNDI name space is hierarchical,
61  * similar to the directory structure of many file
62  * systems. The data source object is bound to a logical JNDI name
63  * by calling Context.bind(). In this case the JNDI name
64  * identifies a subcontext, "jdbc", of the root naming context
65  * and a logical name, "my_datasource_name", within the jdbc
66  * subcontext. This is all of the code required to deploy
67  * a data source object within JNDI. This example is provided
68  * mainly for illustrative purposes. We expect that developers
69  * or system administrators will normally use a GUI tool to
70  * deploy a data source object.
71  * <p/>
72  * Once a data source has been registered with JNDI,
73  * it can then be used by a JDBC application, as is shown in the
74  * following example.
75  * <pre>
76  * javax.naming.Context context = new javax.naming.InitialContext ();
77  * javax.sql.DataSource dataSource = (javax.sql.DataSource) context.lookup ("jdbc/my_datasource_name");
78  * java.sql.Connection connection = dataSource.getConnection ("user", "password");
79  * </pre>
80  * The first line in the example creates a Java object
81  * that references the initial JNDI naming context. Next, the
82  * initial naming context is used to do a lookup operation
83  * using the logical name of the data source. The
84  * Context.lookup() method returns a reference to a Java Object,
85  * which is narrowed to a javax.sql.DataSource object. In
86  * the last line, the DataSource.getConnection() method
87  * is called to produce a database connection.
88  * <p/>
89  * This simple data source subclass of ClientBaseDataSource maintains
90  * it's own private <code>password</code> property.
91  * <p/>
92  * The specified password, along with the user, is validated by DERBY.
93  * This property can be overwritten by specifing
94  * the password parameter on the DataSource.getConnection() method call.
95  * <p/>
96  * This password property is not declared transient, and therefore
97  * may be serialized to a file in clear-text, or stored
98  * to a JNDI server in clear-text when the data source is saved.
99  * Care must taken by the user to prevent security
100  * breaches.
101  * <p/>
102  */

103 public class ClientDataSource40 extends ClientDataSource {
104     
105     public ClientDataSource40() {
106         super();
107     }
108     
109     /**
110      * Returns false unless <code>interfaces</code> is implemented
111      *
112      * @param interfaces a Class defining an interface.
113      * @return true if this implements the interface or
114      * directly or indirectly wraps an object
115      * that does.
116      * @throws java.sql.SQLException if an error occurs while determining
117      * whether this is a wrapper for an object
118      * with the given interface.
119      */

120     public boolean isWrapperFor(Class JavaDoc<?> interfaces) throws SQLException JavaDoc {
121         return interfaces.isInstance(this);
122     }
123     
124     /**
125      * Returns <code>this</code> if this class implements the interface
126      *
127      * @param interfaces a Class defining an interface
128      * @return an object that implements the interface
129      * @throws java.sql.SQLExption if no object if found that implements the
130      * interface
131      */

132     public <T> T unwrap(java.lang.Class JavaDoc<T> interfaces)
133                                    throws SQLException JavaDoc {
134         try {
135             return interfaces.cast(this);
136         } catch (ClassCastException JavaDoc cce) {
137             throw new SqlException(null,new ClientMessageId(SQLState.UNABLE_TO_UNWRAP),
138                     interfaces).getSQLException();
139         }
140     }
141     
142 }
143
Popular Tags