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     */
019    package org.crsh.lang.impl.java;
020    
021    import org.crsh.cli.descriptor.CommandDescriptor;
022    import org.crsh.cli.impl.descriptor.HelpDescriptor;
023    import org.crsh.cli.impl.descriptor.IntrospectionException;
024    import org.crsh.cli.impl.invocation.InvocationMatch;
025    import org.crsh.cli.impl.lang.CommandFactory;
026    import org.crsh.cli.impl.lang.Instance;
027    import org.crsh.cli.impl.lang.ObjectCommandInvoker;
028    import org.crsh.cli.spi.Completer;
029    import org.crsh.command.BaseCommand;
030    import org.crsh.shell.ErrorKind;
031    import org.crsh.shell.impl.command.spi.Command;
032    import org.crsh.shell.impl.command.spi.CommandException;
033    import org.crsh.shell.impl.command.spi.CommandMatch;
034    import org.crsh.command.InvocationContext;
035    import org.crsh.command.Pipe;
036    import org.crsh.command.RuntimeContext;
037    import org.crsh.util.Utils;
038    
039    import java.lang.reflect.InvocationTargetException;
040    import java.lang.reflect.Type;
041    
042    /** @author Julien Viet */
043    public class ClassShellCommand<T extends BaseCommand> extends Command<Instance<T>> {
044    
045      /** . */
046      private final Class<T> clazz;
047    
048      /** . */
049      private final CommandDescriptor<Instance<T>> descriptor;
050    
051      public ClassShellCommand(Class<T> clazz) throws IntrospectionException {
052        CommandFactory factory = new CommandFactory(getClass().getClassLoader());
053        this.clazz = clazz;
054        this.descriptor = HelpDescriptor.create(factory.create(clazz));
055      }
056    
057      public CommandDescriptor<Instance<T>> getDescriptor() {
058        return descriptor;
059      }
060    
061      protected Completer getCompleter(final RuntimeContext context) throws CommandException {
062        final T command = createCommand();
063        if (command instanceof Completer) {
064          command.context = context;
065          return (Completer)command;
066        } else {
067          return null;
068        }
069      }
070    
071      @Override
072      protected CommandMatch<?, ?> resolve(InvocationMatch<Instance<T>> match) {
073    
074        // Cast to the object invoker
075        org.crsh.cli.impl.invocation.CommandInvoker<Instance<T>,?> invoker = match.getInvoker();
076    
077        // Do we have a pipe command or not ?
078        if (Pipe.class.isAssignableFrom(invoker.getReturnType())) {
079          org.crsh.cli.impl.invocation.CommandInvoker tmp = invoker;
080          return getPipeInvoker(tmp);
081        } else {
082    
083          // Determine the produced type
084          Class<?> producedType;
085          if (void.class.equals(invoker.getReturnType())) {
086            producedType = Object.class;
087          } else {
088            producedType = invoker.getReturnType();
089          }
090    
091          // Override produced type from InvocationContext<P> if any
092          if (invoker instanceof ObjectCommandInvoker) {
093            ObjectCommandInvoker<T, ?> objectInvoker = (ObjectCommandInvoker<T, ?>)invoker;
094            Class<?>[] parameterTypes = objectInvoker.getParameterTypes();
095            for (int i = 0;i < parameterTypes.length;i++) {
096              Class<?> parameterType = parameterTypes[i];
097              if (InvocationContext.class.isAssignableFrom(parameterType)) {
098                Type contextGenericParameterType = objectInvoker.getGenericParameterTypes()[i];
099                producedType = Utils.resolveToClass(contextGenericParameterType, InvocationContext.class, 0);
100                break;
101              }
102            }
103          }
104    
105          //
106          return getProducerInvoker(invoker, producedType);
107        }
108      }
109    
110      T createCommand() throws CommandException {
111        T command;
112        try {
113          command = clazz.getConstructor().newInstance();
114        }
115        catch (InvocationTargetException e) {
116          String name = clazz.getSimpleName();
117          throw new CommandException(ErrorKind.EVALUATION, "Could not create command " + name + " instance", e.getCause());
118        }
119        catch (Exception e) {
120          String name = clazz.getSimpleName();
121          throw new CommandException(ErrorKind.INTERNAL, "Could not create command " + name + " instance", e);
122        }
123        return command;
124      }
125    
126      private <C, P, PC extends Pipe<C, P>> CommandMatch<C, P> getPipeInvoker(final org.crsh.cli.impl.invocation.CommandInvoker<Instance<T>, PC> invoker) {
127        return new PipeCommandMatch<T, C, P, PC>(this, invoker);
128      }
129    
130      private <P> CommandMatch<Void, P> getProducerInvoker(final org.crsh.cli.impl.invocation.CommandInvoker<Instance<T>, ?> invoker, final Class<P> producedType) {
131        return new ProducerCommandMatch<T, P>(this, invoker, producedType);
132      }
133    
134    }