You can define custom target policies by implementing the TargetPolicy interface and providing a getCommandTarget method appropriate for your application. The TargetableCommandImpl class provides setTargetPolicy and getTargetPolicy methods for managing custom target policies.
So far, the target of all the commands has been a checking-account entity bean. Suppose that someone introduces a session enterprise bean (MySessionBean) that can also act as a command target. The following code example shows a simple custom policy that sets the target of every command to MySessionBean.
import java.io.*; import java.util.*; import java.beans.*; import com.ibm.websphere.command.*; public class CustomTargetPolicy implements TargetPolicy, Serializable { public CustomTargetPolicy { super(); } public CommandTarget getCommandTarget(TargetableCommand command) { CommandTarget = null; try { target = (CommandTarget)Beans.instantiate(null, "com.ibm.sfc.cmd.test.MySessionBean"); } catch (Exception e) { e.printStackTrace(); } } }
Because commands are implemented as JavaBeans components, using custom target policies requires importing the java.beans package and writing some elementary JavaBeans code. Also, your custom target-policy class must also implement the java.io.Serializable interface.