/
FunctionProvider.java
122 lines (110 loc) · 3.12 KB
/
FunctionProvider.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
/*******************************************************************************
* 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 java.util.logging.Level;
import java.util.logging.Logger;
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 Logger logger = Logger.getLogger( FunctionProvider.class
.getName( ) );
private static String PROVIDER_CLASS = "org.eclipse.birt.core.internal.function.impl.FunctionProviderImpl"; //$NON-NLS-1$
private static IFunctionProvider instance;
/**
* Set the current function provider impl.
* @param provider
*/
public static void setFunctionProvider( IFunctionProvider provider )
{
if ( !isLoaded( ) )
{
FunctionProvider.instance = provider;
}
else
{
logger.warning( "FunctionProvider should not set twice." ); //$NON-NLS-1$
}
}
public static boolean isLoaded( )
{
return FunctionProvider.instance != null;
}
protected synchronized static IFunctionProvider getFunctionProvider( )
{
if ( instance == null )
{
try
{
Class<?> clazz = Class.forName( PROVIDER_CLASS );
if ( clazz != null )
{
instance = (IFunctionProvider) clazz.newInstance( );
}
}
catch ( Exception ex )
{
logger.log( Level.WARNING,
"failed to initialize IFunctionProvider instance", ex ); //$NON-NLS-1$
}
}
return instance;
}
/**
* Return all the categories defined by extensions.
*
* @return
* @throws BirtException
*/
public static IScriptFunctionCategory[] getCategories( )
throws BirtException
{
IFunctionProvider provider = getFunctionProvider( );
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
{
IFunctionProvider provider = getFunctionProvider( );
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
{
IFunctionProvider provider = getFunctionProvider( );
if ( provider != null )
provider.registerScriptFunction( cx, scope );
}
}