1 |
51
|
jones
|
/**
|
2 |
203
|
jones
|
* '$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
|
7 |
51
|
jones
|
*
|
8 |
203
|
jones
|
* '$Author$'
|
9 |
|
|
* '$Date$'
|
10 |
|
|
* '$Revision$'
|
11 |
51
|
jones
|
*/
|
12 |
|
|
|
13 |
|
|
package edu.ucsb.nceas.metacat;
|
14 |
|
|
|
15 |
185
|
jones
|
import java.io.File;
|
16 |
|
|
import java.net.URL;
|
17 |
|
|
import java.net.MalformedURLException;
|
18 |
50
|
jones
|
import java.sql.Connection;
|
19 |
|
|
import java.sql.DriverManager;
|
20 |
|
|
import java.sql.SQLException;
|
21 |
184
|
jones
|
import java.util.PropertyResourceBundle;
|
22 |
50
|
jones
|
|
23 |
|
|
/**
|
24 |
|
|
* A suite of utility classes for the metadata catalog server
|
25 |
|
|
*/
|
26 |
|
|
public class MetaCatUtil {
|
27 |
|
|
|
28 |
184
|
jones
|
private PropertyResourceBundle options = null;
|
29 |
|
|
private static String propertiesFile = "edu.ucsb.nceas.metacat.metacat";
|
30 |
203
|
jones
|
private static boolean debug = false;
|
31 |
109
|
bojilova
|
|
32 |
184
|
jones
|
/**
|
33 |
|
|
* Construct an instance of the utility class
|
34 |
|
|
*/
|
35 |
|
|
public MetaCatUtil() {
|
36 |
|
|
options = (PropertyResourceBundle)
|
37 |
|
|
PropertyResourceBundle.getBundle(propertiesFile);
|
38 |
|
|
}
|
39 |
|
|
|
40 |
50
|
jones
|
/**
|
41 |
184
|
jones
|
* 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 |
50
|
jones
|
* 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 |
184
|
jones
|
|
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 |
185
|
jones
|
|
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 |
203
|
jones
|
public static void debugMessage(int flag) {
|
110 |
|
|
if (debug) {
|
111 |
|
|
System.err.println("DEBUG FLAG: " + flag);
|
112 |
|
|
}
|
113 |
185
|
jones
|
}
|
114 |
203
|
jones
|
|
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 |
50
|
jones
|
}
|
126 |
203
|
jones
|
|
127 |
|
|
/**
|
128 |
|
|
* '$Log$
|
129 |
|
|
* 'Revision 1.9.2.4 2000/06/26 00:51:06 jones
|
130 |
|
|
* 'If docid passed to DBWriter.write() is not unique, classes now generate
|
131 |
|
|
* 'an AccessionNumberException containing the new docid generated as a
|
132 |
|
|
* 'replacement. The docid is then extracted from the exception and
|
133 |
|
|
* 'returned to the calling application for user feedback or client processing.
|
134 |
|
|
* '
|
135 |
|
|
* 'Revision 1.9.2.3 2000/06/25 23:38:17 jones
|
136 |
|
|
* 'Added RCSfile keyword
|
137 |
|
|
* '
|
138 |
|
|
* 'Revision 1.9.2.2 2000/06/25 23:34:18 jones
|
139 |
|
|
* 'Changed documentation formatting, added log entries at bottom of source files
|
140 |
|
|
* ''
|
141 |
|
|
*/
|