pmlopes     2004/10/25 08:34:44

  Modified:    modules/core/src/java/org/openejb/server/telnet Command.java
                        TextConsole.java
  Log:

  Stateful passivator fix and some inner class optimizations
  
  Revision  Changes    Path
  1.2       +377 -189  
openejb1/modules/core/src/java/org/openejb/server/telnet/Command.java
  
  Index: Command.java
  ===================================================================
  RCS file: 
/home/projects/openejb/scm/openejb1/modules/core/src/java/org/openejb/server/telnet/Command.java,v
  retrieving revision 1.1
  retrieving revision 1.2
  diff -u -r1.1 -r1.2
  --- Command.java      26 Mar 2004 21:42:54 -0000      1.1
  +++ Command.java      25 Oct 2004 12:34:44 -0000      1.2
  @@ -1,189 +1,377 @@
  -/**

  - * Redistribution and use of this software and associated documentation

  - * ("Software"), with or without modification, are permitted provided

  - * that the following conditions are met:

  - *

  - * 1. Redistributions of source code must retain copyright

  - *    statements and notices.  Redistributions must also contain a

  - *    copy of this document.

  - *

  - * 2. Redistributions in binary form must reproduce the

  - *    above copyright notice, this list of conditions and the

  - *    following disclaimer in the documentation and/or other

  - *    materials provided with the distribution.

  - *

  - * 3. The name "OpenEJB" must not be used to endorse or promote

  - *    products derived from this Software without prior written

  - *    permission of The OpenEJB Group.  For written permission,

  - *    please contact [EMAIL PROTECTED]

  - *

  - * 4. Products derived from this Software may not be called "OpenEJB"

  - *    nor may "OpenEJB" appear in their names without prior written

  - *    permission of The OpenEJB Group. OpenEJB is a registered

  - *    trademark of The OpenEJB Group.

  - *

  - * 5. Due credit should be given to the OpenEJB Project

  - *    (http://openejb.sf.net/).

  - *

  - * THIS SOFTWARE IS PROVIDED BY THE OPENEJB GROUP AND CONTRIBUTORS

  - * ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT

  - * NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND

  - * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL

  - * THE OPENEJB GROUP OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT,

  - * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES

  - * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR

  - * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)

  - * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,

  - * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)

  - * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED

  - * OF THE POSSIBILITY OF SUCH DAMAGE.

  - *

  - * Copyright 2001 (C) The OpenEJB Group. All Rights Reserved.

  - *

  - * $Id$

  - */

  -package org.openejb.server.telnet;

  -

  -import java.io.DataInputStream;

  -import java.io.IOException;

  -import java.io.PrintStream;

  -import java.util.ArrayList;

  -import java.util.HashMap;

  -import java.util.Iterator;

  -import java.util.StringTokenizer;

  -

  -/**

  - * @author <a href="mailto:[EMAIL PROTECTED]">David Blevins</a>

  - */

  -public class Command {

  -

  -

  -    protected static HashMap commands = new HashMap();

  -

  -    static

  -    {

  -        loadCommandList();

  -    }

  -

  -    protected static Command unknownCommand = new Command();

  -

  -    protected static void register( String name, Command cmd ) {

  -        commands.put( name, cmd );

  -    }

  -

  -    protected static void register( String name, Class cmd ) {

  -        commands.put( name, cmd );

  -    }

  -

  -    public static Command getCommand( String name ) {

  -        Object cmd = commands.get( name );

  -

  -        if ( cmd instanceof Class ) {

  -            cmd = loadCommand( ( Class ) cmd );

  -            register( name, ( Command ) cmd );

  -        }

  -

  -        return( Command ) cmd;

  -    }

  -

  -    // - Public methods - //

  -

  -    public void exec( Arguments args, DataInputStream in, PrintStream out ) throws 
IOException

  -    {

  -        out.println( "not implemented" );

  -    }

  -

  -

  -    // - Protected methods - //

  -    protected static Command loadCommand( Class commandClass ) {

  -        Command cmd = null;

  -        try {

  -            cmd = ( Command ) commandClass.newInstance();

  -        } catch ( Exception e ) {

  -            //throw new IOException("Cannot instantiate command class 
"+commandClass+"\n"+e.getClass().getName()+":\n"+e.getMessage());

  -        }

  -

  -        return cmd;

  -    }

  -

  -

  -    /*

  -    TODO:

  -    - Create the basic list in ant

  -    - Add the regexp package to the ant scripts

  -    - update the loadCommandList to read the list

  -      made in the ant script

  -

  -    */

  -    protected static void loadCommandList() {

  -        Exit.register();

  -        Help.register();

  -        Lookup.register();

  -        Ls.register();

  -        Stop.register();

  -        Version.register();

  -        GroovySh.register();

  -    }

  -

  -    public static class Arguments {

  -        // holds the whole string representing what's been typed by a user after a 
command name

  -        private String args;

  -

  -        private String[] argsArray = new String[0];

  -

  -        private boolean alreadyParsed = false;

  -

  -        Arguments( String args ) {

  -            this.args = args;

  -        }

  -

  -        String get() {

  -            return args;

  -        }

  -

  -        /**

  -         * @param i

  -         * @return i-th argument

  -         */

  -        String get( int i ) {

  -            parseArgs();

  -            return( argsArray != null ? argsArray[i] : null );

  -        }

  -

  -        int count() {

  -            parseArgs();

  -            return( argsArray != null ? argsArray.length : 0 );

  -        }

  -

  -        Iterator iterator() {

  -            return new Iterator() {

  -                StringTokenizer st = new StringTokenizer( args );

  -

  -                public boolean hasNext() {

  -                    return st.hasMoreTokens();

  -                }

  -

  -                public Object next() {

  -                    return st.nextToken();

  -                }

  -

  -                public void remove() {

  -                    // not supported

  -                }

  -            };

  -        }

  -

  -        private void parseArgs() {

  -            if ( !alreadyParsed ) {

  -                ArrayList arrayList = new ArrayList();

  -                Iterator it = iterator();

  -                while ( it.hasNext() ) {

  -                    arrayList.add( it.next() );

  -                }

  -                argsArray = ( String[] ) arrayList.toArray( argsArray );

  -                alreadyParsed = true;

  -            }

  -        }

  -    }

  -}

  -

  +/**
  +
  + * Redistribution and use of this software and associated documentation
  +
  + * ("Software"), with or without modification, are permitted provided
  +
  + * that the following conditions are met:
  +
  + *
  +
  + * 1. Redistributions of source code must retain copyright
  +
  + *    statements and notices.  Redistributions must also contain a
  +
  + *    copy of this document.
  +
  + *
  +
  + * 2. Redistributions in binary form must reproduce the
  +
  + *    above copyright notice, this list of conditions and the
  +
  + *    following disclaimer in the documentation and/or other
  +
  + *    materials provided with the distribution.
  +
  + *
  +
  + * 3. The name "OpenEJB" must not be used to endorse or promote
  +
  + *    products derived from this Software without prior written
  +
  + *    permission of The OpenEJB Group.  For written permission,
  +
  + *    please contact [EMAIL PROTECTED]
  +
  + *
  +
  + * 4. Products derived from this Software may not be called "OpenEJB"
  +
  + *    nor may "OpenEJB" appear in their names without prior written
  +
  + *    permission of The OpenEJB Group. OpenEJB is a registered
  +
  + *    trademark of The OpenEJB Group.
  +
  + *
  +
  + * 5. Due credit should be given to the OpenEJB Project
  +
  + *    (http://openejb.sf.net/).
  +
  + *
  +
  + * THIS SOFTWARE IS PROVIDED BY THE OPENEJB GROUP AND CONTRIBUTORS
  +
  + * ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT
  +
  + * NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
  +
  + * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL
  +
  + * THE OPENEJB GROUP OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
  +
  + * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  +
  + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  +
  + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  +
  + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
  +
  + * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  +
  + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
  +
  + * OF THE POSSIBILITY OF SUCH DAMAGE.
  +
  + *
  +
  + * Copyright 2001 (C) The OpenEJB Group. All Rights Reserved.
  +
  + *
  +
  + * $Id$
  +
  + */
  +
  +package org.openejb.server.telnet;
  +
  +
  +
  +import java.io.DataInputStream;
  +
  +import java.io.IOException;
  +
  +import java.io.PrintStream;
  +
  +import java.util.ArrayList;
  +
  +import java.util.HashMap;
  +
  +import java.util.Iterator;
  +
  +import java.util.StringTokenizer;
  +
  +
  +
  +/**
  +
  + * @author <a href="mailto:[EMAIL PROTECTED]">David Blevins</a>
  +
  + */
  +
  +public class Command {
  +
  +
  +
  +
  +
  +    protected static final HashMap commands = new HashMap();
  +
  +
  +
  +    static
  +
  +    {
  +
  +        loadCommandList();
  +
  +    }
  +
  +
  +
  +    protected static final Command unknownCommand = new Command();
  +
  +
  +
  +    protected static void register( String name, Command cmd ) {
  +
  +        commands.put( name, cmd );
  +
  +    }
  +
  +
  +
  +    protected static void register( String name, Class cmd ) {
  +
  +        commands.put( name, cmd );
  +
  +    }
  +
  +
  +
  +    public static Command getCommand( String name ) {
  +
  +        Object cmd = commands.get( name );
  +
  +
  +
  +        if ( cmd instanceof Class ) {
  +
  +            cmd = loadCommand( ( Class ) cmd );
  +
  +            register( name, ( Command ) cmd );
  +
  +        }
  +
  +
  +
  +        return( Command ) cmd;
  +
  +    }
  +
  +
  +
  +    // - Public methods - //
  +
  +
  +
  +    public void exec( Arguments args, DataInputStream in, PrintStream out ) throws 
IOException
  +
  +    {
  +
  +        out.println( "not implemented" );
  +
  +    }
  +
  +
  +
  +
  +
  +    // - Protected methods - //
  +
  +    protected static Command loadCommand( Class commandClass ) {
  +
  +        Command cmd = null;
  +
  +        try {
  +
  +            cmd = ( Command ) commandClass.newInstance();
  +
  +        } catch ( Exception e ) {
  +
  +            //throw new IOException("Cannot instantiate command class 
"+commandClass+"\n"+e.getClass().getName()+":\n"+e.getMessage());
  +
  +        }
  +
  +
  +
  +        return cmd;
  +
  +    }
  +
  +
  +
  +
  +
  +    /*
  +
  +    TODO:
  +
  +    - Create the basic list in ant
  +
  +    - Add the regexp package to the ant scripts
  +
  +    - update the loadCommandList to read the list
  +
  +      made in the ant script
  +
  +
  +
  +    */
  +
  +    protected static void loadCommandList() {
  +
  +        Exit.register();
  +
  +        Help.register();
  +
  +        Lookup.register();
  +
  +        Ls.register();
  +
  +        Stop.register();
  +
  +        Version.register();
  +
  +        GroovySh.register();
  +
  +    }
  +
  +
  +
  +    public static class Arguments {
  +
  +        // holds the whole string representing what's been typed by a user after a 
command name
  +
  +        private String args;
  +
  +
  +
  +        private String[] argsArray = new String[0];
  +
  +
  +
  +        private boolean alreadyParsed = false;
  +
  +
  +
  +        Arguments( String args ) {
  +
  +            this.args = args;
  +
  +        }
  +
  +
  +
  +        String get() {
  +
  +            return args;
  +
  +        }
  +
  +
  +
  +        /**
  +
  +         * @param i
  +
  +         * @return i-th argument
  +
  +         */
  +
  +        String get( int i ) {
  +
  +            parseArgs();
  +
  +            return( argsArray != null ? argsArray[i] : null );
  +
  +        }
  +
  +
  +
  +        int count() {
  +
  +            parseArgs();
  +
  +            return( argsArray != null ? argsArray.length : 0 );
  +
  +        }
  +
  +
  +
  +        Iterator iterator() {
  +
  +            return new Iterator() {
  +
  +                StringTokenizer st = new StringTokenizer( args );
  +
  +
  +
  +                public boolean hasNext() {
  +
  +                    return st.hasMoreTokens();
  +
  +                }
  +
  +
  +
  +                public Object next() {
  +
  +                    return st.nextToken();
  +
  +                }
  +
  +
  +
  +                public void remove() {
  +
  +                    // not supported
  +
  +                }
  +
  +            };
  +
  +        }
  +
  +
  +
  +        private void parseArgs() {
  +
  +            if ( !alreadyParsed ) {
  +
  +                ArrayList arrayList = new ArrayList();
  +
  +                Iterator it = iterator();
  +
  +                while ( it.hasNext() ) {
  +
  +                    arrayList.add( it.next() );
  +
  +                }
  +
  +                argsArray = ( String[] ) arrayList.toArray( argsArray );
  +
  +                alreadyParsed = true;
  +
  +            }
  +
  +        }
  +
  +    }
  +
  +}
  +
  +
  
  
  
  1.2       +325 -163  
openejb1/modules/core/src/java/org/openejb/server/telnet/TextConsole.java
  
  Index: TextConsole.java
  ===================================================================
  RCS file: 
/home/projects/openejb/scm/openejb1/modules/core/src/java/org/openejb/server/telnet/TextConsole.java,v
  retrieving revision 1.1
  retrieving revision 1.2
  diff -u -r1.1 -r1.2
  --- TextConsole.java  26 Mar 2004 21:42:56 -0000      1.1
  +++ TextConsole.java  25 Oct 2004 12:34:44 -0000      1.2
  @@ -1,163 +1,325 @@
  -/**

  - * Redistribution and use of this software and associated documentation

  - * ("Software"), with or without modification, are permitted provided

  - * that the following conditions are met:

  - *

  - * 1. Redistributions of source code must retain copyright

  - *    statements and notices.  Redistributions must also contain a

  - *    copy of this document.

  - *

  - * 2. Redistributions in binary form must reproduce the

  - *    above copyright notice, this list of conditions and the

  - *    following disclaimer in the documentation and/or other

  - *    materials provided with the distribution.

  - *

  - * 3. The name "OpenEJB" must not be used to endorse or promote

  - *    products derived from this Software without prior written

  - *    permission of The OpenEJB Group.  For written permission,

  - *    please contact [EMAIL PROTECTED]

  - *

  - * 4. Products derived from this Software may not be called "OpenEJB"

  - *    nor may "OpenEJB" appear in their names without prior written

  - *    permission of The OpenEJB Group. OpenEJB is a registered

  - *    trademark of The OpenEJB Group.

  - *

  - * 5. Due credit should be given to the OpenEJB Project

  - *    (http://openejb.sf.net/).

  - *

  - * THIS SOFTWARE IS PROVIDED BY THE OPENEJB GROUP AND CONTRIBUTORS

  - * ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT

  - * NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND

  - * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL

  - * THE OPENEJB GROUP OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT,

  - * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES

  - * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR

  - * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)

  - * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,

  - * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)

  - * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED

  - * OF THE POSSIBILITY OF SUCH DAMAGE.

  - *

  - * Copyright 2001 (C) The OpenEJB Group. All Rights Reserved.

  - *

  - * $Id$

  - */

  -package org.openejb.server.telnet;

  -

  -import java.io.DataInputStream;

  -import java.io.IOException;

  -import java.io.InputStream;

  -import java.io.PrintStream;

  -import java.util.Properties;

  -import java.util.StringTokenizer;

  -import java.util.Vector;

  -

  -import org.openejb.util.Logger;

  -

  -/**

  - * @author <a href="mailto:[EMAIL PROTECTED]">David Blevins</a>

  - */

  -public class TextConsole  {

  -

  -    Logger logger = Logger.getInstance( "OpenEJB.admin", 
"org.openejb.server.util.resources" );

  -

  -    Properties props;

  -    

  -    public TextConsole() {

  -    }

  -

  -    public void init( Properties props ) throws Exception {

  -        this.props = props;

  -    }

  -

  -    boolean stop = false;

  -

  -    DataInputStream in = null;

  -    PrintStream out = null;

  -

  -    public static final char ESC = ( char ) 27;

  -

  -    public static final String TTY_Reset      = ESC + "[0m";

  -    public static final String TTY_Bright     = ESC + "[1m";

  -    public static final String TTY_Dim        = ESC + "[2m";

  -    public static final String TTY_Underscore = ESC + "[4m";

  -    public static final String TTY_Blink      = ESC + "[5m";

  -    public static final String TTY_Reverse    = ESC + "[7m";

  -    public static final String TTY_Hidden     = ESC + "[8m";

  -

  -    /* Foreground Colors */

  -    public static final String TTY_FG_Black   = ESC + "[30m";

  -    public static final String TTY_FG_Red     = ESC + "[31m";

  -    public static final String TTY_FG_Green   = ESC + "[32m";

  -    public static final String TTY_FG_Yellow  = ESC + "[33m";

  -    public static final String TTY_FG_Blue    = ESC + "[34m";

  -    public static final String TTY_FG_Magenta = ESC + "[35m";

  -    public static final String TTY_FG_Cyan    = ESC + "[36m";

  -    public static final String TTY_FG_White   = ESC + "[37m";

  -

  -    /* Background Colors */

  -    public static final String TTY_BG_Black   = ESC + "[40m";

  -    public static final String TTY_BG_Red     = ESC + "[41m";

  -    public static final String TTY_BG_Green   = ESC + "[42m";

  -    public static final String TTY_BG_Yellow  = ESC + "[43m";

  -    public static final String TTY_BG_Blue    = ESC + "[44m";

  -    public static final String TTY_BG_Magenta = ESC + "[45m";

  -    public static final String TTY_BG_Cyan    = ESC + "[46m";

  -    public static final String TTY_BG_White   = ESC + "[47m";

  -

  -    public static String PROMPT = TTY_Reset + TTY_Bright + "[openejb]$ " + 
TTY_Reset;

  -

  -    protected void exec( InputStream input, PrintStream out ) {

  -        DataInputStream in = new DataInputStream(input);

  -        while (!stop) {

  -            prompt(in,out);

  -        }

  -        

  -    }

  -        

  -    protected void prompt( DataInputStream in, PrintStream out ) {

  -        

  -        try {

  -            out.print( PROMPT );

  -            out.flush();

  -

  -            String commandline = in.readLine();

  -            logger.debug( "command: " + commandline );

  -            commandline = commandline.trim();

  -

  -            if ( commandline.length() < 1 ) return;

  -

  -            String command = commandline;

  -            Command.Arguments args = null;

  -

  -            int spacePosition = commandline.indexOf( ' ' );

  -            int tabPosition = commandline.indexOf( '\t' );

  -            if ( spacePosition != -1 || tabPosition != -1 ) {

  -                int cutPosition = ( spacePosition > tabPosition ? spacePosition : 
tabPosition );

  -                command = commandline.substring( 0, cutPosition );

  -                args = new Command.Arguments( commandline.substring( cutPosition + 
1 ) );

  -            }

  -

  -            Command cmd = Command.getCommand( command );

  -

  -            if ( cmd == null ) {

  -                out.print( command );

  -                out.println( ": command not found" );

  -            } else {

  -                cmd.exec( args, in, out );

  -            }

  -        } catch ( UnsupportedOperationException e ) {

  -            this.stop = true;

  -        } catch ( Throwable e ) {

  -            e.printStackTrace( new PrintStream( out ) );

  -            //e.printStackTrace( );

  -            this.stop = true;

  -        }

  -    }

  -

  -    protected void badCommand( DataInputStream in, PrintStream out ) throws 
IOException

  -    {

  -        //asdf: command not found

  -    }

  -}

  -

  +/**
  +
  + * Redistribution and use of this software and associated documentation
  +
  + * ("Software"), with or without modification, are permitted provided
  +
  + * that the following conditions are met:
  +
  + *
  +
  + * 1. Redistributions of source code must retain copyright
  +
  + *    statements and notices.  Redistributions must also contain a
  +
  + *    copy of this document.
  +
  + *
  +
  + * 2. Redistributions in binary form must reproduce the
  +
  + *    above copyright notice, this list of conditions and the
  +
  + *    following disclaimer in the documentation and/or other
  +
  + *    materials provided with the distribution.
  +
  + *
  +
  + * 3. The name "OpenEJB" must not be used to endorse or promote
  +
  + *    products derived from this Software without prior written
  +
  + *    permission of The OpenEJB Group.  For written permission,
  +
  + *    please contact [EMAIL PROTECTED]
  +
  + *
  +
  + * 4. Products derived from this Software may not be called "OpenEJB"
  +
  + *    nor may "OpenEJB" appear in their names without prior written
  +
  + *    permission of The OpenEJB Group. OpenEJB is a registered
  +
  + *    trademark of The OpenEJB Group.
  +
  + *
  +
  + * 5. Due credit should be given to the OpenEJB Project
  +
  + *    (http://openejb.sf.net/).
  +
  + *
  +
  + * THIS SOFTWARE IS PROVIDED BY THE OPENEJB GROUP AND CONTRIBUTORS
  +
  + * ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT
  +
  + * NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
  +
  + * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL
  +
  + * THE OPENEJB GROUP OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
  +
  + * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  +
  + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  +
  + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  +
  + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
  +
  + * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  +
  + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
  +
  + * OF THE POSSIBILITY OF SUCH DAMAGE.
  +
  + *
  +
  + * Copyright 2001 (C) The OpenEJB Group. All Rights Reserved.
  +
  + *
  +
  + * $Id$
  +
  + */
  +
  +package org.openejb.server.telnet;
  +
  +
  +
  +import java.io.DataInputStream;
  +
  +import java.io.IOException;
  +
  +import java.io.InputStream;
  +
  +import java.io.PrintStream;
  +
  +import java.util.Properties;
  +
  +import java.util.StringTokenizer;
  +
  +import java.util.Vector;
  +
  +
  +
  +import org.openejb.util.Logger;
  +
  +
  +
  +/**
  +
  + * @author <a href="mailto:[EMAIL PROTECTED]">David Blevins</a>
  +
  + */
  +
  +public class TextConsole  {
  +
  +
  +
  +    Logger logger = Logger.getInstance( "OpenEJB.admin", 
"org.openejb.server.util.resources" );
  +
  +
  +
  +    Properties props;
  +
  +
  +
  +    public TextConsole() {
  +
  +    }
  +
  +
  +
  +    public void init( Properties props ) throws Exception {
  +
  +        this.props = props;
  +
  +    }
  +
  +
  +
  +    boolean stop = false;
  +
  +
  +
  +    DataInputStream in = null;
  +
  +    PrintStream out = null;
  +
  +
  +
  +    public static final char ESC = ( char ) 27;
  +
  +
  +
  +    public static final String TTY_Reset      = ESC + "[0m";
  +
  +    public static final String TTY_Bright     = ESC + "[1m";
  +
  +    public static final String TTY_Dim        = ESC + "[2m";
  +
  +    public static final String TTY_Underscore = ESC + "[4m";
  +
  +    public static final String TTY_Blink      = ESC + "[5m";
  +
  +    public static final String TTY_Reverse    = ESC + "[7m";
  +
  +    public static final String TTY_Hidden     = ESC + "[8m";
  +
  +
  +
  +    /* Foreground Colors */
  +
  +    public static final String TTY_FG_Black   = ESC + "[30m";
  +
  +    public static final String TTY_FG_Red     = ESC + "[31m";
  +
  +    public static final String TTY_FG_Green   = ESC + "[32m";
  +
  +    public static final String TTY_FG_Yellow  = ESC + "[33m";
  +
  +    public static final String TTY_FG_Blue    = ESC + "[34m";
  +
  +    public static final String TTY_FG_Magenta = ESC + "[35m";
  +
  +    public static final String TTY_FG_Cyan    = ESC + "[36m";
  +
  +    public static final String TTY_FG_White   = ESC + "[37m";
  +
  +
  +
  +    /* Background Colors */
  +
  +    public static final String TTY_BG_Black   = ESC + "[40m";
  +
  +    public static final String TTY_BG_Red     = ESC + "[41m";
  +
  +    public static final String TTY_BG_Green   = ESC + "[42m";
  +
  +    public static final String TTY_BG_Yellow  = ESC + "[43m";
  +
  +    public static final String TTY_BG_Blue    = ESC + "[44m";
  +
  +    public static final String TTY_BG_Magenta = ESC + "[45m";
  +
  +    public static final String TTY_BG_Cyan    = ESC + "[46m";
  +
  +    public static final String TTY_BG_White   = ESC + "[47m";
  +
  +
  +
  +    static String PROMPT = TTY_Reset + TTY_Bright + "[openejb]$ " + TTY_Reset;
  +
  +
  +
  +    protected void exec( InputStream input, PrintStream out ) {
  +
  +        DataInputStream in = new DataInputStream(input);
  +
  +        while (!stop) {
  +
  +            prompt(in,out);
  +
  +        }
  +
  +
  +
  +    }
  +
  +
  +
  +    protected void prompt( DataInputStream in, PrintStream out ) {
  +
  +
  +
  +        try {
  +
  +            out.print( PROMPT );
  +
  +            out.flush();
  +
  +
  +
  +            String commandline = in.readLine();
  +
  +            logger.debug( "command: " + commandline );
  +
  +            commandline = commandline.trim();
  +
  +
  +
  +            if ( commandline.length() < 1 ) return;
  +
  +
  +
  +            String command = commandline;
  +
  +            Command.Arguments args = null;
  +
  +
  +
  +            int spacePosition = commandline.indexOf( ' ' );
  +
  +            int tabPosition = commandline.indexOf( '\t' );
  +
  +            if ( spacePosition != -1 || tabPosition != -1 ) {
  +
  +                int cutPosition = ( spacePosition > tabPosition ? spacePosition : 
tabPosition );
  +
  +                command = commandline.substring( 0, cutPosition );
  +
  +                args = new Command.Arguments( commandline.substring( cutPosition + 
1 ) );
  +
  +            }
  +
  +
  +
  +            Command cmd = Command.getCommand( command );
  +
  +
  +
  +            if ( cmd == null ) {
  +
  +                out.print( command );
  +
  +                out.println( ": command not found" );
  +
  +            } else {
  +
  +                cmd.exec( args, in, out );
  +
  +            }
  +
  +        } catch ( UnsupportedOperationException e ) {
  +
  +            this.stop = true;
  +
  +        } catch ( Throwable e ) {
  +
  +            e.printStackTrace( new PrintStream( out ) );
  +
  +            //e.printStackTrace( );
  +
  +            this.stop = true;
  +
  +        }
  +
  +    }
  +
  +
  +
  +    protected void badCommand( DataInputStream in, PrintStream out ) throws 
IOException
  +
  +    {
  +
  +        //asdf: command not found
  +
  +    }
  +
  +}
  +
  +
  
  
  

Reply via email to