View Javadoc

1   /****************************************************************
2    * Licensed to the Apache Software Foundation (ASF) under one   *
3    * or more contributor license agreements.  See the NOTICE file *
4    * distributed with this work for additional information        *
5    * regarding copyright ownership.  The ASF licenses this file   *
6    * to you under the Apache License, Version 2.0 (the            *
7    * "License"); you may not use this file except in compliance   *
8    * with the License.  You may obtain a copy of the License at   *
9    *                                                              *
10   *   http://www.apache.org/licenses/LICENSE-2.0                 *
11   *                                                              *
12   * Unless required by applicable law or agreed to in writing,   *
13   * software distributed under the License is distributed on an  *
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY       *
15   * KIND, either express or implied.  See the License for the    *
16   * specific language governing permissions and limitations      *
17   * under the License.                                           *
18   ****************************************************************/
19  
20  package org.apache.james.remotemanager;
21  
22  import java.io.PrintWriter;
23  
24  import org.apache.avalon.framework.logger.AbstractLogEnabled;
25  import org.apache.avalon.framework.logger.Logger;
26  
27  /**
28   * Registers remote manager commands.
29   */
30  public class CommandRegistry extends AbstractLogEnabled {
31  
32      private final Command[] commands;
33      
34      public CommandRegistry(final Command[] commands) {
35          this.commands = commands;
36      }
37      
38      public boolean execute(final String commandName, final String args, final PrintWriter out) {
39          boolean result = true;
40          for (int i=0; i<commands.length;i++) {
41              final Command command = commands[i];
42              if (commandName.equalsIgnoreCase(command.getName())) {
43                  final Logger logger = getLogger();
44                  if (logger != null) logger.debug("Found matching command");
45                  result = command.execute(args, out);
46              }
47          }
48          return result;
49      }
50      
51      public void printHelp(final PrintWriter out) {
52          for (int i=0; i<commands.length;i++) {
53              final Command command = commands[i];
54              final StringBuffer buffer = new StringBuffer(command.getName());
55              while (buffer.length() <= 39) {
56                  buffer.append(' ');
57              }
58              buffer.append(command.help());
59              out.println(buffer);
60          }
61      }
62  }