001: /*
002: (c) Copyright 2006, 2007, 2008 Hewlett-Packard Development Company, LP
003: All rights reserved - see end of file.
004: $Id: ConnectionAssembler.java,v 1.11 2008/01/02 12:09:36 andy_seaborne Exp $
005: */
006:
007: package com.hp.hpl.jena.assembler.assemblers;
008:
009: import com.hp.hpl.jena.JenaRuntime;
010: import com.hp.hpl.jena.assembler.*;
011: import com.hp.hpl.jena.rdf.model.*;
012:
013: /**
014: A ConnectionAssembler assembles a ConnectionDescription object which
015: contains a database URL, user name, user password, and database type.
016: Some of the components may have been specified in advance when the
017: Assembler was constructed. The ConnectionAssembler will also load any
018: classes specified by dbClass[Property] statements of the root.
019:
020: @author kers
021: */
022: public class ConnectionAssembler extends AssemblerBase implements
023: Assembler {
024: public final String defaultURL;
025: public final String defaultUser;
026: public final String defaultPassword;
027: public final String defaultType;
028:
029: protected static final Resource emptyRoot = ModelFactory
030: .createDefaultModel().createResource();
031:
032: public ConnectionAssembler(Resource init) {
033: defaultUser = get(init, "dbUser", null);
034: defaultPassword = get(init, "dbPassword", null);
035: defaultURL = get(init, "dbURL", null);
036: defaultType = get(init, "dbType", null);
037: }
038:
039: public ConnectionAssembler() {
040: this (emptyRoot);
041: }
042:
043: public Object open(Assembler a, Resource root, Mode irrelevant) {
044: checkType(root, JA.Connection);
045: String dbUser = getUser(root), dbPassword = getPassword(root);
046: String dbURL = getURL(root), dbType = getType(root);
047: loadClasses(root);
048: return createConnection(root.getURI(), dbURL, dbType, dbUser,
049: dbPassword);
050: }
051:
052: /**
053: Load all the classes that are named by the object of dbClass statements
054: of <code>root</code>. Load all the classes named by the contents of
055: system properties which are the objects of dbClassProperty statements
056: of <code>root</code>.
057: */
058: private void loadClasses(Resource root) {
059: for (StmtIterator it = root.listProperties(JA.dbClassProperty); it
060: .hasNext();) {
061: String propertyName = getString(it.nextStatement());
062: String className = JenaRuntime
063: .getSystemProperty(propertyName);
064: loadClass(root, className);
065: }
066: for (StmtIterator it = root.listProperties(JA.dbClass); it
067: .hasNext();) {
068: String className = getString(it.nextStatement());
069: loadClass(root, className);
070: }
071: }
072:
073: protected ConnectionDescription createConnection(String subject,
074: String dbURL, String dbType, String dbUser,
075: String dbPassword) {
076: return ConnectionDescription.create(subject, dbURL, dbUser,
077: dbPassword, dbType);
078: }
079:
080: public String getUser(Resource root) {
081: return get(root, "dbUser", defaultUser);
082: }
083:
084: public String getPassword(Resource root) {
085: return get(root, "dbPassword", defaultPassword);
086: }
087:
088: public String getURL(Resource root) {
089: return get(root, "dbURL", defaultURL);
090: }
091:
092: public String getType(Resource root) {
093: return get(root, "dbType", defaultType);
094: }
095:
096: protected String get(Resource root, String label, String ifAbsent) {
097: Property property = JA.property(label);
098: RDFNode L = getUnique(root, property);
099: return L == null ? getIndirect(root, label, ifAbsent) : L
100: .isLiteral() ? ((Literal) L).getLexicalForm()
101: : ((Resource) L).getURI();
102: }
103:
104: private String getIndirect(Resource root, String label,
105: String ifAbsent) {
106: Property property = JA.property(label + "Property");
107: Literal name = getUniqueLiteral(root, property);
108: return name == null ? ifAbsent : JenaRuntime
109: .getSystemProperty(name.getLexicalForm());
110: }
111: }
112:
113: /*
114: * (c) Copyright 2006, 2007, 2008 Hewlett-Packard Development Company, LP
115: * All rights reserved.
116: *
117: * Redistribution and use in source and binary forms, with or without
118: * modification, are permitted provided that the following conditions
119: * are met:
120: * 1. Redistributions of source code must retain the above copyright
121: * notice, this list of conditions and the following disclaimer.
122: * 2. Redistributions in binary form must reproduce the above copyright
123: * notice, this list of conditions and the following disclaimer in the
124: * documentation and/or other materials provided with the distribution.
125: * 3. The name of the author may not be used to endorse or promote products
126: * derived from this software without specific prior written permission.
127: *
128: * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
129: * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
130: * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
131: * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
132: * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
133: * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
134: * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
135: * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
136: * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
137: * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
138: */
|