Many resources are needed to download a project. Please understand that we have to compensate our server costs. Thank you in advance. Project price only 1 $
You can buy this project and download/modify it how often you want.
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.tomcat.util;
import java.io.File;
import java.io.FilenameFilter;
import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.net.InetAddress;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.UnknownHostException;
import java.util.Hashtable;
import java.util.StringTokenizer;
import java.util.Vector;
/**
* Utils for introspection and reflection
*/
public final class IntrospectionUtils {
private static final org.apache.juli.logging.Log log=
org.apache.juli.logging.LogFactory.getLog( IntrospectionUtils.class );
/**
* Call execute() - any ant-like task should work
* @deprecated Not used
*/
@Deprecated
public static void execute(Object proxy, String method) throws Exception {
Method executeM = null;
Class> c = proxy.getClass();
Class> params[] = new Class[0];
// params[0]=args.getClass();
executeM = findMethod(c, method, params);
if (executeM == null) {
throw new RuntimeException("No execute in " + proxy.getClass());
}
executeM.invoke(proxy, (Object[]) null);//new Object[] { args });
}
/**
* Call void setAttribute( String ,Object )
* @deprecated Not used
*/
@Deprecated
public static void setAttribute(Object proxy, String n, Object v)
throws Exception {
if (proxy instanceof AttributeHolder) {
((AttributeHolder) proxy).setAttribute(n, v);
return;
}
Method executeM = null;
Class> c = proxy.getClass();
Class> params[] = new Class[2];
params[0] = String.class;
params[1] = Object.class;
executeM = findMethod(c, "setAttribute", params);
if (executeM == null) {
if (log.isDebugEnabled())
log.debug("No setAttribute in " + proxy.getClass());
return;
}
if (log.isDebugEnabled())
log.debug("Setting " + n + "=" + v + " in " + proxy);
executeM.invoke(proxy, new Object[] { n, v });
return;
}
/**
* Call void getAttribute( String )
* @deprecated Not used
*/
@Deprecated
public static Object getAttribute(Object proxy, String n) throws Exception {
Method executeM = null;
Class> c = proxy.getClass();
Class> params[] = new Class[1];
params[0] = String.class;
executeM = findMethod(c, "getAttribute", params);
if (executeM == null) {
if (log.isDebugEnabled())
log.debug("No getAttribute in " + proxy.getClass());
return null;
}
return executeM.invoke(proxy, new Object[] { n });
}
/**
* Construct a URLClassLoader. Will compile and work in JDK1.1 too.
* @deprecated Not used
*/
@Deprecated
public static ClassLoader getURLClassLoader(URL urls[], ClassLoader parent) {
try {
Class> urlCL = Class.forName("java.net.URLClassLoader");
Class> paramT[] = new Class[2];
paramT[0] = urls.getClass();
paramT[1] = ClassLoader.class;
Method m = findMethod(urlCL, "newInstance", paramT);
if (m == null)
return null;
ClassLoader cl = (ClassLoader) m.invoke(urlCL, new Object[] { urls,
parent });
return cl;
} catch (ClassNotFoundException ex) {
// jdk1.1
return null;
} catch (Exception ex) {
ex.printStackTrace();
return null;
}
}
/**
* @deprecated No longer required. Will be removed in Tomcat 8.0.x.
*/
@Deprecated
public static String guessInstall(String installSysProp,
String homeSysProp, String jarName) {
return guessInstall(installSysProp, homeSysProp, jarName, null);
}
/**
* Guess a product install/home by analyzing the class path. It works for
* product using the pattern: lib/executable.jar or if executable.jar is
* included in classpath by a shell script. ( java -jar also works )
*
* Insures both "install" and "home" System properties are set. If either or
* both System properties are unset, "install" and "home" will be set to the
* same value. This value will be the other System property that is set, or
* the guessed value if neither is set.
*
* @deprecated No longer required. Will be removed in Tomcat 8.0.x.
*/
@Deprecated
public static String guessInstall(String installSysProp,
String homeSysProp, String jarName, String classFile) {
String install = null;
String home = null;
if (installSysProp != null)
install = System.getProperty(installSysProp);
if (homeSysProp != null)
home = System.getProperty(homeSysProp);
if (install != null) {
if (home == null)
System.getProperties().put(homeSysProp, install);
return install;
}
// Find the directory where jarName.jar is located
String cpath = System.getProperty("java.class.path");
String pathSep = File.pathSeparator;
StringTokenizer st = new StringTokenizer(cpath, pathSep);
while (st.hasMoreTokens()) {
String path = st.nextToken();
// log( "path " + path );
if (path.endsWith(jarName)) {
home = path.substring(0, path.length() - jarName.length());
try {
if ("".equals(home)) {
home = new File("./").getCanonicalPath();
} else if (home.endsWith(File.separator)) {
home = home.substring(0, home.length() - 1);
}
File f = new File(home);
String parentDir = f.getParent();
if (parentDir == null)
parentDir = home; // unix style
File f1 = new File(parentDir);
install = f1.getCanonicalPath();
if (installSysProp != null)
System.getProperties().put(installSysProp, install);
if (home == null && homeSysProp != null)
System.getProperties().put(homeSysProp, install);
return install;
} catch (Exception ex) {
ex.printStackTrace();
}
} else {
String fname = path + (path.endsWith("/") ? "" : "/")
+ classFile;
if (new File(fname).exists()) {
try {
File f = new File(path);
String parentDir = f.getParent();
if (parentDir == null)
parentDir = path; // unix style
File f1 = new File(parentDir);
install = f1.getCanonicalPath();
if (installSysProp != null)
System.getProperties().put(installSysProp, install);
if (home == null && homeSysProp != null)
System.getProperties().put(homeSysProp, install);
return install;
} catch (Exception ex) {
ex.printStackTrace();
}
}
}
}
// if install directory can't be found, use home as the default
if (home != null) {
System.getProperties().put(installSysProp, home);
return home;
}
return null;
}
/**
* Debug method, display the classpath
* @deprecated Not used
*/
@Deprecated
public static void displayClassPath(String msg, URL[] cp) {
if (log.isDebugEnabled()) {
log.debug(msg);
for (int i = 0; i < cp.length; i++) {
log.debug(cp[i].getFile());
}
}
}
/**
* @deprecated Used only by deprecated method
*/
@Deprecated
public static final String PATH_SEPARATOR = File.pathSeparator;
/**
* Adds classpath entries from a vector of URL's to the "tc_path_add" System
* property. This System property lists the classpath entries common to web
* applications. This System property is currently used by Jasper when its
* JSP servlet compiles the Java file for a JSP.
* @deprecated Not used
*/
@Deprecated
public static String classPathAdd(URL urls[], String cp) {
if (urls == null)
return cp;
for (int i = 0; i < urls.length; i++) {
if (cp != null)
cp += PATH_SEPARATOR + urls[i].getFile();
else
cp = urls[i].getFile();
}
return cp;
}
/**
* Find a method with the right name If found, call the method ( if param is
* int or boolean we'll convert value to the right type before) - that means
* you can have setDebug(1).
*/
public static boolean setProperty(Object o, String name, String value) {
return setProperty(o,name,value,true);
}
public static boolean setProperty(Object o, String name, String value,
boolean invokeSetProperty) {
if (log.isDebugEnabled())
log.debug("IntrospectionUtils: setProperty(" +
o.getClass() + " " + name + "=" + value + ")");
String setter = "set" + capitalize(name);
try {
Method methods[] = findMethods(o.getClass());
Method setPropertyMethodVoid = null;
Method setPropertyMethodBool = null;
// First, the ideal case - a setFoo( String ) method
for (int i = 0; i < methods.length; i++) {
Class> paramT[] = methods[i].getParameterTypes();
if (setter.equals(methods[i].getName()) && paramT.length == 1
&& "java.lang.String".equals(paramT[0].getName())) {
methods[i].invoke(o, new Object[] { value });
return true;
}
}
// Try a setFoo ( int ) or ( boolean )
for (int i = 0; i < methods.length; i++) {
boolean ok = true;
if (setter.equals(methods[i].getName())
&& methods[i].getParameterTypes().length == 1) {
// match - find the type and invoke it
Class> paramType = methods[i].getParameterTypes()[0];
Object params[] = new Object[1];
// Try a setFoo ( int )
if ("java.lang.Integer".equals(paramType.getName())
|| "int".equals(paramType.getName())) {
try {
params[0] = Integer.valueOf(value);
} catch (NumberFormatException ex) {
ok = false;
}
// Try a setFoo ( long )
}else if ("java.lang.Long".equals(paramType.getName())
|| "long".equals(paramType.getName())) {
try {
params[0] = Long.valueOf(value);
} catch (NumberFormatException ex) {
ok = false;
}
// Try a setFoo ( boolean )
} else if ("java.lang.Boolean".equals(paramType.getName())
|| "boolean".equals(paramType.getName())) {
params[0] = Boolean.valueOf(value);
// Try a setFoo ( InetAddress )
} else if ("java.net.InetAddress".equals(paramType
.getName())) {
try {
params[0] = InetAddress.getByName(value);
} catch (UnknownHostException exc) {
if (log.isDebugEnabled())
log.debug("IntrospectionUtils: Unable to resolve host name:" + value);
ok = false;
}
// Unknown type
} else {
if (log.isDebugEnabled())
log.debug("IntrospectionUtils: Unknown type " +
paramType.getName());
}
if (ok) {
methods[i].invoke(o, params);
return true;
}
}
// save "setProperty" for later
if ("setProperty".equals(methods[i].getName())) {
if (methods[i].getReturnType()==Boolean.TYPE){
setPropertyMethodBool = methods[i];
}else {
setPropertyMethodVoid = methods[i];
}
}
}
// Ok, no setXXX found, try a setProperty("name", "value")
if (invokeSetProperty && (setPropertyMethodBool != null ||
setPropertyMethodVoid != null)) {
Object params[] = new Object[2];
params[0] = name;
params[1] = value;
if (setPropertyMethodBool != null) {
try {
return ((Boolean) setPropertyMethodBool.invoke(o,
params)).booleanValue();
}catch (IllegalArgumentException biae) {
//the boolean method had the wrong
//parameter types. lets try the other
if (setPropertyMethodVoid!=null) {
setPropertyMethodVoid.invoke(o, params);
return true;
}else {
throw biae;
}
}
} else {
setPropertyMethodVoid.invoke(o, params);
return true;
}
}
} catch (IllegalArgumentException ex2) {
log.warn("IAE " + o + " " + name + " " + value, ex2);
} catch (SecurityException ex1) {
log.warn("IntrospectionUtils: SecurityException for " +
o.getClass() + " " + name + "=" + value + ")", ex1);
} catch (IllegalAccessException iae) {
log.warn("IntrospectionUtils: IllegalAccessException for " +
o.getClass() + " " + name + "=" + value + ")", iae);
} catch (InvocationTargetException ie) {
ExceptionUtils.handleThrowable(ie.getCause());
log.warn("IntrospectionUtils: InvocationTargetException for " +
o.getClass() + " " + name + "=" + value + ")", ie);
}
return false;
}
public static Object getProperty(Object o, String name) {
String getter = "get" + capitalize(name);
String isGetter = "is" + capitalize(name);
try {
Method methods[] = findMethods(o.getClass());
Method getPropertyMethod = null;
// First, the ideal case - a getFoo() method
for (int i = 0; i < methods.length; i++) {
Class> paramT[] = methods[i].getParameterTypes();
if (getter.equals(methods[i].getName()) && paramT.length == 0) {
return methods[i].invoke(o, (Object[]) null);
}
if (isGetter.equals(methods[i].getName()) && paramT.length == 0) {
return methods[i].invoke(o, (Object[]) null);
}
if ("getProperty".equals(methods[i].getName())) {
getPropertyMethod = methods[i];
}
}
// Ok, no setXXX found, try a getProperty("name")
if (getPropertyMethod != null) {
Object params[] = new Object[1];
params[0] = name;
return getPropertyMethod.invoke(o, params);
}
} catch (IllegalArgumentException ex2) {
log.warn("IAE " + o + " " + name, ex2);
} catch (SecurityException ex1) {
log.warn("IntrospectionUtils: SecurityException for " +
o.getClass() + " " + name + ")", ex1);
} catch (IllegalAccessException iae) {
log.warn("IntrospectionUtils: IllegalAccessException for " +
o.getClass() + " " + name + ")", iae);
} catch (InvocationTargetException ie) {
ExceptionUtils.handleThrowable(ie.getCause());
log.warn("IntrospectionUtils: InvocationTargetException for " +
o.getClass() + " " + name + ")", ie);
}
return null;
}
/**
* @deprecated Not used
*/
@Deprecated
public static void setProperty(Object o, String name) {
String setter = "set" + capitalize(name);
try {
Method methods[] = findMethods(o.getClass());
// find setFoo() method
for (int i = 0; i < methods.length; i++) {
Class> paramT[] = methods[i].getParameterTypes();
if (setter.equals(methods[i].getName()) && paramT.length == 0) {
methods[i].invoke(o, new Object[] {});
return;
}
}
} catch (Exception ex1) {
if (log.isDebugEnabled())
log.debug("IntrospectionUtils: Exception for " +
o.getClass() + " " + name, ex1);
}
}
/**
* Replace ${NAME} with the property value
*/
public static String replaceProperties(String value,
Hashtable