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.shell.impl.command.spi; 020 021import org.crsh.plugin.CRaSHPlugin; 022import org.crsh.plugin.PluginContext; 023 024import java.lang.reflect.Constructor; 025import java.util.HashMap; 026import java.util.Set; 027import java.util.concurrent.atomic.AtomicReference; 028 029/** 030 * A command manager that is able to load a command manager via reflection. 031 * 032 * @author Julien Viet 033 */ 034public class CommandManagerProxy extends CRaSHPlugin<CommandManager> implements CommandManager { 035 036 /** . */ 037 private final AtomicReference<CommandManager> real = new AtomicReference<CommandManager>(); 038 039 /** . */ 040 private final String name; 041 042 /** . */ 043 private final String className; 044 045 /** . */ 046 private final Set<String> ext; 047 048 public CommandManagerProxy(String name, String className, Set<String> ext) { 049 this.name = name; 050 this.className = className; 051 this.ext = ext; 052 } 053 054 @Override 055 public CommandManager getImplementation() { 056 return this; 057 } 058 059 @Override 060 public void init() { 061 try { 062 Class<CommandManager> mgrClass = (Class<CommandManager>)CommandManagerProxy.class.getClassLoader().loadClass(className); 063 Constructor<CommandManager> mgrCtor = mgrClass.getConstructor(PluginContext.class); 064 CommandManager mgr = mgrCtor.newInstance(getContext()); 065 real.set(mgr); 066 } 067 catch (Exception e) { 068 log.info("Plugin is inactive"); 069 } 070 catch (NoClassDefFoundError e) { 071 log.info("Plugin is inactive"); 072 } 073 } 074 075 @Override 076 public boolean isActive() { 077 return real.get() != null; 078 } 079 080 @Override 081 public Set<String> getExtensions() { 082 return ext; 083 } 084 085 @Override 086 public CommandResolution resolveCommand(String name, byte[] source) throws CommandCreationException, NullPointerException { 087 CommandManager mgr = real.get(); 088 if (mgr != null) { 089 return mgr.resolveCommand(name, source); 090 } else { 091 throw new IllegalStateException(name + " command manager is not available"); 092 } 093 } 094 095 @Override 096 public void init(HashMap<String, Object> session) { 097 CommandManager mgr = real.get(); 098 if (mgr != null) { 099 mgr.init(session); 100 } else { 101 throw new IllegalStateException(name + " command manager is not available"); 102 } 103 } 104 105 @Override 106 public void destroy(HashMap<String, Object> session) { 107 CommandManager mgr = real.get(); 108 if (mgr != null) { 109 mgr.destroy(session); 110 } else { 111 throw new IllegalStateException(name + " command manager is not available"); 112 } 113 } 114 115 @Override 116 public String doCallBack(HashMap<String, Object> session, String name, String defaultValue) { 117 CommandManager mgr = real.get(); 118 if (mgr != null) { 119 return mgr.doCallBack(session, name, defaultValue); 120 } else { 121 throw new IllegalStateException(name + " command manager is not available"); 122 } 123 } 124}