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.lang.java;
020
021import org.crsh.command.BaseShellCommand;
022import org.crsh.command.CommandCreationException;
023import org.crsh.command.DescriptionFormat;
024import org.crsh.command.ShellCommand;
025import org.crsh.plugin.CRaSHPlugin;
026import org.crsh.plugin.PluginContext;
027import org.crsh.shell.ErrorType;
028import org.crsh.shell.impl.command.CommandManager;
029import org.crsh.shell.impl.command.CommandResolution;
030
031import java.io.IOException;
032import java.util.Collections;
033import java.util.HashMap;
034import java.util.List;
035import java.util.Set;
036
037/** @author Julien Viet */
038public class JavaCommandManager extends CRaSHPlugin<CommandManager> implements CommandManager {
039
040  /** . */
041  private static final Set<String> EXT = Collections.singleton("java");
042
043  /** . */
044  private Compiler compiler;
045
046  @Override
047  public void init() {
048    PluginContext context = getContext();
049    ClassLoader loader = context.getLoader();
050    Compiler compiler = new Compiler(loader);
051
052    //
053    this.compiler = compiler;
054  }
055
056  @Override
057  public CommandManager getImplementation() {
058    return this;
059  }
060
061  public Set<String> getExtensions() {
062    return EXT;
063  }
064
065  public CommandResolution resolveCommand(String name, byte[] source) throws CommandCreationException, NullPointerException {
066    String script = new String(source);
067    List<JavaClassFileObject> classFiles;
068    try {
069      classFiles = compiler.compile(name, script);
070    }
071    catch (IOException e) {
072      throw new CommandCreationException(name, ErrorType.INTERNAL, "Could not access command", e);
073    }
074    catch (CompilationFailureException e) {
075        throw new CommandCreationException(name, ErrorType.EVALUATION, "Could not compile command", e);
076    }
077    for (JavaClassFileObject classFile : classFiles) {
078      String className = classFile.getClassName();
079      String simpleName = className.substring(className.lastIndexOf('.') + 1);
080      if (simpleName.equals(name)) {
081        LoadingClassLoader loader = new LoadingClassLoader(getContext().getLoader(), classFiles);
082        try {
083          Class<?> clazz = loader.loadClass(classFile.getClassName());
084          final BaseShellCommand command = new BaseShellCommand(clazz);
085          final String description = command.describe(name, DescriptionFormat.DESCRIBE);
086          return new CommandResolution() {
087            @Override
088            public String getDescription() {
089              return description;
090            }
091            @Override
092            public ShellCommand getCommand() throws CommandCreationException {
093              return command;
094            }
095          };
096        }
097        catch (ClassNotFoundException e) {
098          throw new CommandCreationException(name, ErrorType.EVALUATION, "Command cannot be loaded", e);
099        }
100      }
101    }
102    throw new CommandCreationException(name, ErrorType.EVALUATION, "Command class not found");
103  }
104
105  public void init(HashMap<String, Object> session) {
106    //
107  }
108
109  public void destroy(HashMap<String, Object> session) {
110    //
111  }
112
113  public String doCallBack(HashMap<String, Object> session, String name, String defaultValue) {
114    throw new UnsupportedOperationException("not yet implemented");
115  }
116}