CommandTarget インターフェースは、クライアントが実装する 1 つのメソッド executeCommand を 宣言します。executeCommand メソッドは、TargetableCommand オブジェクトを入力として 取り、また TargetableCommand を戻します。
public TargetableCommand executeCommand(TargetableCommand command) throws CommandException { try { // Serialize the command byte[] array = serialize(command); // Create a connection to the servlet URL url = new URL ("http://" + hostName + "/servlet/com.ibm.websphere.command.servlet.CommandServlet"); HttpURLConnection httpURLConnection = (HttpURLConnection) url.openConnection(); // Set the properties of the connection ... // Put the serialized command on the output stream OutputStream outputStream = httpURLConnection.getOutputStream(); outputStream.write(array); // Create a return stream InputStream inputStream = httpURLConnection.getInputStream(); // Send the command to the servlet httpURLConnection.connect(); ObjectInputStream objectInputStream = new ObjectInputStream(inputStream); // Retrieve the command returned from the servlet Object object = objectInputStream.readObject(); if (object instanceof CommandException) { throw ((CommandException) object); } // Pass the returned command back to the calling method return (TargetableCommand) object; } // Handle exceptions .... }