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