001: /*****************************************************************************
002: * Source code information
003: * -----------------------
004: * Original author Ian Dickinson, HP Labs Bristol
005: * Author email ian.dickinson@hp.com
006: * Package Jena 2
007: * Web http://jena.sourceforge.net
008: * Created 22-Aug-2003
009: * Filename $RCSfile: Main.java,v $
010: * Revision $Revision: 1.2 $
011: * Release status $State: Exp $
012: *
013: * Last modified on $Date: 2005/10/08 08:38:07 $
014: * by $Author: ian_dickinson $
015: *
016: * (c) Copyright 2002, 2003, 2004, 2005 Hewlett-Packard Development Company, LP
017: * (see footer for full conditions)
018: *****************************************************************************/package jena.examples.ontology.persistentOntology;
019:
020: // Imports
021: ///////////////
022: import com.hp.hpl.jena.ontology.*;
023: import com.hp.hpl.jena.rdf.model.ModelMaker;
024:
025: /**
026: * <p>
027: * Simple execution wrapper for persistent ontology example.
028: * </p>
029: * <p>
030: * Usage:
031: * <pre>
032: * java jena.examples.ontology.persistentOntology.Main
033: * [--dbUser string] e.g: --dbUser ijd
034: * [--dbURL string] e.g: --dbURL jdbc:postgresql://localhost/jenatest
035: * [--dbPw string] e.g: --dbPw nosecrets
036: * [--dbType string] e.g: --dbType PostgreSQL
037: * [--dbDriver string] e.g: --dbDriver org.postgresql.Driver
038: * [--reload] if true will reload the source data
039: * [sourceURL] optional source URL for the data to persist
040: * </pre>
041: * If no db parameters or source URL is given, defaults will be used.
042: * </p>
043: *
044: * @author Ian Dickinson, HP Labs
045: * (<a href="mailto:Ian.Dickinson@hp.com" >email</a>)
046: * @version CVS $Id: Main.java,v 1.2 2005/10/08 08:38:07 ian_dickinson Exp $
047: */
048: public class Main {
049: // Constants
050: //////////////////////////////////
051:
052: public static final String ONT1 = "urn:x-hp-jena:test1";
053: public static final String ONT2 = "urn:x-hp-jena:test2";
054:
055: public static final String DB_URL = "jdbc:postgresql://localhost/jenatest";
056: public static final String DB_USER = "ijd";
057: public static final String DB_PASSWD = "";
058: public static final String DB = "PostgreSQL";
059: public static final String DB_DRIVER = "org.postgresql.Driver";
060:
061: // Static variables
062: //////////////////////////////////
063:
064: // database connection parameters, with defaults
065: private static String s_dbURL = DB_URL;
066: private static String s_dbUser = DB_USER;
067: private static String s_dbPw = DB_PASSWD;
068: private static String s_dbType = DB;
069: private static String s_dbDriver = DB_DRIVER;
070:
071: // if true, reload the data
072: private static boolean s_reload = false;
073:
074: // source URL to load data from; if null, use default
075: private static String s_source;
076:
077: // Instance variables
078: //////////////////////////////////
079:
080: // Constructors
081: //////////////////////////////////
082:
083: // External signature methods
084: //////////////////////////////////
085:
086: public static void main(String[] args) {
087: processArgs(args);
088:
089: // check for default sources
090: if (s_source == null) {
091: s_source = getDefaultSource();
092: }
093:
094: // create the helper class we use to handle the persistent ontologies
095: PersistentOntology po = new PersistentOntology();
096:
097: // ensure the JDBC driver class is loaded
098: try {
099: Class.forName(s_dbDriver);
100: } catch (Exception e) {
101: System.err
102: .println("Failed to load the driver for the database: "
103: + e.getMessage());
104: System.err
105: .println("Have you got the CLASSPATH set correctly?");
106: }
107:
108: // are we re-loading the data this time?
109: if (s_reload) {
110:
111: // we pass cleanDB=true to clear out existing models
112: // NOTE: this will remove ALL Jena models from the named persistent store, so
113: // use with care if you have existing data stored
114: ModelMaker maker = po.getRDBMaker(s_dbURL, s_dbUser,
115: s_dbPw, s_dbType, true);
116:
117: // now load the source data into the newly cleaned db
118: po.loadDB(maker, s_source);
119: }
120:
121: // now we list the classes in the database, to show that the persistence worked
122: ModelMaker maker = po.getRDBMaker(s_dbURL, s_dbUser, s_dbPw,
123: s_dbType, false);
124: po.listClasses(maker, s_source);
125: }
126:
127: // Internal implementation methods
128: //////////////////////////////////
129:
130: /**
131: * Process any command line arguments
132: */
133: private static void processArgs(String[] args) {
134: int i = 0;
135: while (i < args.length) {
136: String arg = args[i++];
137:
138: if (arg.equals("--dbUser")) {
139: s_dbURL = args[i++];
140: } else if (arg.equals("--dbURL")) {
141: s_dbURL = args[i++];
142: } else if (arg.equals("--dbPasswd")) {
143: s_dbPw = args[i++];
144: } else if (arg.equals("--dbType")) {
145: s_dbType = args[i++];
146: } else if (arg.equals("--reload")) {
147: s_reload = true;
148: } else if (arg.equals("--dbDriver")) {
149: s_dbDriver = args[i++];
150: } else {
151: // assume this is a URL to load data from
152: s_source = arg;
153: }
154: }
155: }
156:
157: /**
158: * Answer the default source document, and set up the document manager
159: * so that we can find it on the file system
160: *
161: * @return The URI of the default source document
162: */
163: private static String getDefaultSource() {
164: // use the ont doc mgr to map from a generic URN to a local source file
165: OntDocumentManager.getInstance().addAltEntry(ONT1,
166: "file:src-examples/data/test1.owl");
167: OntDocumentManager.getInstance().addAltEntry(ONT2,
168: "file:src-examples/data/test2.owl");
169:
170: return ONT1;
171: }
172:
173: //==============================================================================
174: // Inner class definitions
175: //==============================================================================
176:
177: }
178:
179: /*
180: (c) Copyright 2002, 2003, 2004, 2005 Hewlett-Packard Development Company, LP
181: All rights reserved.
182:
183: Redistribution and use in source and binary forms, with or without
184: modification, are permitted provided that the following conditions
185: are met:
186:
187: 1. Redistributions of source code must retain the above copyright
188: notice, this list of conditions and the following disclaimer.
189:
190: 2. Redistributions in binary form must reproduce the above copyright
191: notice, this list of conditions and the following disclaimer in the
192: documentation and/or other materials provided with the distribution.
193:
194: 3. The name of the author may not be used to endorse or promote products
195: derived from this software without specific prior written permission.
196:
197: THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
198: IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
199: OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
200: IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
201: INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
202: NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
203: DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
204: THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
205: (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
206: THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
207: */
|