Class | Line # | Actions | |||||
---|---|---|---|---|---|---|---|
ContextLinkCheckerThreadInitializer | 47 | 9 | 0% | 3 | 2 |
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.linkchecker.internal; | |
21 | ||
22 | import javax.inject.Inject; | |
23 | import javax.inject.Named; | |
24 | import javax.inject.Singleton; | |
25 | ||
26 | import org.xwiki.component.annotation.Component; | |
27 | import org.xwiki.context.Execution; | |
28 | import org.xwiki.context.ExecutionContext; | |
29 | import org.xwiki.context.ExecutionContextException; | |
30 | import org.xwiki.context.ExecutionContextManager; | |
31 | import org.xwiki.rendering.transformation.linkchecker.LinkCheckerThreadInitializer; | |
32 | ||
33 | import com.xpn.xwiki.XWikiContext; | |
34 | import com.xpn.xwiki.util.XWikiStubContextProvider; | |
35 | ||
36 | /** | |
37 | * Initialize the Execution Context so that Event Listeners that listen to | |
38 | * {@link org.xwiki.rendering.transformation.linkchecker.InvalidURLEvent} are sure to have a properly set up | |
39 | * Execution Context. | |
40 | * | |
41 | * @version $Id: b73086089f894e8553f369bd5b41daaf3f4cbb0d $ | |
42 | * @since 4.0M2 | |
43 | */ | |
44 | @Component | |
45 | @Named("context") | |
46 | @Singleton | |
47 | public class ContextLinkCheckerThreadInitializer implements LinkCheckerThreadInitializer | |
48 | { | |
49 | /** | |
50 | * Used to get the Execution Context. | |
51 | */ | |
52 | @Inject | |
53 | private Execution execution; | |
54 | ||
55 | /** | |
56 | * Used to create a new Execution Context from scratch. | |
57 | */ | |
58 | @Inject | |
59 | private ExecutionContextManager executionContextManager; | |
60 | ||
61 | /** | |
62 | * Used to clone the XWiki Context. | |
63 | */ | |
64 | @Inject | |
65 | private XWikiStubContextProvider stubContextProvider; | |
66 | ||
67 | 1 | ![]() |
68 | public void initialize() | |
69 | { | |
70 | 1 | ExecutionContext context = this.execution.getContext(); |
71 | 1 | if (context == null) { |
72 | // Create a clean Execution Context | |
73 | 1 | context = new ExecutionContext(); |
74 | ||
75 | 1 | try { |
76 | 1 | this.executionContextManager.initialize(context); |
77 | } catch (ExecutionContextException e) { | |
78 | 0 | throw new RuntimeException("Failed to initialize the Execution Context", e); |
79 | } | |
80 | ||
81 | // Bridge with old XWiki Context, required for old code. | |
82 | 1 | XWikiContext xwikiContext = this.stubContextProvider.createStubContext(); |
83 | 1 | xwikiContext.declareInExecutionContext(context); |
84 | ||
85 | 1 | this.execution.pushContext(context); |
86 | } | |
87 | } | |
88 | } |