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

File DefaultExecutionContextManager.java

 

Coverage histogram

../../../../img/srcFileCovDistChart9.png
38% of files have more coverage

Code metrics

0
14
3
1
118
52
5
0.36
4.67
3
1.67

Classes

Class Line # Actions
DefaultExecutionContextManager 42 14 0% 5 2
0.8823529588.2%
 

Contributing tests

This file is covered by 126 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.context.internal;
21   
22    import java.util.List;
23   
24    import javax.inject.Inject;
25    import javax.inject.Provider;
26    import javax.inject.Singleton;
27   
28    import org.xwiki.component.annotation.Component;
29    import org.xwiki.context.Execution;
30    import org.xwiki.context.ExecutionContext;
31    import org.xwiki.context.ExecutionContextException;
32    import org.xwiki.context.ExecutionContextInitializer;
33    import org.xwiki.context.ExecutionContextManager;
34   
35    /**
36    * Default implementation of {@link ExecutionContextManager}.
37    *
38    * @version $Id: 50e9fcedf475562bf0872ff6fdae137b6d18f003 $
39    */
40    @Component
41    @Singleton
 
42    public class DefaultExecutionContextManager implements ExecutionContextManager
43    {
44    /**
45    * Used to set the {@link ExecutionContext}.
46    */
47    @Inject
48    private Execution execution;
49   
50    /**
51    * Used to initialize the passed {@link ExecutionContext}. Note that we use a Provider so that the loading of
52    * all the Initializers is done lazily when they are required. This allows Extensions to contribute Initializers.
53    * Incidentally it also prevents creating Component Manager cycles since an Initializer could depend on
54    * something required to initialize this class.
55    */
56    @Inject
57    private Provider<List<ExecutionContextInitializer>> initializerProvider;
58   
 
59  16972 toggle @Override
60    public ExecutionContext clone(ExecutionContext context) throws ExecutionContextException
61    {
62  16974 ExecutionContext clonedContext = new ExecutionContext();
63   
64    // Ideally we would like to do a deep cloning here. However it's just too hard since we don't control
65    // objects put in the Execution Context and they can be of any type, including Maps which are cloneable
66    // but only do shallow clones.
67    // Thus instead we recreate the Execution Context from scratch and reinitialize it by calling all the
68    // Execution Context Initializers on it.
69  16972 try {
70  16972 this.execution.pushContext(clonedContext);
71    } catch (RuntimeException e) {
72    // If inheritance fails, we will get an unchecked exception here. So we'll wrap it in an
73    // ExecutionContextException.
74  0 throw new ExecutionContextException("Failed to push cloned execution context.", e);
75    }
76  16973 try {
77  16973 runInitializers(clonedContext);
78    } finally {
79    // #initialize set the context but we just want to clone it so we need to restore it
80  16972 this.execution.popContext();
81    }
82   
83  16973 return clonedContext;
84    }
85   
 
86  19437 toggle @Override
87    public void initialize(ExecutionContext context) throws ExecutionContextException
88    {
89    // Make sure we set Execution Context in the Execution component before we call the initialization
90    // so that we don't get any NPE if some initializer code asks to get the Execution Context. This
91    // happens for example with the Velocity Execution Context initializer which in turns calls the Velocity
92    // Context initializers and some of them look inside the Execution Context.
93    //
94    // Also, inherited values will be copied from the current context, if there is a current context.
95  19442 try {
96  19431 this.execution.setContext(context);
97    } catch (RuntimeException e) {
98    // If inheritance fails, we will get an unchecked exception here. So we'll wrap it in an
99    // ExecutionContextException.
100  0 throw new ExecutionContextException("Failed to set the execution context.", e);
101    }
102   
103  19424 runInitializers(context);
104    }
105   
106    /**
107    * Run the initializers.
108    *
109    * @param context the execution context to initialize
110    * @throws ExecutionContextException in case one {@link ExecutionContextInitializer} fails to execute
111    */
 
112  36335 toggle private void runInitializers(ExecutionContext context) throws ExecutionContextException
113    {
114  36354 for (ExecutionContextInitializer initializer : this.initializerProvider.get()) {
115  178116 initializer.initialize(context);
116    }
117    }
118    }