
uk.org.retep.microkernel.shell.KernelShell Maven / Gradle / Ivy
/*
* Copyright (c) 1998-2010, Peter T Mount
* All rights reserved.
*
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 3 of the License, or
* (at your option) any later version.
*
*
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
*
*
* You should have received a copy of the GNU General Public License along
* with this program. If not, see .
*
*
*
* GNU GENERAL PUBLIC LICENSE - CLASSPATH EXCEPTION
*
*
*
* Linking this library statically or dynamically with other modules
* is making a combined work based on this library. Thus, the terms
* and conditions of the GNU General Public License cover the whole
* combination.
*
*
*
* As a special exception, the copyright holders of this library give
* you permission to link this library with independent modules to
* produce an executable, regardless of the license terms of these
* independent modules, and to copy and distribute the resulting
* executable under terms of your choice, provided that you also meet,
* for each linked independent module, the terms and conditions of the
* license of that module.
*
*
*
* An independent module is a module which is either not derived from or based
* on this library, or a module who's classes extend those within this library
* as part of the implementation of the library.
*
*
*
* If you modify this library, you may extend this exception to your version
* of the library, but you are not obligated to do so. If you do not wish to
* do so, delete this exception statement from your version.
*
*/
package uk.org.retep.microkernel.shell;
import groovy.lang.Binding;
import groovy.lang.GroovyShell;
import groovy.lang.Script;
import java.io.Console;
import java.io.File;
import java.io.IOException;
import java.util.Collection;
import java.util.Iterator;
import java.util.Map;
import javax.annotation.Nonnull;
import org.codehaus.groovy.control.CompilationFailedException;
import uk.org.retep.logging.LogSupport;
import uk.org.retep.util.services.Service;
import uk.org.retep.util.string.StringUtils;
/**
* A Groovy shell loosely based on {@link groovy.ui.KernelShell}
* @author peter
*/
public class KernelShell
extends LogSupport
{
private static final String GLOBAL_END = "\nstatic main(String[] args){}\n}";
private static final String GLOBAL_START = "class Global {\n";
private static final String GROOVY = ".groovy";
private static final String LOG_EXECUTE = "execute: %s";
private static final String LOG_RESULT = "result: %s";
private static final String NO_CONSOLE_AVAILABLE = "No console available";
private static final String SHELL = "shell";
//
private final KernelShellModule module;
private final Console console;
private final GroovyShell shell;
private File scriptHome;
/***
* Default constructor.
* @param module The parent module
*/
KernelShell( @Nonnull final KernelShellModule module )
{
this.module = module;
console = System.console();
if( console == null )
{
throw new NullPointerException( NO_CONSOLE_AVAILABLE );
}
shell = new GroovyShell( new Binding() );
// This is in the groovy example so it's here
@SuppressWarnings( "unchecked" )
final Map variables = shell.getContext().getVariables();
if( variables.get( SHELL ) != null )
{
variables.put( SHELL, shell );
}
// Provide access to this class to scripts
GlobalAdapter.getInstance().setShell( this );
generateGlobal();
}
final GroovyShell getShell()
{
return shell;
}
final KernelShellModule getModule()
{
return module;
}
final Console getConsole()
{
return console;
}
private void generateGlobal()
{
// Preload any imports etc from a services file
final Collection globalClass = Service.providerClassNames(
KernelShell.class.getName(),
Thread.currentThread().getContextClassLoader() );
final Iterator it = globalClass.iterator();
while( it.hasNext() )
{
final String imp = it.next();
// Remove blank or lines beginning with #
if( StringUtils.isStringEmpty( imp ) || imp.charAt( 0 ) == '#' )
{
it.remove();
}
}
// Evaluate to generate the Global class
evaluate(
GLOBAL_START + StringUtils.join( '\n', globalClass ) + GLOBAL_END );
}
public Object call( @Nonnull final String script )
throws CompilationFailedException,
IOException
{
final String scriptName = script.endsWith( GROOVY ) ? script : (script + GROOVY);
// locate the script in the current directory (or if it's absolute)
File scriptFile = new File( scriptName );
// Don't hunt if the supplied script name is absolute
if( !scriptFile.exists() && !scriptFile.isAbsolute() )
{
// Look in script directory
if( !scriptFile.exists() )
{
scriptFile = new File( module.getScriptDir(), scriptName );
}
// If we have a previous script file reference, look in that directory
if( !scriptFile.exists() && scriptHome != null )
{
scriptFile = new File( scriptHome, scriptName );
}
}
if( scriptFile.exists() )
{
return evaluate( scriptFile );
}
else
{
throw new ScriptNotFoundException( script );
}
}
public Object evaluate( @Nonnull final String command )
{
getLog().debug( LOG_EXECUTE, command );
return evaluate( shell.parse( command ) );
}
public Object evaluate( @Nonnull final File file )
throws CompilationFailedException,
IOException
{
getLog().debug( LOG_EXECUTE, file.getAbsolutePath() );
if( !file.exists() )
{
throw new ScriptNotFoundException( file );
}
// set scriptHome so we can call scripts in the same directory
if( scriptHome == null )
{
scriptHome = file.getParentFile();
}
return evaluate( shell.parse( file ) );
}
public Object evaluate( @Nonnull final Script script )
{
script.setBinding( shell.getContext() );
final Object result = script.run();
if( getLog().isDebugEnabled() )
{
getLog().debug( LOG_RESULT, result );
}
return result;
}
public void setVariable( final String name, final Object value )
{
shell.setVariable( name, value );
}
public Object getVariable( final String name )
{
return shell.getVariable( name );
}
}