~ubuntu-branches/ubuntu/jaunty/ant/jaunty-proposed

« back to all changes in this revision

Viewing changes to src/main/org/apache/tools/ant/taskdefs/optional/net/TelnetTask.java

  • Committer: Bazaar Package Importer
  • Author(s): Stefan Gybas
  • Date: 2002-02-14 14:28:48 UTC
  • Revision ID: james.westby@ubuntu.com-20020214142848-2ww7ynmqkj31vlmn
Tags: upstream-1.4.1
ImportĀ upstreamĀ versionĀ 1.4.1

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * The Apache Software License, Version 1.1
 
3
 *
 
4
 * Copyright (c) 2000 The Apache Software Foundation.  All rights
 
5
 * reserved.
 
6
 *
 
7
 * Redistribution and use in source and binary forms, with or without
 
8
 * modification, are permitted provided that the following conditions
 
9
 * are met:
 
10
 *
 
11
 * 1. Redistributions of source code must retain the above copyright
 
12
 *    notice, this list of conditions and the following disclaimer.
 
13
 *
 
14
 * 2. Redistributions in binary form must reproduce the above copyright
 
15
 *    notice, this list of conditions and the following disclaimer in
 
16
 *    the documentation and/or other materials provided with the
 
17
 *    distribution.
 
18
 *
 
19
 * 3. The end-user documentation included with the redistribution, if
 
20
 *    any, must include the following acknowlegement:
 
21
 *       "This product includes software developed by the
 
22
 *        Apache Software Foundation (http://www.apache.org/)."
 
23
 *    Alternately, this acknowlegement may appear in the software itself,
 
24
 *    if and wherever such third-party acknowlegements normally appear.
 
25
 *
 
26
 * 4. The names "The Jakarta Project", "Ant", and "Apache Software
 
27
 *    Foundation" must not be used to endorse or promote products derived
 
28
 *    from this software without prior written permission. For written
 
29
 *    permission, please contact apache@apache.org.
 
30
 *
 
31
 * 5. Products derived from this software may not be called "Apache"
 
32
 *    nor may "Apache" appear in their names without prior written
 
33
 *    permission of the Apache Group.
 
34
 *
 
35
 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
 
36
 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
 
37
 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
 
38
 * DISCLAIMED.  IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
 
39
 * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
 
40
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
 
41
 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
 
42
 * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
 
43
 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
 
44
 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
 
45
 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
 
46
 * SUCH DAMAGE.
 
47
 * ====================================================================
 
48
 *
 
49
 * This software consists of voluntary contributions made by many
 
50
 * individuals on behalf of the Apache Software Foundation.  For more
 
51
 * information on the Apache Software Foundation, please see
 
52
 * <http://www.apache.org/>.
 
53
 */
 
54
 
 
55
package org.apache.tools.ant.taskdefs.optional.net;
 
56
 
 
57
import org.apache.tools.ant.BuildException;
 
58
import org.apache.tools.ant.Task;
 
59
import org.apache.tools.ant.Project;
 
60
import org.apache.tools.ant.DirectoryScanner;
 
61
import org.apache.tools.ant.taskdefs.MatchingTask;
 
62
import com.oroinc.net.telnet.*;
 
63
import org.apache.tools.ant.BuildException;
 
64
import java.io.*;
 
65
import java.lang.*;
 
66
import java.util.*;
 
67
 
 
68
/**
 
69
 * Class to provide automated telnet protocol support for the Ant build tool
 
70
 *
 
71
 * @author Scott Carlson<a href="mailto:ScottCarlson@email.com">ScottCarlson@email.com</a>
 
72
 * @version $Revision: 1.2 $
 
73
 */
 
74
 
 
75
public class TelnetTask extends Task {
 
76
    /**
 
77
     *  The userid to login with, if automated login is used
 
78
     */
 
79
    private String userid  = null;
 
80
 
 
81
    /**
 
82
     *  The password to login with, if automated login is used
 
83
     */
 
84
    private String password= null;
 
85
 
 
86
    /**
 
87
     *  The server to connect to. 
 
88
     */
 
89
    private String server  = null;
 
90
 
 
91
    /**
 
92
     *  The tcp port to connect to. 
 
93
     */
 
94
    private int port = 23;
 
95
 
 
96
    /**
 
97
     *  The Object which handles the telnet session.
 
98
     */
 
99
    private AntTelnetClient telnet = null;
 
100
 
 
101
    /**
 
102
     *  The list of read/write commands for this session
 
103
     */
 
104
    private Vector telnetTasks = new Vector();
 
105
 
 
106
    /** 
 
107
     *  If true, adds a CR to beginning of login script
 
108
     */
 
109
    private boolean addCarriageReturn = false;
 
110
 
 
111
    /**
 
112
     *  Default time allowed for waiting for a valid response
 
113
     *  for all child reads.  A value of 0 means no limit.
 
114
     */
 
115
    private Integer defaultTimeout = null;
 
116
 
 
117
    /** 
 
118
     *  Verify that all parameters are included. 
 
119
     *  Connect and possibly login
 
120
     *  Iterate through the list of Reads and writes 
 
121
     */
 
122
    public void execute() throws BuildException 
 
123
    {
 
124
       /**  A server name is required to continue */
 
125
       if (server== null)
 
126
           throw new BuildException("No Server Specified");
 
127
       /**  A userid and password must appear together 
 
128
        *   if they appear.  They are not required.
 
129
        */
 
130
       if (userid == null && password != null)
 
131
           throw new BuildException("No Userid Specified");
 
132
       if (password == null && userid != null)
 
133
           throw new BuildException("No Password Specified");
 
134
 
 
135
       /**  Create the telnet client object */
 
136
       telnet = new AntTelnetClient();
 
137
       try {
 
138
           telnet.connect(server, port);
 
139
       } catch(IOException e) {
 
140
           throw new BuildException("Can't connect to "+server);
 
141
       }
 
142
       /**  Login if userid and password were specified */
 
143
       if (userid != null && password != null)
 
144
          login();
 
145
       /**  Process each sub command */
 
146
       Enumeration tasksToRun = telnetTasks.elements();
 
147
       while (tasksToRun!=null && tasksToRun.hasMoreElements())
 
148
       {
 
149
           TelnetSubTask task = (TelnetSubTask) tasksToRun.nextElement();
 
150
           if (task instanceof TelnetRead && defaultTimeout != null)
 
151
               ((TelnetRead)task).setDefaultTimeout(defaultTimeout);
 
152
           task.execute(telnet);
 
153
       }
 
154
    }
 
155
 
 
156
    /**  
 
157
     *  Process a 'typical' login.  If it differs, use the read 
 
158
     *  and write tasks explicitely
 
159
     */
 
160
    private void login()
 
161
    {
 
162
       if (addCarriageReturn)
 
163
          telnet.sendString("\n", true);
 
164
       telnet.waitForString("ogin:");
 
165
       telnet.sendString(userid, true);
 
166
       telnet.waitForString("assword:");
 
167
       telnet.sendString(password, false);
 
168
    }
 
169
 
 
170
    /**
 
171
     *  Set the userid attribute 
 
172
     */
 
173
    public void setUserid(String u) { this.userid = u; }
 
174
 
 
175
    /**
 
176
     *  Set the password attribute 
 
177
     */
 
178
    public void setPassword(String p) { this.password = p; }
 
179
 
 
180
    /**
 
181
     *  Set the server address attribute 
 
182
     */
 
183
    public void setServer(String m) { this.server = m; }
 
184
 
 
185
    /**
 
186
     *  Set the tcp port to connect to attribute 
 
187
     */
 
188
    public void setPort(int p) { this.port = p; }
 
189
 
 
190
    /**
 
191
     *  Set the tcp port to connect to attribute 
 
192
     */
 
193
    public void setInitialCR(boolean b)
 
194
    {
 
195
       this.addCarriageReturn = b;
 
196
    }
 
197
 
 
198
    /**
 
199
     *  Change the default timeout to wait for 
 
200
     *  valid responses
 
201
     */
 
202
    public void setTimeout(Integer i)
 
203
    {
 
204
       this.defaultTimeout = i;
 
205
    }
 
206
 
 
207
    /**
 
208
     *  A subTask <read> tag was found.  Create the object, 
 
209
     *  Save it in our list, and return it.
 
210
     */
 
211
   
 
212
    public TelnetSubTask createRead()
 
213
    {
 
214
        TelnetSubTask task = (TelnetSubTask)new TelnetRead();
 
215
        telnetTasks.addElement(task);
 
216
        return task;
 
217
    }
 
218
 
 
219
    /**
 
220
     *  A subTask <write> tag was found.  Create the object, 
 
221
     *  Save it in our list, and return it.
 
222
     */
 
223
    public TelnetSubTask createWrite()
 
224
    {
 
225
        TelnetSubTask task = (TelnetSubTask)new TelnetWrite();
 
226
        telnetTasks.addElement(task);
 
227
        return task;
 
228
    }
 
229
 
 
230
    /**  
 
231
     *  This class is the parent of the Read and Write tasks.
 
232
     *  It handles the common attributes for both.
 
233
     */
 
234
    public class TelnetSubTask
 
235
    {
 
236
        protected String taskString= "";
 
237
        public void execute(AntTelnetClient telnet) 
 
238
                throws BuildException
 
239
        {
 
240
            throw new BuildException("Shouldn't be able instantiate a SubTask directly");
 
241
        }
 
242
        public void addText(String s) { setString(s);}
 
243
        public void setString(String s)
 
244
        {
 
245
           taskString += s; 
 
246
        }
 
247
    }
 
248
    /**
 
249
     *  This class sends text to the connected server 
 
250
     */
 
251
    public class TelnetWrite extends TelnetSubTask
 
252
    {
 
253
        private boolean echoString = true;
 
254
        public void execute(AntTelnetClient telnet) 
 
255
               throws BuildException
 
256
        {
 
257
           telnet.sendString(taskString, echoString);
 
258
        }
 
259
        
 
260
        public void setEcho(boolean b)
 
261
        {
 
262
           echoString = b;
 
263
        }
 
264
    }
 
265
    /**
 
266
     *  This class reads the output from the connected server
 
267
     *  until the required string is found. 
 
268
     */
 
269
    public class TelnetRead extends TelnetSubTask
 
270
    {
 
271
        private Integer timeout = null;
 
272
        public void execute(AntTelnetClient telnet) 
 
273
               throws BuildException
 
274
        {
 
275
            telnet.waitForString(taskString, timeout);
 
276
        }
 
277
        /**
 
278
         *  Override any default timeouts
 
279
         */
 
280
        public void setTimeout(Integer i)
 
281
        {
 
282
           this.timeout = i;
 
283
        }
 
284
        /**
 
285
         *  Sets the default timeout if none has been set already
 
286
         */
 
287
        public void setDefaultTimeout(Integer defaultTimeout)
 
288
        {
 
289
           if (timeout == null)
 
290
              timeout = defaultTimeout;
 
291
    }
 
292
    }
 
293
    /**
 
294
     *  This class handles the abstraction of the telnet protocol.
 
295
     *  Currently it is a wrapper around <a href="www.oroinc.com">ORO</a>'s 
 
296
     *  NetComponents
 
297
     */
 
298
    public class AntTelnetClient extends TelnetClient
 
299
    {
 
300
      /**
 
301
       * Read from the telnet session until the string we are 
 
302
       * waiting for is found 
 
303
       * @parm s The string to wait on 
 
304
       */
 
305
      public void waitForString(String s)
 
306
      {
 
307
           waitForString(s, null);
 
308
      }
 
309
 
 
310
      /**
 
311
       * Read from the telnet session until the string we are 
 
312
       * waiting for is found or the timeout has been reached
 
313
       * @parm s The string to wait on 
 
314
       * @parm timeout The maximum number of seconds to wait
 
315
       */
 
316
      public void waitForString(String s, Integer timeout)
 
317
      {
 
318
        InputStream is =this.getInputStream();
 
319
        try {
 
320
          StringBuffer sb = new StringBuffer();
 
321
          if (timeout == null || timeout.intValue() == 0)
 
322
          {
 
323
              while (sb.toString().indexOf(s) == -1)
 
324
                  {
 
325
                      sb.append((char) is.read());
 
326
                  }
 
327
          }
 
328
          else
 
329
          {
 
330
              Calendar endTime = Calendar.getInstance(); 
 
331
              endTime.add(Calendar.SECOND,timeout.intValue());
 
332
              while ( sb.toString().indexOf(s) == -1)
 
333
              {
 
334
                  while (Calendar.getInstance().before(endTime) &&
 
335
                         is.available() == 0) {
 
336
                      Thread.sleep(250);
 
337
                  }
 
338
                  if (is.available() == 0)
 
339
                      throw new BuildException("Response Timed-Out", getLocation());
 
340
                  sb.append((char) is.read());
 
341
              }
 
342
          }
 
343
          log(sb.toString(), Project.MSG_INFO);
 
344
        } catch (BuildException be)
 
345
        { 
 
346
            throw be;
 
347
        } catch (Exception e)
 
348
        { 
 
349
            throw new BuildException(e, getLocation());
 
350
        }
 
351
      }
 
352
 
 
353
    
 
354
      /**
 
355
       * Write this string to the telnet session.
 
356
       * @parm echoString  Logs string sent
 
357
       */
 
358
      public void sendString(String s, boolean echoString)
 
359
      {
 
360
        OutputStream os =this.getOutputStream();
 
361
        try {
 
362
          os.write((s + "\n").getBytes());
 
363
          if (echoString)
 
364
              log(s, Project.MSG_INFO);
 
365
          os.flush();
 
366
        } catch (Exception e)
 
367
        { 
 
368
          throw new BuildException(e, getLocation());
 
369
        }
 
370
      }
 
371
    }
 
372
}