Package com.identityworksllc.iiq.common
Class BSHResourceCloser<T,U>
- java.lang.Object
-
- com.identityworksllc.iiq.common.BSHResourceCloser<T,U>
-
- Type Parameters:
T- The resource typeU- The return type
public abstract class BSHResourceCloser<T,U> extends Object
An interface to implement the “try with resources” type of structure in Beanshell, which does not support it.You should create an anonymous inner class extending
BSHResourceCloser, then pass it toexecute(BSHResourceCloser).The open() method will be called and should produce an AutoCloseable resource. After that, the run() method will be invoked with the opened resource. If you need more than one thing to be opened, you may want to return a
Closer.If your resource is AutoCloseable, its close() method will be invoked. If not, you must implement close() in your class.
You may
Example:
BSHResourceCloser.execute(new BSHResourceCloser() { public Object open() { // Open a connection or something } });
-
-
Constructor Summary
Constructors Constructor Description BSHResourceCloser()
-
Method Summary
All Methods Static Methods Instance Methods Abstract Methods Concrete Methods Modifier and Type Method Description voidclose(T resource)Closes the given resource.static <T,U>
Uexecute(BSHResourceCloser<T,U> closer)Executes your action after getting the resource requiredstatic <T> Objectexecute(T resource, bsh.This bshThis, String methodName)Executes the given Beanshell method with the resource, closing it once the method has completed.abstract Topen()Opens the resource for this actionabstract Urun(T resource)Executes the action, passing the resource returned from open()
-
-
-
Constructor Detail
-
BSHResourceCloser
public BSHResourceCloser()
-
-
Method Detail
-
execute
public static <T> Object execute(T resource, bsh.This bshThis, String methodName) throws Exception
Executes the given Beanshell method with the resource, closing it once the method has completed.- Type Parameters:
T- The type of the resource- Parameters:
resource- The resource to usebshThis- The ‘this’ reference in your Beanshell scriptmethodName- The method name to invoke with the resource- Returns:
- The result of the Beanshell method
- Throws:
Exception- if anything fails
-
execute
public static <T,U> U execute(BSHResourceCloser<T,U> closer) throws Exception
Executes your action after getting the resource required- Type Parameters:
T- The resource typeU- The return type- Parameters:
closer- The resource closer- Returns:
- The return from your run() method
- Throws:
Exception- if the resource open or the call fails
-
close
public void close(T resource)
Closes the given resource.This will be invoked if the resource does not implement AutoCloseable.
- Parameters:
resource- the resource to close
-
open
public abstract T open() throws Exception
Opens the resource for this action- Returns:
- The resource
- Throws:
Exception- if anything fails
-
-