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 */ 019 package org.crsh.lang.impl.groovy; 020 021 import groovy.lang.Binding; 022 import groovy.lang.GroovySystem; 023 import org.codehaus.groovy.runtime.InvokerHelper; 024 import org.crsh.lang.impl.groovy.command.GroovyScript; 025 import org.crsh.lang.spi.Language; 026 import org.crsh.lang.spi.Repl; 027 import org.crsh.plugin.PluginContext; 028 import org.crsh.plugin.ResourceKind; 029 import org.crsh.shell.impl.command.ShellSession; 030 import org.crsh.shell.impl.command.spi.CommandException; 031 import org.crsh.util.ClassCache; 032 import org.crsh.util.TimestampedObject; 033 034 /** 035 * @author Julien Viet 036 */ 037 public class GroovyLanguage implements Language { 038 039 /** . */ 040 private ClassCache<GroovyScript> scriptCache; 041 042 /** . */ 043 private GroovyRepl repl; 044 045 /** . */ 046 private GroovyCompiler compiler; 047 048 public GroovyLanguage(PluginContext context) { 049 compiler = new GroovyCompiler(context); 050 repl = new GroovyRepl(this); 051 scriptCache = new ClassCache<GroovyScript>(context, new GroovyClassFactory<GroovyScript>(context.getLoader(), GroovyScript.class, GroovyScript.class), ResourceKind.LIFECYCLE); 052 } 053 054 public String getName() { 055 return "groovy"; 056 } 057 058 @Override 059 public String getDisplayName() { 060 return "Groovy " + GroovySystem.getVersion(); 061 } 062 063 @Override 064 public boolean isActive() { 065 return true; 066 } 067 068 @Override 069 public Repl getRepl() { 070 return repl; 071 } 072 073 @Override 074 public org.crsh.lang.spi.Compiler getCompiler() { 075 return compiler; 076 } 077 078 @Override 079 public void init(ShellSession session) { 080 try { 081 GroovyScript login = getLifeCycle(session, "login"); 082 if (login != null) { 083 login.setBinding(new Binding(session)); 084 login.run(); 085 } 086 } 087 catch (CommandException e) { 088 e.printStackTrace(); 089 } 090 } 091 092 @Override 093 public void destroy(ShellSession session) { 094 try { 095 GroovyScript logout = getLifeCycle(session, "logout"); 096 if (logout != null) { 097 logout.setBinding(new Binding(session)); 098 logout.run(); 099 } 100 } 101 catch (CommandException e) { 102 e.printStackTrace(); 103 } 104 } 105 106 public GroovyScript getLifeCycle(ShellSession session, String name) throws CommandException, NullPointerException { 107 TimestampedObject<Class<? extends GroovyScript>> ref = scriptCache.getClass(name); 108 if (ref != null) { 109 Class<? extends GroovyScript> scriptClass = ref.getObject(); 110 GroovyScript script = (GroovyScript)InvokerHelper.createScript(scriptClass, new Binding(session)); 111 script.setBinding(new Binding(session)); 112 return script; 113 } else { 114 return null; 115 } 116 } 117 118 }