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