Author: niclas
Date: Sat Aug  7 06:38:54 2004
New Revision: 36064

Added:
   
avalon/trunk/planet/facilities/console/api/src/main/org/apache/avalon/facilities/console/CommandInterpreter.java
   (contents, props changed)
   
avalon/trunk/planet/facilities/console/commands/src/main/org/apache/avalon/facilities/console/commands/ShowModelCmd.java
      - copied, changed from rev 36060, 
avalon/trunk/planet/facilities/console/commands/src/main/org/apache/avalon/facilities/console/commands/ViewModelCmd.java
   
avalon/trunk/planet/facilities/console/impl/src/main/org/apache/avalon/facilities/console/impl/CommandInterpreterImpl.java
   (contents, props changed)
Removed:
   
avalon/trunk/planet/facilities/console/commands/src/main/org/apache/avalon/facilities/console/commands/ViewModelCmd.java
Modified:
   
avalon/trunk/planet/facilities/console/api/src/main/org/apache/avalon/facilities/console/ConsoleCommand.java
   avalon/trunk/planet/facilities/console/blocks/default/build.xml
   
avalon/trunk/planet/facilities/console/commands/src/main/org/apache/avalon/facilities/console/commands/DummyCmd.java
   
avalon/trunk/planet/facilities/console/commands/src/main/org/apache/avalon/facilities/console/commands/EchoCmd.java
   
avalon/trunk/planet/facilities/console/commands/src/main/org/apache/avalon/facilities/console/commands/ExitCmd.java
   
avalon/trunk/planet/facilities/console/commands/src/main/org/apache/avalon/facilities/console/commands/HelpCmd.java
   
avalon/trunk/planet/facilities/console/commands/src/main/org/apache/avalon/facilities/console/commands/ThreadCmd.java
   
avalon/trunk/planet/facilities/console/impl/src/main/org/apache/avalon/facilities/console/impl/ConsoleImpl.java
   
avalon/trunk/planet/facilities/console/impl/src/main/org/apache/avalon/facilities/console/impl/LoginCmd.java
Log:
Added support for context navigation in the model and multi-word commands. Also 
commands are now defined by their name in the block.

Added: 
avalon/trunk/planet/facilities/console/api/src/main/org/apache/avalon/facilities/console/CommandInterpreter.java
==============================================================================
--- (empty file)
+++ 
avalon/trunk/planet/facilities/console/api/src/main/org/apache/avalon/facilities/console/CommandInterpreter.java
    Sat Aug  7 06:38:54 2004
@@ -0,0 +1,30 @@
+/*
+ * Copyright 1997-2004 Apache Software Foundation
+ * 
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ * 
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ * 
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.avalon.facilities.console;
+
+import org.apache.avalon.composition.model.ContainmentModel;
+
+public interface CommandInterpreter
+{
+    void addCommand( ConsoleCommand cmd );
+    void removeCommand( ConsoleCommand cmd );
+    void removeCommand( String commandname );
+    
+    ContainmentModel getCurrentContainer();
+    
+    void setCurrentNode( ContainmentModel node );
+}
+

Modified: 
avalon/trunk/planet/facilities/console/api/src/main/org/apache/avalon/facilities/console/ConsoleCommand.java
==============================================================================
--- 
avalon/trunk/planet/facilities/console/api/src/main/org/apache/avalon/facilities/console/ConsoleCommand.java
        (original)
+++ 
avalon/trunk/planet/facilities/console/api/src/main/org/apache/avalon/facilities/console/ConsoleCommand.java
        Sat Aug  7 06:38:54 2004
@@ -34,6 +34,8 @@
      */
     String getDescription();
     
-    void execute( BufferedReader input, BufferedWriter output, String[] arguments )
+    void execute( CommandInterpreter intp, 
+                  BufferedReader input, BufferedWriter output, 
+                  String[] arguments )
         throws Exception;
 }

Modified: avalon/trunk/planet/facilities/console/blocks/default/build.xml
==============================================================================
--- avalon/trunk/planet/facilities/console/blocks/default/build.xml     (original)
+++ avalon/trunk/planet/facilities/console/blocks/default/build.xml     Sat Aug  7 
06:38:54 2004
@@ -41,8 +41,8 @@
       <x:component name="help" 
                    class="org.apache.avalon.facilities.console.commands.HelpCmd"/>
                    
-      <x:component name="viewmodel" 
-                   
class="org.apache.avalon.facilities.console.commands.ViewModelCmd"/>
+      <x:component name="show model" 
+                   
class="org.apache.avalon.facilities.console.commands.ShowModelCmd"/>
     </x:block>
   </target>
 

Modified: 
avalon/trunk/planet/facilities/console/commands/src/main/org/apache/avalon/facilities/console/commands/DummyCmd.java
==============================================================================
--- 
avalon/trunk/planet/facilities/console/commands/src/main/org/apache/avalon/facilities/console/commands/DummyCmd.java
        (original)
+++ 
avalon/trunk/planet/facilities/console/commands/src/main/org/apache/avalon/facilities/console/commands/DummyCmd.java
        Sat Aug  7 06:38:54 2004
@@ -18,9 +18,14 @@
 import java.io.BufferedReader;
 import java.io.BufferedWriter;
 
+import org.apache.avalon.facilities.console.CommandInterpreter;
 import org.apache.avalon.facilities.console.Console;
 import org.apache.avalon.facilities.console.ConsoleCommand;
 
+import org.apache.avalon.framework.context.Context;
+import org.apache.avalon.framework.context.ContextException;
+import org.apache.avalon.framework.context.Contextualizable;
+
 import org.apache.avalon.framework.service.Serviceable;
 import org.apache.avalon.framework.service.ServiceException;
 import org.apache.avalon.framework.service.ServiceManager;
@@ -30,20 +35,42 @@
  * @avalon.service type="org.apache.avalon.facilities.console.ConsoleCommand"
  */
 public class DummyCmd
-    implements ConsoleCommand, Serviceable
+    implements ConsoleCommand, Serviceable, Contextualizable
 {
+    private String m_Name;
+    
+    
     public String getName()
     {
-        return "dummy";
+        return m_Name;
     }
     
     public String getDescription()
     {
-        String str = "usage: dummy\n\nDoes absolutely nothing.";
+        String str = "usage: " + m_Name + "\n\nDoes absolutely nothing.";
         return str;
     }
     
     /**
+     * Contextulaization of the listener by the container during 
+     * which we are supplied with the root composition model for 
+     * the application.
+     *
+     * @param ctx the supplied listener context
+     *
+     * @exception ContextException if a contextualization error occurs
+     *
+     * @avalon.entry key="urn:avalon:name" 
+     *               type="java.lang.String" 
+     */
+    public void contextualize( Context ctx ) 
+        throws ContextException
+    {
+        m_Name = (String) ctx.get( "urn:avalon:name" );
+    }
+
+    
+    /**
      * @avalon.dependency type="org.apache.avalon.facilities.console.Console"
      *                    key="console"
      */
@@ -54,7 +81,7 @@
         console.addCommand( this );
     }
     
-    public void execute( BufferedReader input, BufferedWriter output, String[] 
arguments )
+    public void execute( CommandInterpreter intp, BufferedReader input, 
BufferedWriter output, String[] arguments )
         throws Exception
     {
         output.newLine();

Modified: 
avalon/trunk/planet/facilities/console/commands/src/main/org/apache/avalon/facilities/console/commands/EchoCmd.java
==============================================================================
--- 
avalon/trunk/planet/facilities/console/commands/src/main/org/apache/avalon/facilities/console/commands/EchoCmd.java
 (original)
+++ 
avalon/trunk/planet/facilities/console/commands/src/main/org/apache/avalon/facilities/console/commands/EchoCmd.java
 Sat Aug  7 06:38:54 2004
@@ -18,9 +18,14 @@
 import java.io.BufferedReader;
 import java.io.BufferedWriter;
 
+import org.apache.avalon.facilities.console.CommandInterpreter;
 import org.apache.avalon.facilities.console.Console;
 import org.apache.avalon.facilities.console.ConsoleCommand;
 
+import org.apache.avalon.framework.context.Context;
+import org.apache.avalon.framework.context.ContextException;
+import org.apache.avalon.framework.context.Contextualizable;
+
 import org.apache.avalon.framework.service.Serviceable;
 import org.apache.avalon.framework.service.ServiceException;
 import org.apache.avalon.framework.service.ServiceManager;
@@ -30,20 +35,40 @@
  * @avalon.service type="org.apache.avalon.facilities.console.ConsoleCommand"
  */
 public class EchoCmd
-    implements ConsoleCommand, Serviceable
+    implements ConsoleCommand, Serviceable, Contextualizable
 {
+    private String m_Name;
+    
     public String getName()
     {
-        return "echo";
+        return m_Name;
     }
     
     public String getDescription()
     {
-        String str = "usage: echo (string)*\n\nSends each of the string arguments 
back separate by a space.";
+        String str = "usage: " + m_Name + " (string)*\n\nSends each of the string 
arguments back separate by a space.";
         return str;
     }
     
     /**
+     * Contextulaization of the listener by the container during 
+     * which we are supplied with the root composition model for 
+     * the application.
+     *
+     * @param ctx the supplied listener context
+     *
+     * @exception ContextException if a contextualization error occurs
+     *
+     * @avalon.entry key="urn:avalon:name" 
+     *               type="java.lang.String" 
+     */
+    public void contextualize( Context ctx ) 
+        throws ContextException
+    {
+        m_Name = (String) ctx.get( "urn:avalon:name" );
+    }
+
+    /**
      * @avalon.dependency type="org.apache.avalon.facilities.console.Console"
      *                    key="console"
      */
@@ -54,7 +79,7 @@
         console.addCommand( this );
     }
     
-    public void execute( BufferedReader input, BufferedWriter output, String[] 
arguments )
+    public void execute( CommandInterpreter intp, BufferedReader input, 
BufferedWriter output, String[] arguments )
         throws Exception
     {
         output.newLine();

Modified: 
avalon/trunk/planet/facilities/console/commands/src/main/org/apache/avalon/facilities/console/commands/ExitCmd.java
==============================================================================
--- 
avalon/trunk/planet/facilities/console/commands/src/main/org/apache/avalon/facilities/console/commands/ExitCmd.java
 (original)
+++ 
avalon/trunk/planet/facilities/console/commands/src/main/org/apache/avalon/facilities/console/commands/ExitCmd.java
 Sat Aug  7 06:38:54 2004
@@ -18,9 +18,14 @@
 import java.io.BufferedReader;
 import java.io.BufferedWriter;
 
+import org.apache.avalon.facilities.console.CommandInterpreter;
 import org.apache.avalon.facilities.console.Console;
 import org.apache.avalon.facilities.console.ConsoleCommand;
 
+import org.apache.avalon.framework.context.Context;
+import org.apache.avalon.framework.context.ContextException;
+import org.apache.avalon.framework.context.Contextualizable;
+
 import org.apache.avalon.framework.service.Serviceable;
 import org.apache.avalon.framework.service.ServiceException;
 import org.apache.avalon.framework.service.ServiceManager;
@@ -30,20 +35,40 @@
  * @avalon.service type="org.apache.avalon.facilities.console.ConsoleCommand"
  */
 public class ExitCmd
-    implements ConsoleCommand, Serviceable
+    implements ConsoleCommand, Serviceable, Contextualizable
 {
+    private String m_Name;
+    
     public String getName()
     {
-        return "exit";
+        return m_Name;
     }
     
     public String getDescription()
     {
-        String str = "usage: exit\n\nTerminates the session.";
+        String str = "usage: " + m_Name + "\n\nTerminates the session.";
         return str;
     }
     
     /**
+     * Contextulaization of the listener by the container during 
+     * which we are supplied with the root composition model for 
+     * the application.
+     *
+     * @param ctx the supplied listener context
+     *
+     * @exception ContextException if a contextualization error occurs
+     *
+     * @avalon.entry key="urn:avalon:name" 
+     *               type="java.lang.String" 
+     */
+    public void contextualize( Context ctx ) 
+        throws ContextException
+    {
+        m_Name = (String) ctx.get( "urn:avalon:name" );
+    }
+
+    /**
      * @avalon.dependency type="org.apache.avalon.facilities.console.Console"
      *                    key="console"
      */
@@ -54,7 +79,7 @@
         console.addCommand( this );
     }
     
-    public void execute( BufferedReader input, BufferedWriter output, String[] 
arguments )
+    public void execute( CommandInterpreter intp, BufferedReader input, 
BufferedWriter output, String[] arguments )
         throws Exception
     {
         output.write( "Logging out..." );

Modified: 
avalon/trunk/planet/facilities/console/commands/src/main/org/apache/avalon/facilities/console/commands/HelpCmd.java
==============================================================================
--- 
avalon/trunk/planet/facilities/console/commands/src/main/org/apache/avalon/facilities/console/commands/HelpCmd.java
 (original)
+++ 
avalon/trunk/planet/facilities/console/commands/src/main/org/apache/avalon/facilities/console/commands/HelpCmd.java
 Sat Aug  7 06:38:54 2004
@@ -21,9 +21,14 @@
 
 import java.util.Iterator;
 
+import org.apache.avalon.facilities.console.CommandInterpreter;
 import org.apache.avalon.facilities.console.Console;
 import org.apache.avalon.facilities.console.ConsoleCommand;
 
+import org.apache.avalon.framework.context.Context;
+import org.apache.avalon.framework.context.ContextException;
+import org.apache.avalon.framework.context.Contextualizable;
+
 import org.apache.avalon.framework.service.Serviceable;
 import org.apache.avalon.framework.service.ServiceException;
 import org.apache.avalon.framework.service.ServiceManager;
@@ -33,22 +38,41 @@
  * @avalon.service type="org.apache.avalon.facilities.console.ConsoleCommand"
  */
 public class HelpCmd
-    implements ConsoleCommand, Serviceable
+    implements ConsoleCommand, Serviceable, Contextualizable
 {
     private Console m_Console;
+    private String m_Name;
     
     public String getName()
     {
-        return "help";
+        return m_Name;
     }
     
     public String getDescription()
     {
-        String str = "usage: help (command)*\n\nGives a short description of each 
command.\nIf no command is given, then a list of available commands are printed.";
+        String str = "usage: " + m_Name + " (command)*\n\nGives a short description 
of each command.\nIf no command is given, then a list of available commands are 
printed.";
         return str;
     }
     
     /**
+     * Contextulaization of the listener by the container during 
+     * which we are supplied with the root composition model for 
+     * the application.
+     *
+     * @param ctx the supplied listener context
+     *
+     * @exception ContextException if a contextualization error occurs
+     *
+     * @avalon.entry key="urn:avalon:name" 
+     *               type="java.lang.String" 
+     */
+    public void contextualize( Context ctx ) 
+        throws ContextException
+    {
+        m_Name = (String) ctx.get( "urn:avalon:name" );
+    }
+
+    /**
      * @avalon.dependency type="org.apache.avalon.facilities.console.Console"
      *                    key="console"
      */
@@ -59,7 +83,7 @@
         m_Console.addCommand( this );
     }
     
-    public void execute( BufferedReader input, BufferedWriter output, String[] 
arguments )
+    public void execute( CommandInterpreter intp, BufferedReader input, 
BufferedWriter output, String[] arguments )
         throws Exception
     {
         output.newLine();

Copied: 
avalon/trunk/planet/facilities/console/commands/src/main/org/apache/avalon/facilities/console/commands/ShowModelCmd.java
 (from rev 36060, 
avalon/trunk/planet/facilities/console/commands/src/main/org/apache/avalon/facilities/console/commands/ViewModelCmd.java)
==============================================================================
--- 
avalon/trunk/planet/facilities/console/commands/src/main/org/apache/avalon/facilities/console/commands/ViewModelCmd.java
    (original)
+++ 
avalon/trunk/planet/facilities/console/commands/src/main/org/apache/avalon/facilities/console/commands/ShowModelCmd.java
    Sat Aug  7 06:38:54 2004
@@ -23,6 +23,7 @@
 import org.apache.avalon.composition.model.ContainmentModel;
 import org.apache.avalon.composition.model.DeploymentModel;
 
+import org.apache.avalon.facilities.console.CommandInterpreter;
 import org.apache.avalon.facilities.console.Console;
 import org.apache.avalon.facilities.console.ConsoleCommand;
 
@@ -35,25 +36,25 @@
 import org.apache.avalon.framework.service.ServiceManager;
 
 /**
- * @avalon.component name="console-viewmodel" lifestyle="singleton"
+ * @avalon.component name="console-showmodel" lifestyle="singleton"
  * @avalon.service type="org.apache.avalon.facilities.console.ConsoleCommand"
  */
-public class ViewModelCmd
+public class ShowModelCmd
     implements ConsoleCommand, Serviceable, Contextualizable
 {
     String LINE = 
       "\n-----------------------------------------------------------";
     
-    private ContainmentModel m_RootModel;
+    private String m_Name;
     
     public String getName()
     {
-        return "viewmodel";
+        return m_Name;
     }
     
     public String getDescription()
     {
-        String str = "usage: viewmodel (path)\n\nDisplays the composition model.";
+        String str = "usage: " + m_Name + " (path)\n\nDisplays the composition 
model.";
         return str;
     }
     
@@ -66,16 +67,15 @@
      *
      * @exception ContextException if a contextualization error occurs
      *
-     * @avalon.entry key="urn:composition:containment.model" 
-     *               type="org.apache.avalon.composition.model.ContainmentModel" 
-     *
+     * @avalon.entry key="urn:avalon:name" 
+     *               type="java.lang.String" 
      */
     public void contextualize( Context ctx ) 
         throws ContextException
     {
-        m_RootModel = (ContainmentModel) ctx.get( "urn:composition:containment.model" 
);
+        m_Name = (String) ctx.get( "urn:avalon:name" );
     }
-    
+
     /**
      * @avalon.dependency type="org.apache.avalon.facilities.console.Console"
      *                    key="console"
@@ -87,16 +87,18 @@
         console.addCommand( this );
     }
     
-    public void execute( BufferedReader input, BufferedWriter output, String[] 
arguments )
+    public void execute( CommandInterpreter intp, BufferedReader input, 
BufferedWriter output, String[] arguments )
         throws Exception
     {
         output.newLine();
+        ContainmentModel current = intp.getCurrentContainer();
         String path;
         if( arguments.length == 0 )
-            path = "/";
+            path = current.getPath();
         else
             path = arguments[0];
-        DeploymentModel model = m_RootModel.getModel( path );
+            
+        DeploymentModel model = current.getModel( path );
         
         String result = printModel( model );
         output.write( result );

Modified: 
avalon/trunk/planet/facilities/console/commands/src/main/org/apache/avalon/facilities/console/commands/ThreadCmd.java
==============================================================================
--- 
avalon/trunk/planet/facilities/console/commands/src/main/org/apache/avalon/facilities/console/commands/ThreadCmd.java
       (original)
+++ 
avalon/trunk/planet/facilities/console/commands/src/main/org/apache/avalon/facilities/console/commands/ThreadCmd.java
       Sat Aug  7 06:38:54 2004
@@ -18,9 +18,14 @@
 import java.io.BufferedReader;
 import java.io.BufferedWriter;
 
+import org.apache.avalon.facilities.console.CommandInterpreter;
 import org.apache.avalon.facilities.console.Console;
 import org.apache.avalon.facilities.console.ConsoleCommand;
 
+import org.apache.avalon.framework.context.Context;
+import org.apache.avalon.framework.context.ContextException;
+import org.apache.avalon.framework.context.Contextualizable;
+
 import org.apache.avalon.framework.service.Serviceable;
 import org.apache.avalon.framework.service.ServiceException;
 import org.apache.avalon.framework.service.ServiceManager;
@@ -30,20 +35,40 @@
  * @avalon.service type="org.apache.avalon.facilities.console.ConsoleCommand"
  */
 public class ThreadCmd
-    implements ConsoleCommand, Serviceable
+    implements ConsoleCommand, Serviceable, Contextualizable
 {
+    private String m_Name;
+    
     public String getName()
     {
-        return "thread";
+        return m_Name;
     }
     
     public String getDescription()
     {
-        String str = "usage: thread\n\nLists all current threads in the JVM.";
+        String str = "usage: " + m_Name + "\n\nLists all current threads in the JVM.";
         return str;
     }
     
     /**
+     * Contextulaization of the listener by the container during 
+     * which we are supplied with the root composition model for 
+     * the application.
+     *
+     * @param ctx the supplied listener context
+     *
+     * @exception ContextException if a contextualization error occurs
+     *
+     * @avalon.entry key="urn:avalon:name" 
+     *               type="java.lang.String" 
+     */
+    public void contextualize( Context ctx ) 
+        throws ContextException
+    {
+        m_Name = (String) ctx.get( "urn:avalon:name" );
+    }
+
+    /**
      * @avalon.dependency type="org.apache.avalon.facilities.console.Console"
      *                    key="console"
      */
@@ -54,7 +79,7 @@
         console.addCommand( this );
     }
     
-    public void execute( BufferedReader input, BufferedWriter output, String[] 
arguments )
+    public void execute( CommandInterpreter intp, BufferedReader input, 
BufferedWriter output, String[] arguments )
         throws Exception
     {
         ThreadGroup tg = Thread.currentThread().getThreadGroup();

Added: 
avalon/trunk/planet/facilities/console/impl/src/main/org/apache/avalon/facilities/console/impl/CommandInterpreterImpl.java
==============================================================================
--- (empty file)
+++ 
avalon/trunk/planet/facilities/console/impl/src/main/org/apache/avalon/facilities/console/impl/CommandInterpreterImpl.java
  Sat Aug  7 06:38:54 2004
@@ -0,0 +1,201 @@
+/*
+ * Copyright 1997-2004 Apache Software Foundation
+ * 
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ * 
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ * 
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.avalon.facilities.console.impl;
+
+import java.io.BufferedReader;
+import java.io.BufferedWriter;
+import java.io.IOException;
+import java.io.InputStreamReader;
+import java.io.OutputStreamWriter;
+
+import java.net.Socket;
+
+import java.util.TreeMap;
+import java.util.Map;
+import java.util.StringTokenizer;
+
+import org.apache.avalon.composition.model.ContainmentModel;
+
+import org.apache.avalon.facilities.console.CommandInterpreter;
+import org.apache.avalon.facilities.console.Console;
+import org.apache.avalon.facilities.console.ConsoleCommand;
+
+class CommandInterpreterImpl extends Thread
+    implements CommandInterpreter
+{
+    private Socket          m_Socket;
+    private boolean         m_Login;
+    private BufferedWriter  m_Output;
+    private BufferedReader  m_Input;
+    private Map             m_Commands;
+    private ContainmentModel m_CurrentNode;
+    
+    CommandInterpreterImpl( Socket socket, String welcome, Map commands, 
ContainmentModel root )
+        throws IOException
+    {
+        m_Socket = socket;
+        m_Commands = commands;
+        welcomeMessage( welcome );
+        m_CurrentNode = root;
+    }
+
+    public void addCommand( ConsoleCommand cmd )
+    {
+        m_Commands.put( cmd.getName(), cmd );
+    }
+    
+    public void removeCommand( ConsoleCommand cmd )
+    {
+        m_Commands.remove( cmd.getName() );
+    }
+    
+    public void removeCommand( String commandname )
+    {
+        m_Commands.remove( commandname );
+    }
+    
+    public ContainmentModel getCurrentContainer()
+    {
+        return m_CurrentNode;
+    }
+    
+    public void setCurrentNode( ContainmentModel node )
+    {
+        m_CurrentNode = node;
+    }
+    
+    public void run()
+    {
+        boolean running = true;
+        while( running )
+        {
+            try
+            {
+                if( ! m_Login )
+                    execute( "login" );
+                m_Login = true; //LoginCmd throws an exception if login fails.
+                execute( waitForCommandLine() );
+            } catch( InterruptedException e )
+            {
+                running = false;
+            } catch( Exception e )
+            {
+                String mess = e.getMessage();
+                if( mess == null )
+                    mess = "<null>";
+                errorMessage( mess );
+                e.printStackTrace();
+            }
+        }
+        try
+        {
+            m_Socket.close();
+        } catch( IOException e )
+        {
+        }
+    }
+
+    private void execute( String cmdline )
+        throws Exception
+    {
+        // TODO: Optimize parser.
+        StringTokenizer st = new StringTokenizer( cmdline, " ", false );
+        String cmd = "";
+        while( st.hasMoreTokens() )
+        {
+            cmd = (cmd + " " + st.nextToken()).trim();
+            ConsoleCommand cc = (ConsoleCommand) m_Commands.get( cmd );
+            if( cc != null )
+            {
+                String[] args = parseArguments( st );
+                cc.execute( this, getInput(), getOutput(), args );
+                return;
+            }
+        }
+        errorMessage( "Unknown command. 'help' for available commands." );
+    }
+
+    private String waitForCommandLine()
+        throws IOException
+    {
+        String cmdline = m_Input.readLine().trim();
+        return cmdline;
+    }
+
+    private String[] parseArguments( StringTokenizer st )
+    {
+        String[] args = new String[ st.countTokens() ];
+        for( int i=0 ; st.hasMoreTokens() ; i++ )
+            args[i] = st.nextToken();
+        return args;
+    }
+
+    private void welcomeMessage( String message )
+    {
+        try
+        {
+            getOutput().write( message );
+            getOutput().newLine();
+            getOutput().flush();
+        } catch( IOException e )
+        {
+            e.printStackTrace( System.out );
+        }
+    }
+
+    private void errorMessage( String message )
+    {
+        try
+        {
+            getOutput().write( message );
+            getOutput().newLine();
+            getOutput().flush();
+        } catch( IOException e )
+        {
+            e.printStackTrace( System.out );
+        }
+    }
+
+    private BufferedWriter getOutput()
+        throws IOException
+    {
+        if( m_Output == null )
+            openOut();
+        return m_Output;
+    }
+
+    private BufferedReader getInput()
+        throws IOException
+    {
+        if( m_Input == null )
+            openIn();
+        return m_Input;
+    }
+
+    private void openOut()
+        throws IOException
+    {
+        m_Output = new BufferedWriter( new OutputStreamWriter( 
m_Socket.getOutputStream() ) );
+    }
+
+    private void openIn()
+        throws IOException
+    {
+        m_Input = new BufferedReader( new InputStreamReader( 
m_Socket.getInputStream() ) );
+    }
+
+}
+

Modified: 
avalon/trunk/planet/facilities/console/impl/src/main/org/apache/avalon/facilities/console/impl/ConsoleImpl.java
==============================================================================
--- 
avalon/trunk/planet/facilities/console/impl/src/main/org/apache/avalon/facilities/console/impl/ConsoleImpl.java
     (original)
+++ 
avalon/trunk/planet/facilities/console/impl/src/main/org/apache/avalon/facilities/console/impl/ConsoleImpl.java
     Sat Aug  7 06:38:54 2004
@@ -26,12 +26,19 @@
 import java.util.Hashtable;
 import java.util.Iterator;
 
+import org.apache.avalon.composition.model.ContainmentModel;
+
+import org.apache.avalon.facilities.console.CommandInterpreter;
 import org.apache.avalon.facilities.console.Console;
 import org.apache.avalon.facilities.console.ConsoleCommand;
 
 import org.apache.avalon.framework.activity.Initializable;
 import org.apache.avalon.framework.activity.Startable;
 
+import org.apache.avalon.framework.context.Context;
+import org.apache.avalon.framework.context.ContextException;
+import org.apache.avalon.framework.context.Contextualizable;
+
 import org.apache.avalon.framework.logger.AbstractLogEnabled;
 
 import org.apache.avalon.framework.parameters.ParameterException;
@@ -44,7 +51,8 @@
  * @avalon.service type="org.apache.avalon.facilities.console.Console"
  */
 public class ConsoleImpl extends AbstractLogEnabled
-    implements Console, Runnable, Startable, Parameterizable, Initializable
+    implements Console, Runnable, Startable, Parameterizable, 
+               Initializable, Contextualizable
 {
     private int         m_Port;
     private ServerSocket m_ServerSocket;
@@ -52,6 +60,26 @@
     private Hashtable   m_Commands;
     private ArrayList   m_Interpreters;
     private String      m_Welcome;    
+    private ContainmentModel m_RootModel;
+    
+    /**
+     * Contextulaization of the listener by the container during 
+     * which we are supplied with the root composition model for 
+     * the application.
+     *
+     * @param ctx the supplied listener context
+     *
+     * @exception ContextException if a contextualization error occurs
+     *
+     * @avalon.entry key="urn:composition:containment.model" 
+     *               type="org.apache.avalon.composition.model.ContainmentModel" 
+     *
+     */
+    public void contextualize( Context ctx ) 
+        throws ContextException
+    {
+        m_RootModel = (ContainmentModel) ctx.get( "urn:composition:containment.model" 
);
+    }
     
     public void parameterize( Parameters params )
         throws ParameterException
@@ -94,17 +122,45 @@
     public void addCommand( ConsoleCommand cmd )
     {
         getLogger().info( "Added " + cmd.getName() + " command." );
-        m_Commands.put( cmd.getName(), cmd );
+        String name = cmd.getName();
+        m_Commands.put( name, cmd );
+        synchronized( m_Interpreters )
+        {
+            Iterator list = m_Interpreters.iterator();
+            while( list.hasNext() )
+            {
+                CommandInterpreter intp = (CommandInterpreter) list.next();
+                intp.addCommand( cmd );
+            }
+        }
     }
     
     public void removeCommand( ConsoleCommand cmd )
     {
         m_Commands.remove( cmd.getName() );
+        synchronized( m_Interpreters )
+        {
+            Iterator list = m_Interpreters.iterator();
+            while( list.hasNext() )
+            {
+                CommandInterpreter intp = (CommandInterpreter) list.next();
+                intp.removeCommand( cmd );
+            }
+        }
     }
     
     public void removeCommand( String commandname )
     {
         m_Commands.remove( commandname );
+        synchronized( m_Interpreters )
+        {
+            Iterator list = m_Interpreters.iterator();
+            while( list.hasNext() )
+            {
+                CommandInterpreter intp = (CommandInterpreter) list.next();
+                intp.removeCommand( commandname );
+            }
+        }
     }
     
     public ConsoleCommand getCommand( String name )
@@ -125,8 +181,11 @@
             try
             {
                 Socket socket = m_ServerSocket.accept();
-                CommandInterpreter intp = new CommandInterpreter( socket, m_Welcome, 
this );
-                m_Interpreters.add( intp );
+                CommandInterpreterImpl intp = new CommandInterpreterImpl( socket, 
m_Welcome, m_Commands, m_RootModel );
+                synchronized( m_Interpreters )
+                {
+                    m_Interpreters.add( intp );
+                }
                 intp.start();
             } catch( IOException e )
             {
@@ -146,7 +205,7 @@
         Iterator list = m_Interpreters.iterator();
         while( list.hasNext() )
         {
-            CommandInterpreter intp = (CommandInterpreter) list.next();
+            CommandInterpreterImpl intp = (CommandInterpreterImpl) list.next();
             intp.interrupt();
         }
     }

Modified: 
avalon/trunk/planet/facilities/console/impl/src/main/org/apache/avalon/facilities/console/impl/LoginCmd.java
==============================================================================
--- 
avalon/trunk/planet/facilities/console/impl/src/main/org/apache/avalon/facilities/console/impl/LoginCmd.java
        (original)
+++ 
avalon/trunk/planet/facilities/console/impl/src/main/org/apache/avalon/facilities/console/impl/LoginCmd.java
        Sat Aug  7 06:38:54 2004
@@ -19,9 +19,14 @@
 import java.io.BufferedWriter;
 import java.io.IOException;
 
+import org.apache.avalon.facilities.console.CommandInterpreter;
 import org.apache.avalon.facilities.console.Console;
 import org.apache.avalon.facilities.console.ConsoleCommand;
 
+import org.apache.avalon.framework.context.Context;
+import org.apache.avalon.framework.context.ContextException;
+import org.apache.avalon.framework.context.Contextualizable;
+
 import org.apache.avalon.framework.parameters.ParameterException;
 import org.apache.avalon.framework.parameters.Parameterizable;
 import org.apache.avalon.framework.parameters.Parameters;
@@ -35,18 +40,19 @@
  * @avalon.service type="org.apache.avalon.facilities.console.ConsoleCommand"
  */
 public class LoginCmd
-    implements ConsoleCommand, Parameterizable, Serviceable
+    implements ConsoleCommand, Parameterizable, Serviceable, Contextualizable
 {
     private String m_Password;
-    
+    private String m_Name;
+        
     public String getName()
     {
-        return "login";
+        return m_Name;
     }
     
     public String getDescription()
     {
-        String str = "usage: login\n\nLogins to the system. This command is executed 
automatically.";
+        String str = "usage: " + m_Name + "\n\nLogins to the system. This command is 
executed automatically.";
         return str;
     }
     
@@ -57,6 +63,24 @@
     }
     
     /**
+     * Contextulaization of the listener by the container during 
+     * which we are supplied with the root composition model for 
+     * the application.
+     *
+     * @param ctx the supplied listener context
+     *
+     * @exception ContextException if a contextualization error occurs
+     *
+     * @avalon.entry key="urn:avalon:name" 
+     *               type="java.lang.String" 
+     */
+    public void contextualize( Context ctx ) 
+        throws ContextException
+    {
+        m_Name = (String) ctx.get( "urn:avalon:name" );
+    }
+
+    /**
      * @avalon.dependency type="org.apache.avalon.facilities.console.Console"
      *                    key="console"
      */
@@ -67,7 +91,7 @@
         console.addCommand( this );
     }
     
-    public void execute( BufferedReader input, BufferedWriter output, String[] 
arguments )
+    public void execute( CommandInterpreter intp, BufferedReader input, 
BufferedWriter output, String[] arguments )
         throws Exception
     {
         output.write( "Password:" );

---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to