KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > joseki > server > source > SourceControllerRDB


1 /*
2  * (c) Copyright 2003, 2004 Hewlett-Packard Development Company, LP
3  * [See end of file]
4  */

5
6 package org.joseki.server.source;
7
8 import org.joseki.vocabulary.JosekiVocab ;
9
10 import com.hp.hpl.jena.rdf.model.* ;
11 import com.hp.hpl.jena.db.* ;
12
13 import org.joseki.server.SourceController;
14 import org.joseki.server.ModelSource;
15 import org.joseki.server.module.Loadable;
16 import org.apache.commons.logging.* ;
17
18 /** SourceController for RDF models held in databases.
19  *
20  * @author Andy Seaborne
21  * @version $Id: SourceControllerRDB.java,v 1.9 2004/04/30 14:13:14 andy_seaborne Exp $
22  */

23
24 public class SourceControllerRDB implements SourceController, Loadable
25 {
26     static Log logger = LogFactory.getLog(SourceControllerRDB.class.getName()) ;
27     
28     String JavaDoc serverURI ;
29     
30     // ----------------------------------------------------------
31
// -- Loadable interface
32

33     public String JavaDoc getInterfaceURI() { return JosekiVocab.SourceController.getURI() ; }
34     public void init(Resource binding, Resource implementation) {}
35     
36     // ----------------------------------------------------------
37
// -- SourceController interface
38

39     // Called once, during configuration
40
public ModelSource createSourceModel(Resource description, String JavaDoc _serverURI)
41     {
42         serverURI = _serverURI ;
43         
44         Resource dbURI = description.getProperty(JosekiVocab.attachedModel).getResource() ;
45         String JavaDoc user, password, dbType, modelName, driver ;
46         try
47         {
48             // Get database details
49
user = description.getRequiredProperty(JosekiVocab.user).getString() ;
50             password = description.getRequiredProperty(JosekiVocab.password).getString();
51             dbType = description.getRequiredProperty(JosekiVocab.dbType).getString();
52             modelName = description.getRequiredProperty(JosekiVocab.dbModelName).getString();
53             driver = description.getRequiredProperty(JosekiVocab.dbDriver).getString();
54         } catch (RDFException rdfEx)
55         {
56             logger.warn("Failed to gather all the database information: "+ rdfEx) ;
57             return null ;
58         }
59
60         try {
61             if ( driver != null )
62                 Class.forName(driver).newInstance();
63         } catch (Exception JavaDoc ex)
64         {
65             logger.warn("Failed to load the driver: "+ex.getMessage()) ;
66         }
67         
68         try {
69             IDBConnection conn = ModelFactory.createSimpleRDBConnection(dbURI.getURI(), user, password, dbType) ;
70             ModelRDB modelRDB = ModelRDB.open(conn, modelName) ;
71             ModelSource mSrc = new ModelSourcePermanent(this, modelRDB, serverURI) ;
72             logger.info("Connected to database: "+dbURI.getURI()) ;
73             return mSrc ;
74         } catch (RDFRDBException ex)
75         {
76             logger.warn("Failed to connect to database: "+ex.getMessage()) ;
77             logger.debug("Exception", ex) ;
78             if ( ex.getCause() != null )
79                 logger.warn("Connect failure cause : "+ex.getCause().getMessage()) ;
80             return null ;
81         }
82     }
83     
84     public String JavaDoc getServerURI() { return serverURI ; }
85     
86     // Called when used.
87
public void activate() { return ; }
88     
89     // Called when not in use any more.
90
public void deactivate() { return ; }
91     
92     // Called each time a source needs to be built.
93
public Model buildSource()
94     {
95         logger.warn("Attempt to build a database source") ;
96         return null ;
97     }
98     
99     // Called when released (if releasable)
100
public void releaseSource()
101     {
102         logger.warn("Attempt to release a database source") ;
103     }
104 }
105
106 /*
107  * (c) Copyright 2003, 2004 Hewlett-Packard Development Company, LP
108  * All rights reserved.
109  *
110  * Redistribution and use in source and binary forms, with or without
111  * modification, are permitted provided that the following conditions
112  * are met:
113  * 1. Redistributions of source code must retain the above copyright
114  * notice, this list of conditions and the following disclaimer.
115  * 2. Redistributions in binary form must reproduce the above copyright
116  * notice, this list of conditions and the following disclaimer in the
117  * documentation and/or other materials provided with the distribution.
118  * 3. The name of the author may not be used to endorse or promote products
119  * derived from this software without specific prior written permission.
120  *
121  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
122  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
123  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
124  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
125  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
126  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
127  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
128  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
129  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
130  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
131  */

132
Popular Tags