📄 run.java
字号:
* <p/>
* Exceptions at invocation time are either rethrown as a ServletException or as thr original exception if we can
* manage to do it.
* <p/>
* We don't log exceptions here, the container can do that.
*/
private RewriteMatch invokeRunMethod(Object classInstanceToRun, HttpServletRequest httpServletRequest,
HttpServletResponse httpServletResponse, FilterChain chain, Object[] matchObjs)
throws ServletException, InvocationTargetException {
if (log.isDebugEnabled()) {
log.debug("running " + classStr + "." + getMethodSignature() + " ");
}
if (classInstanceToRun == null || runMethod == null) return null;
RewriteMatch returned = null;
Object[] params = null;
if (runMethodParams != null && runMethodParams.length > 0) {
params = new Object[runMethodParams.length];
int paramMatchCounter = 0;
for (int i = 0; i < runMethodParams.length; i++) {
Class runMethodParam = runMethodParams[i];
String runMethodParamName = null;
if (runMethodParamNames != null && runMethodParamNames.length > i) {
runMethodParamName = runMethodParamNames[i];
}
Object param;
if (runMethodParamName != null) {
log.debug("need parameter from request called " + runMethodParamName);
Object matchObj = httpServletRequest.getParameter(runMethodParamName);
param = TypeUtils.getConvertedParam(runMethodParam, matchObj);
} else if (runMethodParam.isAssignableFrom(HttpServletRequest.class)) {
param = httpServletRequest;
} else if (runMethodParam.isAssignableFrom(HttpServletResponse.class)) {
param = httpServletResponse;
} else if (runMethodParam.isAssignableFrom(FilterChain.class)) {
param = chain;
} else {
Object matchObj = null;
if (matchObjs != null && matchObjs.length > paramMatchCounter) {
matchObj = matchObjs[paramMatchCounter];
}
param = TypeUtils.getConvertedParam(runMethodParam, matchObj);
paramMatchCounter++;
}
params[i] = param;
if (log.isDebugEnabled()) {
log.debug("argument " + i + " (" + runMethodParam.getName() + "): " + param);
}
}
}
try {
Object objReturned = runMethod.invoke(classInstanceToRun, (Object[]) params);
if ( jsonHandler ) {
returned = new JsonRewriteMatch(objReturned);
} else if (objReturned != null && objReturned instanceof RewriteMatch) {
// if we get a rewriteMatch object then return it for execution later
returned = (RewriteMatch) objReturned;
}
} catch (IllegalAccessException e) {
if (log.isDebugEnabled()) log.debug(e);
throw new ServletException(e);
}
return returned;
}
/**
* Run the underlying destroy methodStr on the run classStr.
*/
public void destroy() {
initialised = false;
valid = false;
invokeDestroy(runClassInstance);
// be paranoid and clean up all hooks to users classStr
destroyMethod = null;
runMethod = null;
initMethod = null;
filterInitMethod = null;
runServletConfig = null;
runConstructor = null;
runClassInstance = null;
methodStr = null;
classStr = null;
error = null;
}
/**
* @deprecated see #execute(HttpServletRequest,HttpServletResponse,Object[],FilterChain)
*/
public RewriteMatch execute(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse)
throws IOException, ServletException, InvocationTargetException {
return execute(httpServletRequest, httpServletResponse, null, null);
}
public RewriteMatch execute(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse,
StringMatchingMatcher matcher, ConditionMatch conditionMatch, FilterChain chain)
throws IOException, ServletException, InvocationTargetException {
int matches = 0;
int condMatches = 0;
if (matcher != null && matcher.isFound()) {
matches = matcher.groupCount();
}
if (conditionMatch != null) {
StringMatchingMatcher condMatcher = conditionMatch.getMatcher();
if (condMatcher != null && condMatcher.isFound()) {
condMatches = condMatcher.groupCount();
}
}
String[] allMatches = null;
if ((matches + condMatches) > 0) {
allMatches = new String[matches + condMatches];
if (matcher != null && matches > 0) {
for (int i = 0; i < matches; i++) {
allMatches[i] = matcher.group(i + 1); // note, good groups start from 1
}
}
if (conditionMatch != null && condMatches > 0) {
for (int i = 0; i < condMatches; i++) {
allMatches[i] = conditionMatch.getMatcher().group(i);
}
}
}
return execute(httpServletRequest, httpServletResponse, allMatches, chain);
}
/**
* Will invoke the instance created in initialise.
*
* @param httpServletRequest
* @param httpServletResponse
*/
public RewriteMatch execute(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse,
Throwable throwable)
throws IOException, ServletException, InvocationTargetException {
Object[] params = new Object[]{throwable};
return execute(httpServletRequest, httpServletResponse, params, null);
}
/**
* @deprecated use execute(HttpServletRequest, HttpServletResponse, Object[], FilterChain)
*/
public RewriteMatch execute(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse,
Object[] params) throws IOException, ServletException, InvocationTargetException {
return execute(httpServletRequest, httpServletResponse, params, null);
}
public RewriteMatch execute(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse,
Object[] params, FilterChain chain)
throws IOException, ServletException, InvocationTargetException {
if (!initialised) {
log.debug("not initialised skipping");
return null;
}
if (!valid) {
log.debug("not valid skipping");
return null;
}
RewriteMatch returned;
try {
if (newEachTime) {
Object newRunClassInstance = fetchNewInstance();
returned = invokeRunMethod(newRunClassInstance, httpServletRequest, httpServletResponse, chain, params);
invokeDestroy(newRunClassInstance);
} else {
returned = invokeRunMethod(runClassInstance, httpServletRequest, httpServletResponse, chain, params);
}
} catch (ServletException e) {
httpServletRequest.setAttribute("javax.servlet.error.exception", e);
throw e;
}
return returned;
}
private void logInvokeException(String methodStr, Exception e) {
Throwable cause = e.getCause();
if (cause == null) {
setError("when invoking " + methodStr + " on " + classStr
+ " got an " + e.toString(), e);
} else {
setError("when invoking " + methodStr + " on " + classStr
+ " got an " + e.toString() + " caused by " + cause.toString(), cause);
}
}
/**
* Get a new instance of the classStr we want to run and init if required.
*
* @return the new instance
*/
private Object fetchNewInstance() {
Object obj;
log.debug("getting new instance of " + classStr);
try {
obj = runConstructor.newInstance((Object[]) null);
} catch (InstantiationException e) {
logInvokeException("constructor", e);
return null;
} catch (IllegalAccessException e) {
logInvokeException("constructor", e);
return null;
} catch (InvocationTargetException e) {
logInvokeException("constructor", e);
return null;
}
if (initMethod != null) {
log.debug("about to run init(ServletConfig) on " + classStr);
Object[] args = new Object[1];
args[0] = runServletConfig;
try {
initMethod.invoke(obj, args);
} catch (IllegalAccessException e) {
logInvokeException("init(ServletConfig)", e);
return null;
} catch (InvocationTargetException e) {
logInvokeException("init(ServletConfig)", e);
return null;
}
}
if (filterInitMethod != null) {
log.debug("about to run init(FilterConfig) on " + classStr);
Object[] args = new Object[1];
args[0] = runServletConfig;
try {
filterInitMethod.invoke(obj, args);
} catch (IllegalAccessException e) {
logInvokeException("init(FilterConfig)", e);
return null;
} catch (InvocationTargetException e) {
logInvokeException("init(FilterConfig)", e);
return null;
}
}
return obj;
}
public String getError() {
return error;
}
public void setId(int id) {
this.id = id;
}
public int getId() {
return id;
}
public boolean isValid() {
return valid;
}
public boolean isInitialised() {
return initialised;
}
/**
* The name of the classStr that will be run for each rule match.
*
* @return String eg, org.tuckey.YellowObject
*/
public String getClassStr() {
return classStr;
}
/**
* The name of the methodStr that will be run for each rule match.
*
* @return String eg, setDate
*/
public String getMethodStr() {
return methodStr;
}
/**
* The name of the method signature ie, setDate(java.util.Date, int). Includes fully qualified object names
* for paramters.
*/
public String getMethodSignature() {
return TypeUtils.getMethodSignature(methodStr, runMethodParams);
}
public boolean isNewEachTime() {
return newEachTime;
}
public void setNewEachTime(boolean newEachTime) {
this.newEachTime = newEachTime;
}
/**
* Gets a handle on the instance of the class run is running.
* <p/>
* If newEachTime is set to true this will always return null.
*/
public Object getRunClassInstance() {
return runClassInstance;
}
public void addInitParam(String name, String value) {
if (name != null) {
initParams.put(name, value);
}
}
public String getInitParam(String paramName) {
return (String) initParams.get(paramName);
}
public void setClassStr(String classStr) {
this.classStr = classStr;
}
public void setMethodStr(String methodStr) {
this.methodStr = methodStr;
}
public static void setLoadClass(boolean loadClass) {
Run.loadClass = loadClass;
}
public void setError(String error, Throwable t) {
this.error = error;
log.error(error, t);
}
public void setError(String error) {
this.error = error;
log.error(error);
}
public String getDisplayName() {
return "Run " + id;
}
public boolean isFilter() {
return filter;
}
public void setJsonHandler(boolean jsonHandler) {
this.jsonHandler = jsonHandler;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -