1 package expectj;
2
3 import java.io.IOException;
4 import java.net.UnknownHostException;
5
6 import com.jcraft.jsch.Channel;
7
8 /***
9 * This class is the starting point of the ExpectJ Utility. This class
10 * acts as factory for all {@link Spawn}s.
11 *
12 * @author Sachin Shekar Shetty
13 */
14 public class ExpectJ {
15 /*** Default timeout, -1 indicating wait for indefinite time */
16 private long m_lDefaultTimeOutSeconds = -1;
17
18 /***
19 * Create a new ExpectJ with specified timeout setting.
20 * @param defaultTimeoutSeconds default time out in seconds for the expect
21 * commands on the spawned process. -1 default time out indicates
22 * indefinite timeout.
23 */
24 public ExpectJ(long defaultTimeoutSeconds) {
25 m_lDefaultTimeOutSeconds = defaultTimeoutSeconds;
26 }
27
28 /***
29 * Create a new ExpectJ with an infinite timeout.
30 */
31 public ExpectJ() {
32
33 }
34
35 /***
36 * This method launches a {@link Spawnable}. Further expect commands can be
37 * invoked on the returned {@link Spawn} object.
38 *
39 * @param spawnable spawnable to be executed
40 * @return The newly spawned process
41 * @throws IOException if the spawning fails
42 */
43 public Spawn spawn(Spawnable spawnable) throws IOException {
44 return new Spawn(spawnable, m_lDefaultTimeOutSeconds);
45 }
46
47 /***
48 * This method spawns a new process. Further expect commands can be invoked
49 * on the returned {@link Spawn} object.
50 *
51 * @param command command to be executed
52 * @return The newly spawned process
53 * @throws IOException if the process spawning fails
54 * @see Runtime#exec(String)
55 */
56 public Spawn spawn(final String command) throws IOException {
57 return spawn(new ProcessSpawn(new Executor() {
58 public Process execute() throws IOException {
59 return Runtime.getRuntime().exec(command);
60 }
61
62 public String toString() {
63 return command;
64 }
65 }));
66 }
67
68 /***
69 * This method spawns a new process. Further expect commands can be invoked
70 * on the returned {@link Spawn} object.
71 *
72 * @param executor Will be called upon to start the new process
73 * @return The newly spawned process
74 * @throws IOException if the process spawning fails
75 * @see Runtime#exec(String[])
76 */
77 public Spawn spawn(Executor executor) throws IOException
78 {
79 return spawn(new ProcessSpawn(executor));
80 }
81
82 /***
83 * This method spawns a telnet connection to the given host and port number.
84 * Further expect commands can be invoked on the returned {@link Spawn}
85 * object.
86 *
87 * @param hostName The name of the host to connect to.
88 * @param port The remote port to connect to.
89 * @return The newly spawned telnet session.
90 * @throws IOException if the telnet spawning fails
91 * @throws UnknownHostException if you specify a bogus host name
92 *
93 * @see TelnetSpawn
94 * @see #spawn(String, int, String, String)
95 * @see #spawn(Channel)
96 */
97 public Spawn spawn(String hostName, int port)
98 throws IOException
99 {
100 return spawn(new TelnetSpawn(hostName, port));
101 }
102
103 /***
104 * This method creates a spawn that controls an SSH connection.
105 *
106 * @param channel The SSH channel to control.
107 *
108 * @return A spawn controlling the SSH channel.
109 *
110 * @throws IOException If taking control over the SSH channel fails.
111 *
112 * @see #spawn(String, int, String, String)
113 *
114 * @see SshSpawn#SshSpawn(Channel)
115 */
116 public Spawn spawn(Channel channel) throws IOException {
117 return spawn(new SshSpawn(channel));
118 }
119
120 /***
121 * This method creates a spawn that controls an SSH connection.
122 *
123 * @param remoteHostName The remote host to connect to.
124 *
125 * @param remotePort The remote port to connect to.
126 *
127 * @param userName The user name with which to authenticate
128 *
129 * @param password The password with which to authenticate
130 *
131 * @return A spawn controlling the SSH channel.
132 *
133 * @throws IOException If taking control over the SSH channel fails.
134 *
135 * @see #spawn(Channel)
136 *
137 * @see SshSpawn#SshSpawn(String, int, String, String)
138 */
139 public Spawn spawn(String remoteHostName, int remotePort, String userName, String password) throws IOException {
140 return spawn(new SshSpawn(remoteHostName, remotePort, userName, password));
141 }
142 }