1 |
295
|
berkley
|
/**
|
2 |
|
|
* '$RCSfile$'
|
3 |
298
|
berkley
|
* Purpose: A Class that implements utility methods for marineServlet.java
|
4 |
295
|
berkley
|
* Copyright: 2000 Regents of the University of California and the
|
5 |
|
|
* National Center for Ecological Analysis and Synthesis
|
6 |
|
|
* Authors: Matt Jones
|
7 |
|
|
*
|
8 |
|
|
* '$Author$'
|
9 |
|
|
* '$Date$'
|
10 |
|
|
* '$Revision$'
|
11 |
|
|
*/
|
12 |
298
|
berkley
|
package edu.ucsb.nceas.metacat.marine;
|
13 |
295
|
berkley
|
|
14 |
|
|
import java.io.File;
|
15 |
|
|
import java.net.URL;
|
16 |
|
|
import java.net.MalformedURLException;
|
17 |
|
|
import java.sql.Connection;
|
18 |
|
|
import java.sql.DriverManager;
|
19 |
|
|
import java.sql.SQLException;
|
20 |
|
|
import java.util.PropertyResourceBundle;
|
21 |
|
|
import oracle.jdbc.driver.*;
|
22 |
|
|
|
23 |
|
|
/**
|
24 |
|
|
* A suite of utility classes for the metadata catalog server
|
25 |
|
|
*/
|
26 |
|
|
public class marineUtil {
|
27 |
|
|
|
28 |
|
|
private PropertyResourceBundle options = null;
|
29 |
304
|
berkley
|
private static String propertiesFile = "edu.ucsb.nceas.metacat.marine.marine";
|
30 |
295
|
berkley
|
private static boolean debug = false;
|
31 |
|
|
|
32 |
|
|
/**
|
33 |
|
|
* Construct an instance of the utility class
|
34 |
|
|
*/
|
35 |
|
|
public marineUtil() {
|
36 |
|
|
options = (PropertyResourceBundle)
|
37 |
|
|
PropertyResourceBundle.getBundle(propertiesFile);
|
38 |
|
|
}
|
39 |
|
|
|
40 |
|
|
/**
|
41 |
|
|
* Utility method to establish a JDBC database connection using connection
|
42 |
|
|
* info from the properties file
|
43 |
|
|
*/
|
44 |
318
|
berkley
|
|
45 |
295
|
berkley
|
public Connection openDBConnection()
|
46 |
|
|
throws SQLException, ClassNotFoundException {
|
47 |
318
|
berkley
|
|
48 |
|
|
//System.err.println(getOption("dbDriver") + " " + getOption("defaultDB") + " " +
|
49 |
|
|
// getOption("user") + " " +getOption("password"));
|
50 |
295
|
berkley
|
return openDBConnection(getOption("dbDriver"), getOption("defaultDB"),
|
51 |
|
|
getOption("user"), getOption("password"));
|
52 |
|
|
}
|
53 |
|
|
|
54 |
|
|
/**
|
55 |
|
|
* Utility method to establish a JDBC database connection
|
56 |
|
|
*
|
57 |
|
|
* @param dbDriver the string representing the database driver
|
58 |
|
|
* @param connection the string representing the database connectin parameters
|
59 |
|
|
* @param user name of the user to use for database connection
|
60 |
|
|
* @param password password for the user to use for database connection
|
61 |
|
|
*/
|
62 |
|
|
public static Connection openDBConnection(String dbDriver, String connection,
|
63 |
|
|
String user, String password)
|
64 |
|
|
throws SQLException, ClassNotFoundException {
|
65 |
|
|
|
66 |
|
|
// Load the Oracle JDBC driver
|
67 |
|
|
Class.forName (dbDriver);
|
68 |
|
|
|
69 |
|
|
// Connect to the database
|
70 |
|
|
Connection conn = DriverManager.getConnection( connection, user, password);
|
71 |
|
|
return conn;
|
72 |
|
|
}
|
73 |
|
|
|
74 |
|
|
/**
|
75 |
|
|
* Utility method to get an option value from the properties file
|
76 |
|
|
*
|
77 |
|
|
* @param option_name the name of the option requested
|
78 |
|
|
*/
|
79 |
|
|
public String getOption(String option_name) {
|
80 |
|
|
// Get the configuration file information
|
81 |
|
|
if (options == null) {
|
82 |
|
|
options = (PropertyResourceBundle)
|
83 |
|
|
PropertyResourceBundle.getBundle(propertiesFile);
|
84 |
|
|
}
|
85 |
|
|
String value = (String)options.handleGetObject(option_name);
|
86 |
|
|
return value;
|
87 |
|
|
}
|
88 |
|
|
|
89 |
|
|
/** Utility method to convert a file handle into a URL */
|
90 |
|
|
public static URL fileToURL(File file)
|
91 |
|
|
{
|
92 |
|
|
String path = file.getAbsolutePath();
|
93 |
|
|
String fSep = System.getProperty("file.separator");
|
94 |
|
|
if (fSep != null && fSep.length() == 1)
|
95 |
|
|
path = path.replace(fSep.charAt(0), '/');
|
96 |
|
|
if (path.length() > 0 && path.charAt(0) != '/')
|
97 |
|
|
path = '/' + path;
|
98 |
|
|
try {
|
99 |
|
|
return new URL("file", null, path);
|
100 |
|
|
}
|
101 |
|
|
catch (java.net.MalformedURLException e) {
|
102 |
|
|
/* According to the spec this could only happen if the file
|
103 |
|
|
protocol were not recognized. */
|
104 |
|
|
throw new Error("unexpected MalformedURLException");
|
105 |
|
|
}
|
106 |
|
|
}
|
107 |
|
|
|
108 |
|
|
/**
|
109 |
|
|
* Utility method to print debugging messages
|
110 |
|
|
*
|
111 |
|
|
* @param flag an integer indicating the message number
|
112 |
|
|
*/
|
113 |
|
|
public static void debugMessage(int flag) {
|
114 |
|
|
if (debug) {
|
115 |
|
|
System.err.println("DEBUG FLAG: " + flag);
|
116 |
|
|
}
|
117 |
|
|
}
|
118 |
|
|
|
119 |
|
|
/**
|
120 |
|
|
* Utility method to print debugging messages
|
121 |
|
|
*
|
122 |
|
|
* @param flag an integer indicating the message number
|
123 |
|
|
*/
|
124 |
|
|
public static void debugMessage(String msg) {
|
125 |
|
|
if (debug) {
|
126 |
|
|
System.err.println(msg);
|
127 |
|
|
}
|
128 |
|
|
}
|
129 |
|
|
}
|