KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > hp > hpl > jena > rdf > model > impl > RDBMakerCreator


1 /*
2   (c) Copyright 2003, 2004, 2005 Hewlett-Packard Development Company, LP
3   [See end of file]
4   $Id: RDBMakerCreator.java,v 1.9 2005/04/11 15:21:32 chris-dollin Exp $
5 */

6
7 package com.hp.hpl.jena.rdf.model.impl;
8
9 import com.hp.hpl.jena.db.IDBConnection;
10 import com.hp.hpl.jena.rdf.model.*;
11 import com.hp.hpl.jena.shared.*;
12 import com.hp.hpl.jena.vocabulary.*;
13
14 /**
15     An RDBMakerCreator makes an RDBModelMaker from its RDF description.
16     This code probably belongs in the jena.db package.
17
18     @author hedgehog
19  */

20
21 public class RDBMakerCreator implements ModelMakerCreator
22     {
23     public ModelMaker create( Model desc, Resource root )
24         {
25         return ModelFactory.createModelRDBMaker( createConnection( desc, root ) );
26         }
27
28     public static IDBConnection createConnection( Model description, Resource root )
29         {
30         Resource connection = description.listStatements( root, JenaModelSpec.hasConnection, (RDFNode) null ).nextStatement().getResource();
31         String JavaDoc url = getURL( description, connection, JenaModelSpec.dbURL );
32         String JavaDoc user = getString( description, connection, JenaModelSpec.dbUser );
33         String JavaDoc password = getString( description, connection , JenaModelSpec.dbPassword );
34         String JavaDoc className = getClassName( description, connection );
35         String JavaDoc dbType = getString( description, connection, JenaModelSpec.dbType );
36         loadDrivers( dbType, className );
37         return ModelFactory.createSimpleRDBConnection( url, user, password, dbType );
38         }
39
40     public static String JavaDoc getClassName( Model description, Resource root )
41         {
42         Statement cnStatement = description.getProperty( root, JenaModelSpec.dbClass );
43         return cnStatement == null ? null : cnStatement.getString();
44         }
45     
46     public static String JavaDoc getURL( Model description, Resource root, Property p )
47         {
48         return description.getRequiredProperty( root, p ).getResource().getURI();
49         }
50
51     public static String JavaDoc getString( Model description, Resource root, Property p )
52         {
53         return description.getRequiredProperty( root, p ).getString();
54         }
55     
56     public static void loadDrivers( String JavaDoc dbType, String JavaDoc className )
57         {
58         try
59             {
60             Class.forName( "com.hp.hpl.jena.db.impl.Driver_" + dbType );
61             if (className != null) Class.forName( className );
62             }
63         catch (ClassNotFoundException JavaDoc c)
64             { throw new JenaException( c ); }
65         }
66 }
67
68 /*
69     (c) Copyright 2003, 2004, 2005 Hewlett-Packard Development Company, LP
70     All rights reserved.
71
72     Redistribution and use in source and binary forms, with or without
73     modification, are permitted provided that the following conditions
74     are met:
75
76     1. Redistributions of source code must retain the above copyright
77        notice, this list of conditions and the following disclaimer.
78
79     2. Redistributions in binary form must reproduce the above copyright
80        notice, this list of conditions and the following disclaimer in the
81        documentation and/or other materials provided with the distribution.
82
83     3. The name of the author may not be used to endorse or promote products
84        derived from this software without specific prior written permission.
85
86     THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
87     IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
88     OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
89     IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
90     INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
91     NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
92     DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
93     THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
94     (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
95     THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
96 */
Popular Tags