Author: rdonkin
Date: Sun Feb  4 12:16:21 2007
New Revision: 503471

URL: http://svn.apache.org/viewvc?view=rev&rev=503471
Log:
Added ability to register custom commands through the configuration.

Added:
    james/server/trunk/src/java/org/apache/james/remotemanager/Command.java
    
james/server/trunk/src/java/org/apache/james/remotemanager/CommandRegistry.java
    james/server/trunk/src/test/org/apache/james/remotemanager/EchoCommand.java
Modified:
    
james/server/trunk/src/java/org/apache/james/remotemanager/RemoteManager.java
    
james/server/trunk/src/java/org/apache/james/remotemanager/RemoteManagerHandler.java
    
james/server/trunk/src/java/org/apache/james/remotemanager/RemoteManagerHandlerConfigurationData.java
    
james/server/trunk/src/test/org/apache/james/remotemanager/RemoteManagerTest.java
    
james/server/trunk/src/test/org/apache/james/remotemanager/RemoteManagerTestConfiguration.java

Added: james/server/trunk/src/java/org/apache/james/remotemanager/Command.java
URL: 
http://svn.apache.org/viewvc/james/server/trunk/src/java/org/apache/james/remotemanager/Command.java?view=auto&rev=503471
==============================================================================
--- james/server/trunk/src/java/org/apache/james/remotemanager/Command.java 
(added)
+++ james/server/trunk/src/java/org/apache/james/remotemanager/Command.java Sun 
Feb  4 12:16:21 2007
@@ -0,0 +1,48 @@
+/****************************************************************
+ * Licensed to the Apache Software Foundation (ASF) under one   *
+ * or more contributor license agreements.  See the NOTICE file *
+ * distributed with this work for additional information        *
+ * regarding copyright ownership.  The ASF licenses this file   *
+ * to you 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.james.remotemanager;
+
+import java.io.PrintWriter;
+
+/**
+ * Commands JAMES through the remote management console.
+ */
+public interface Command {
+
+    /**
+     * Gets the name of this command.
+     * @return command name, not null
+     */
+    public String getName();
+    
+    /**
+     * Outputs useful information for the user of this command.
+     * @return user help, not null
+     */
+    public String help();
+
+    /**
+     * Executes this command.
+     * @param args raw arguments, not null
+     * @param out <code>PrintWriter</code> for user feedback
+     * @return true additional commands are expected, false otherwise. Note 
+     */
+    public boolean execute(String args, final PrintWriter out);
+}

Added: 
james/server/trunk/src/java/org/apache/james/remotemanager/CommandRegistry.java
URL: 
http://svn.apache.org/viewvc/james/server/trunk/src/java/org/apache/james/remotemanager/CommandRegistry.java?view=auto&rev=503471
==============================================================================
--- 
james/server/trunk/src/java/org/apache/james/remotemanager/CommandRegistry.java 
(added)
+++ 
james/server/trunk/src/java/org/apache/james/remotemanager/CommandRegistry.java 
Sun Feb  4 12:16:21 2007
@@ -0,0 +1,62 @@
+/****************************************************************
+ * Licensed to the Apache Software Foundation (ASF) under one   *
+ * or more contributor license agreements.  See the NOTICE file *
+ * distributed with this work for additional information        *
+ * regarding copyright ownership.  The ASF licenses this file   *
+ * to you 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.james.remotemanager;
+
+import java.io.PrintWriter;
+
+import org.apache.avalon.framework.logger.AbstractLogEnabled;
+import org.apache.avalon.framework.logger.Logger;
+
+/**
+ * Registers remote manager commands.
+ */
+public class CommandRegistry extends AbstractLogEnabled {
+
+    private final Command[] commands;
+    
+    public CommandRegistry(final Command[] commands) {
+        this.commands = commands;
+    }
+    
+    public boolean execute(final String commandName, final String args, final 
PrintWriter out) {
+        boolean result = true;
+        for (int i=0; i<commands.length;i++) {
+            final Command command = commands[i];
+            if (commandName.equalsIgnoreCase(command.getName())) {
+                final Logger logger = getLogger();
+                if (logger != null) logger.debug("Found matching command");
+                result = command.execute(args, out);
+            }
+        }
+        return result;
+    }
+    
+    public void printHelp(final PrintWriter out) {
+        for (int i=0; i<commands.length;i++) {
+            final Command command = commands[i];
+            final StringBuffer buffer = new StringBuffer(command.getName());
+            while (buffer.length() <= 39) {
+                buffer.append(' ');
+            }
+            buffer.append(command.help());
+            out.println(buffer);
+        }
+    }
+}

Modified: 
james/server/trunk/src/java/org/apache/james/remotemanager/RemoteManager.java
URL: 
http://svn.apache.org/viewvc/james/server/trunk/src/java/org/apache/james/remotemanager/RemoteManager.java?view=diff&rev=503471&r1=503470&r2=503471
==============================================================================
--- 
james/server/trunk/src/java/org/apache/james/remotemanager/RemoteManager.java 
(original)
+++ 
james/server/trunk/src/java/org/apache/james/remotemanager/RemoteManager.java 
Sun Feb  4 12:16:21 2007
@@ -22,8 +22,10 @@
 package org.apache.james.remotemanager;
 
 import org.apache.avalon.cornerstone.services.store.Store;
+import org.apache.avalon.framework.configuration.Configurable;
 import org.apache.avalon.framework.configuration.Configuration;
 import org.apache.avalon.framework.configuration.ConfigurationException;
+import org.apache.avalon.framework.logger.Logger;
 import org.apache.avalon.framework.service.ServiceException;
 import org.apache.avalon.framework.service.ServiceManager;
 import org.apache.james.core.AbstractJamesService;
@@ -36,6 +38,8 @@
 import org.apache.james.services.UsersStore;
 import org.apache.james.services.VirtualUserTableManagementService;
 
+import java.util.ArrayList;
+import java.util.Collection;
 import java.util.HashMap;
 
 /**
@@ -84,6 +88,8 @@
      */
     private Store store;
     
+    private Command[] commands = {};
+    
     /**
      * reference to administration of Bayesian analyzer
      */
@@ -232,9 +238,40 @@
             if (promtConfiguration != null) prompt = 
promtConfiguration.getValue();
             if (prompt == null) prompt = ""; 
             else if (!prompt.equals("") && !prompt.endsWith(" ")) prompt += " 
"; 
+            configureCommands(configuration);
         }
     }
   
+    private void configureCommands(final Configuration configuration) throws 
ConfigurationException {
+        Collection commands = new ArrayList();
+        Configuration[] commandConfigurations = configuration.getChildren( 
"command" );
+        if (commandConfigurations != null) {
+            for(int i=0;i<commandConfigurations.length;i++) {
+                final Configuration commandConfiguration = 
commandConfigurations[i];
+                Configuration classConfiguration 
+                = commandConfiguration.getChild( "class-name" );
+                String className = classConfiguration.getValue();
+                if (className != null) {
+                    try {
+                        Command command 
+                        = (Command) Class.forName(className).newInstance();
+                        if (command instanceof Configurable) {
+                            Configurable configurable = (Configurable) command;
+                            configurable.configure(commandConfiguration);
+                        }
+                        commands.add(command);
+                    } catch (Exception e) {
+                        final Logger logger = getLogger();
+                        if (logger != null) {
+                            logger.error("Failed to load custom command", e);
+                        }
+                    }
+                }
+            }
+        }
+        this.commands = (Command[]) commands.toArray(this.commands);
+    }
+    
     /**
      * @see org.apache.james.core.AbstractJamesService#getDefaultPort()
      */
@@ -353,6 +390,13 @@
          */
         public DomainListManagementService getDomainListManagement() {
             return RemoteManager.this.domListManagement;
+        }
+        
+        /**
+         * @see 
org.apache.james.neo.remotemanager.RemoteManagerHandlerConfigurationData#getCommands()
+         */
+        public Command[] getCommands() {
+            return RemoteManager.this.commands;
         }
     }
 

Modified: 
james/server/trunk/src/java/org/apache/james/remotemanager/RemoteManagerHandler.java
URL: 
http://svn.apache.org/viewvc/james/server/trunk/src/java/org/apache/james/remotemanager/RemoteManagerHandler.java?view=diff&rev=503471&r1=503470&r2=503471
==============================================================================
--- 
james/server/trunk/src/java/org/apache/james/remotemanager/RemoteManagerHandler.java
 (original)
+++ 
james/server/trunk/src/java/org/apache/james/remotemanager/RemoteManagerHandler.java
 Sun Feb  4 12:16:21 2007
@@ -113,6 +113,8 @@
      */
     private UsersRepository users;
     
+    private CommandRegistry commandRegistry;
+    
     private final static String HEADER_IDENTIFIER = "header=";
     private final static String REGEX_IDENTIFIER = "regex=";
     private final static String KEY_IDENTIFIER = "key=";
@@ -132,6 +134,9 @@
 
             // Reset the users repository to the default.
             users = theConfigData.getUsersRepository();
+            
+            Command[] commands = theConfigData.getCommands();
+            commandRegistry = new CommandRegistry(commands);
         } else {
             throw new IllegalArgumentException("Configuration object does not 
implement RemoteManagerHandlerConfigurationData");
         }
@@ -249,7 +254,11 @@
         }
         command = command.toUpperCase(Locale.US);
         
-        if (!COMMANDLIST.contains(command)) return doUnknownCommand(command);
+        if (!COMMANDLIST.contains(command)) {
+            final boolean result = commandRegistry.execute(command, argument, 
out);
+            out.flush();
+            return result;
+        }
         
         try {
             Method method = getClass().getDeclaredMethod("do"+command, 
WORKER_METHOD_PARAMETERSET);

Modified: 
james/server/trunk/src/java/org/apache/james/remotemanager/RemoteManagerHandlerConfigurationData.java
URL: 
http://svn.apache.org/viewvc/james/server/trunk/src/java/org/apache/james/remotemanager/RemoteManagerHandlerConfigurationData.java?view=diff&rev=503471&r1=503470&r2=503471
==============================================================================
--- 
james/server/trunk/src/java/org/apache/james/remotemanager/RemoteManagerHandlerConfigurationData.java
 (original)
+++ 
james/server/trunk/src/java/org/apache/james/remotemanager/RemoteManagerHandlerConfigurationData.java
 Sun Feb  4 12:16:21 2007
@@ -125,4 +125,10 @@
      * @return the DomainListManagementService
      */
     DomainListManagementService getDomainListManagement();
+    
+    /**
+     * Gets avaliable commands.
+     * @return <code>Command</code>'s, not null possibly empty
+    */
+    Command[] getCommands();
 }

Added: 
james/server/trunk/src/test/org/apache/james/remotemanager/EchoCommand.java
URL: 
http://svn.apache.org/viewvc/james/server/trunk/src/test/org/apache/james/remotemanager/EchoCommand.java?view=auto&rev=503471
==============================================================================
--- james/server/trunk/src/test/org/apache/james/remotemanager/EchoCommand.java 
(added)
+++ james/server/trunk/src/test/org/apache/james/remotemanager/EchoCommand.java 
Sun Feb  4 12:16:21 2007
@@ -0,0 +1,39 @@
+/****************************************************************
+ * Licensed to the Apache Software Foundation (ASF) under one   *
+ * or more contributor license agreements.  See the NOTICE file *
+ * distributed with this work for additional information        *
+ * regarding copyright ownership.  The ASF licenses this file   *
+ * to you 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.james.remotemanager;
+
+import java.io.PrintWriter;
+
+public class EchoCommand implements Command {
+
+    public boolean execute(String args, PrintWriter out) {
+        out.print(args);
+        return false;
+    }
+
+    public String getName() {
+        return "Echo";
+    }
+
+    public String help() {
+        return "Echos arguments to console";
+    }
+
+}

Modified: 
james/server/trunk/src/test/org/apache/james/remotemanager/RemoteManagerTest.java
URL: 
http://svn.apache.org/viewvc/james/server/trunk/src/test/org/apache/james/remotemanager/RemoteManagerTest.java?view=diff&rev=503471&r1=503470&r2=503471
==============================================================================
--- 
james/server/trunk/src/test/org/apache/james/remotemanager/RemoteManagerTest.java
 (original)
+++ 
james/server/trunk/src/test/org/apache/james/remotemanager/RemoteManagerTest.java
 Sun Feb  4 12:16:21 2007
@@ -213,6 +213,16 @@
         return dns;
     }
 
+    public void testCustomCommand() throws Exception {
+        finishSetUp(m_testConfiguration);
+        connect();
+        login();
+
+        sendCommand("echo hsif eht lla rof sknaht");
+        String lastLine = getLastLine(readAnswer());
+        assertEquals("Arguments echoed", "hsif eht lla rof sknaht", lastLine);
+    }
+    
     public void testLogin() throws IOException {
         finishSetUp(m_testConfiguration);
         connect();

Modified: 
james/server/trunk/src/test/org/apache/james/remotemanager/RemoteManagerTestConfiguration.java
URL: 
http://svn.apache.org/viewvc/james/server/trunk/src/test/org/apache/james/remotemanager/RemoteManagerTestConfiguration.java?view=diff&rev=503471&r1=503470&r2=503471
==============================================================================
--- 
james/server/trunk/src/test/org/apache/james/remotemanager/RemoteManagerTestConfiguration.java
 (original)
+++ 
james/server/trunk/src/test/org/apache/james/remotemanager/RemoteManagerTestConfiguration.java
 Sun Feb  4 12:16:21 2007
@@ -30,13 +30,28 @@
     private Integer m_connectionLimit = null;
     private String m_loginName = "testLogin";
     private String m_loginPassword = "testPassword";
-
+    private String commandClassName = 
"org.apache.james.remotemanager.EchoCommand";
+    
     public RemoteManagerTestConfiguration(int smtpListenerPort) {
         super("smptserver");
 
         m_remoteManagerListenerPort = smtpListenerPort;
     }
 
+    
+    
+    public String getCommandClassName() {
+        return commandClassName;
+    }
+
+
+
+    public void setCommandClassName(String commandClassName) {
+        this.commandClassName = commandClassName;
+    }
+
+
+
     public void setConnectionLimit(int iConnectionLimit) {
         m_connectionLimit = new Integer(iConnectionLimit);
     }
@@ -82,6 +97,11 @@
 
         
handlerConfig.addChild(Util.createRemoteManagerHandlerChainConfiguration());
         addChild(handlerConfig);
+        
+        DefaultConfiguration commandConfiguration = new 
DefaultConfiguration("command");
+        
commandConfiguration.addChild(Util.getValuedConfiguration("class-name", 
commandClassName));
+        
+        addChild(commandConfiguration);
     }
 
 }



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

Reply via email to