/***************************************************************************
 *   Copyright (C) 2008 by Fabrizio Montesi                                *
 *                                                                         *
 *   This program is free software; you can redistribute it and/or modify  *
 *   it under the terms of the GNU Library General Public License as       *
 *   published by the Free Software Foundation; either version 2 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 Library General Public     *
 *   License along with this program; if not, write to the                 *
 *   Free Software Foundation, Inc.,                                       *
 *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
 *                                                                         *
 *   For details about the authors of this software, see the AUTHORS file. *
 ***************************************************************************/

package joliex.metaservice.impl;

import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.Future;
import jolie.CommandLineException;
import jolie.Interpreter;
import jolie.net.CommChannel;
import jolie.net.LocalCommChannel;
import joliex.metaservice.MetaService;
import joliex.metaservice.MetaServiceChannel;

/**
 * This class executes a MetaService service in the local Java Virtual Machine.
 * @author Fabrizio Montesi
 */
public class EmbeddedMetaService extends MetaService
{	
	final private Interpreter interpreter;
	final private MetaServiceChannel channel;
	final static String fs = jolie.lang.Constants.fileSeparator;
	final static String ps = jolie.lang.Constants.pathSeparator;

	final private static String defaultFilepath = fs + "include" + fs + "services" + fs + "metaservice" + fs + "metaservice.ol";
	
	private static String[] buildInterpreterArguments( String jh, String metaServiceFilepath )
	{
		// jh stays for jolie_home, i.e. the JOLIE installation directory
		return new String[] {
			// Location
			"-C", "MetaServiceLocation=\"local\"",
			// Libraries
			"-l", jh + fs + "lib" + ps + jh + fs + "javaServices" + fs + "*" + ps + jh + fs + "extensions" + fs + "*",
			// Includes
			"-i", jh + fs + "include",
			// MetaService source file
			metaServiceFilepath
		};
	}
	
	/**
	 * Creates an embedded MetaService instance, 
	 * executing a JOLIE interpreter in the local JVM.
	 * @param jolieHome the path pointing to the local JOLIE installation directory.
	 */
	public EmbeddedMetaService( String jolieHome )
		throws IOException, ExecutionException
	{
		try {
			interpreter =
				new Interpreter( buildInterpreterArguments( jolieHome, jolieHome + defaultFilepath ) );
			startInterpreter();
			channel = new MetaServiceChannel( this, "/" );
		} catch( CommandLineException e ) {
			throw new IOException( e );
		} catch( FileNotFoundException e ) {
			throw new IOException( e );
		}
	}
	
	private void startInterpreter()
		throws ExecutionException
	{
		Future< Exception > f = interpreter.start();
		try {
			Exception e = f.get();
			if ( e != null ) {
				throw new ExecutionException( e );
			}
		} catch( InterruptedException e ) {
			throw new ExecutionException( e );
		}
	}
	
	/**
	 * Creates an embedded MetaService instance, 
	 * executing a JOLIE interpreter in the local JVM.
	 * @param jolieHome the path pointing to the local JOLIE installation directory.
	 * @param metaserviceFilepath the path pointing to the metaservice source file to load.
	 */
	public EmbeddedMetaService( String jolieHome, String metaserviceFilepath )
		throws IOException, ExecutionException
	{
		try {
			interpreter =
				new Interpreter( buildInterpreterArguments( jolieHome, metaserviceFilepath ) );
			startInterpreter();
			channel = new MetaServiceChannel( this, "/" );
		} catch( CommandLineException e ) {
			throw new IOException( e );
		} catch( FileNotFoundException e ) {
			throw new IOException( e );
		}
	}
	
	protected CommChannel createCommChannel()
	{
		return new LocalCommChannel( interpreter, interpreter.commCore().localListener() );
	}
	
	public MetaServiceChannel getChannel()
	{
		return channel;
	}
}

@@ -2311,16 +2311,77 @@
 rator;%0A%0A
+%09private static final String JOLIE_HOME_ENV = %22JOLIE_HOME%22;%0A%0A
 %09final p
@@ -3086,90 +3086,8 @@
 VM.%0A
-%09 * @param jolieHome the path pointing to the local JOLIE installation directory.%0A
 %09 */
@@ -3119,26 +3119,8 @@
 ice(
- String jolieHome 
 )%0A%09%09
@@ -3168,245 +3168,299 @@
 %0A%09%09t
-ry %7B%0A%09%09%09interp
+his( System.getenv( JOLIE_HOME_ENV ) );%0A%09%7D%0A%0A%09/**%0A%09 * C
 re
+a
 te
-r =%0A%09%09%09%09new Interpreter( buildInterpreterArguments( jolieHome, jolieHome + defaultFilepath ) );%0A%09%09%09startInterpreter();%0A%09%09%09channel = new MetaServiceChannel( this, %22/%22 );%0A%09%09%7D catch( CommandLineException e ) %7B%0A%09
+s an embedded MetaService instance, %0A%09 * executing a JOLIE interpreter in the local JVM.%0A%09 * @param jolieHome the path pointing to the local JOLIE installation directory.%0A%09 */%0A%09public EmbeddedMetaService( String jolieHome )%0A
 %09%09throw
- new
+s
  IOE
@@ -3471,38 +3471,19 @@
 tion
-( e );%0A%09%09%7D catch( FileNotFound
+, Execution
 Exce
@@ -3491,49 +3491,61 @@
 tion
- e ) 
+%0A%09
 %7B%0A%09%09
-%09
 th
-row new IOException( e );%0A%09%09%7D
+is( jolieHome, jolieHome + defaultFilepath );
 %0A%09%7D%0A

Add a new constructor that uses $JOLIE_HOME as default path to jolie.