📄 p6util.java
字号:
checkedPath += "Classloader via Class: <"+getClassPathAsString(P6Util.class.getClassLoader())+">\n\n";
checkedPath += "java.class.path: <"+System.getProperty("java.class.path")+">\n\n";
return checkedPath;
}
protected static String getClassPathAsString(ClassLoader classLoader) {
String path = "";
try {
URL[] urls = ((URLClassLoader)classLoader).getURLs();
for(int i=0; i< urls.length; i++) {
if (path != "") {
path += ";";
}
path += urls[i].toString();
}
} catch(ClassCastException e) {
}
return path;
}
// this is our own version, which we need to do to ensure the order is kept
// in the property file
public static ArrayList loadProperties(String file, String prefix) {
ArrayList props = new ArrayList();
FileReader in = null;
BufferedReader reader = null;
try {
String path = classPathFile(file);
if (path == null) {
P6LogQuery.logError("Can't find " + file + ". "+getCheckedPath());
} else {
in = new FileReader(path);
// read the file
reader = new BufferedReader(in);
String line;
while ((line = reader.readLine()) != null) {
if ((line.trim()).startsWith(prefix)) {
StringTokenizer st = new StringTokenizer(line, "=");
try {
String name = st.nextToken();
String value = st.nextToken();
KeyValue kv = new KeyValue(name.trim(), value.trim());
props.add(kv);
} catch (NoSuchElementException e) {
// ignore; means that you have
// something like:
// realdriver2=
}
}
}
}
} catch (FileNotFoundException e1) {
P6LogQuery.logError("File not found " + file + " " + e1);
} catch (IOException e2) {
P6LogQuery.logError("IO Error reading file " + file + " " + e2);
} finally {
try {
if (reader != null) {
reader.close();
}
if (in != null) {
in.close();
}
} catch (IOException e) {
}
}
return props;
}
public static ArrayList reverseArrayList(ArrayList arraylist) {
ArrayList newlist = new ArrayList(arraylist.size());
for (int i = arraylist.size()-1;i >= 0; i--) {
newlist.add(arraylist.get(i));
}
return newlist;
}
/**
* Here we attempt to find the file in the current dir and the classpath
* If we can't find it then we return null
*/
public static String classPathFile(String file) {
File fp = null;
String path = null;
String separator = System.getProperty("path.separator");
String slash = System.getProperty("file.separator");
String classpath = "." + separator + System.getProperty("java.class.path");
String local = System.getProperty("p6.home");
// first try to load options via the classloader
try {
// If p6.home is specified, just look there
if (local != null) {
fp = new File(local, file);
} else {
// try to get the classloader via the current thread
fp = classLoadPropertyFile(Thread.currentThread().getContextClassLoader().getResource(file));
if (fp == null) {
// next try the current class
fp = classLoadPropertyFile(P6Util.class.getClassLoader().getResource(file));
}
if (fp == null) {
// now the ClassLoader system resource
classLoadPropertyFile(ClassLoader.getSystemResource(file));
}
}
if (fp.exists()) {
return fp.getCanonicalPath();
}
} catch (Exception exc) {
}
// if that failed, see what we can do on our own
StringTokenizer tok = new StringTokenizer(classpath, separator);
do {
String dir = tok.nextToken();
path = dir.equals(".") ? file : dir + slash + file;
fp = new File(path);
} while (!fp.exists() && tok.hasMoreTokens());
return fp.exists() ? path : null;
}
public static File classLoadPropertyFile(java.net.URL purl) {
try {
if (purl != null) {
// modified by jayakumar for JDK 1.2 support
//return new File(purl.getPath());
return new File(getPath(purl));
// end of modification
}
} catch (Exception e) {
// we ignore this, since JDK 1.2 does not suppport this method
}
return null;
}
public static java.util.Date timeNow() {
return(new java.util.Date());
}
public static PrintStream getPrintStream(String file, boolean append) throws IOException {
PrintStream stream = null;
FileOutputStream fw = new FileOutputStream(file, append);
stream = new PrintStream(fw, P6SpyOptions.getAutoflush());
return(stream);
}
public static String timeTaken(java.util.Date start, String msg) {
double t = (double) elapsed(start) / (double) 1000;
return "Time: " + msg + ": " + t;
}
public static long elapsed(java.util.Date start) {
return(start == null) ? 0 : (timeNow().getTime() - start.getTime());
}
/**
* A utiltity for using the current class loader (rather than the
* system class loader) when instantiating a new class.
* <p>
* The idea is that the thread's current loader might have an
* obscure notion of what your class path is (e.g. an app server) that
* will not be captured properly by the system class loader.
* <p>
* taken from http://sourceforge.net/forum/message.php?msg_id=1720229
*
* @param name class name to load
* @return the newly loaded class
*/
public static Class forName(String name) throws ClassNotFoundException {
ClassLoader ctxLoader = null;
try {
ctxLoader = Thread.currentThread().getContextClassLoader();
return Class.forName(name, true, ctxLoader);
} catch(ClassNotFoundException ex) {
// try to fall through and use the default
// Class.forName
//if(ctxLoader == null) { throw ex; }
} catch(SecurityException ex) {
}
return Class.forName(name);
}
/** A utility for dynamically setting the value of a given static class
* method */
public static void dynamicSet(Class klass, String property, String value) {
try {
P6Util.set(klass, property, new String[] {value});
} catch (IntrospectionException e) {
P6LogQuery.logError("Could not set property "+property+" due to IntrospectionException");
} catch (IllegalAccessException e) {
P6LogQuery.logError("Could not set property "+property+" due to IllegalAccessException");
} catch (NoSuchMethodException e) {
// we are avoid this because it is perfectly okay for there to be get methods
// we do not really want to set
} catch (InvocationTargetException e) {
P6LogQuery.logError("Could not set property "+property+" due to InvoicationTargetException");
}
}
public static void set(Class klass, String method, Object[] args) throws IntrospectionException, IllegalAccessException, NoSuchMethodException, InvocationTargetException {
Method m = klass.getDeclaredMethod(method, new Class[] {String.class});
m.invoke(null,args);
}
/** A utility for dynamically getting the value of a given static class
* method */
public static String dynamicGet(Class klass, String property) {
try {
Object value = P6Util.get(klass, property);
return value == null ? null : value.toString();
} catch (IntrospectionException e) {
P6LogQuery.logError("Could not get property "+property+" due to IntrospectionException");
} catch (IllegalAccessException e) {
P6LogQuery.logError("Could not get property "+property+" due to IllegalAccessException");
} catch (NoSuchMethodException e) {
P6LogQuery.logError("Could not get property "+property+" due to NoSuchMethodException");
} catch (InvocationTargetException e) {
P6LogQuery.logError("Could not get property "+property+" due to InvoicationTargetException");
}
return null;
}
public static Object get(Class klass, String method) throws IntrospectionException, IllegalAccessException, NoSuchMethodException, InvocationTargetException {
Method m = klass.getDeclaredMethod(method, null);
return (m.invoke(null,null));
}
public static Collection dynamicGetOptions(Class klass) {
try {
return (P6Util.findAllMethods(klass));
} catch (IntrospectionException e) {
P6LogQuery.logError("Could not get options list due to IntrospectionException");
}
return null;
}
public static ArrayList findAllMethods(Class klass) throws IntrospectionException {
ArrayList list = new ArrayList();
Method[] methods = klass.getDeclaredMethods();
for(int i=0; methods != null && i < methods.length; i++) {
Method method = methods[i];
String methodName = method.getName();
if (methodName.startsWith("get")) {
list.add(methodName);
}
}
return list;
}
// method add by jayakumar for JDK1.2 support for URL.getPath()
public static String getPath(URL theURL) {
String file = theURL.getFile();
String path = null;
if (file != null) {
int q = file.lastIndexOf('?');
if (q != -1) {
path = file.substring(0, q);
} else {
path = file;
}
}
return path;
}
// end of support method
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -