Skip to content

Commit

Permalink
Merge pull request #749 from volosied/MYFACES-4666-5.0
Browse files Browse the repository at this point in the history
[5.0] MYFACES-4666: Overload simpleClassForName -- new param to log exception only
  • Loading branch information
volosied authored Sep 10, 2024
2 parents b062baf + 1970e9c commit 64d4245
Show file tree
Hide file tree
Showing 3 changed files with 78 additions and 11 deletions.
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

0 comments on commit 64d4245

Please sign in to comment.