001: // You can redistribute this software and/or modify it under the terms of
002: // the Ozone Library License version 1 published by ozone-db.org.
003: //
004: // The original code and portions created by SMB are
005: // Copyright (C) 1997-@year@ by SMB GmbH. All rights reserved.
006: //
007: // $Id: RemoteDatabase.java,v 1.1 2001/12/18 10:31:30 per_nyfelt Exp $
008:
009: package org.ozoneDB;
010:
011: import java.io.*;
012: import java.util.*;
013: import org.ozoneDB.DxLib.*;
014: import org.ozoneDB.DxLib.net.*;
015: import org.ozoneDB.core.*;
016: import org.ozoneDB.util.*;
017: import org.ozoneDB.core.DbRemote.*;
018:
019: /**
020: * This class represents a remote database server that is connected via
021: * a network connection. For a detailed description of the API see {@link
022: * OzoneInterface}.
023: *
024: *
025: * @author <a href="http://www.softwarebuero.de/">SMB</a>
026: * @version $Revision: 1.1 $Date: 2001/12/18 10:31:30 $
027: * @see OzoneInterface
028: */
029: public final class RemoteDatabase extends ExternalDatabase {
030:
031: protected String hostname;
032:
033: protected int portNum;
034:
035: protected String userName;
036:
037: protected String passwd;
038:
039: public RemoteDatabase() {
040: }
041:
042: /**
043: * Open a new database connection.
044: * @param hostname name of the host where the server resides
045: * @param port port of the server (default server setting: 3000)
046: */
047: public void open(String _hostName, int _portNum) throws Exception {
048: String userName = System.getProperty("user.name");
049: open(_hostName, _portNum, userName, userName);
050: }
051:
052: /**
053: * Open a new database connection.
054: * @param hostname name of the host where the server resides
055: * @param port port of the server (default server setting: 3000)
056: * @param userName
057: * @param passwd
058: */
059: public void open(String _hostName, int _portNum, String _userName,
060: String _passwd) throws Exception {
061: Hashtable props = new Hashtable();
062: props.put(PROP_HOST, _hostName);
063: props.put(PROP_PORT, new Integer(_portNum));
064: props.put(PROP_USER, _userName);
065: props.put(PROP_PASSWD, _passwd);
066: open(props);
067: }
068:
069: /**
070: * @param _props The properties for the new connection.
071: */
072: protected synchronized void open(Hashtable _props) throws Exception {
073: try {
074: super .open(_props);
075:
076: hostname = (String) _props.get(PROP_HOST);
077: portNum = 0;
078: if (_props.get(PROP_PORT) != null) {
079: portNum = ((Integer) _props.get(PROP_PORT)).intValue();
080: }
081: userName = (String) _props.get(PROP_USER);
082: passwd = (String) _props.get(PROP_HOST);
083:
084: if (hostname == null || portNum == 0) {
085: throw new RuntimeException(
086: "No host and/or port specified.");
087: }
088:
089: // try to open one real database connection to see if we are able to
090: // connect the server
091: DbClient connection = acquirePooledConnection();
092: releasePooledConnection(connection);
093: } catch (Exception e) {
094: close();
095: throw e;
096: }
097: }
098:
099: protected DbClient newConnection() throws Exception {
100: if (portNum == 0) {
101: throw new DbNotOpenExc();
102: }
103: DbClient connection = new DbRemoteClient(this , hostname,
104: portNum, userName);
105: sendCommand(new DbOpen(userName), true, connection);
106: return connection;
107: }
108:
109: public String toString() {
110: return "RemoteDatabase: " + hostname + ", " + portNum;
111: }
112:
113: protected void finalize() throws Exception {
114: close();
115: }
116:
117: }
|