1. Project Clover database Tue Dec 20 2016 21:24:09 CET
  2. Package org.xwiki.groovy.internal

File GroovyScriptEngineFactory.java

 

Coverage histogram

../../../../img/srcFileCovDistChart10.png
0% of files have more coverage

Code metrics

2
7
1
1
74
33
2
0.29
7
1
2

Classes

Class Line # Actions
GroovyScriptEngineFactory 48 7 0% 2 0
1.0100%
 

Contributing tests

This file is covered by 27 tests. .

Source view

1    /*
2    * See the NOTICE file distributed with this work for additional
3    * information regarding copyright ownership.
4    *
5    * This is free software; you can redistribute it and/or modify it
6    * under the terms of the GNU Lesser General Public License as
7    * published by the Free Software Foundation; either version 2.1 of
8    * the License, or (at your option) any later version.
9    *
10    * This software is distributed in the hope that it will be useful,
11    * but WITHOUT ANY WARRANTY; without even the implied warranty of
12    * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13    * Lesser General Public License for more details.
14    *
15    * You should have received a copy of the GNU Lesser General Public
16    * License along with this software; if not, write to the Free
17    * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
18    * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
19    */
20    package org.xwiki.groovy.internal;
21   
22    import java.util.List;
23   
24    import javax.inject.Inject;
25    import javax.inject.Named;
26    import javax.inject.Singleton;
27    import javax.script.ScriptEngine;
28    import javax.script.ScriptEngineFactory;
29   
30    import org.codehaus.groovy.control.CompilerConfiguration;
31    import org.codehaus.groovy.control.customizers.CompilationCustomizer;
32    import org.codehaus.groovy.jsr223.GroovyScriptEngineImpl;
33    import org.xwiki.component.annotation.Component;
34    import org.xwiki.groovy.GroovyConfiguration;
35   
36    import groovy.lang.GroovyClassLoader;
37   
38    /**
39    * This class is required since the JSR223 doesn't allow configuring the classloader used by the Script Engine
40    * implementation and this is how Groovy supports customizing script compilation.
41    *
42    * @version $Id: 396a68d447ad52891874c713d3e662b452b1d0ad $
43    * @since 4.1M1
44    */
45    @Component(roles = { ScriptEngineFactory.class })
46    @Named("groovy")
47    @Singleton
 
48    public class GroovyScriptEngineFactory extends org.codehaus.groovy.jsr223.GroovyScriptEngineFactory
49    {
50    /**
51    * The Groovy configuration, used to get the list of Groovy Compilation Customizers.
52    */
53    @Inject
54    private GroovyConfiguration configuration;
55   
 
56  94 toggle @Override
57    public ScriptEngine getScriptEngine()
58    {
59    // Add all the defined Customizers
60  94 CompilerConfiguration config = new CompilerConfiguration();
61   
62  94 List<CompilationCustomizer> customizers = this.configuration.getCompilationCustomizers();
63  94 if (!customizers.isEmpty()) {
64  4 config.addCompilationCustomizers(customizers.toArray(new CompilationCustomizer[customizers.size()]));
65    }
66   
67  94 ClassLoader parentClassLoader = Thread.currentThread().getContextClassLoader();
68  94 GroovyClassLoader loader = new GroovyClassLoader(parentClassLoader, config);
69   
70    // We configure the Groovy Script Engine with a custom GroovyClassLoader that we specifically configure with
71    // Compilation Configurations to protect for example against scripts taking too long to execute.
72  94 return new GroovyScriptEngineImpl(loader);
73    }
74    }