Project

General

Profile

1
/**
2
 *  '$RCSfile$'
3
 *    Purpose: A Class that implements utility methods for a metadata catalog
4
 *  Copyright: 2000 Regents of the University of California and the
5
 *             National Center for Ecological Analysis and Synthesis
6
 *    Authors: Matt Jones, Jivka Bojilova
7
 * 
8
 *   '$Author: bojilova $'
9
 *     '$Date: 2000-08-01 11:26:50 -0700 (Tue, 01 Aug 2000) $'
10
 * '$Revision: 309 $'
11
 */
12

    
13
package edu.ucsb.nceas.metacat;
14

    
15
import java.io.File;
16
import java.net.URL;
17
import java.net.MalformedURLException;
18
import java.sql.Connection;
19
import java.sql.DriverManager;
20
import java.sql.SQLException;
21
import java.util.PropertyResourceBundle;
22
import java.util.Hashtable;
23
import java.util.Enumeration;
24

    
25
/**
26
 * A suite of utility classes for the metadata catalog server
27
 */
28
public class MetaCatUtil {
29

    
30
  private PropertyResourceBundle options = null;
31
  private static String propertiesFile = "edu.ucsb.nceas.metacat.metacat";
32
  private Hashtable connectionPool = new Hashtable();
33
  private static boolean debug = false;
34

    
35
  /**
36
   * Construct an instance of the utility class
37
   */
38
  public MetaCatUtil() {
39
    options = (PropertyResourceBundle)
40
          PropertyResourceBundle.getBundle(propertiesFile);
41
  }
42

    
43
  /** 
44
   * Utility method to establish a JDBC database connection using connection
45
   * info from the properties file
46
   */
47
  public Connection openDBConnection()
48
                throws SQLException, ClassNotFoundException {
49
    return openDBConnection(getOption("dbDriver"), getOption("defaultDB"),
50
                     getOption("user"), getOption("password"));
51
  }
52

    
53
  /** 
54
   * Utility method to establish a JDBC database connection 
55
   *
56
   * @param dbDriver the string representing the database driver
57
   * @param connection the string representing the database connectin parameters
58
   * @param user name of the user to use for database connection
59
   * @param password password for the user to use for database connection
60
   */
61
  public static Connection openDBConnection(String dbDriver, String connection,
62
                String user, String password)
63
                throws SQLException, ClassNotFoundException {
64

    
65
     // Load the Oracle JDBC driver
66
     Class.forName (dbDriver);
67

    
68
     // Connect to the database
69
     Connection conn = DriverManager.getConnection( connection, user, password);
70
     return conn;
71
  }
72

    
73
  /** 
74
   * Utility method to get an option value from the properties file
75
   *
76
   * @param option_name the name of the option requested
77
   */
78
  public String getOption(String option_name) {
79
      // Get the configuration file information
80
      if (options == null) {
81
        options = (PropertyResourceBundle)
82
          PropertyResourceBundle.getBundle(propertiesFile);
83
      }
84
      String value = (String)options.handleGetObject(option_name);
85
      return value;
86
  }
87

    
88
  /* Utility method to create and return a pool of Connection objects */
89
  public Hashtable getConnectionPool()
90
                throws SQLException, ClassNotFoundException {
91

    
92
    int initConn = (new Integer(getOption("initialConnections"))).intValue();
93
    
94
    for ( int i = 0; i < initConn; i++ ) {
95
        connectionPool.put( openDBConnection(), Boolean.FALSE );
96
    }    
97
    
98
    return connectionPool;
99
  }
100
  
101
  /* Utility method to get a unused Connection object from the pool */
102
  public Connection getConnection()
103
                throws SQLException, ClassNotFoundException {
104

    
105
    Connection conn = null;
106
    Enumeration connections = connectionPool.keys();
107
    
108
    synchronized (connectionPool) {
109
      while (connections.hasMoreElements()) {
110
        conn = (Connection)connections.nextElement();
111
        Boolean b = (Boolean)connectionPool.get(conn);
112
        
113
        if (b == Boolean.FALSE) {
114
            // unused connection; test if it works and get it
115
            try {
116
                conn.setAutoCommit(true);
117
            } catch (SQLException e) {
118
                // problem with the connection, replace it
119
                conn = openDBConnection();
120
            }    
121
            // update the pool to show this one taken
122
            connectionPool.put(conn, Boolean.TRUE);    
123
            return conn;
124
        }    
125
      }  
126
    }    
127
    
128
    // If we get here, there no free connections;
129
    // we need to make more
130
    int incConn = (new Integer(getOption("incrementConnections"))).intValue();
131
    for ( int i = 0; i < incConn; i++ ) {
132
        connectionPool.put( openDBConnection(), Boolean.FALSE );
133
    }
134
    
135
    // Recurse to get one of the new connections
136
    return getConnection();
137
  }
138
  
139
  /* Utility method to give the connection back to the pool */
140
  public void returnConnection (Connection returned) {
141
    
142
    Connection conn;
143
    Enumeration connections = connectionPool.keys();
144
    
145
    while (connections.hasMoreElements()) {
146
        conn = (Connection)connections.nextElement();
147
        if ( conn == returned ) {
148
            connectionPool.put( conn, Boolean.FALSE );
149
            break;
150
        }    
151
    }    
152
  }
153
  
154
  
155
  /** Utility method to convert a file handle into a URL */
156
  public static URL fileToURL(File file)
157
  {
158
     String path = file.getAbsolutePath();
159
     String fSep = System.getProperty("file.separator");
160
     if (fSep != null && fSep.length() == 1)
161
       path = path.replace(fSep.charAt(0), '/');
162
     if (path.length() > 0 && path.charAt(0) != '/')
163
       path = '/' + path;
164
     try {
165
       return new URL("file", null, path);
166
     }
167
     catch (java.net.MalformedURLException e) {
168
       /* According to the spec this could only happen if the file
169
          protocol were not recognized. */
170
       throw new Error("unexpected MalformedURLException");
171
     }
172
  }
173

    
174
  /** 
175
   * Utility method to print debugging messages
176
   *
177
   * @param flag an integer indicating the message number
178
   */
179
  public static void debugMessage(int flag) {
180
    if (debug) {
181
      System.err.println("DEBUG FLAG: " + flag);
182
    }
183
  }
184

    
185
  /** 
186
   * Utility method to print debugging messages
187
   *
188
   * @param flag an integer indicating the message number
189
   */
190
  public static void debugMessage(String msg) {
191
    if (debug) {
192
      System.err.println(msg);
193
    }
194
  }
195
}
196

    
197
/**
198
 * '$Log$
199
 * 'Revision 1.10  2000/06/26 10:35:05  jones
200
 * 'Merged in substantial changes to DBWriter and associated classes and to
201
 * 'the MetaCatServlet in order to accomodate the new UPDATE and DELETE
202
 * 'functions.  The command line tools and the parameters for the
203
 * 'servlet have changed substantially.
204
 * '
205
 * 'Revision 1.9.2.4  2000/06/26 00:51:06  jones
206
 * 'If docid passed to DBWriter.write() is not unique, classes now generate
207
 * 'an AccessionNumberException containing the new docid generated as a
208
 * 'replacement.  The docid is then extracted from the exception and
209
 * 'returned to the calling application for user feedback or client processing.
210
 * '
211
 * 'Revision 1.9.2.3  2000/06/25 23:38:17  jones
212
 * 'Added RCSfile keyword
213
 * '
214
 * 'Revision 1.9.2.2  2000/06/25 23:34:18  jones
215
 * 'Changed documentation formatting, added log entries at bottom of source files
216
 * ''
217
 */
(22-22/26)