<commit_before>package org.neo4j.util.shell;

import java.io.Serializable;
import java.rmi.RemoteException;

/**
 * A common implementation of a {@link ShellClient}.
 */
public abstract class AbstractClient implements ShellClient
{
	/**
	 * The session key for the prompt key, just like in Bash.
	 */
	public static final String PROMPT_KEY = "PS1";
	
	/**
	 * The session key for whether or not to print stack traces for exceptions. 
	 */
	public static final String STACKTRACES_KEY = "STACKTRACES";
	
	private Console console;
	
	public void grabPrompt()
	{
		this.init();
		while ( true )
		{
			try
			{
				this.console.format( tryGetProperPromptString() );
				String line = this.readLine();
				String result = this.getServer().interpretLine(
					line, this.session(), this.getOutput() );
				if ( result == null || result.trim().length() == 0 )
				{
					continue;
				}
				if ( result.contains( "e" ) )
				{
					break;
				}
			}
			catch ( Exception e )
			{
				if ( this.shouldPrintStackTraces() )
				{
					e.printStackTrace();
				}
				this.console.format( e.getMessage() + "\n" );
			}
		}
		this.shutdown();
	}
	
	protected String tryGetProperPromptString()
	{
		String result = null;
		try
		{
			result = ( String ) getSessionVariable( PROMPT_KEY, null, true );
		}
		catch ( Exception e )
		{
			result = ( String ) getSessionVariable( PROMPT_KEY, null, false );
		}
		return result;
	}
	
	protected void shutdown()
	{
	}
	
	private boolean shouldPrintStackTraces()
	{
		try
		{
			Object value = this.session().get( STACKTRACES_KEY );
			return this.getSafeBooleanValue( value, false );
		}
		catch ( RemoteException e )
		{
			return true;
		}
	}
	
	private boolean getSafeBooleanValue( Object value, boolean def )
	{
		if ( value == null )
		{
			return def;
		}
		return Boolean.parseBoolean( value.toString() );
	}
	
	private void init()
	{
		try
		{
			possiblyGrabDefaultVariableFromServer( PROMPT_KEY, "$ " );
			this.getOutput().println( this.getServer().welcome() );
			
			// Grab a jline console if available, else a standard one.
			this.console = JLineConsole.newConsoleOrNullIfNotFound( this );
			this.console = this.console != null ? this.console :
				new StandardConsole();
		}
		catch ( RemoteException e )
		{
			throw new RuntimeException( e );
		}
	}
	
	protected void possiblyGrabDefaultVariableFromServer( String key,
		Serializable defaultValue )
	{
		try
		{
			if ( this.session().get( key ) == null )
			{
				Serializable value = this.getServer().getProperty( key );
				if ( value == null )
				{<commit_msg>Adds a comment about jline if it isn't on the classpath.<commit_after>package org.neo4j.util.shell;

import java.io.Serializable;
import java.rmi.RemoteException;

/**
 * A common implementation of a {@link ShellClient}.
 */
public abstract class AbstractClient implements ShellClient
{
	/**
	 * The session key for the prompt key, just like in Bash.
	 */
	public static final String PROMPT_KEY = "PS1";
	
	/**
	 * The session key for whether or not to print stack traces for exceptions. 
	 */
	public static final String STACKTRACES_KEY = "STACKTRACES";
	
	private Console console;
	
	public void grabPrompt()
	{
		this.init();
		while ( true )
		{
			try
			{
				this.console.format( tryGetProperPromptString() );
				String line = this.readLine();
				String result = this.getServer().interpretLine(
					line, this.session(), this.getOutput() );
				if ( result == null || result.trim().length() == 0 )
				{
					continue;
				}
				if ( result.contains( "e" ) )
				{
					break;
				}
			}
			catch ( Exception e )
			{
				if ( this.shouldPrintStackTraces() )
				{
					e.printStackTrace();
				}
				this.console.format( e.getMessage() + "\n" );
			}
		}
		this.shutdown();
	}
	
	protected String tryGetProperPromptString()
	{
		String result = null;
		try
		{
			result = ( String ) getSessionVariable( PROMPT_KEY, null, true );
		}
		catch ( Exception e )
		{
			result = ( String ) getSessionVariable( PROMPT_KEY, null, false );
		}
		return result;
	}
	
	protected void shutdown()
	{
	}
	
	private boolean shouldPrintStackTraces()
	{
		try
		{
			Object value = this.session().get( STACKTRACES_KEY );
			return this.getSafeBooleanValue( value, false );
		}
		catch ( RemoteException e )
		{
			return true;
		}
	}
	
	private boolean getSafeBooleanValue( Object value, boolean def )
	{
		if ( value == null )
		{
			return def;
		}
		return Boolean.parseBoolean( value.toString() );
	}
	
	private void init()
	{
		try
		{
			possiblyGrabDefaultVariableFromServer( PROMPT_KEY, "$ " );
			this.getOutput().println( this.getServer().welcome() );
			
			// Grab a jline console if available, else a standard one.
			this.console = JLineConsole.newConsoleOrNullIfNotFound( this );
			if ( this.console == null )
			{
				System.out.println( "Want bash-like features? throw in " +
					"jLine on the classpath" );
				this.console = new StandardConsole();
			}
		}
		catch ( RemoteException e )
		{
			throw new RuntimeException( e );
		}
	}
	
	protected void possiblyGrabDefaultVariableFromServer( String key,
		Serializable defaultValue )
	{
		try
		{
			if ( this.session().get( key ) == null )
			{
				Serializable value = this.getServer().getProperty( key );
				if ( value == null )
				{
{'fraction_non_alphanumeric': 0.1194228213404215, 'fraction_numerical': 0.001139168407062844, 'mean_word_length': 2.902222222222222, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2645109', 'n_tokens_mistral': 2017, 'n_tokens_neox': 1668, 'n_words': 436}