Copyright© 2008-2022 Sitevision AB, all rights reserved.
public interface ExceptionSuppressingProxy extends VelocityAccess.ExceptionSuppressingProxyConstants
This interface is an exception suppressing implementation of a so called
Dynamic proxy.
This means it will proxy all methods of an object that are declared in an interface implemented by the object.
If a method invocation through this proxy throws an Exception
, it will be catched and the default value will be returned
(e.g. null
, 0
, false
etc).
Important note! This interface is intended to be used only in contexts where exceptions will cause serious trouble if they arise and the context doesn't provide proper exception handling (e.g. when executing a Velocity template). Suppressing exceptions is generally a really bad idea and using this proxy will affect performance. Hence, this interface should only be used where the upsides with suppressing exceptions far outweighs the downsides. This interface should be used only as a temporary patch/solution until the "real" problem causing the exception has been solved.
The state of an ExceptionSuppressingProxy is available at all times and updated for each method invocation
(except for invocations of methods declared in this interface).
If last method invocation resulted in a thrown exception the proxy status will be EXCEPTION_THROWN_STATUS
and the exception will be available via getCurrentException()
.
An instance of the Sitevision class implementing this interface can be obtained via
Utils.getExceptionSuppressingProxy(Object)
.
See Utils
for how to obtain an instance of the Utils
interface.
----------------------------------------------------------------------------------------------------
Example of how ExceptionSuppressingProxy
could be used in Velocity:
Case description:
You have a Velocity template where a certain method invocation sometimes throws an exception.
The rendered output of the template gets corrupt whenever an exception is thrown and you have not yet tracked down the root cause
of the exception. Until the root cause is found and the problem is fixed, you need a temporary solution that ignores exceptions
to keep template output acceptable. Since the method is declared in an interface and implemented in the instance you're invoking,
you're in sheer luck. You can use an ExceptionSuppressingProxy
to suppress exceptions in order to diminish output
problems.
This is what the code looks like before your temporary fix:
<p>
$myObject.thisNeverThrowsExceptions()
$myObject.thisMethodSometimesThrowsExceptions() ## This method is declared in an interface, hence can be proxied
</p>
This is how you could use an ExceptionSuppressingProxy
to apply a temporary fix:
<p>
$myObject.thisNeverThrowsExceptions()
## Get an ExceptionSuppressingProxy instance for myObject
#set ($proxy = $sitevisionUtils.getExceptionSuppressingProxy($myObject))
$!proxy.thisMethodSometimesThrowsExceptions() ## Method invoked through a proxy, might return null
</p>
Modifier and Type | Field and Description |
---|---|
static int |
EXCEPTION_THROWN_STATUS
The status indicating that last method invocation did throw an exception
|
static int |
NO_EXCEPTION_THROWN_STATUS
The status indicating that last method invocation did not throw an exception.
|
static int |
NO_OBJECT_PROXIED_STATUS
The status indicating that this proxy has no object set, nothing to proxy.
|
Modifier and Type | Method and Description |
---|---|
Exception |
getCurrentException()
Returns the exception that was thrown during last method invocation of the proxied object.
|
int |
getCurrentStatus()
Returns current status
|
Object |
getProxiedObject()
Returns the proxied object
|
getEXCEPTION_THROWN_STATUS, getNO_EXCEPTION_THROWN_STATUS, getNO_OBJECT_PROXIED_STATUS
static final int NO_OBJECT_PROXIED_STATUS
static final int NO_EXCEPTION_THROWN_STATUS
static final int EXCEPTION_THROWN_STATUS
int getCurrentStatus()
Exception getCurrentException()
null
if no exeption was thrownObject getProxiedObject()
null
if no proxied object is setSitevision - Portal and Content Management Made Easy
Sitevision is an advanced Java enterprise portal product and a portlet container (JSR 286) that implements Java Content Repository (JSR 283).
Copyright© 2008-2022 Sitevision AB, all rights reserved.