01: /*
02: * Licensed to the Apache Software Foundation (ASF) under one or more
03: * contributor license agreements. See the NOTICE file distributed with
04: * this work for additional information regarding copyright ownership.
05: * The ASF licenses this file to You under the Apache License, Version 2.0
06: * (the "License"); you may not use this file except in compliance with
07: * the License. You may obtain a copy of the License at
08: *
09: * http://www.apache.org/licenses/LICENSE-2.0
10: *
11: * Unless required by applicable law or agreed to in writing, software
12: * distributed under the License is distributed on an "AS IS" BASIS,
13: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14: * See the License for the specific language governing permissions and
15: * limitations under the License.
16: */
17: package org.apache.wicket.threadtest.tester;
18:
19: import java.util.List;
20:
21: import org.apache.commons.httpclient.HttpClient;
22: import org.apache.commons.logging.Log;
23: import org.apache.commons.logging.LogFactory;
24:
25: /**
26: * Runs a command.
27: *
28: * @author eelcohillenius
29: */
30: public class CommandRunner implements Runnable {
31:
32: public static interface CommandRunnerObserver {
33:
34: void onDone(CommandRunner runner);
35:
36: void onError(CommandRunner runner, Exception e);
37: }
38:
39: private static final Log log = LogFactory
40: .getLog(CommandRunner.class);
41:
42: private HttpClient client;
43:
44: private final List<Command> commands;
45:
46: private final CommandRunnerObserver observer;
47:
48: /**
49: * Construct.
50: *
51: * @param commands
52: * @param client
53: */
54: public CommandRunner(List<Command> commands, HttpClient client,
55: CommandRunnerObserver observer) {
56: this .commands = commands;
57: this .client = client;
58: this .observer = observer;
59: }
60:
61: /**
62: * Gets the HTTP client.
63: *
64: * @return the HTTP client
65: */
66: public HttpClient getClient() {
67: return this .client;
68: }
69:
70: /**
71: * @see java.lang.Runnable#run()
72: */
73: public void run() {
74:
75: for (Command command : commands) {
76: try {
77: command.execute(this );
78: } catch (Exception e) {
79: log.fatal("execution of command " + command
80: + ", thread " + Thread.currentThread()
81: + " failed", e);
82: observer.onError(this, e);
83: return;
84: }
85: }
86: observer.onDone(this);
87: }
88: }
|