ant-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Matthew Inger <ma...@sedonacorp.com>
Subject Re: running daemons
Date Fri, 03 Aug 2001 15:39:55 GMT
Paul S. Kilroy wrote:

> Lauren Commons wrote:
>
>> I've looked for the answer to this, but couldn't find
>> it.  If I missed it in the docs, please let me know...
>> I am building and testing (!) an asynchronous
>> messaging system (based on JMS).  For automated
>> testing, I'm looking for a way to start the server in
>> it's own process then run a test script and retrieve
>> the output for comparison to expected results.
>> The problem is how do I have Ant kick off the server
>> then move on to the next task (calling a junit test)?
>>
>> Thanks
>> Mr. Lauren Commons
>>
> I wrote a task to do this. It also (since I needed this also...) lets 
> you specify standard input to the java process. Its called JavaInput...
>
>
> It entends the Java task to add parameters "spawn" (boolean) this 
> spawns off the Java code in a seperate thread (daemon), "sleep" (long) 
> milliseconds to wait before returning (so the daemon has time to 
> start), and nested "<input line="blah">" tags for specifying standard 
> input.
>
> Paul
>
>
>
>------------------------------------------------------------------------
>
>package com.channelpoint.build.ant.publiclib;
>
>import org.apache.tools.ant.*;
>import org.apache.tools.ant.taskdefs.*;
>
>import java.io.*;
>
>/**
> * This is an enhanced Java task. It can run java programs in another thread,
> * pause while they startup, and give them input.
> *
> * Arguments include:
> * <li>
> *   <ul>spawn (true or false to create a separate thread for this execution)
> *   <ul>sleep (milliseconds to pause before returning)
> *   <ul>input (nested ex. <input line="blah"/>)
> * </li>
> */
>public class JavaInputTask extends Java {
>    private Input _input = null;
>    private boolean _spawn = false;
>    private long _sleep = 0;
>
>    public void setSpawn( boolean spawn ) {
>        _spawn = spawn;
>    }
>
>    public void setSleep( long sleep ) {
>        _sleep = sleep;
>    }
>
>    public Input createInput() {
>        if( _input == null ) {
>            _input = new Input();
>        }
>        return _input;
>    }
>
>    public void doit() throws BuildException {
>        super.execute();
>    }
>    public void execute() throws BuildException {
>        InputStream old = null;
>        if( _input != null ) {
>            old = System.in;
>            System.setIn( new ByteArrayInputStream( _input.getBytes() ) );
>        }
>
>        if( _spawn ) {
>            Spawn s = new Spawn( this );
>            s.start();
>            try {
>                Thread.currentThread().sleep( _sleep );
>            } catch ( InterruptedException e ) {
>                throw new BuildException( e );
>            }
>        } else {
>            doit();
>        }
>
>        if( old != null ) {
>            System.setIn( old );
>        }
>    }
>
>    public class Spawn extends Thread {
>        private JavaInputTask _ji;
>
>        public Spawn( JavaInputTask ji ) {
>            _ji = ji;
>        }
>
>        public void run() {
>            _ji.doit();
>        }
>    }
>
>    public class Input {
>        private StringBuffer _buf = new StringBuffer();
>
>        public void setLine( String line ) {
>            _buf.append( line + "\n" );
>        }
>
>        public byte[] getBytes() {
>            return _buf.toString().getBytes();
>        }
>    }
>}
>
> JavaInputTask.java
>
> Content-Type:
>
> text/plain
> Content-Encoding:
>
> 7bit
>
>
isn't that a little unsafe?  resetting System.in?

What if:
    1) The thread isn't finished with the input before you set System.in 
back
         to it's original value (say for instance if it processes one 
line of input
         at a time, and then does some io over a slow network, though 
this is
         a bad design in general, but could happen)?

    2) You have multiple threads running which use standard input (though
         unlikely, it could happen)?

The first one is really the one to worry about.  I would think
it's safer to just pass input like that in a file, or as command
line arguments.

Just my $.02


-- 
Matt Inger (matt.inger@sedonacorp.com)
Sedona Corporation
455 S. Gulph Road, Suite 300
King of Prussia, PA 19406
(484) 679-2213
"Self-respect - the secure feeling that no one,
 as yet, is suspicious." -H.L. Mencken 




Mime
View raw message