Package org.crsh.shell.impl.command.spi

Examples of org.crsh.shell.impl.command.spi.CommandException


    try {
      command = clazz.getConstructor().newInstance();
    }
    catch (InvocationTargetException e) {
      String name = clazz.getSimpleName();
      throw new CommandException(ErrorKind.EVALUATION, "Could not create command " + name + " instance", e.getCause());
    }
    catch (Exception e) {
      String name = clazz.getSimpleName();
      throw new CommandException(ErrorKind.INTERNAL, "Could not create command " + name + " instance", e);
    }
    return command;
  }
View Full Code Here


    List<JavaClassFileObject> classFiles;
    try {
      classFiles = compiler.compile(name, script);
    }
    catch (IOException e) {
      throw new CommandException(ErrorKind.INTERNAL, "Could not access command", e);
    }
    catch (CompilationFailureException e) {
        throw new CommandException(ErrorKind.INTERNAL, "Could not compile command", e);
    }
    for (JavaClassFileObject classFile : classFiles) {
      String className = classFile.getClassName();
      String simpleName = className.substring(className.lastIndexOf('.') + 1);
      if (simpleName.equals(name)) {
        LoadingClassLoader loader = new LoadingClassLoader(this.loader, classFiles);
        try {
          Class<?> clazz = loader.loadClass(classFile.getClassName());
          final ClassShellCommand command;
          try {
            command = new ClassShellCommand(clazz);
          }
          catch (IntrospectionException e) {
            throw new CommandException(ErrorKind.INTERNAL, "Invalid cli annotations", e);
          }
          final String description = command.describe(name, Format.DESCRIBE);
          return new CommandResolution() {
            @Override
            public String getDescription() {
              return description;
            }
            @Override
            public Command<Object> getCommand() throws CommandException {
              return command;
            }
          };
        }
        catch (ClassNotFoundException e) {
          throw new CommandException(ErrorKind.INTERNAL, "Command cannot be loaded", e);
        }
      }
    }
    throw new CommandException(ErrorKind.INTERNAL, "Command class not found");
  }
View Full Code Here

              };
            }
          };
        }
        catch (IntrospectionException e) {
          throw new CommandException(ErrorKind.SYNTAX, "Script " + name + " failed unexpectedly", e);
        }

        return new Command<Object>() {
          @Override
          public CommandDescriptor<Object> getDescriptor() {
            return descriptor;
          }
          @Override
          protected Completer getCompleter(RuntimeContext context) {
            return null;
          }
          @Override
          protected CommandMatch<?, ?> resolve(InvocationMatch<Object> match) {
            return new CommandMatch<Void, Object>() {
              @Override
              public CommandInvoker<Void, Object> getInvoker() {
                return new CommandInvoker<Void, Object>() {

                  /** . */
                  private CommandContext<?> consumer;

                  @Override
                  public void provide(Void element) throws IOException {
                  }

                  @Override
                  public Class<Void> getConsumedType() {
                    return Void.class;
                  }

                  @Override
                  public void flush() throws IOException {
                    consumer.flush();
                  }

                  @Override
                  public Class<Object> getProducedType() {
                    return Object.class;
                  }

                  @Override
                  public void open(CommandContext<? super Object> consumer) {
                    this.consumer = consumer;
                  }

                  @Override
                  public void close() throws IOException, CommandException {

                    // Execute sequentially the script
                    BufferedReader reader = new BufferedReader(new InputStreamReader(new ByteArrayInputStream(source)));

                    // A bit nasty but well it's ok
                    ShellSession session = (ShellSession)consumer.getSession();

                    while (true) {
                      String request = reader.readLine();
                      if (request == null) {
                        break;
                      }
                      request = request.trim();
                      if (request.length() == 0) {
                        break;
                      }
                      ReplResponse response = ScriptRepl.getInstance().eval(session, request);
                      if (response instanceof ReplResponse.Response) {
                        ReplResponse.Response shellResponse = (ReplResponse.Response)response;
                        Exception ex = new Exception("Was not expecting response " + shellResponse.response);
                        throw new CommandException(ErrorKind.EVALUATION, "Failure when evaluating '" + request + "'  in script " + name, ex);
                      } else if (response instanceof ReplResponse.Invoke) {
                        ReplResponse.Invoke invokeResponse = (ReplResponse.Invoke)response;
                        CommandInvoker invoker =  invokeResponse.invoker;
                        invoker.invoke(consumer);
                      }
View Full Code Here

        String source;
        try {
          source = new String(script.getContent(), "UTF-8");
        }
        catch (UnsupportedEncodingException e) {
          throw new CommandException(ErrorKind.INTERNAL, "Could not compile command script " + name, e);
        }

        //
        Class<? extends T> clazz = classFactory.parse(name, source);
        providerRef = new TimestampedObject<Class<? extends T>>(script.getTimestamp(), clazz);
View Full Code Here

    if (next != null) {
      PipeLineFactory nextFactory = next.createFactory();
      if (nextFactory != null) {
        return new PipeLineFactory(value, nextFactory);
      } else {
        throw new CommandException(ErrorKind.SYNTAX, "");
      }
    } else {
      return Utils.notBlank(value) ? new PipeLineFactory(value, null) : null;
    }
  }
View Full Code Here

      GroovyCodeSource gcs = new GroovyCodeSource(source, name, "/groovy/shell");
      GroovyClassLoader gcl = new GroovyClassLoader(baseLoader, config);
      clazz = gcl.parseClass(gcs, false);
    }
    catch (NoClassDefFoundError e) {
      throw new CommandException(ErrorKind.INTERNAL, "Could not compile command script " + name, e);
    }
    catch (CompilationFailedException e) {
      throw new CommandException(ErrorKind.INTERNAL, "Could not compile command script " + name, e);
    }

    if (baseClass.isAssignableFrom(clazz)) {
      return clazz.asSubclass(baseClass);
    } else {
      throw new CommandException(ErrorKind.INTERNAL, "Parsed script " + clazz.getName() +
          " does not implements " + baseClass.getName());
    }
  }
View Full Code Here

          }
          catch (CommandException e) {
            throw e;
          }
          catch (Exception e) {
            throw new CommandException(ErrorKind.EVALUATION, "An error occured during the evalution of '" + request + "'", e);
          }
        }
      }
      public void close() throws IOException, CommandException {
        try {
          consumer.flush();
          consumer.close();
        }
        catch (Exception e) {
          throw new CommandException(ErrorKind.EVALUATION, "An error occured during the evalution of '" + request + "'", e);
        }
      }
    };
    return new ReplResponse.Invoke(invoker);
  }
View Full Code Here

        sb.append(' ');
        for (PipeLineFactory factory = next;factory != null;factory = factory.next) {
          sb.append('|').append(factory.line);
        }
      }
      throw new CommandException(ErrorKind.SYNTAX, sb.toString());
    }
  }
View Full Code Here

    final String script;
    try {
      script = new String(source, "UTF-8");
    }
    catch (UnsupportedEncodingException e) {
      throw new CommandException(ErrorKind.INTERNAL, "Could not compile command script " + name, e);
    }

    // Get the description using a partial compilation because it is much faster than compiling the class
    // the class will be compiled lazyly
    String resolveDescription = null;
    CompilationUnit cu = new CompilationUnit(objectGroovyClassFactory.config);
    cu.addSource(name, script);
    try {
      cu.compile(Phases.CONVERSION);
    }
    catch (CompilationFailedException e) {
      throw new CommandException(ErrorKind.INTERNAL, "Could not compile command", e);
    }
    CompileUnit ast = cu.getAST();
    if (ast.getClasses().size() > 0) {
      ClassNode classNode= (ClassNode)ast.getClasses().get(0);
      if (classNode != null) {
        for (AnnotationNode annotation : classNode.getAnnotations()) {
          if (annotation.getClassNode().getName().equals(Usage.class.getSimpleName())) {
            resolveDescription = annotation.getMember("value").getText();
            break;
          }
        }
        if (resolveDescription == null) {
          for (MethodNode main : classNode.getMethods("main")) {
            for (AnnotationNode annotation : main.getAnnotations()) {
              if (annotation.getClassNode().getName().equals(Usage.class.getSimpleName())) {
                resolveDescription = annotation.getMember("value").getText();
                break;
              }
            }
          }
        }
      }
    }
    final String description = resolveDescription;

    //
    return new CommandResolution() {
      Command<?> command;
      @Override
      public String getDescription() {
        return description;
      }
      @Override
      public Command<?> getCommand() throws CommandException {
        if (command == null) {
          Class<?> clazz = objectGroovyClassFactory.parse(name, script);
          if (BaseCommand.class.isAssignableFrom(clazz)) {
            Class<? extends BaseCommand> cmd = clazz.asSubclass(BaseCommand.class);
            try {
              command = make(cmd);
            }
            catch (IntrospectionException e) {
              throw new CommandException(ErrorKind.INTERNAL, "Invalid cli annotations for command " + name, e);
            }
          }
          else if (GroovyScriptCommand.class.isAssignableFrom(clazz)) {
            Class<? extends GroovyScriptCommand> cmd = clazz.asSubclass(GroovyScriptCommand.class);
            try {
              command = make2(cmd);
            }
            catch (IntrospectionException e) {
              throw new CommandException(ErrorKind.INTERNAL, "Invalid cli annotations for command " + name, e);
            }
          }
          else {
            throw new CommandException(ErrorKind.INTERNAL, "Could not create command " + name + " instance");
          }
        }
        return command;
      }
    };
View Full Code Here

  }

  private ShellResponse.Error build(Throwable throwable) {
    ErrorKind errorType;
    if (throwable instanceof CommandException) {
      CommandException ce = (CommandException)throwable;
      errorType = ce.getErrorKind();
      Throwable cause = throwable.getCause();
      if (cause != null) {
        throwable = cause;
      }
    } else {
View Full Code Here

TOP

Related Classes of org.crsh.shell.impl.command.spi.CommandException

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.