Copyright© 2008-2022 Sitevision AB, all rights reserved.
public interface ExceptionSuppressingCollection extends Collection<ExceptionSuppressingProxy>
ExceptionSuppressingIterator
to ensure exceptions are suppressed for all
iterator invocations.
The sole purpose of the ExceptionSuppressingCollection
is to provide easy access to a decorated iterator
(i.e. an ExceptionSuppressingIterator
). You should not create a collection of exception suppressing proxys
(i.e. Collection<ExceptionSuppressingProxy>
) yourself. You should use ExceptionSuppressingCollection
with your "regular" collection since the actual proxying is done by the iterator itself.
Note that the next
method of the interator returns an instance of a Dynamic proxy so not all methods can be invoked
directly on the returned object. To invoke a method that is not proxied (i.e. a method not declared in an interface the object implements),
you must extract the "real" object from the proxy and use that reference when invoking the method. See ExceptionSuppressingIterator
for more information about the iterator and ExceptionSuppressingProxy
for more information about dynamic proxies and how to extract
"real" object.
Important note! This interface is intended to be used only in contexts where exceptions when iterating a collection will cause serious trouble if they arise and the context doesn't provide proper exception handling (e.g. when rendering a Velocity template). Suppressing exceptions is generally a bad idea and using this decorator will affect performance. Hence, this interface should only be used where the upsides with suppressing exceptions outweighs the downsides. This interface should be used only as a temporary patch/solution until the "real" problem causing the exception has been solved.
An instance of the Sitevision class implementing this interface can be obtained via
Utils.getExceptionSuppressingCollection(java.util.Collection)
.
See Utils
for how to obtain an instance of the Utils
interface.
----------------------------------------------------------------------------------------------------
Example of how ExceptionSuppressingCollection
could be used in Velocity:
Case description:
You have a Velocity template where you iterate a collection. Sometimes a certain method 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 decorate your collection with a ExceptionSuppressingCollection
to get a decorated iterator. When interating, all returned elements are proxied by an ExceptionSuppressingProxy
.
Invoking the method through the proxy will suppress exceptions and you can diminish the output problems.
Note that you should invoke the real object for all non-problematic methods and methods that can't be proxied
(i.e. methods not declared in an interface). To get a reference to the real object,
use ExceptionSuppressingProxy.getProxiedObject()
.
This is what the code looks like before your temporary fix:
#foreach ($item in $myCollection)
<p>
$item.thisNeverThrowsExceptions()
$item.thisMethodSometimesThrowsExceptions()
</p>
#end
This is how you could use an ExceptionSuppressingCollection
to apply a temporary fix:
## Decorate collection
#set ($decoratedCollection = $sitevisionUtils.getExceptionSuppressingCollection($myCollection))
#foreach ($item in $decoratedCollection)
<p>
## Get the real object and invoke non-problematic method as usual
#set ($realObject = $item.getProxiedObject())
$realObject.thisNeverThrowsExceptions()
$!item.thisMethodSometimesThrowsExceptions() ## Executed through a proxy, might return null
</p>
#end
</p>
Modifier and Type | Method and Description |
---|---|
Iterator<ExceptionSuppressingProxy> |
iterator()
Returns an exception suppressing iterator that ensures no exceptions will be thrown during iteration.
|
add, addAll, clear, contains, containsAll, equals, hashCode, isEmpty, parallelStream, remove, removeAll, removeIf, retainAll, size, spliterator, stream, toArray, toArray
Iterator<ExceptionSuppressingProxy> iterator()
iterator
in interface Collection<ExceptionSuppressingProxy>
iterator
in interface Iterable<ExceptionSuppressingProxy>
ExceptionSuppressingIterator
that suppresses exceptions and returns proxied itemsSitevision - 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.