001/*
002 * Copyright (C) 2012 eXo Platform SAS.
003 *
004 * This is free software; you can redistribute it and/or modify it
005 * under the terms of the GNU Lesser General Public License as
006 * published by the Free Software Foundation; either version 2.1 of
007 * the License, or (at your option) any later version.
008 *
009 * This software is distributed in the hope that it will be useful,
010 * but WITHOUT ANY WARRANTY; without even the implied warranty of
011 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
012 * Lesser General Public License for more details.
013 *
014 * You should have received a copy of the GNU Lesser General Public
015 * License along with this software; if not, write to the Free
016 * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
017 * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
018 */
019package org.crsh.shell.impl.command.system;
020
021import org.crsh.cli.descriptor.Format;
022import org.crsh.cli.impl.descriptor.IntrospectionException;
023import org.crsh.command.BaseCommand;
024import org.crsh.shell.ErrorKind;
025import org.crsh.shell.impl.command.spi.CommandException;
026import org.crsh.lang.impl.java.ClassShellCommand;
027import org.crsh.shell.impl.command.spi.CommandResolver;
028import org.crsh.lang.spi.CommandResolution;
029import org.crsh.shell.impl.command.spi.Command;
030
031import java.util.HashMap;
032import java.util.Map;
033
034/**
035 * @author Julien Viet
036 */
037public class SystemResolver implements CommandResolver {
038
039  /** . */
040  public static final SystemResolver INSTANCE = new SystemResolver();
041
042  /** . */
043  private static final HashMap<String, Class<? extends BaseCommand>> commands = new HashMap<String, Class<? extends BaseCommand>>();
044
045  /** . */
046  private static final HashMap<String, String> descriptions = new HashMap<String, String>();
047
048  static {
049    commands.put("help", help.class);
050    commands.put("repl", repl.class);
051    descriptions.put("help", "provides basic help");
052    descriptions.put("repl", "list the repl or change the current repl");
053  }
054
055  private SystemResolver() {
056  }
057
058  @Override
059  public Iterable<Map.Entry<String, String>> getDescriptions() {
060    return descriptions.entrySet();
061  }
062
063  @Override
064  public Command<?> resolveCommand(String name) throws CommandException, NullPointerException {
065    final Class<? extends BaseCommand> systemCommand = commands.get(name);
066    if (systemCommand != null) {
067      return createCommand(systemCommand).getCommand();
068    }
069    return null;
070  }
071
072  private <C extends BaseCommand> CommandResolution createCommand(final Class<C> commandClass) throws CommandException {
073    final ClassShellCommand<C> shellCommand;
074    final String description;
075    try {
076      shellCommand = new ClassShellCommand<C>(commandClass);
077      description = shellCommand.describe(commandClass.getSimpleName(), Format.DESCRIBE);
078    }
079    catch (IntrospectionException e) {
080      throw new CommandException(ErrorKind.SYNTAX, "Invalid cli annotation in command " + commandClass.getSimpleName(), e);
081    }
082    return new CommandResolution() {
083      @Override
084      public String getDescription() {
085        return description;
086      }
087      @Override
088      public Command<?> getCommand() throws CommandException {
089        return shellCommand;
090      }
091    };
092  }
093}