1
|
/**
|
2
|
* '$RCSfile$'
|
3
|
* Purpose: A Class that implements utility methods for marineServlet.java
|
4
|
* 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: berkley $'
|
9
|
* '$Date: 2000-07-28 12:00:12 -0700 (Fri, 28 Jul 2000) $'
|
10
|
* '$Revision: 304 $'
|
11
|
*/
|
12
|
package edu.ucsb.nceas.metacat.marine;
|
13
|
|
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
|
private static String propertiesFile = "edu.ucsb.nceas.metacat.marine.marine";
|
30
|
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
|
public Connection openDBConnection()
|
45
|
throws SQLException, ClassNotFoundException {
|
46
|
return openDBConnection(getOption("dbDriver"), getOption("defaultDB"),
|
47
|
getOption("user"), getOption("password"));
|
48
|
}
|
49
|
|
50
|
/**
|
51
|
* Utility method to establish a JDBC database connection
|
52
|
*
|
53
|
* @param dbDriver the string representing the database driver
|
54
|
* @param connection the string representing the database connectin parameters
|
55
|
* @param user name of the user to use for database connection
|
56
|
* @param password password for the user to use for database connection
|
57
|
*/
|
58
|
public static Connection openDBConnection(String dbDriver, String connection,
|
59
|
String user, String password)
|
60
|
throws SQLException, ClassNotFoundException {
|
61
|
|
62
|
// Load the Oracle JDBC driver
|
63
|
Class.forName (dbDriver);
|
64
|
|
65
|
// Connect to the database
|
66
|
Connection conn = DriverManager.getConnection( connection, user, password);
|
67
|
return conn;
|
68
|
}
|
69
|
|
70
|
/**
|
71
|
* Utility method to get an option value from the properties file
|
72
|
*
|
73
|
* @param option_name the name of the option requested
|
74
|
*/
|
75
|
public String getOption(String option_name) {
|
76
|
// Get the configuration file information
|
77
|
if (options == null) {
|
78
|
options = (PropertyResourceBundle)
|
79
|
PropertyResourceBundle.getBundle(propertiesFile);
|
80
|
}
|
81
|
String value = (String)options.handleGetObject(option_name);
|
82
|
return value;
|
83
|
}
|
84
|
|
85
|
/** Utility method to convert a file handle into a URL */
|
86
|
public static URL fileToURL(File file)
|
87
|
{
|
88
|
String path = file.getAbsolutePath();
|
89
|
String fSep = System.getProperty("file.separator");
|
90
|
if (fSep != null && fSep.length() == 1)
|
91
|
path = path.replace(fSep.charAt(0), '/');
|
92
|
if (path.length() > 0 && path.charAt(0) != '/')
|
93
|
path = '/' + path;
|
94
|
try {
|
95
|
return new URL("file", null, path);
|
96
|
}
|
97
|
catch (java.net.MalformedURLException e) {
|
98
|
/* According to the spec this could only happen if the file
|
99
|
protocol were not recognized. */
|
100
|
throw new Error("unexpected MalformedURLException");
|
101
|
}
|
102
|
}
|
103
|
|
104
|
/**
|
105
|
* Utility method to print debugging messages
|
106
|
*
|
107
|
* @param flag an integer indicating the message number
|
108
|
*/
|
109
|
public static void debugMessage(int flag) {
|
110
|
if (debug) {
|
111
|
System.err.println("DEBUG FLAG: " + flag);
|
112
|
}
|
113
|
}
|
114
|
|
115
|
/**
|
116
|
* Utility method to print debugging messages
|
117
|
*
|
118
|
* @param flag an integer indicating the message number
|
119
|
*/
|
120
|
public static void debugMessage(String msg) {
|
121
|
if (debug) {
|
122
|
System.err.println(msg);
|
123
|
}
|
124
|
}
|
125
|
}
|
126
|
|