エンタープライズ Bean クラス CheckingAccountBean は、
CommandTarget インターフェースだけでなく EntityBean インターフェースをインプリメントします。このクラスには、
リモート・インターフェース内のメソッドのビジネス・ロジック、必要なライフ・サイクル・メソッド
(ejbActivate、ejbStore など)、および CommandTarget インターフェースによって
宣言される executeCommand が含まれます。executeCommand メソッドは、エンタープライズ Bean クラス内の
唯一のコマンド固有コードです。これは、コマンドでの performExecute メソッドの実行を試行したり、
エラーが発生した場合には CommandException をスローしたりします。performExecute メソッドが
正常に実行されている場合、executeCommand メソッドは
hasOutputProperties メソッドを使用して、戻す必要がある出力プロパティーがあるかどうかを判別します。
コマンドに出力プロパティーがある場合、メソッドはクライアントにコマンド・オブジェクトを戻します。
次のコード例は CheckingAccountBean クラスの関連部分を示しています。
...
public class CheckingAccountBean implements EntityBean, CommandTarget {
// Bean variables
...
// Business methods from remote interface
...
// Life-cycle methods for CMP entity beans
...
// Method from the CommandTarget interface
public TargetableCommand executeCommand(TargetableCommand command)
throws RemoteException, CommandException
{
try {
command.performExecute();
}
catch(Exception ex) {
if (ex instanceof RemoteException) {
RemoveException remoteException = (RemoteException)ex;
if (remoteException.detail != null) {
throw new CommandException(remoteException.detail);
}
throw new CommandException(ex);
}
}
if (command.hasOutputProperties()) {
return command;
}
return null;
}
}