in gremlin-console/src/main/groovy/org/apache/tinkerpop/gremlin/console/Console.groovy [69:181]
public Console(final IO io, final List<List<String>> scriptsAndArgs, final boolean interactive) {
this.io = io
this.interactive = interactive
if (!io.quiet) {
io.out.println()
io.out.println(" " + Colorizer.render(Preferences.gremlinColor, "\\,,,/"))
io.out.println(" " + Colorizer.render(Preferences.gremlinColor, "(o o)"))
io.out.println("" + Colorizer.render(Preferences.gremlinColor, "-----oOOo-(" + Gremlin.majorVersion() + ")-oOOo-----"))
}
final Mediator mediator = new Mediator(this)
// make sure that remotes are closed on jvm shutdown
addShutdownHook { mediator.close() }
// try to grab ctrl+c to interrupt an evaluation.
final Thread main = Thread.currentThread()
Signal.handle(new Signal("INT"), new SignalHandler() {
@Override
void handle(final Signal signal) {
if (mediator.evaluating.get()) {
io.out.println("Execution interrupted by ctrl+c")
main.interrupt()
}
}
})
groovy = new GremlinGroovysh(mediator, io)
def commandsToRemove = groovy.getRegistry().commands().findAll { it instanceof SetCommand }
commandsToRemove.each { groovy.getRegistry().remove(it) }
groovy.register(new GremlinSetCommand(groovy))
groovy.register(new UninstallCommand(groovy, mediator))
groovy.register(new InstallCommand(groovy, mediator))
groovy.register(new PluginCommand(groovy, mediator))
groovy.register(new ClsCommand(groovy, mediator))
// hide output temporarily while imports execute
showShellEvaluationOutput(false)
org.codehaus.groovy.control.customizers.ImportCustomizer ic = new org.codehaus.groovy.control.customizers.ImportCustomizer()
def imports = (ImportCustomizer) CoreGremlinPlugin.instance().getCustomizers("gremlin-groovy").get()[0]
ic.addStarImports(imports.getClassPackages().collect() { it.getName() }.toArray(new String[0]))
ic.addStaticStars(imports.getMethodClasses().collect() { it.getCanonicalName() }.toArray(new String[0]))
ic.addStaticStars(imports.getEnumClasses().collect() { it.getCanonicalName() }.toArray(new String[0]))
ic.addStaticStars(imports.getFieldClasses().collect() { it.getCanonicalName() }.toArray(new String[0]))
groovy.getCompilerConfiguration().addCompilationCustomizers(ic)
final InteractiveShellRunner runner = new InteractiveShellRunner(groovy, handlePrompt)
runner.reader.setHandleUserInterrupt(false)
runner.reader.setHandleLitteralNext(false)
runner.setErrorHandler(handleError)
try {
final FileHistory history = new FileHistory(new File(ConsoleFs.HISTORY_FILE))
groovy.setHistory(history)
runner.setHistory(history)
} catch (IOException ignored) {
io.err.println(Colorizer.render(Preferences.errorColor, "Unable to create history file: " + ConsoleFs.HISTORY_FILE))
}
GremlinLoader.load()
// check for available plugins on the path and track them by plugin class name
def activePlugins = Mediator.readPluginState()
ServiceLoader.load(GremlinPlugin, groovy.getInterp().getClassLoader()).each { plugin ->
if (!mediator.availablePlugins.containsKey(plugin.class.name)) {
def pluggedIn = new PluggedIn((GremlinPlugin) plugin, groovy, io, false)
mediator.availablePlugins.put(plugin.class.name, pluggedIn)
pluggedIn.activate()
}
}
// if there are active plugins then initialize them in the order that they are listed
activePlugins.each { pluginName ->
def pluggedIn = mediator.availablePlugins[pluginName]
if (pluggedIn != null) {
pluggedIn.activate()
if (!io.quiet)
io.out.println(Colorizer.render(Preferences.infoColor, "plugin activated: " + pluggedIn.getPlugin().getName()))
} else if (!io.quiet) {
io.out.println(Colorizer.render(Preferences.infoColor, "invalid plugin: " + pluginName))
}
}
// remove any "uninstalled" plugins from plugin state as it means they were installed, activated, but not
// deactivated, and are thus hanging about (e.g. user deleted the plugin directories to uninstall). checking
// the number of expected active plugins from the plugins.txt file against the number activated on startup
// should be enough to tell if something changed which would justify that the file be re-written
if (activePlugins.size() != mediator.activePlugins().size())
mediator.writePluginState()
try {
// if the init script contains :x command it will throw an ExitNotification so init script execution
// needs to appear in the try/catch
if (scriptsAndArgs != null && !scriptsAndArgs.isEmpty()) executeInShell(scriptsAndArgs)
// start iterating results to show as output
showShellEvaluationOutput(true)
runner.run()
} catch (ExitNotification ignored) {
// occurs on exit
} catch (Throwable t) {
t.printStackTrace()
} finally {
// shutdown hook defined above will kill any open remotes
System.exit(0)
}
}