KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > hp > hpl > jena > db > DBConnection


1 /*
2  * (c) Copyright 2003, 2004, 2005 Hewlett-Packard Development Company, LP
3  * All rights reserved.
4  *
5  *
6  */

7
8 package com.hp.hpl.jena.db;
9
10 import java.sql.*;
11
12 import com.hp.hpl.jena.db.impl.*;
13 import com.hp.hpl.jena.graph.*;
14 import com.hp.hpl.jena.rdf.model.*;
15 import com.hp.hpl.jena.util.iterator.*;
16
17 /**
18 * Encapsulate the specification of a jdbc connection.
19 * This is mostly used to simplify the calling pattern for ModelRDB factory methods.
20 *
21 * @author csayers (based in part on the jena 1 implementation by der).
22 * @version $Revision: 1.14 $
23 */

24
25 public class DBConnection implements IDBConnection {
26
27     /** The jdbc connection being wrapped up */
28     protected Connection m_connection;
29
30     /** The url for the connection, may be null if the connection was passed in pre-opened */
31     protected String JavaDoc m_url;
32
33     /** The user name for the connection, may be null if the connection was passed in pre-opened */
34     protected String JavaDoc m_user;
35
36     /** The password for the connection, may be null if the connection was passed in pre-opened */
37     protected String JavaDoc m_password;
38
39     /** The database type: "Oracle", "mySQL, etc...
40      * This is new in Jena2 - for compatability with older code we allow this to
41      * be left unspecified at the loss of some jena2 functionality.
42      */

43     protected String JavaDoc m_databaseType = null;
44     
45     /** Driver to connect to this database */
46     protected IRDBDriver m_driver = null;
47         
48     
49     /**
50      * Create a connection specification based on jdbc address and
51      * appropriate authentication information.
52      * @param url the jdbc url for the database, note that the format of this
53      * is database dependent and that the appropriate jdbc driver will need to
54      * be specified via the standard pattern
55      * <pre>
56      * Class.forName("my.sql.driver");
57      * </pre>
58      * @param user the user name to log on with
59      * @param password the password corresponding to this user
60      * @deprecated As of Jena 2.0, it is recommended to use one of the DBConnection
61      * constructors which takes a database type as an argument. (The DBConnection can
62      * operate more efficiently if it knows the database type).
63      */

64     public DBConnection(String JavaDoc url, String JavaDoc user, String JavaDoc password) {
65         this( url, user, password, null);
66     }
67
68     /**
69      * Create a connection specification based on jdbc address and
70      * appropriate authentication information.
71      * @param url the jdbc url for the database, note that the format of this
72      * is database dependent and that the appropriate jdbc driver will need to
73      * be specified via the standard pattern
74      * <pre>
75      * Class.forName("my.sql.driver");
76      * </pre>
77      * @param user the user name to log on with
78      * @param password the password corresponding to this user
79      * @param databaseType the type of database to which we are connecting.
80      *
81      * @since Jena 2.0
82      */

83     public DBConnection(String JavaDoc url, String JavaDoc user, String JavaDoc password, String JavaDoc databaseType) {
84         m_url = url;
85         m_user = user;
86         m_password = password;
87         setDatabaseType(databaseType);
88     }
89
90     /**
91      * Create a connection specification that just wraps up an existing database
92      * connection.
93      * @param connection the open jdbc connection to use
94      * @deprecated As of Jena 2.0, it is recommended to use one of the DBConnection
95      * constructors which takes a database type as an argument. (The DBConnection can
96      * operate more efficiently if it knows the database type).
97      */

98     public DBConnection(Connection connection) {
99         this(connection, null);
100     }
101
102     /**
103      * Create a connection specification that just wraps up an existing database
104      * connection.
105      * @param connection the open jdbc connection to use
106      * @param databaseType the type of database to which we are connecting.
107      *
108      * @since Jena 2.0
109      */

110     public DBConnection(Connection connection, String JavaDoc databaseType) {
111         m_connection = connection;
112         setDatabaseType(databaseType);
113     }
114         
115
116     /* (non-Javadoc)
117      * @see com.hp.hpl.jena.db.IDBConnection#getConnection()
118      */

119     public Connection getConnection() throws SQLException {
120         if (m_connection == null) {
121             if (m_url != null) {
122                 m_connection =
123                     DriverManager.getConnection(m_url, m_user, m_password);
124                 m_connection.setAutoCommit(true);
125             }
126         }
127         return m_connection;
128     }
129
130     /* (non-Javadoc)
131      * @see com.hp.hpl.jena.db.IDBConnection#close()
132      */

133     public void close() throws SQLException {
134         if( m_driver != null ) {
135             m_driver.close();
136             m_driver = null;
137         }
138         if (m_connection != null) {
139             m_connection.close();
140             m_connection = null;
141         }
142     }
143
144     /* (non-Javadoc)
145      * @see com.hp.hpl.jena.db.IDBConnection#cleanDB()
146      */

147     public void cleanDB() throws SQLException {
148         if (m_driver == null)
149             m_driver = getDriver();
150         m_driver.cleanDB();
151     }
152
153     /* (non-Javadoc)
154      * @see com.hp.hpl.jena.db.IDBConnection#isFormatOK()
155      */

156     public boolean isFormatOK() {
157 // Removed exception trap, an exception might be a connection
158
// failure on a well formated database - der 24/7/04
159
// try {
160
if( m_driver == null )
161                 m_driver = getDriver();
162             return m_driver.isDBFormatOK();
163 // } catch (Exception e) {
164
// return false;
165
// }
166
}
167
168     /* (non-Javadoc)
169      * @see com.hp.hpl.jena.db.IDBConnection#setDatabaseProperties(com.hp.hpl.jena.rdf.model.Model)
170      */

171     public void setDatabaseProperties(Model dbProperties) throws RDFRDBException {
172         if (m_driver == null)
173             m_driver = getDriver();
174         m_driver.setDatabaseProperties( dbProperties.getGraph());
175     }
176
177     /* (non-Javadoc)
178      * @see com.hp.hpl.jena.db.IDBConnection#getDatabaseProperties()
179      */

180     public Model getDatabaseProperties() throws RDFRDBException {
181         if (m_driver == null)
182             m_driver = getDriver();
183         Model resultModel = ModelFactory.createDefaultModel();
184         copySpecializedGraphToModel( m_driver.getSystemSpecializedGraph(true),
185                                      resultModel,
186                                      Triple.createMatch( null, null, null ));
187         return resultModel;
188     }
189     
190     /* (non-Javadoc)
191      * @see com.hp.hpl.jena.db.IDBConnection#getDefaultModelProperties()
192      */

193     public Model getDefaultModelProperties() throws RDFRDBException {
194         if (m_driver == null)
195             m_driver = getDriver();
196         DBPropGraph defaultProps = m_driver.getDefaultModelProperties();
197         Model resultModel = ModelFactory.createDefaultModel();
198         copySpecializedGraphToModel( m_driver.getSystemSpecializedGraph(true),
199                                      resultModel,
200                                      Triple.createMatch(defaultProps.getNode(), null, null));
201         return resultModel;
202     }
203     
204     /* (non-Javadoc)
205      * @see com.hp.hpl.jena.db.IDBConnection#getAllModelNames()
206      */

207     public ExtendedIterator getAllModelNames() throws RDFRDBException {
208         if (m_driver == null)
209             m_driver = getDriver();
210         SpecializedGraph sg = m_driver.getSystemSpecializedGraph(false);
211         ExtendedIterator it;
212         if ( sg == null )
213             it = new ResultSetIterator();
214         else {
215             DBPropDatabase dbprops = new DBPropDatabase(sg);
216             it = dbprops.getAllGraphNames();
217         }
218         return it;
219     }
220     
221      /* (non-Javadoc)
222      * @see com.hp.hpl.jena.db.IDBConnection#containsModel(java.lang.String)
223      */

224     public boolean containsModel(String JavaDoc name) throws RDFRDBException {
225         boolean res = false;
226         if (m_driver == null)
227             m_driver = getDriver();
228         SpecializedGraph sg = m_driver.getSystemSpecializedGraph(false);
229         if ( sg != null ) {
230             DBPropGraph g = DBPropGraph.findPropGraphByName(sg,name);
231             res = g == null ? false : g.isDBPropGraphOk(name);
232         }
233         return res;
234      }
235
236      /* (non-Javadoc)
237      * @see com.hp.hpl.jena.db.IDBConnection#containsDefaultModel()
238      */

239     public boolean containsDefaultModel() throws RDFRDBException {
240         return containsModel(GraphRDB.DEFAULT);
241      }
242
243     /**
244      * Copy the contents of a specialized graph to a new Model.
245      *
246      * This has package scope - for internal use only.
247      *
248      * @since Jena 2.0
249      */

250     static void copySpecializedGraphToModel( SpecializedGraph fromGraph, Model toModel, TripleMatch filter) throws RDFRDBException {
251         Graph toGraph = toModel.getGraph();
252         SpecializedGraph.CompletionFlag complete = new SpecializedGraph.CompletionFlag();
253         ExtendedIterator it = fromGraph.find( filter, complete);
254         while(it.hasNext())
255             toGraph.add((Triple)(it.next()));
256         it.close();
257     }
258         
259     /* (non-Javadoc)
260      * @see com.hp.hpl.jena.db.IDBConnection#setDatabaseType(java.lang.String)
261      */

262     public void setDatabaseType( String JavaDoc databaseType ) {
263         if (databaseType != null) {
264             if (databaseType.compareToIgnoreCase("mysql") == 0) {
265                     m_databaseType = "MySQL";
266             } else {
267                 m_databaseType = databaseType;
268             }
269         }
270                     
271     }
272     
273     /* (non-Javadoc)
274      * @see com.hp.hpl.jena.db.IDBConnection#getDatabaseType()
275      */

276     public String JavaDoc getDatabaseType() { return m_databaseType; }
277     
278     /* (non-Javadoc)
279      * @see com.hp.hpl.jena.db.IDBConnection#getDriver()
280      */

281     public IRDBDriver getDriver() throws RDFRDBException {
282         try {
283             if (m_connection == null)
284                 getConnection();
285
286             if (m_driver == null) {
287                 // need to look for a suitable driver
288
if (m_databaseType == null) {
289                     // without knowing the database type there's not much we can do.
290
throw new RDFRDBException("Error - attempt to call DBConnection.getDriver before setting the database type");
291                 }
292                 m_driver = (IRDBDriver) (Class.forName("com.hp.hpl.jena.db.impl.Driver_" + m_databaseType).newInstance());
293                 m_driver.setConnection( this );
294             }
295         } catch (Exception JavaDoc e) {
296             // e.printStackTrace( System.err );
297
throw new RDFRDBException("Failure to instantiate DB Driver:"+ m_databaseType+ " "+ e.toString());
298         }
299
300         return m_driver;
301     }
302
303     /* (non-Javadoc)
304      * @see com.hp.hpl.jena.db.IDBConnection#setDriver(com.hp.hpl.jena.db.impl.IRDBDriver)
305      */

306     public void setDriver(IRDBDriver driver) {
307         m_driver = driver;
308     }
309
310     /**
311      * Helper function to locate and instantiate the driver class corresponding
312      * to a given layout and database name
313      * Throws an RDFRDBexception if the driver can't be instantiated
314      * @deprecated As of Jena 2.0 this call should not be used. Instead specify the database type
315      * when constructing a DBConnection and then pass that connection to the GraphRDB. There is
316      * no longer any need for applications to interact with the IRDBDriver. To customize the
317      * database configuration/layout use the formatDB(propertyModel) call.
318      */

319     public IRDBDriver getDriver(String JavaDoc layout, String JavaDoc database) throws RDFRDBException {
320         // the layout is not supported in Jena2 - ignore this parameter
321
setDatabaseType(database);
322         return getDriver();
323     }
324     
325 }
326
327 /*
328  * (c) Copyright 2003, 2004, 2005 Hewlett-Packard Development Company, LP
329  * All rights reserved.
330  *
331  * Redistribution and use in source and binary forms, with or without
332  * modification, are permitted provided that the following conditions
333  * are met:
334  * 1. Redistributions of source code must retain the above copyright
335  * notice, this list of conditions and the following disclaimer.
336  * 2. Redistributions in binary form must reproduce the above copyright
337  * notice, this list of conditions and the following disclaimer in the
338  * documentation and/or other materials provided with the distribution.
339  * 3. The name of the author may not be used to endorse or promote products
340  * derived from this software without specific prior written permission.
341
342  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
343  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
344  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
345  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
346  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
347  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
348  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
349  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
350  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
351  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
352  */

353
Popular Tags