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 |
309
|
bojilova
|
* Authors: Matt Jones, Jivka Bojilova
|
7 |
349
|
jones
|
* Release: @release@
|
8 |
51
|
jones
|
*
|
9 |
203
|
jones
|
* '$Author$'
|
10 |
|
|
* '$Date$'
|
11 |
|
|
* '$Revision$'
|
12 |
669
|
jones
|
*
|
13 |
|
|
* This program is free software; you can redistribute it and/or modify
|
14 |
|
|
* it under the terms of the GNU General Public License as published by
|
15 |
|
|
* the Free Software Foundation; either version 2 of the License, or
|
16 |
|
|
* (at your option) any later version.
|
17 |
|
|
*
|
18 |
|
|
* This program is distributed in the hope that it will be useful,
|
19 |
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
20 |
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
21 |
|
|
* GNU General Public License for more details.
|
22 |
|
|
*
|
23 |
|
|
* You should have received a copy of the GNU General Public License
|
24 |
|
|
* along with this program; if not, write to the Free Software
|
25 |
|
|
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
26 |
51
|
jones
|
*/
|
27 |
|
|
|
28 |
|
|
package edu.ucsb.nceas.metacat;
|
29 |
|
|
|
30 |
185
|
jones
|
import java.io.File;
|
31 |
|
|
import java.net.URL;
|
32 |
|
|
import java.net.MalformedURLException;
|
33 |
50
|
jones
|
import java.sql.Connection;
|
34 |
|
|
import java.sql.DriverManager;
|
35 |
|
|
import java.sql.SQLException;
|
36 |
184
|
jones
|
import java.util.PropertyResourceBundle;
|
37 |
309
|
bojilova
|
import java.util.Hashtable;
|
38 |
|
|
import java.util.Enumeration;
|
39 |
887
|
berkley
|
import java.util.Vector;
|
40 |
50
|
jones
|
|
41 |
777
|
bojilova
|
import edu.ucsb.nceas.dbadapter.AbstractDatabase;
|
42 |
747
|
bojilova
|
|
43 |
50
|
jones
|
/**
|
44 |
|
|
* A suite of utility classes for the metadata catalog server
|
45 |
|
|
*/
|
46 |
|
|
public class MetaCatUtil {
|
47 |
|
|
|
48 |
777
|
bojilova
|
public static AbstractDatabase dbAdapter;
|
49 |
747
|
bojilova
|
private static PropertyResourceBundle options = null;
|
50 |
|
|
private static String propertiesFile = "edu.ucsb.nceas.metacat.metacat";
|
51 |
1394
|
tao
|
private static boolean debug = true;
|
52 |
109
|
bojilova
|
|
53 |
1217
|
tao
|
//private Hashtable connectionPool = new Hashtable();
|
54 |
747
|
bojilova
|
|
55 |
|
|
/**
|
56 |
|
|
* Determine our db adapter class and create an instance of that class
|
57 |
|
|
*/
|
58 |
|
|
static {
|
59 |
|
|
try {
|
60 |
777
|
bojilova
|
dbAdapter = (AbstractDatabase)createObject(getOption("dbAdapter"));
|
61 |
747
|
bojilova
|
} catch (Exception e) {
|
62 |
|
|
System.err.println("Error in MetaCatUtil static block:" + e.getMessage());
|
63 |
|
|
}
|
64 |
|
|
}
|
65 |
|
|
|
66 |
|
|
|
67 |
1217
|
tao
|
|
68 |
184
|
jones
|
/**
|
69 |
747
|
bojilova
|
* Instantiate a class using the name of the class at runtime
|
70 |
|
|
*
|
71 |
|
|
* @param className the fully qualified name of the class to instantiate
|
72 |
184
|
jones
|
*/
|
73 |
747
|
bojilova
|
public static Object createObject(String className) throws Exception {
|
74 |
|
|
|
75 |
|
|
Object object = null;
|
76 |
|
|
try {
|
77 |
|
|
Class classDefinition = Class.forName(className);
|
78 |
|
|
object = classDefinition.newInstance();
|
79 |
|
|
} catch (InstantiationException e) {
|
80 |
|
|
throw e;
|
81 |
|
|
} catch (IllegalAccessException e) {
|
82 |
|
|
throw e;
|
83 |
|
|
} catch (ClassNotFoundException e) {
|
84 |
|
|
throw e;
|
85 |
|
|
}
|
86 |
|
|
return object;
|
87 |
|
|
}
|
88 |
|
|
|
89 |
|
|
/**
|
90 |
|
|
* Utility method to get an option value from the properties file
|
91 |
|
|
*
|
92 |
|
|
* @param optionName the name of the option requested
|
93 |
|
|
*/
|
94 |
|
|
public static String getOption(String optionName) {
|
95 |
|
|
// Get the configuration file information
|
96 |
|
|
if (options == null) {
|
97 |
|
|
options = (PropertyResourceBundle)
|
98 |
184
|
jones
|
PropertyResourceBundle.getBundle(propertiesFile);
|
99 |
747
|
bojilova
|
}
|
100 |
|
|
String value = (String)options.handleGetObject(optionName);
|
101 |
|
|
return value;
|
102 |
184
|
jones
|
}
|
103 |
747
|
bojilova
|
|
104 |
|
|
/**
|
105 |
|
|
* Utility method to get an option value from a properties file
|
106 |
|
|
*
|
107 |
|
|
* @param optionName the name of the option requested
|
108 |
|
|
* @param propFile the name of the file where to get the properties from
|
109 |
354
|
berkley
|
*/
|
110 |
747
|
bojilova
|
public String getOption(String optionName, String propFile) {
|
111 |
|
|
// Get the configuration file information
|
112 |
|
|
PropertyResourceBundle options = (PropertyResourceBundle)
|
113 |
|
|
PropertyResourceBundle.getBundle(propFile);
|
114 |
|
|
String value = (String)options.handleGetObject(optionName);
|
115 |
|
|
return value;
|
116 |
354
|
berkley
|
}
|
117 |
184
|
jones
|
|
118 |
1217
|
tao
|
|
119 |
309
|
bojilova
|
|
120 |
|
|
|
121 |
185
|
jones
|
/** Utility method to convert a file handle into a URL */
|
122 |
|
|
public static URL fileToURL(File file)
|
123 |
|
|
{
|
124 |
|
|
String path = file.getAbsolutePath();
|
125 |
|
|
String fSep = System.getProperty("file.separator");
|
126 |
|
|
if (fSep != null && fSep.length() == 1)
|
127 |
|
|
path = path.replace(fSep.charAt(0), '/');
|
128 |
|
|
if (path.length() > 0 && path.charAt(0) != '/')
|
129 |
|
|
path = '/' + path;
|
130 |
|
|
try {
|
131 |
|
|
return new URL("file", null, path);
|
132 |
|
|
}
|
133 |
|
|
catch (java.net.MalformedURLException e) {
|
134 |
|
|
/* According to the spec this could only happen if the file
|
135 |
|
|
protocol were not recognized. */
|
136 |
|
|
throw new Error("unexpected MalformedURLException");
|
137 |
|
|
}
|
138 |
|
|
}
|
139 |
|
|
|
140 |
|
|
/**
|
141 |
566
|
jones
|
* Utility method to parse the query part of a URL into parameters. This
|
142 |
|
|
* method assumes the format of the query par tof the url is an
|
143 |
|
|
* ampersand separated list of name/value pairs, with equal signs separating
|
144 |
|
|
* the name from the value (e.g., name=tom&zip=99801 ). Returns a
|
145 |
|
|
* has of the name value pairs, hashed on name.
|
146 |
|
|
*/
|
147 |
|
|
public static Hashtable parseQuery(String query) throws MalformedURLException
|
148 |
|
|
{
|
149 |
|
|
String[][] params = new String[200][2];
|
150 |
|
|
Hashtable parameters = new Hashtable();
|
151 |
|
|
|
152 |
|
|
String temp = "";
|
153 |
|
|
boolean ampflag = true;
|
154 |
|
|
boolean poundflag = false;
|
155 |
|
|
int arrcount = 0;
|
156 |
|
|
|
157 |
731
|
bojilova
|
if ( query != null ) {
|
158 |
|
|
for (int i=0; i < query.length(); i++) {
|
159 |
566
|
jones
|
|
160 |
731
|
bojilova
|
// go throught the remainder of the query one character at a time.
|
161 |
|
|
if (query.charAt(i) == '=') {
|
162 |
|
|
// if the current char is a # then the preceding should be a name
|
163 |
|
|
if (!poundflag && ampflag) {
|
164 |
|
|
params[arrcount][0] = temp.trim();
|
165 |
|
|
temp = "";
|
166 |
|
|
} else {
|
167 |
|
|
//if there are two #s or &s in a row throw an exception.
|
168 |
|
|
throw new MalformedURLException("metacatURL: Two parameter names "+
|
169 |
|
|
"not allowed in sequence");
|
170 |
|
|
}
|
171 |
|
|
poundflag = true;
|
172 |
|
|
ampflag = false;
|
173 |
|
|
} else if (query.charAt(i) == '&' || i == query.length()-1) {
|
174 |
|
|
//the text preceding the & should be the param value.
|
175 |
|
|
if (i == query.length() - 1) {
|
176 |
|
|
//if at the end of the string grab the last value and append it.
|
177 |
|
|
if (query.charAt(i) != '=') {
|
178 |
566
|
jones
|
//ignore an extra & on the end of the string
|
179 |
731
|
bojilova
|
temp += query.charAt(i);
|
180 |
|
|
}
|
181 |
566
|
jones
|
}
|
182 |
731
|
bojilova
|
|
183 |
|
|
if (!ampflag && poundflag) {
|
184 |
|
|
params[arrcount][1] = temp.trim();
|
185 |
|
|
parameters.put(params[arrcount][0], params[arrcount][1]);
|
186 |
|
|
temp = "";
|
187 |
|
|
arrcount++; //increment the array to the next row.
|
188 |
|
|
} else {
|
189 |
|
|
//if there are two =s or &s in a row through an exception
|
190 |
|
|
throw new MalformedURLException("metacatURL: Two parameter values "+
|
191 |
|
|
"not allowed in sequence");
|
192 |
|
|
}
|
193 |
|
|
poundflag = false;
|
194 |
|
|
ampflag = true;
|
195 |
566
|
jones
|
} else {
|
196 |
731
|
bojilova
|
//get the next character in the string
|
197 |
|
|
temp += query.charAt(i);
|
198 |
566
|
jones
|
}
|
199 |
|
|
}
|
200 |
|
|
}
|
201 |
|
|
return parameters;
|
202 |
|
|
}
|
203 |
|
|
|
204 |
1053
|
tao
|
/**
|
205 |
|
|
* Utility method to print debugging messages. User can set debug level
|
206 |
|
|
* for this message. The number is fewer, the message is more important
|
207 |
|
|
* @param msg, the content of the message
|
208 |
|
|
* @param debugLevel, an integer indicating the message debug leve
|
209 |
|
|
*/
|
210 |
|
|
public static void debugMessage(String msg, int debugLevel)
|
211 |
|
|
{
|
212 |
1394
|
tao
|
if (debug)
|
213 |
1053
|
tao
|
{
|
214 |
1394
|
tao
|
int limit = 1;
|
215 |
|
|
try
|
216 |
|
|
{
|
217 |
|
|
limit=Integer.parseInt(getOption("debuglevel"));
|
218 |
1053
|
tao
|
|
219 |
1394
|
tao
|
}
|
220 |
|
|
catch (Exception e)
|
221 |
|
|
{
|
222 |
|
|
System.out.println(e.getMessage());
|
223 |
|
|
}
|
224 |
|
|
//don't allow the user set debugLevel less than or equals 0
|
225 |
|
|
if (debugLevel<=0)
|
226 |
|
|
{
|
227 |
|
|
debugLevel=1;
|
228 |
|
|
}
|
229 |
1053
|
tao
|
|
230 |
1394
|
tao
|
if (debugLevel < limit)
|
231 |
|
|
{
|
232 |
|
|
System.err.println(msg);
|
233 |
|
|
}
|
234 |
1053
|
tao
|
}
|
235 |
|
|
}
|
236 |
|
|
|
237 |
566
|
jones
|
/**
|
238 |
185
|
jones
|
* Utility method to print debugging messages
|
239 |
|
|
*
|
240 |
|
|
* @param flag an integer indicating the message number
|
241 |
|
|
*/
|
242 |
203
|
jones
|
public static void debugMessage(int flag) {
|
243 |
|
|
if (debug) {
|
244 |
|
|
System.err.println("DEBUG FLAG: " + flag);
|
245 |
|
|
}
|
246 |
185
|
jones
|
}
|
247 |
203
|
jones
|
|
248 |
|
|
/**
|
249 |
|
|
* Utility method to print debugging messages
|
250 |
|
|
*
|
251 |
|
|
* @param flag an integer indicating the message number
|
252 |
|
|
*/
|
253 |
|
|
public static void debugMessage(String msg) {
|
254 |
|
|
if (debug) {
|
255 |
|
|
System.err.println(msg);
|
256 |
|
|
}
|
257 |
|
|
}
|
258 |
887
|
berkley
|
|
259 |
1000
|
berkley
|
public static void debug(String msg)
|
260 |
|
|
{
|
261 |
|
|
if(debug)
|
262 |
|
|
{
|
263 |
|
|
System.err.println(msg);
|
264 |
|
|
}
|
265 |
|
|
}
|
266 |
|
|
|
267 |
1053
|
tao
|
|
268 |
|
|
|
269 |
887
|
berkley
|
public static Vector getOptionList(String optiontext)
|
270 |
|
|
{
|
271 |
|
|
Vector options = new Vector();
|
272 |
|
|
if(optiontext.indexOf(",") == -1)
|
273 |
|
|
{
|
274 |
|
|
options.addElement(optiontext);
|
275 |
|
|
return options;
|
276 |
|
|
}
|
277 |
|
|
|
278 |
|
|
while(optiontext.indexOf(",") != -1)
|
279 |
|
|
{
|
280 |
|
|
String s = optiontext.substring(0, optiontext.indexOf(","));
|
281 |
|
|
options.addElement(s.trim());
|
282 |
|
|
optiontext = optiontext.substring(optiontext.indexOf(",") + 1,
|
283 |
|
|
optiontext.length());
|
284 |
|
|
if(optiontext.indexOf(",") == -1)
|
285 |
|
|
{ //catch the last list entry
|
286 |
|
|
options.addElement(optiontext.trim());
|
287 |
|
|
}
|
288 |
|
|
}
|
289 |
|
|
return options;
|
290 |
|
|
}
|
291 |
894
|
berkley
|
|
292 |
|
|
/** Normalizes the given string. Taken from configXML.java*/
|
293 |
|
|
public static String normalize(String s)
|
294 |
|
|
{
|
295 |
|
|
StringBuffer str = new StringBuffer();
|
296 |
|
|
|
297 |
|
|
int len = (s != null) ? s.length() : 0;
|
298 |
|
|
for (int i = 0; i < len; i++)
|
299 |
|
|
{
|
300 |
|
|
char ch = s.charAt(i);
|
301 |
|
|
switch (ch)
|
302 |
|
|
{
|
303 |
|
|
case '<':
|
304 |
|
|
{
|
305 |
|
|
str.append("<");
|
306 |
|
|
break;
|
307 |
|
|
}
|
308 |
|
|
case '>':
|
309 |
|
|
{
|
310 |
|
|
str.append(">");
|
311 |
|
|
break;
|
312 |
|
|
}
|
313 |
|
|
case '&':
|
314 |
|
|
{
|
315 |
|
|
str.append("&");
|
316 |
|
|
break;
|
317 |
|
|
}
|
318 |
|
|
case '"':
|
319 |
|
|
{
|
320 |
|
|
str.append(""");
|
321 |
|
|
break;
|
322 |
|
|
}
|
323 |
|
|
case '\r':
|
324 |
|
|
case '\n':
|
325 |
|
|
{
|
326 |
|
|
// else, default append char
|
327 |
|
|
}
|
328 |
|
|
default:
|
329 |
|
|
{
|
330 |
|
|
str.append(ch);
|
331 |
|
|
}
|
332 |
|
|
}
|
333 |
|
|
}
|
334 |
|
|
return (str.toString());
|
335 |
|
|
}
|
336 |
|
|
|
337 |
942
|
tao
|
/**
|
338 |
|
|
* Utility method to get docid from a given string
|
339 |
|
|
* @param string, the given string should be these two format:
|
340 |
|
|
* 1) str1.str2 in this case docid= str1.str2
|
341 |
|
|
* 2) str1.str2.str3, in this case docid =str1.str2
|
342 |
|
|
* @param the sperator char
|
343 |
|
|
*/
|
344 |
|
|
public static String getDocIdFromString(String str)
|
345 |
|
|
{
|
346 |
|
|
String docId = null;
|
347 |
|
|
int dotNumber = 0;//count how many dots in given string
|
348 |
|
|
int indexOfLastDot = 0;
|
349 |
|
|
|
350 |
|
|
//assume that seperator is one charactor string
|
351 |
|
|
char seperator=getOption("accNumSeparator").charAt(0);
|
352 |
|
|
|
353 |
|
|
for (int i=0; i<str.length(); i++)
|
354 |
|
|
{
|
355 |
|
|
if ( str.charAt(i)==seperator)
|
356 |
|
|
{
|
357 |
|
|
dotNumber++;//count how many dots
|
358 |
|
|
indexOfLastDot=i;//keep the last dot postion
|
359 |
|
|
}
|
360 |
|
|
}//for
|
361 |
|
|
|
362 |
|
|
//The string formatt is wrong, because it has more than two or less than
|
363 |
|
|
//one seperator
|
364 |
|
|
if ( dotNumber>2 || dotNumber < 1)
|
365 |
|
|
{
|
366 |
|
|
docId=null;
|
367 |
|
|
}
|
368 |
|
|
else if (dotNumber == 2) //the case for str1.str2.str3
|
369 |
|
|
{
|
370 |
|
|
docId=str.substring(0, indexOfLastDot);
|
371 |
|
|
}
|
372 |
|
|
else if (dotNumber == 1) //the case for str1.str2
|
373 |
|
|
{
|
374 |
|
|
docId=str;
|
375 |
|
|
}
|
376 |
|
|
|
377 |
|
|
return docId;
|
378 |
|
|
}//getDocIdFromString
|
379 |
|
|
|
380 |
|
|
/**
|
381 |
|
|
* Utility method to get version number from a given string
|
382 |
|
|
* @param string, the given string should be these two format:
|
383 |
|
|
* 1) str1.str2(no version) version =-1;
|
384 |
|
|
* 2) str1.str2.str3, in this case version = str3;
|
385 |
|
|
* 3) other, vresion =-2
|
386 |
|
|
*/
|
387 |
|
|
public static int getVersionFromString(String str)
|
388 |
|
|
throws NumberFormatException
|
389 |
|
|
{
|
390 |
|
|
int version=-1;
|
391 |
|
|
String versionString=null;
|
392 |
|
|
int dotNumber = 0;//count how many dots in given string
|
393 |
|
|
int indexOfLastDot = 0;
|
394 |
|
|
|
395 |
|
|
//assume that seperator is one charactor string
|
396 |
|
|
char seperator=getOption("accNumSeparator").charAt(0);
|
397 |
|
|
|
398 |
|
|
for (int i=0; i<str.length(); i++)
|
399 |
|
|
{
|
400 |
|
|
if ( str.charAt(i)==seperator)
|
401 |
|
|
{
|
402 |
|
|
dotNumber++;//count how many dots
|
403 |
|
|
indexOfLastDot=i;//keep the last dot postion
|
404 |
|
|
}
|
405 |
|
|
}//for
|
406 |
|
|
|
407 |
|
|
//The string formatt is wrong, because it has more than two or less than
|
408 |
|
|
//one seperator
|
409 |
|
|
if ( dotNumber>2 || dotNumber < 1)
|
410 |
|
|
{
|
411 |
|
|
version=-2;
|
412 |
|
|
}
|
413 |
1292
|
tao
|
else if (dotNumber == 2 && (indexOfLastDot != (str.length() -1)))
|
414 |
|
|
//the case for str1.str2.str3
|
415 |
942
|
tao
|
{
|
416 |
|
|
versionString=str.substring((indexOfLastDot+1), str.length());
|
417 |
|
|
version=Integer.parseInt(versionString);
|
418 |
|
|
}
|
419 |
|
|
else if (dotNumber == 1) //the case for str1.str2
|
420 |
|
|
{
|
421 |
|
|
version=-1;
|
422 |
|
|
}
|
423 |
|
|
|
424 |
|
|
return version;
|
425 |
|
|
}//getVersionFromString
|
426 |
|
|
|
427 |
1292
|
tao
|
|
428 |
|
|
/**
|
429 |
|
|
* Utility method to get version string from a given string
|
430 |
|
|
* @param string, the given string should be these two format:
|
431 |
|
|
* 1) str1.str2(no version) version=null;
|
432 |
|
|
* 2) str1.str2.str3, in this case version = str3;
|
433 |
|
|
* 3) other, vresion =null;
|
434 |
|
|
*/
|
435 |
|
|
public static String getRevisionStringFromString(String str)
|
436 |
|
|
throws NumberFormatException
|
437 |
|
|
{
|
438 |
|
|
// String to store the version
|
439 |
|
|
String versionString=null;
|
440 |
|
|
int dotNumber = 0;//count how many dots in given string
|
441 |
|
|
int indexOfLastDot = 0;
|
442 |
|
|
|
443 |
|
|
//assume that seperator is one charactor string
|
444 |
|
|
char seperator=getOption("accNumSeparator").charAt(0);
|
445 |
|
|
|
446 |
|
|
for (int i=0; i<str.length(); i++)
|
447 |
|
|
{
|
448 |
|
|
if ( str.charAt(i)==seperator)
|
449 |
|
|
{
|
450 |
|
|
dotNumber++;//count how many dots
|
451 |
|
|
indexOfLastDot=i;//keep the last dot postion
|
452 |
|
|
}
|
453 |
|
|
}//for
|
454 |
|
|
|
455 |
|
|
//The string formatt is wrong, because it has more than two or less than
|
456 |
|
|
//one seperator
|
457 |
|
|
if ( dotNumber>2 || dotNumber < 1)
|
458 |
|
|
{
|
459 |
|
|
versionString = null;
|
460 |
|
|
}
|
461 |
|
|
else if (dotNumber == 2 && (indexOfLastDot != (str.length() -1)))
|
462 |
|
|
{
|
463 |
|
|
//the case for str1.str2.str3
|
464 |
|
|
// indexOfLastDot != (str.length() -1) means get rid of str1.str2.
|
465 |
|
|
versionString=str.substring((indexOfLastDot+1), str.length());
|
466 |
|
|
}
|
467 |
|
|
else if (dotNumber == 1) //the case for str1.str2 or str1.str2.
|
468 |
|
|
{
|
469 |
|
|
versionString=null;
|
470 |
|
|
}
|
471 |
|
|
|
472 |
|
|
return versionString;
|
473 |
|
|
}//getVersionFromString
|
474 |
|
|
|
475 |
|
|
/**
|
476 |
1013
|
tao
|
* Method to get the name of local replication server
|
477 |
|
|
*/
|
478 |
1060
|
tao
|
public static String getLocalReplicationServerName()
|
479 |
1013
|
tao
|
{
|
480 |
|
|
String replicationServerName=null;
|
481 |
|
|
String serverHost=null;
|
482 |
|
|
serverHost=getOption("server");
|
483 |
|
|
// append "context/servelet/replication" to the host name
|
484 |
|
|
replicationServerName=serverHost+getOption("replicationpath");
|
485 |
|
|
return replicationServerName;
|
486 |
|
|
|
487 |
|
|
}
|
488 |
|
|
|
489 |
50
|
jones
|
}
|