001: /*
002: (c) Copyright 2005, 2006, 2007, 2008 Hewlett-Packard Development Company, LP
003: All rights reserved - see end of file.
004: $Id: DriverMap.java,v 1.4 2008/01/02 12:08:23 andy_seaborne Exp $
005: */
006:
007: package com.hp.hpl.jena.db.impl;
008:
009: import java.io.InputStream;
010: import java.util.*;
011:
012: import com.hp.hpl.jena.rdf.model.*;
013: import com.hp.hpl.jena.util.FileManager;
014:
015: /**
016: The DriverMap statics maintain a map from short database type names to
017: database driver classes. The map contains some built-in items (for MySQL
018: and Postgres) and can be extended by files in the etc directory.
019:
020: @author kers
021: */
022: public class DriverMap {
023: public static final String uri = "http://db.jena.hpl.hp.com/vocabulary#";
024:
025: public static final Property driverClass = property("driverClass");
026: public static final Property driverName = property("driverName");
027:
028: private final static Map mapped = new HashMap();
029:
030: static {
031: add("mysql", "com.mysql.jdbc.Driver");
032: add("postgres", "org.postgresql.Driver");
033: add("postgresql", "org.postgresql.Driver");
034: addIfPresent("etc/db-default-drivers.n3");
035: addIfPresent("etc/db-extra-drivers.n3");
036: }
037:
038: /**
039: Answer the class name associated with the driver name in the map.
040: <code>name</code> is lower-cased before lookup.
041: */
042:
043: public static String get(String name) {
044: return (String) mapped.get(name.toLowerCase());
045: }
046:
047: /**
048: Add a mapping from a driver name to its class name. The driver name
049: is lower-cased before adding.
050: */
051: public static void add(String name, String className) {
052: mapped.put(name.toLowerCase(), className);
053: }
054:
055: /**
056: Add the mappings specified in the file named by <code>fileName</code>,
057: if it exists; that file must be an N3 file.
058: */
059: public static void addIfPresent(String fileName) {
060: InputStream in = FileManager.get().open(fileName);
061: if (in != null)
062: add(in);
063: }
064:
065: static void add(InputStream in) {
066: Property ANY = null;
067: Model m = ModelFactory.createDefaultModel();
068: m.read(in, "", "N3");
069: StmtIterator A = m.listStatements(ANY, DriverMap.driverClass,
070: ANY);
071: while (A.hasNext()) {
072: Statement st = A.nextStatement();
073: Resource S = st.getSubject();
074: String className = st.getString();
075: StmtIterator B = m.listStatements(S, DriverMap.driverName,
076: ANY);
077: while (B.hasNext())
078: add(B.nextStatement().getString(), className);
079: }
080: }
081:
082: static Property property(String s) {
083: return ResourceFactory.createProperty(uri + s);
084: }
085: }
086:
087: /*
088: * (c) Copyright 2005, 2006, 2007, 2008 Hewlett-Packard Development Company, LP
089: * All rights reserved.
090: *
091: * Redistribution and use in source and binary forms, with or without
092: * modification, are permitted provided that the following conditions
093: * are met:
094: * 1. Redistributions of source code must retain the above copyright
095: * notice, this list of conditions and the following disclaimer.
096: * 2. Redistributions in binary form must reproduce the above copyright
097: * notice, this list of conditions and the following disclaimer in the
098: * documentation and/or other materials provided with the distribution.
099: * 3. The name of the author may not be used to endorse or promote products
100: * derived from this software without specific prior written permission.
101: *
102: * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
103: * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
104: * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
105: * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
106: * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
107: * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
108: * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
109: * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
110: * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
111: * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
112: */
|