/***************************************************************************
 *   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" + ps + "*",
			// 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;
	}
}

@@ -2795,25 +2795,25 @@
 tensions%22 + 
-p
+f
 s + %22*%22,%0A%09%09%09

Use the *file* separator, not the path separator.