1. Project Clover database Sat Feb 2 2019 06:45:20 CET
  2. Package org.xwiki.script.internal

File DefaultScriptContextManager.java

 

Coverage histogram

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

Code metrics

0
5
2
1
79
33
2
0.4
2.5
2
1

Classes

Class Line # Actions
DefaultScriptContextManager 40 5 0% 2 0
1.0100%
 

Contributing tests

This file is covered by 49 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.script.internal;
21   
22    import java.util.List;
23   
24    import javax.inject.Inject;
25    import javax.inject.Singleton;
26    import javax.script.ScriptContext;
27   
28    import org.xwiki.component.annotation.Component;
29    import org.xwiki.context.Execution;
30    import org.xwiki.script.ScriptContextInitializer;
31    import org.xwiki.script.ScriptContextManager;
32   
33    /**
34    * Default implementation of {@link ScriptContextManager}.
35    *
36    * @version $Id: 9ee688691dd4ff9cc532bf832d5e18f138d7aa9f $
37    */
38    @Component
39    @Singleton
 
40    public class DefaultScriptContextManager implements ScriptContextManager
41    {
42    /**
43    * Used to get and insert script context in current execution context.
44    */
45    @Inject
46    private Execution execution;
47   
48    /**
49    * The {@link ScriptContextInitializer} list used to initialize {@link ScriptContext}.
50    */
51    @Inject
52    private List<ScriptContextInitializer> scriptContextInitializerList;
53   
 
54  1828013 toggle @Override
55    public ScriptContext getScriptContext()
56    {
57  1828579 ScriptContext context = getCurrentScriptContext();
58   
59    // We re-initialize the Script Context with all Script Context Initializers. We do this in order to ensure
60    // that the Script Context always contain correct values even if user scripts or XWiki code have modified them.
61    // For example the current document in the Script Context could have changed and thus needs to be set back.
62    // Also note that we don't clone the context since we want that in the same request several script executions
63    // can share bindings.
64  1829619 for (ScriptContextInitializer scriptContextInitializer : this.scriptContextInitializerList) {
65  3657943 scriptContextInitializer.initialize(context);
66    }
67   
68  1829591 return context;
69    }
70   
 
71  1890041 toggle @Override
72    public ScriptContext getCurrentScriptContext()
73    {
74    // The Script Context is set in ScriptExecutionContextInitializer, when the XWiki Execution Context is
75    // initialized so we are guaranteed it is defined when this method is called.
76  1890359 return (ScriptContext) this.execution.getContext()
77    .getProperty(ScriptExecutionContextInitializer.SCRIPT_CONTEXT_ID);
78    }
79    }