1 |
51
|
jones
|
/**
|
2 |
168
|
jones
|
* Name: MetaCatUtil.java
|
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 |
168
|
jones
|
* Version: '$Id$'
|
9 |
51
|
jones
|
*/
|
10 |
|
|
|
11 |
|
|
package edu.ucsb.nceas.metacat;
|
12 |
|
|
|
13 |
185
|
jones
|
import java.io.File;
|
14 |
|
|
import java.net.URL;
|
15 |
|
|
import java.net.MalformedURLException;
|
16 |
50
|
jones
|
import java.sql.Connection;
|
17 |
|
|
import java.sql.DriverManager;
|
18 |
|
|
import java.sql.SQLException;
|
19 |
184
|
jones
|
import java.util.PropertyResourceBundle;
|
20 |
50
|
jones
|
|
21 |
|
|
/**
|
22 |
|
|
* A suite of utility classes for the metadata catalog server
|
23 |
|
|
*/
|
24 |
|
|
public class MetaCatUtil {
|
25 |
|
|
|
26 |
184
|
jones
|
private PropertyResourceBundle options = null;
|
27 |
|
|
private static String propertiesFile = "edu.ucsb.nceas.metacat.metacat";
|
28 |
109
|
bojilova
|
|
29 |
184
|
jones
|
/**
|
30 |
|
|
* Construct an instance of the utility class
|
31 |
|
|
*/
|
32 |
|
|
public MetaCatUtil() {
|
33 |
|
|
options = (PropertyResourceBundle)
|
34 |
|
|
PropertyResourceBundle.getBundle(propertiesFile);
|
35 |
|
|
}
|
36 |
|
|
|
37 |
50
|
jones
|
/**
|
38 |
184
|
jones
|
* Utility method to establish a JDBC database connection using connection
|
39 |
|
|
* info from the properties file
|
40 |
|
|
*/
|
41 |
|
|
public Connection openDBConnection()
|
42 |
|
|
throws SQLException, ClassNotFoundException {
|
43 |
|
|
return openDBConnection(getOption("dbDriver"), getOption("defaultDB"),
|
44 |
|
|
getOption("user"), getOption("password"));
|
45 |
|
|
}
|
46 |
|
|
|
47 |
|
|
/**
|
48 |
50
|
jones
|
* Utility method to establish a JDBC database connection
|
49 |
|
|
*
|
50 |
|
|
* @param dbDriver the string representing the database driver
|
51 |
|
|
* @param connection the string representing the database connectin parameters
|
52 |
|
|
* @param user name of the user to use for database connection
|
53 |
|
|
* @param password password for the user to use for database connection
|
54 |
|
|
*/
|
55 |
|
|
public static Connection openDBConnection(String dbDriver, String connection,
|
56 |
|
|
String user, String password)
|
57 |
|
|
throws SQLException, ClassNotFoundException {
|
58 |
|
|
|
59 |
|
|
// Load the Oracle JDBC driver
|
60 |
|
|
Class.forName (dbDriver);
|
61 |
|
|
|
62 |
|
|
// Connect to the database
|
63 |
|
|
Connection conn = DriverManager.getConnection( connection, user, password);
|
64 |
|
|
return conn;
|
65 |
|
|
}
|
66 |
184
|
jones
|
|
67 |
|
|
/**
|
68 |
|
|
* Utility method to get an option value from the properties file
|
69 |
|
|
*
|
70 |
|
|
* @param option_name the name of the option requested
|
71 |
|
|
*/
|
72 |
|
|
public String getOption(String option_name) {
|
73 |
|
|
// Get the configuration file information
|
74 |
|
|
if (options == null) {
|
75 |
|
|
options = (PropertyResourceBundle)
|
76 |
|
|
PropertyResourceBundle.getBundle(propertiesFile);
|
77 |
|
|
}
|
78 |
|
|
String value = (String)options.handleGetObject(option_name);
|
79 |
|
|
return value;
|
80 |
|
|
}
|
81 |
185
|
jones
|
|
82 |
|
|
/** Utility method to convert a file handle into a URL */
|
83 |
|
|
public static URL fileToURL(File file)
|
84 |
|
|
{
|
85 |
|
|
String path = file.getAbsolutePath();
|
86 |
|
|
String fSep = System.getProperty("file.separator");
|
87 |
|
|
if (fSep != null && fSep.length() == 1)
|
88 |
|
|
path = path.replace(fSep.charAt(0), '/');
|
89 |
|
|
if (path.length() > 0 && path.charAt(0) != '/')
|
90 |
|
|
path = '/' + path;
|
91 |
|
|
try {
|
92 |
|
|
return new URL("file", null, path);
|
93 |
|
|
}
|
94 |
|
|
catch (java.net.MalformedURLException e) {
|
95 |
|
|
/* According to the spec this could only happen if the file
|
96 |
|
|
protocol were not recognized. */
|
97 |
|
|
throw new Error("unexpected MalformedURLException");
|
98 |
|
|
}
|
99 |
|
|
}
|
100 |
|
|
|
101 |
|
|
/**
|
102 |
|
|
* Utility method to print debugging messages
|
103 |
|
|
*
|
104 |
|
|
* @param flag an integer indicating the message number
|
105 |
|
|
*/
|
106 |
|
|
public static void dbg(int flag) {
|
107 |
|
|
System.err.println("DEBUG FLAG: " + flag);
|
108 |
|
|
}
|
109 |
50
|
jones
|
}
|