KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > ozoneDB > RemoteDatabase


1 // You can redistribute this software and/or modify it under the terms of
2
// the Ozone Library License version 1 published by ozone-db.org.
3
//
4
// The original code and portions created by SMB are
5
// Copyright (C) 1997-@year@ by SMB GmbH. All rights reserved.
6
//
7
// $Id: RemoteDatabase.java,v 1.5 2002/09/18 06:54:13 per_nyfelt Exp $
8

9 package org.ozoneDB;
10
11 import org.ozoneDB.core.DbRemote.*;
12
13 import java.io.IOException JavaDoc;
14 import java.util.Hashtable JavaDoc;
15
16
17 /**
18  * This class represents a remote database server that is connected via
19  * a network connection. For a detailed description of the API see {@link
20  * OzoneInterface}.
21  *
22  *
23  * @author <a HREF="http://www.softwarebuero.de/">SMB</a>
24  * @version $Revision: 1.5 $Date: 2002/09/18 06:54:13 $
25  * @see OzoneInterface
26  */

27 public final class RemoteDatabase extends ExternalDatabase {
28     
29     protected String JavaDoc hostname;
30     
31     protected int portNum;
32     
33     protected String JavaDoc userName;
34     
35     protected String JavaDoc passwd;
36     
37     
38     public RemoteDatabase() {
39     }
40     
41     
42     /**
43      * Open a new database connection.
44      * @param hostName name of the host where the server resides
45      * @param portNum port of the server (default server setting: 3000)
46      */

47     public void open( String JavaDoc hostName, int portNum ) throws Exception JavaDoc {
48         String JavaDoc userName = System.getProperty( "user.name" );
49         open( hostName, portNum, userName, userName );
50     }
51     
52     
53     /**
54      * Open a new database connection.
55      * @param hostName name of the host where the server resides
56      * @param portNum port of the server (default server setting: 3000)
57      * @param userName
58      * @param passwd
59      */

60     public void open( String JavaDoc hostName, int portNum, String JavaDoc userName, String JavaDoc passwd ) throws Exception JavaDoc {
61         Hashtable JavaDoc props = new Hashtable JavaDoc();
62         props.put( PROP_HOST, hostName );
63         props.put( PROP_PORT, new Integer JavaDoc( portNum ) );
64         props.put( PROP_USER, userName );
65         props.put( PROP_PASSWD, passwd );
66         open( props );
67     }
68     
69     
70     /**
71      * @param _props The properties for the new connection.
72      */

73     protected synchronized void open( Hashtable JavaDoc _props ) throws Exception JavaDoc {
74         try {
75             super.open( _props );
76             
77             hostname = (String JavaDoc)_props.get( PROP_HOST );
78             portNum = 0;
79             if (_props.get( PROP_PORT ) != null) {
80                 portNum = ((Integer JavaDoc)_props.get( PROP_PORT )).intValue();
81             }
82             userName = (String JavaDoc)_props.get( PROP_USER );
83             passwd = (String JavaDoc)_props.get( PROP_HOST );
84             
85             if (hostname == null || portNum == 0) {
86                 throw new RuntimeException JavaDoc( "No host and/or port specified." );
87             }
88             
89             // try to open one real database connection to see if we are able to
90
// connect the server
91
DbClient connection = acquirePooledConnection();
92             releasePooledConnection( connection );
93         }
94         catch (Exception JavaDoc e) {
95             close();
96             throw e;
97         }
98     }
99     
100     
101     protected DbClient newConnection() throws Exception JavaDoc,DatabaseNotOpenException,IOException JavaDoc,org.ozoneDB.TransactionException,java.lang.ClassNotFoundException JavaDoc,org.ozoneDB.OzoneObjectException {
102         if (portNum == 0) {
103             throw new DatabaseNotOpenException();
104         }
105         DbClient connection = new DbRemoteClient(this,hostname,portNum,userName);
106
107         sendCommand(new DbOpen(userName),true,connection);
108
109         return connection;
110     }
111     
112     
113     public String JavaDoc toString() {
114         return "RemoteDatabase: " + hostname + ", " + portNum;
115     }
116     
117     
118     protected void finalize() throws Exception JavaDoc {
119         close();
120     }
121     
122 }
123
Popular Tags