/
ScriptEvaluatorFactory.java
110 lines (91 loc) · 3.73 KB
/
ScriptEvaluatorFactory.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
/*
* JBoss, Home of Professional Open Source
* Copyright 2010, Red Hat, Inc. and/or its affiliates, and individual contributors
* by the @authors tag. See the copyright.txt in the distribution for a
* full listing of individual contributors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
* http://www.apache.org/licenses/LICENSE-2.0
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.hibernate.validator.internal.util.scriptengine;
import java.lang.ref.Reference;
import java.lang.ref.SoftReference;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentMap;
import javax.script.ScriptEngine;
import javax.script.ScriptEngineManager;
import javax.script.ScriptException;
import org.hibernate.validator.internal.util.logging.Log;
import org.hibernate.validator.internal.util.logging.LoggerFactory;
/**
* Factory responsible for the creation of {@link ScriptEvaluator}s. This
* class is thread-safe.
*
* @author Gunnar Morling
* @author Kevin Pollet <kevin.pollet@serli.com> (C) 2011 SERLI
*/
public class ScriptEvaluatorFactory {
private static final Log log = LoggerFactory.make();
/**
* A reference with an instance of this factory. Allows the factory to be reused several times, but can be GC'ed if required.
*/
private static Reference<ScriptEvaluatorFactory> INSTANCE = new SoftReference<ScriptEvaluatorFactory>( new ScriptEvaluatorFactory() );
/**
* A cache of script executors (keyed by language name).
*/
private ConcurrentMap<String, ScriptEvaluator> scriptExecutorCache = new ConcurrentHashMap<String, ScriptEvaluator>();
private ScriptEvaluatorFactory() {
}
/**
* Retrieves an instance of this factory.
*
* @return A script evaluator factory. Never null.
*/
public static synchronized ScriptEvaluatorFactory getInstance() {
ScriptEvaluatorFactory theValue = INSTANCE.get();
if ( theValue == null ) {
theValue = new ScriptEvaluatorFactory();
INSTANCE = new SoftReference<ScriptEvaluatorFactory>( theValue );
}
return theValue;
}
/**
* Retrieves a script executor for the given language.
*
* @param languageName The name of a scripting language as expected by {@link ScriptEngineManager#getEngineByName(String)}.
*
* @return A script executor for the given language. Never null.
*
* @throws ScriptException In case no JSR 223 compatible engine for the given language could be found.
*/
public ScriptEvaluator getScriptEvaluatorByLanguageName(String languageName) throws ScriptException {
if ( !scriptExecutorCache.containsKey( languageName ) ) {
ScriptEvaluator scriptExecutor = createNewScriptEvaluator( languageName );
scriptExecutorCache.putIfAbsent( languageName, scriptExecutor );
}
return scriptExecutorCache.get( languageName );
}
/**
* Creates a new script executor for the given language.
*
* @param languageName A JSR 223 language name.
*
* @return A newly created script executor for the given language.
*
* @throws ScriptException In case no JSR 223 compatible engine for the given language could be found.
*/
private ScriptEvaluator createNewScriptEvaluator(String languageName) throws ScriptException {
ScriptEngine engine = new ScriptEngineManager().getEngineByName( languageName );
if ( engine == null ) {
throw new ScriptException( log.unableToFindScriptEngine( languageName ) );
}
return new ScriptEvaluator( engine );
}
}