001: /*
002: (c) Copyright 2006, 2007, 2008 Hewlett-Packard Development Company, LP
003: All rights reserved - see end of file.
004: $Id: ConnectionDescription.java,v 1.7 2008/01/03 15:19:07 chris-dollin Exp $
005: */
006:
007: package com.hp.hpl.jena.assembler;
008:
009: import com.hp.hpl.jena.db.IDBConnection;
010: import com.hp.hpl.jena.rdf.model.ModelFactory;
011: import com.hp.hpl.jena.shared.JenaException;
012:
013: /**
014: A ConnectionDescription holds the information required to construct an
015: IDBConnection, and can construct that connection on demand.
016:
017: @author kers
018: */
019: public class ConnectionDescription {
020: public final String subject;
021: public final String dbURL;
022: public final String dbUser;
023: public final String dbPassword;
024: public final String dbType;
025:
026: protected IDBConnection connection;
027:
028: /**
029: Initialise a description from the given arguments.
030:
031: @param subject the URI of the subject node from which this description is created
032: @param dbURL the URL of the database to connect to
033: @param dbUser the name of the user authorising the connection
034: @param dbPassword the password of that user
035: @param dbType the type of the database
036: */
037: public ConnectionDescription(String subject, String dbURL,
038: String dbUser, String dbPassword, String dbType) {
039: this .subject = subject;
040: this .dbURL = dbURL;
041: this .dbUser = dbUser;
042: this .dbPassword = dbPassword;
043: this .dbType = dbType;
044: }
045:
046: /**
047: Answer the connection specified by the description. Once created, that same
048: connection is returned for each call to getConnection.
049: */
050: public IDBConnection getConnection() {
051: if (connection == null) {
052: if (dbURL == null || dbType == null)
053: throw new JenaException(
054: "this connection "
055: + this
056: + " cannot be opened because no dbURL or dbType was specified");
057: connection = ModelFactory.createSimpleRDBConnection(dbURL,
058: dbUser, dbPassword, dbType);
059: }
060: return connection;
061: }
062:
063: /**
064: Answer a description containing the specified components.
065: */
066: public static ConnectionDescription create(String subject,
067: String dbURL, String dbUser, String dbPassword,
068: String dbType) {
069: return new ConnectionDescription(subject, dbURL, dbUser,
070: dbPassword, dbType);
071: }
072:
073: /**
074: Answer a descriptive string for this connection object, including whether or
075: not it has already had its connection opened.
076: @see java.lang.Object#toString()
077: */
078: public String toString() {
079: return "UrlConnection (" + "subject=" + subject + " url="
080: + dbURL + " type=" + dbType + " user=" + dbUser
081: + " password=" + dbPassword
082: + (connection == null ? " unopened" : " opened") + ")";
083: }
084:
085: }
086:
087: /*
088: * (c) Copyright 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: */
|