/*******************************************************************************
 * Copyright (c) 2004, 2008 Actuate Corporation.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *  Actuate Corporation  - initial API and implementation
 *******************************************************************************/

package org.eclipse.birt.core.script.functionservice.impl;

import org.eclipse.birt.core.exception.BirtException;
import org.eclipse.birt.core.script.functionservice.IScriptFunction;
import org.eclipse.birt.core.script.functionservice.IScriptFunctionCategory;
import org.mozilla.javascript.Context;
import org.mozilla.javascript.Scriptable;

/**
 * 
 */

public class FunctionProvider
{
	private static IFunctionProvider provider;

	/**
	 * Set the current function provider impl.
	 * @param provider
	 */
	public static void setFunctionProvider( IFunctionProvider provider )
	{
		FunctionProvider.provider = provider;
	}
	/**
	 * Return all the categories defined by extensions.
	 * 
	 * @return
	 * @throws BirtException
	 */
	public static IScriptFunctionCategory[] getCategories( )
			throws BirtException
	{
		if( provider!= null )
			return provider.getCategories();
		return new IScriptFunctionCategory[]{};
	}

	/**
	 * Return the functions that defined in a category.
	 * 
	 * @param categoryName
	 * @return
	 * @throws BirtException
	 */
	public static IScriptFunction[] getFunctions( String categoryName )
			throws BirtException
	{
		if( provider!= null )
			return provider.getFunctions( categoryName );
		return new IScriptFunction[0];
	}

	/**
	 * Register script functions to scope.
	 * 
	 * @param cx
	 * @param scope
	 * @throws BirtException
	 */
	public static void registerScriptFunction( Context cx, Scriptable scope )
			throws BirtException
	{
		if( provider != null )
			FunctionProvider.provider.registerScriptFunction( cx, scope);
	}
}

@@ -590,16 +590,82 @@
 .impl;%0A%0A
+import java.util.logging.Level;%0Aimport java.util.logging.Logger;%0A%0A
 import o
@@ -983,16 +983,17 @@
 vider%0A%7B%0A
+%0A
 %09private
@@ -1004,222 +1004,848 @@
 tic 
-IFunctionProvider provider;%0A%0A%09/**%0A%09 * Set the current function provider impl.%0A%09 * @param provider%0A%09 */%0A%09public static void setFunctionProvider( IFunctionProvider provider )%0A%09%7B%0A%09%09FunctionProvider.provider = provider
+Logger logger = Logger.getLogger( FunctionProvider.class%0A%09%09%09.getName( ) );%0A%09private static String PROVIDER_CLASS = %22org.eclipse.birt.core.internal.function.impl.FunctionProviderImpl%22;%0A%09private static IFunctionProvider instance;%0A%0A%09/**%0A%09 * Set the current function provider impl.%0A%09 * @param provider%0A%09 */%0A%09public static void setFunctionProvider( IFunctionProvider provider )%0A%09%7B%0A%09%09FunctionProvider.instance = provider;%0A%09%7D%0A%0A%09protected synchronized static IFunctionProvider getFunctionProvider( )%0A%09%7B%0A%09%09if ( instance == null )%0A%09%09%7B%0A%09%09%09try%0A%09%09%09%7B%0A%09%09%09%09Class clazz = Class.forName( PROVIDER_CLASS );%0A%09%09%09%09if ( clazz != null )%0A%09%09%09%09%7B%0A%09%09%09%09%09instance = (IFunctionProvider) clazz.newInstance( );%0A%09%09%09%09%7D%0A%09%09%09%7D%0A%09%09%09catch ( Exception ex )%0A%09%09%09%7B%0A%09%09%09%09logger.log( Level.WARNING,%0A%09%09%09%09%09%09%22failed to initialize IFunctionProvider instance%22, ex );%0A%09%09%09%7D%0A%09%09%7D%0A%0A%09%09return instance
 ;%0A%09%7D
@@ -2028,36 +2028,92 @@
 irtException%0A%09%7B%0A
+%09%09IFunctionProvider provider = getFunctionProvider( );%0A
 %09%09if
+ 
 ( provider!= nul
@@ -2098,32 +2098,33 @@
 %0A%09%09if ( provider
+ 
 != null )%0A%09%09%09ret
@@ -2150,16 +2150,17 @@
 egories(
+ 
 );%0A%09%09ret
@@ -2418,36 +2418,92 @@
 irtException%0A%09%7B%0A
+%09%09IFunctionProvider provider = getFunctionProvider( );%0A
 %09%09if
+ 
 ( provider!= nul
@@ -2496,16 +2496,17 @@
 provider
+ 
 != null 
@@ -2813,11 +2813,25 @@
 %7B%0A%09%09
-if(
+IFunctionProvider
  pro
@@ -2836,29 +2836,21 @@
 rovider 
-!
 = 
-null )%0A%09%09%09
+get
 Function
@@ -2857,17 +2857,50 @@
 Provider
-.
+( );%0A%09%09if ( provider != null )%0A%09%09%09
 provider
@@ -2933,16 +2933,17 @@
 x, scope
+ 
 );%0A%09%7D%0A%7D%0A

Initialize the function provider for OSGi-less runtime