You are viewing a plain text version of this content. The canonical link for it is here.
Posted to server-dev@james.apache.org by no...@apache.org on 2011/04/05 14:31:31 UTC

svn commit: r1089000 - in /james/server/trunk/cli: ./ src/main/java/org/apache/james/cli/ src/main/java/org/apache/james/cli/probe/ src/main/java/org/apache/james/cli/probe/impl/ src/main/java/org/apache/james/cli/type/ src/test/ src/test/java/ src/tes...

Author: norman
Date: Tue Apr  5 12:31:30 2011
New Revision: 1089000

URL: http://svn.apache.org/viewvc?rev=1089000&view=rev
Log:
Refactor cli module to use enum for values and an interface for the ServerProbe. This changes was contributed by Beau Grantham. Thanks for the patch. See JAMES-1121

Added:
    james/server/trunk/cli/src/main/java/org/apache/james/cli/probe/
    james/server/trunk/cli/src/main/java/org/apache/james/cli/probe/ServerProbe.java
    james/server/trunk/cli/src/main/java/org/apache/james/cli/probe/impl/
    james/server/trunk/cli/src/main/java/org/apache/james/cli/probe/impl/JmxServerProbe.java
    james/server/trunk/cli/src/main/java/org/apache/james/cli/type/
    james/server/trunk/cli/src/main/java/org/apache/james/cli/type/CmdType.java
    james/server/trunk/cli/src/test/
    james/server/trunk/cli/src/test/java/
    james/server/trunk/cli/src/test/java/org/
    james/server/trunk/cli/src/test/java/org/apache/
    james/server/trunk/cli/src/test/java/org/apache/james/
    james/server/trunk/cli/src/test/java/org/apache/james/cli/
    james/server/trunk/cli/src/test/java/org/apache/james/cli/type/
    james/server/trunk/cli/src/test/java/org/apache/james/cli/type/CmdTypeTest.java
Removed:
    james/server/trunk/cli/src/main/java/org/apache/james/cli/ServerProbe.java
Modified:
    james/server/trunk/cli/pom.xml
    james/server/trunk/cli/src/main/java/org/apache/james/cli/ServerCmd.java

Modified: james/server/trunk/cli/pom.xml
URL: http://svn.apache.org/viewvc/james/server/trunk/cli/pom.xml?rev=1089000&r1=1088999&r2=1089000&view=diff
==============================================================================
--- james/server/trunk/cli/pom.xml (original)
+++ james/server/trunk/cli/pom.xml Tue Apr  5 12:31:30 2011
@@ -70,5 +70,10 @@
       <groupId>commons-cli</groupId>
       <artifactId>commons-cli</artifactId>
     </dependency>
+    <dependency>
+      <groupId>junit</groupId>
+      <artifactId>junit</artifactId>
+      <scope>test</scope>
+    </dependency>
   </dependencies>
 </project>
\ No newline at end of file

Modified: james/server/trunk/cli/src/main/java/org/apache/james/cli/ServerCmd.java
URL: http://svn.apache.org/viewvc/james/server/trunk/cli/src/main/java/org/apache/james/cli/ServerCmd.java?rev=1089000&r1=1088999&r2=1089000&view=diff
==============================================================================
--- james/server/trunk/cli/src/main/java/org/apache/james/cli/ServerCmd.java (original)
+++ james/server/trunk/cli/src/main/java/org/apache/james/cli/ServerCmd.java Tue Apr  5 12:31:30 2011
@@ -28,7 +28,13 @@ import org.apache.commons.cli.Option;
 import org.apache.commons.cli.Options;
 import org.apache.commons.cli.ParseException;
 import org.apache.commons.cli.PosixParser;
-
+import org.apache.james.cli.probe.ServerProbe;
+import org.apache.james.cli.probe.impl.JmxServerProbe;
+import org.apache.james.cli.type.CmdType;
+
+/**
+ * Command line utility for managing various aspect of the James server.
+ */
 public class ServerCmd {
     private static final String HOST_OPT_LONG = "host";
     private static final String HOST_OPT_SHORT = "h";
@@ -46,30 +52,14 @@ public class ServerCmd {
     }
 
     /**
-     * Prints usage information to stdout.
+     * Main method to initialize the class.
+     * 
+     * @param args
+     *            Command-line arguments.
+     * @throws IOException
+     * @throws InterruptedException
+     * @throws ParseException
      */
-    private static void printUsage() {
-        HelpFormatter hf = new HelpFormatter();
-        String header = String.format("%nAvailable commands:%n" + "adduser <username> <password>%n" + "removeuser <username>%n" + "listusers%n" + "adddomain <domainname>%n" + "removedomain <domainname>%n" + "listdomains%n" + "addMapping <address|regex> <user> <domain> <fromaddress|regexstring>%n"
-                + "removeMapping <address|regex> <user> <domain> <fromaddress|regexstring>%n" + "listMappings [<user> <domain>]%n");
-        String usage = String.format("java %s --host <arg> <command>%n", ServerCmd.class.getName());
-        hf.printHelp(usage, "", options, header);
-    }
-
-    private void onException(Exception e, PrintStream out) {
-
-        out.println("Error while execute command:");
-        out.println(e.getMessage());
-    }
-
-    public void print(String[] data, PrintStream out) {
-        for (int i = 0; i < data.length; i++) {
-            String u = data[i];
-            out.println(u);
-        }
-        out.println();
-    }
-
     public static void main(String[] args) throws IOException, InterruptedException, ParseException {
         CommandLineParser parser = new PosixParser();
         CommandLine cmd = null;
@@ -82,6 +72,13 @@ public class ServerCmd {
             System.exit(1);
         }
 
+        // Verify arguments
+        if (cmd.getArgs().length < 1) {
+            System.err.println("Missing argument for command.");
+            printUsage();
+            System.exit(1);
+        }
+        
         String host = cmd.getOptionValue(HOST_OPT_LONG);
         int port = defaultPort;
 
@@ -96,62 +93,58 @@ public class ServerCmd {
 
         ServerProbe probe = null;
         try {
-            probe = new ServerProbe(host, port);
+            probe = new JmxServerProbe(host, port);
         } catch (IOException ioe) {
             System.err.println("Error connecting to remote JMX agent!");
             ioe.printStackTrace();
             System.exit(3);
         }
 
-        if (cmd.getArgs().length < 1) {
-            System.err.println("Missing argument for command.");
-            printUsage();
-            System.exit(1);
-        }
-
         ServerCmd sCmd = new ServerCmd();
 
         // Execute the requested command.
         String[] arguments = cmd.getArgs();
         String cmdName = arguments[0];
         try {
-            if (cmdName.equals("adduser")) {
-                if (arguments.length == 3) {
+            CmdType cmdType = CmdType.lookup(cmdName);
+
+            if (CmdType.ADDUSER.equals(cmdType)) {
+                if (cmdType.hasCorrectArguments(arguments.length)) {
                     probe.addUser(arguments[1], arguments[2]);
                 } else {
                     printUsage();
                     System.exit(1);
                 }
-            } else if (cmdName.equals("removeuser")) {
-                if (arguments.length == 2) {
+            } else if (CmdType.REMOVEUSER.equals(cmdType)) {
+                if (cmdType.hasCorrectArguments(arguments.length)) {
                     probe.removeUser(arguments[1]);
                 } else {
                     printUsage();
                     System.exit(1);
                 }
-            } else if (cmdName.equals("listusers")) {
-                if (arguments.length == 1) {
+            } else if (CmdType.LISTUSERS.equals(cmdType)) {
+                if (cmdType.hasCorrectArguments(arguments.length)) {
                     sCmd.print(probe.listUsers(), System.out);
                 } else {
                     printUsage();
                     System.exit(1);
                 }
-            } else if (cmdName.equals("adddomain")) {
-                if (arguments.length == 2) {
+            } else if (CmdType.ADDDOMAIN.equals(cmdType)) {
+                if (cmdType.hasCorrectArguments(arguments.length)) {
                     probe.addDomain(arguments[1]);
                 } else {
                     printUsage();
                     System.exit(1);
                 }
-            } else if (cmdName.equals("removedomain")) {
-                if (arguments.length == 2) {
+            } else if (CmdType.REMOVEDOMAIN.equals(cmdType)) {
+                if (cmdType.hasCorrectArguments(arguments.length)) {
                     probe.removeDomain(arguments[1]);
                 } else {
                     printUsage();
                     System.exit(1);
                 }
-            } else if (cmdName.equals("listdomains")) {
-                if (arguments.length == 1) {
+            } else if (CmdType.LISTDOMAINS.equals(cmdType)) {
+                if (cmdType.hasCorrectArguments(arguments.length)) {
                     sCmd.print(probe.listDomains(), System.out);
                 } else {
                     printUsage();
@@ -170,4 +163,51 @@ public class ServerCmd {
         System.exit(0);
     }
 
+    /**
+     * Print data to an output stream.
+     * 
+     * @param data
+     *            The data to print, each element representing a line.
+     * @param out
+     *            The output stream to which printing should occur.
+     */
+    public void print(String[] data, PrintStream out) {
+        if (data == null)
+            return;
+        
+        for (int i = 0; i < data.length; i++) {
+            String u = data[i];
+            out.println(u);
+        }
+        
+        out.println();
+    }
+
+    /*
+     * Prints usage information to stdout.
+     */
+    private static void printUsage() {
+        HelpFormatter hf = new HelpFormatter();
+        String header = String.format("%nAvailable commands:%n" 
+                + "adduser <username> <password>%n"
+                + "removeuser <username>%n" 
+                + "listusers%n" 
+                + "adddomain <domainname>%n"
+                + "removedomain <domainname>%n" 
+                + "listdomains%n"
+                // + "addMapping <address|regex> <user> <domain> <fromaddress|regexstring>%n"
+                // + "removeMapping <address|regex> <user> <domain> <fromaddress|regexstring>%n"
+                // + "listMappings [<user> <domain>]%n"
+                );
+        String usage = String.format("java %s --host <arg> <command>%n", ServerCmd.class.getName());
+        hf.printHelp(usage, "", options, header);
+    }
+
+    /*
+     * Handle an exception.
+     */
+    private void onException(Exception e, PrintStream out) {
+        out.println("Error while execute command:");
+        out.println(e.getMessage());
+    }
 }

Added: james/server/trunk/cli/src/main/java/org/apache/james/cli/probe/ServerProbe.java
URL: http://svn.apache.org/viewvc/james/server/trunk/cli/src/main/java/org/apache/james/cli/probe/ServerProbe.java?rev=1089000&view=auto
==============================================================================
--- james/server/trunk/cli/src/main/java/org/apache/james/cli/probe/ServerProbe.java (added)
+++ james/server/trunk/cli/src/main/java/org/apache/james/cli/probe/ServerProbe.java Tue Apr  5 12:31:30 2011
@@ -0,0 +1,146 @@
+package org.apache.james.cli.probe;
+
+import java.util.Collection;
+import java.util.Map;
+
+public interface ServerProbe {
+    /**
+     * Add a user to this mail server.
+     * 
+     * @param userName
+     *            The name of the user being added.
+     * @param password
+     *            The password of the user being added.
+     * @throws Exception
+     */
+    public void addUser(String userName, String password) throws Exception;
+
+    /**
+     * Delete a user from this mail server.
+     * 
+     * @param username
+     *            The name of the user being deleted.
+     * @throws Exception
+     */
+    public void removeUser(String username) throws Exception;
+
+    /**
+     * Get a List the names of all users.
+     * 
+     * @return a List of all user names.
+     * @throws Exception
+     */
+    public String[] listUsers() throws Exception;
+
+    /**
+     * Set a user's password.
+     * 
+     * @param userName
+     *            The name of the user whose password will be changed.
+     * @param password
+     *            The new password.
+     * @throws Exception
+     */
+    public void setPassword(String userName, String password) throws Exception;
+
+    /**
+     * Add domain to the service.
+     * 
+     * @param domain
+     *            The domain to add.
+     * @throws Exception
+     */
+    public void addDomain(String domain) throws Exception;
+
+    /**
+     * Remove domain from the service
+     * 
+     * @param domain
+     *            The domain to remove.
+     * @throws Exception
+     */
+    public void removeDomain(String domain) throws Exception;
+
+    /**
+     * Get a list of domains for the service.
+     * 
+     * @return domains an array of domains, or null if no domains exist.
+     * @throws Exception
+     */
+    public String[] listDomains() throws Exception;
+
+    /**
+     * Get a Map which holds all mappings. The key is the user@domain and the
+     * value is a Collection which holds all mappings.
+     * 
+     * @return a Map which holds all mappings.
+     * @throws Exception
+     */
+    public Map<String, Collection<String>> listMappings() throws Exception;
+
+    /**
+     * Add address mapping.
+     * 
+     * @param user
+     *            The username, or null if no username should be used.
+     * @param domain
+     *            The domain, or null if no domain should be used.
+     * @param toAaddress
+     *            The address.
+     * @throws Exception
+     */
+    public void addAddressMapping(String user, String domain, String toAddress) throws Exception;
+
+    /**
+     * Remove address mapping.
+     * 
+     * @param user
+     *            The username, or null if no username should be used.
+     * @param domain
+     *            The domain, or null if no domain should be used
+     * @param fromAddress
+     *            The address.
+     * @throws Exception
+     */
+    public void removeAddressMapping(String user, String domain, String fromAddress) throws Exception;
+
+    /**
+     * Return the explicit mapping stored for the given user and domain. Return
+     * null if no mapping was found
+     * 
+     * @param user
+     *            The username.
+     * @param domain
+     *            The domain.
+     * @return the collection which holds the mappings, or null if no mapping is
+     *         found.
+     * @throws Exception
+     */
+    public Collection<String> listUserDomainMappings(String user, String domain) throws Exception;
+
+    /**
+     * Remove regex mapping.
+     * 
+     * @param user
+     *            The username, or null if no username should be used.
+     * @param domain
+     *            The domain, or null if no domain should be used.
+     * @param regex
+     *            The regex.
+     * @throws Exception
+     */
+    public void addRegexMapping(String user, String domain, String regex) throws Exception;
+
+    /**
+     * Remove regex mapping.
+     * 
+     * @param user
+     *            The username, or null if no username should be used.
+     * @param domain
+     *            The domain, or null if no domain should be used.
+     * @param regex
+     *            The regex.
+     * @throws Exception
+     */
+    public void removeRegexMapping(String user, String domain, String regex) throws Exception;
+}

Added: james/server/trunk/cli/src/main/java/org/apache/james/cli/probe/impl/JmxServerProbe.java
URL: http://svn.apache.org/viewvc/james/server/trunk/cli/src/main/java/org/apache/james/cli/probe/impl/JmxServerProbe.java?rev=1089000&view=auto
==============================================================================
--- james/server/trunk/cli/src/main/java/org/apache/james/cli/probe/impl/JmxServerProbe.java (added)
+++ james/server/trunk/cli/src/main/java/org/apache/james/cli/probe/impl/JmxServerProbe.java Tue Apr  5 12:31:30 2011
@@ -0,0 +1,224 @@
+/****************************************************************
+ * 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.cli.probe.impl;
+
+import java.io.IOException;
+import java.util.Collection;
+import java.util.Map;
+
+import javax.management.MBeanServerConnection;
+import javax.management.MBeanServerInvocationHandler;
+import javax.management.MalformedObjectNameException;
+import javax.management.ObjectName;
+import javax.management.remote.JMXConnector;
+import javax.management.remote.JMXConnectorFactory;
+import javax.management.remote.JMXServiceURL;
+
+import org.apache.james.cli.probe.ServerProbe;
+import org.apache.james.domainlist.api.DomainListManagementMBean;
+import org.apache.james.user.api.UsersRepositoryManagementMBean;
+import org.apache.james.vut.api.VirtualUserTableManagementMBean;
+
+public class JmxServerProbe implements ServerProbe {
+
+    // TODO: Move this to somewhere else
+    private final static String DOMAINLIST_OBJECT_NAME = "org.apache.james:type=component,name=domainlist";
+    private final static String VIRTUALUSERTABLE_OBJECT_NAME = "org.apache.james:type=component,name=virtualusertable";
+    private final static String USERSREPOSITORY_OBJECT_NAME = "org.apache.james:type=component,name=usersrepository";
+
+    private MBeanServerConnection mbeanServerConn;
+    private DomainListManagementMBean domainListProcxy;
+    private VirtualUserTableManagementMBean virtualUserTableProxy;
+    private UsersRepositoryManagementMBean usersRepositoryProxy;
+
+    private static final String fmtUrl = "service:jmx:rmi:///jndi/rmi://%s:%d/jmxrmi";
+    private static final int defaultPort = 9999;
+    private String host;
+    private int port;
+
+    /**
+     * Creates a ServerProbe using the specified JMX host and port.
+     * 
+     * @param host
+     *            hostname or IP address of the JMX agent
+     * @param port
+     *            TCP port of the remote JMX agent
+     * @throws IOException
+     *             on connection failures
+     */
+    public JmxServerProbe(String host, int port) throws IOException, InterruptedException {
+        this.host = host;
+        this.port = port;
+        connect();
+    }
+
+    /**
+     * Creates a NodeProbe using the specified JMX host and default port.
+     * 
+     * @param host
+     *            hostname or IP address of the JMX agent
+     * @throws IOException
+     *             on connection failures
+     */
+    public JmxServerProbe(String host) throws IOException, InterruptedException {
+        this.host = host;
+        this.port = defaultPort;
+        connect();
+    }
+
+    /*
+     * Create a connection to the JMX agent and setup the M[X]Bean proxies.
+     * 
+     * @throws IOException
+     *             on connection failures
+     */
+    private void connect() throws IOException {
+        JMXServiceURL jmxUrl = new JMXServiceURL(String.format(fmtUrl, host, port));
+        JMXConnector jmxc = JMXConnectorFactory.connect(jmxUrl, null);
+        mbeanServerConn = jmxc.getMBeanServerConnection();
+
+        try {
+            ObjectName name = new ObjectName(DOMAINLIST_OBJECT_NAME);
+            domainListProcxy = (DomainListManagementMBean) MBeanServerInvocationHandler.newProxyInstance(mbeanServerConn, name, DomainListManagementMBean.class, true);
+            name = new ObjectName(VIRTUALUSERTABLE_OBJECT_NAME);
+            virtualUserTableProxy = (VirtualUserTableManagementMBean) MBeanServerInvocationHandler.newProxyInstance(mbeanServerConn, name, VirtualUserTableManagementMBean.class, true);
+            name = new ObjectName(USERSREPOSITORY_OBJECT_NAME);
+            usersRepositoryProxy = (UsersRepositoryManagementMBean) MBeanServerInvocationHandler.newProxyInstance(mbeanServerConn, name, UsersRepositoryManagementMBean.class, true);
+        } catch (MalformedObjectNameException e) {
+            throw new RuntimeException("Invalid ObjectName? Please report this as a bug.", e);
+        }
+    }
+
+    /*
+     * (non-Javadoc)
+     * 
+     * @see org.apache.james.cli.probe.ServerProbe#addUser(java.lang.String, java.lang.String)
+     */
+    public void addUser(String userName, String password) throws Exception {
+        usersRepositoryProxy.addUser(userName, password);
+    }
+
+    /* 
+     * (non-Javadoc)
+     * 
+     * @see org.apache.james.cli.probe.ServerProbe#removeUser(java.lang.String)
+     */
+    public void removeUser(String username) throws Exception {
+        usersRepositoryProxy.deleteUser(username);
+    }
+
+    /* 
+     * (non-Javadoc)
+     * 
+     * @see org.apache.james.cli.probe.ServerProbe#listUsers()
+     */
+    public String[] listUsers() throws Exception {
+        return usersRepositoryProxy.listAllUsers();
+    }
+
+    /* 
+     * (non-Javadoc)
+     * 
+     * @see org.apache.james.cli.probe.ServerProbe#setPassword(java.lang.String, java.lang.String)
+     */
+    public void setPassword(String userName, String password) throws Exception {
+        usersRepositoryProxy.setPassword(userName, password);
+    }
+
+    /* 
+     * (non-Javadoc)
+     * 
+     * @see org.apache.james.cli.probe.ServerProbe#addDomain(java.lang.String)
+     */
+    public void addDomain(String domain) throws Exception {
+        domainListProcxy.addDomain(domain);
+    }
+
+    /* 
+     * (non-Javadoc)
+     * 
+     * @see org.apache.james.cli.probe.ServerProbe#removeDomain(java.lang.String)
+     */
+    public void removeDomain(String domain) throws Exception {
+        domainListProcxy.removeDomain(domain);
+    }
+
+    /* 
+     * (non-Javadoc)
+     * 
+     * @see org.apache.james.cli.probe.ServerProbe#listDomains()
+     */
+    public String[] listDomains() throws Exception {
+        return domainListProcxy.getDomains();
+    }
+
+    /* 
+     * (non-Javadoc)
+     * 
+     * @see org.apache.james.cli.probe.ServerProbe#listMappings()
+     */
+    public Map<String, Collection<String>> listMappings() throws Exception {
+        return virtualUserTableProxy.getAllMappings();
+    }
+
+    /* 
+     * (non-Javadoc)
+     * 
+     * @see org.apache.james.cli.probe.ServerProbe#addAddressMapping(java.lang.String, java.lang.String, java.lang.String)
+     */
+    public void addAddressMapping(String user, String domain, String toAddress) throws Exception {
+        virtualUserTableProxy.addAddressMapping(user, domain, toAddress);
+    }
+
+    /* 
+     * (non-Javadoc)
+     * 
+     * @see org.apache.james.cli.probe.ServerProbe#removeAddressMapping(java.lang.String, java.lang.String, java.lang.String)
+     */
+    public void removeAddressMapping(String user, String domain, String fromAddress) throws Exception {
+        virtualUserTableProxy.removeAddressMapping(user, domain, fromAddress);
+    }
+
+    /* 
+     * (non-Javadoc)
+     * 
+     * @see org.apache.james.cli.probe.ServerProbe#listUserDomainMappings(java.lang.String, java.lang.String)
+     */
+    public Collection<String> listUserDomainMappings(String user, String domain) throws Exception {
+        return virtualUserTableProxy.getUserDomainMappings(user, domain);
+    }
+
+    /* 
+     * (non-Javadoc)
+     * 
+     * @see org.apache.james.cli.probe.ServerProbe#addRegexMapping(java.lang.String, java.lang.String, java.lang.String)
+     */
+    public void addRegexMapping(String user, String domain, String regex) throws Exception {
+        virtualUserTableProxy.addRegexMapping(user, domain, regex);
+    }
+
+    /* 
+     * (non-Javadoc)
+     * 
+     * @see org.apache.james.cli.probe.ServerProbe#removeRegexMapping(java.lang.String, java.lang.String, java.lang.String)
+     */
+    public void removeRegexMapping(String user, String domain, String regex) throws Exception {
+        virtualUserTableProxy.removeRegexMapping(user, domain, regex);
+    }
+}

Added: james/server/trunk/cli/src/main/java/org/apache/james/cli/type/CmdType.java
URL: http://svn.apache.org/viewvc/james/server/trunk/cli/src/main/java/org/apache/james/cli/type/CmdType.java?rev=1089000&view=auto
==============================================================================
--- james/server/trunk/cli/src/main/java/org/apache/james/cli/type/CmdType.java (added)
+++ james/server/trunk/cli/src/main/java/org/apache/james/cli/type/CmdType.java Tue Apr  5 12:31:30 2011
@@ -0,0 +1,88 @@
+/****************************************************************
+ * 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.cli.type;
+
+/**
+ * Enumeration of valid command types.
+ */
+public enum CmdType {
+    ADDUSER("adduser", 3), 
+    REMOVEUSER("removeuser", 2), 
+    LISTUSERS("listusers", 1), 
+    ADDDOMAIN("adddomain", 2), 
+    REMOVEDOMAIN("removedomain", 2), 
+    LISTDOMAINS("listdomains", 1);
+
+    private String command;
+    private int arguments;
+
+    private CmdType(String command, int arguments) {
+        this.command = command;
+        this.arguments = arguments;
+    }
+
+    /**
+     * Validate that the number of arguments match the passed value.
+     * 
+     * @param arguments
+     *            The number of argument to compare.
+     * @return true if values match, false otherwise.
+     */
+    public boolean hasCorrectArguments(int arguments) {
+        if (this.arguments == arguments)
+            return true;
+
+        return false;
+    }
+
+    /**
+     * Return a CmdType enumeration that matches the passed command.
+     * 
+     * @param command
+     *            The command to use for lookup.
+     * @return the CmdType enumeration that matches the passed command, or null
+     *         if not found.
+     */
+    public static CmdType lookup(String command) {
+        if (command != null) {
+            for (CmdType cmd : values())
+                if (cmd.getCommand().equalsIgnoreCase(command))
+                    return cmd;
+        }
+        return null;
+    }
+    
+    /**
+     * Return the value of command.
+     * 
+     * @return the value of command.
+     */
+    public String getCommand() {
+        return this.command;
+    }
+
+    /**
+     * Return the value of arguments.
+     * 
+     * @return the value of arguments.
+     */
+    public int getArguments() {
+        return this.arguments;
+    }
+}

Added: james/server/trunk/cli/src/test/java/org/apache/james/cli/type/CmdTypeTest.java
URL: http://svn.apache.org/viewvc/james/server/trunk/cli/src/test/java/org/apache/james/cli/type/CmdTypeTest.java?rev=1089000&view=auto
==============================================================================
--- james/server/trunk/cli/src/test/java/org/apache/james/cli/type/CmdTypeTest.java (added)
+++ james/server/trunk/cli/src/test/java/org/apache/james/cli/type/CmdTypeTest.java Tue Apr  5 12:31:30 2011
@@ -0,0 +1,86 @@
+/****************************************************************
+ * 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.cli.type;
+
+import junit.framework.TestCase;
+
+/**
+ * Test class for the CmdType enum.
+ */
+public class CmdTypeTest extends TestCase {
+    
+    /**
+     * Test the hasCorrectArguments method.
+     */
+    public void testHasCorrectArguments() {
+        CmdType cmd;
+        boolean result;
+
+        cmd = CmdType.ADDDOMAIN;
+
+        // Test bogus number
+        result = cmd.hasCorrectArguments(-1);
+        assertEquals(false, result);
+
+        // Test actual number
+        result = cmd.hasCorrectArguments(cmd.getArguments());
+        assertEquals(true, result);
+
+        // Test known bad number
+        result = cmd.hasCorrectArguments(cmd.getArguments() - 1);
+        assertEquals(false, result);
+    }
+
+    /**
+     * Test the lookup method.
+     */
+    public void testLookup() {
+        CmdType result;
+
+        // Test happy path
+        result = CmdType.lookup(CmdType.ADDUSER.getCommand());
+        assertEquals(CmdType.ADDUSER, result);
+
+        result = CmdType.lookup(CmdType.REMOVEUSER.getCommand());
+        assertEquals(CmdType.REMOVEUSER, result);
+
+        result = CmdType.lookup(CmdType.LISTUSERS.getCommand());
+        assertEquals(CmdType.LISTUSERS, result);
+
+        result = CmdType.lookup(CmdType.ADDDOMAIN.getCommand());
+        assertEquals(CmdType.ADDDOMAIN, result);
+
+        result = CmdType.lookup(CmdType.REMOVEDOMAIN.getCommand());
+        assertEquals(CmdType.REMOVEDOMAIN, result);
+
+        result = CmdType.lookup(CmdType.LISTDOMAINS.getCommand());
+        assertEquals(CmdType.LISTDOMAINS, result);
+
+        // Test known bad value
+        result = CmdType.lookup("");
+        assertEquals(null, result);
+
+        result = CmdType.lookup("error");
+        assertEquals(null, result);
+
+        // Test null value
+        result = CmdType.lookup(null);
+        assertEquals(null, result);
+    }
+}
\ No newline at end of file



---------------------------------------------------------------------
To unsubscribe, e-mail: server-dev-unsubscribe@james.apache.org
For additional commands, e-mail: server-dev-help@james.apache.org