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

File ExecutionContextCopier.java

 

Coverage histogram

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

Code metrics

0
9
1
1
84
39
2
0.22
9
1
2

Classes

Class Line # Actions
ExecutionContextCopier 45 9 0% 2 1
0.990%
 

Contributing tests

No tests hitting this source file were found.

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.mail.internal.thread.context;
21   
22    import javax.inject.Inject;
23    import javax.inject.Named;
24    import javax.inject.Singleton;
25   
26    import org.apache.commons.lang3.exception.CloneFailedException;
27    import org.xwiki.component.annotation.Component;
28    import org.xwiki.context.ExecutionContext;
29    import org.xwiki.context.ExecutionContextInitializer;
30    import org.xwiki.context.ExecutionContextManager;
31    import org.xwiki.velocity.internal.VelocityExecutionContextInitializer;
32   
33    import com.xpn.xwiki.XWikiContext;
34   
35    /**
36    * Additionally to {@link ExecutionContextManager#clone(ExecutionContext)}, this method also clones the
37    * {@link XWikiContext} by using {@link XWikiContextCopier} and thus the resulting {@link ExecutionContext} should be
38    * usable in a new thread.
39    *
40    * @version $Id: b06178b18752d5c394d21796f02a4d676903121c $
41    * @since 7.1M2
42    */
43    @Component
44    @Singleton
 
45    public class ExecutionContextCopier implements Copier<ExecutionContext>
46    {
47    @Inject
48    private ExecutionContextManager executionContextManager;
49   
50    @Inject
51    private Copier<XWikiContext> xwikiContextCloner;
52   
53    @Inject
54    @Named("velocity")
55    private ExecutionContextInitializer velocityExecutionContextInitializer;
56   
 
57  34 toggle @Override
58    public ExecutionContext copy(ExecutionContext originalExecutionContext) throws CloneFailedException
59    {
60  34 try {
61  34 ExecutionContext clonedExecutionContext = this.executionContextManager.clone(originalExecutionContext);
62   
63    // XWikiContext
64    // The above clone just creates and initializes an empty XWiki Context, so it needs special handling.
65  34 XWikiContext xwikiContext =
66    (XWikiContext) originalExecutionContext.getProperty(XWikiContext.EXECUTIONCONTEXT_KEY);
67  34 XWikiContext clonedXWikiContext = xwikiContextCloner.copy(xwikiContext);
68  34 clonedExecutionContext.setProperty(XWikiContext.EXECUTIONCONTEXT_KEY, clonedXWikiContext);
69   
70    // VelocityContext
71    // Reset the VelocityContext from the EC by removing it and calling the Velocity ECInitializer which is
72    // normally called by the execution of the ECInitializers by ECManager.clone(). This ensures a clean new
73    // VC is created. It'll get filled when VelocityContextManager.getVelocityContext() is called by whatever
74    // code need the VC.
75  34 clonedExecutionContext.removeProperty(VelocityExecutionContextInitializer.VELOCITY_CONTEXT_ID);
76  34 this.velocityExecutionContextInitializer.initialize(clonedExecutionContext);
77   
78  34 return clonedExecutionContext;
79    } catch (Exception e) {
80  0 throw new CloneFailedException(String.format("Failed to clone [%s]", originalExecutionContext), e);
81    }
82    }
83   
84    }