Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[5.0] MYFACES-4666: Overload simpleClassForName -- new param to log exception only #749

Merged
merged 1 commit into from
Sep 10, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -163,14 +163,15 @@ public static Class simpleClassForName(String type)

/**
* Same as {link {@link #simpleClassForName(String)}, but will only
* log the exception and rethrow a RunTimeException if logException is true.
* log the exception and rethrow a RunTimeException if logAndThrowException is true.
*
* @param type
* @param logException - true to log/throw FacesException, false to avoid logging/throwing FacesException
* @param logAndThrowException - true to log and throw FacesException, false to avoid logging and throwing
* the FacesException
* @return the corresponding Class
* @throws FacesException if class not found and logException is true
* @throws FacesException if class not found and logAndThrowException is true
*/
public static Class simpleClassForName(String type, boolean logException)
public static Class simpleClassForName(String type, boolean logAndThrowException)
{
Class returnClass = null;
try
Expand All @@ -179,7 +180,7 @@ public static Class simpleClassForName(String type, boolean logException)
}
catch (ClassNotFoundException e)
{
if (logException)
if (logAndThrowException)
{
log.log(Level.SEVERE, "Class " + type + " not found", e);
throw new FacesException(e);
Expand All @@ -188,6 +189,38 @@ public static Class simpleClassForName(String type, boolean logException)
return returnClass;
}

/**
* Same as {link {@link #simpleClassForName(String)}, but accepts two booleans
* One to log an exception and another to rethrow a FacesExceptions
*
* @param type
* @param logException - true to log the ClassNotFoundException, false to avoid logging
* @param throwException - true to throw a FacesException, false to avoid throwing a FacesException
* @return the corresponding Class
* @throws FacesException if class not found and throwException is true
*/
public static Class simpleClassForName(String type, boolean throwException, boolean logException)
{
Class returnClass = null;
try
{
returnClass = classForName(type);
}
catch (ClassNotFoundException e)
{
if(logException)
{
log.log(Level.SEVERE, "Class " + type + " not found", e);
}
if (throwException)
{
throw new FacesException(e);
}
}
return returnClass;
}


/**
* Similar as {@link #classForName(String)}, but also supports primitive types and arrays as specified for the
* JavaType element in the JavaServer Faces Config DTD.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -174,7 +174,7 @@ public static boolean isFacesServlet(String servletClassName)
return true;
}

Class servletClass = ClassUtils.simpleClassForName(servletClassName, true);
Class servletClass = ClassUtils.simpleClassForName(servletClassName, false, true);
if (servletClass != null)
{
return FacesServlet.class.isAssignableFrom(servletClass);
Expand Down
44 changes: 39 additions & 5 deletions impl/src/main/java/org/apache/myfaces/util/lang/ClassUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -241,16 +241,17 @@ public static Class simpleClassForName(String type)

/**
* Same as {link {@link #simpleClassForName(String)}, but will only
* log the exception and rethrow a RunTimeException if logException is true.
* log the exception and rethrow a RunTimeException if logAndThrowException is true.
*
* @param type
* @param logException - true to log/throw FacesException, false to avoid logging/throwing FacesException
* @param logAndThrowException - true to log and throw FacesException, false to avoid logging and throwing
* the FacesException
* @return the corresponding Class
* @throws FacesException if class not found and logException is true
* @throws FacesException if class not found and logAndThrowException is true
*/
// @Override MYFACES-4449: Methods that call ClassUtils.class.getClassLoader() need to be here
// as well as in the API ClassUtils so that the correct ClassLoader is used.
public static Class simpleClassForName(String type, boolean logException)
public static Class simpleClassForName(String type, boolean logAndThrowException)
{
Class returnClass = null;
try
Expand All @@ -259,7 +260,7 @@ public static Class simpleClassForName(String type, boolean logException)
}
catch (ClassNotFoundException e)
{
if (logException)
if (logAndThrowException)
{
log.log(Level.SEVERE, "Class " + type + " not found", e);
throw new FacesException(e);
Expand All @@ -268,6 +269,39 @@ public static Class simpleClassForName(String type, boolean logException)
return returnClass;
}

/**
* Same as {link {@link #simpleClassForName(String)}, but accepts two booleans
* One to log an exception and another to rethrow a FacesExceptions
*
* @param type
* @param logException - true to log the ClassNotFoundException, false to avoid logging
* @param throwException - true to throw a FacesException, false to avoid throwing a FacesException
* @return the corresponding Class
* @throws FacesException if class not found and throwException is true
*/
// @Override MYFACES-4449: Methods that call ClassUtils.class.getClassLoader() need to be here
// as well as in the API ClassUtils so that the correct ClassLoader is used.
public static Class simpleClassForName(String type, boolean throwException, boolean logException)
{
Class returnClass = null;
try
{
returnClass = classForName(type);
}
catch (ClassNotFoundException e)
{
if(logException)
{
log.log(Level.SEVERE, "Class " + type + " not found", e);
}
if (throwException)
{
throw new FacesException(e);
}
}
return returnClass;
}

// @Override MYFACES-4449: Methods that call ClassUtils.class.getClassLoader() need to be here
// as well as in the API ClassUtils so that the correct ClassLoader is used.
public static URL getResource(String resource)
Expand Down