1. Project Clover database Tue Dec 20 2016 21:24:09 CET
  2. Package com.xpn.xwiki.util

File AbstractXWikiRunnable.java

 

Coverage histogram

../../../../img/srcFileCovDistChart8.png
54% of files have more coverage

Code metrics

0
17
7
1
141
60
8
0.47
2.43
7
1.14

Classes

Class Line # Actions
AbstractXWikiRunnable 42 17 0% 8 6
0.7575%
 

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 com.xpn.xwiki.util;
21   
22    import java.util.HashMap;
23    import java.util.Map;
24   
25    import org.slf4j.Logger;
26    import org.slf4j.LoggerFactory;
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   
32    import com.xpn.xwiki.web.Utils;
33   
34    /**
35    * Base class for any XWiki daemon class. It provide tools to initialize execution context.
36    *
37    * @since 1.8.4
38    * @since 1.9RC1
39    * @since 2.0M1
40    * @version $Id: f990bf0907ead3ce77dc00b8cf58dc9301d1e1bf $
41    */
 
42    public abstract class AbstractXWikiRunnable implements Runnable
43    {
44    /**
45    * Logging tools.
46    */
47    private static final Logger LOGGER = LoggerFactory.getLogger(AbstractXWikiRunnable.class);
48   
49    private final Map<String, Object> properties = new HashMap<String, Object>();
50   
51    /**
52    * A reference to the Execution component to be used in {@link #cleanupExecutionContext()} when this thread is
53    * stopped. Since we're not inside a component we cannot inject this dependency so we initialize it in
54    * {@link #initExecutionContext()}. The reason we keep this reference is because this thread can be stopped after
55    * the Component Manager disposes its components so a lookup in {@link #cleanupExecutionContext()} can fail.
56    */
57    private Execution execution;
58   
 
59  64 toggle protected AbstractXWikiRunnable()
60    {
61    }
62   
63    /**
64    * @param propertyName the name of the property to put in the initialized context
65    * @param propertyValue the value of the property to put in the initialized context
66    */
 
67  0 toggle protected AbstractXWikiRunnable(String propertyName, Object propertyValue)
68    {
69  0 this.properties.put(propertyName, propertyValue);
70    }
71   
72    /**
73    * @param properties properties to put in the initialized context
74    */
 
75  0 toggle protected AbstractXWikiRunnable(Map<String, Object> properties)
76    {
77  0 this.properties.putAll(properties);
78    }
79   
80    /**
81    * Lets subclasses declare execution context properties.
82    *
83    * @param executionContext the execution context.
84    */
 
85  4 toggle protected void declareProperties(ExecutionContext executionContext)
86    {
87    }
88   
89    /**
90    * Initialize execution context for the current thread.
91    *
92    * @return the new execution context
93    * @throws ExecutionContextException error when try to initialize execution context
94    */
 
95  64 toggle protected ExecutionContext initExecutionContext() throws ExecutionContextException
96    {
97    // Keep a reference to the Execution component to avoid a lookup in #cleanupExecutionContext() in case this
98    // thread is stopped after the Component Manager disposes its components.
99  64 this.execution = Utils.getComponent(Execution.class);
100   
101  64 ExecutionContextManager ecim = Utils.getComponent(ExecutionContextManager.class);
102  64 ExecutionContext context = new ExecutionContext();
103   
104  64 declareProperties(context);
105   
106  64 ecim.initialize(context);
107   
108  64 context.setProperties(this.properties);
109   
110  64 return context;
111    }
112   
 
113  3 toggle protected void cleanupExecutionContext()
114    {
115    // We must ensure we clean the ThreadLocal variables located in the Execution
116    // component as otherwise we will have a potential memory leak.
117  3 this.execution.removeContext();
118    }
119   
 
120  64 toggle @Override
121    public final void run()
122    {
123  64 try {
124    // initialize execution context
125  64 initExecutionContext();
126    } catch (ExecutionContextException e) {
127  0 LOGGER.error("Failed to initialize execution context", e);
128  0 return;
129    }
130   
131  64 try {
132    // call run
133  64 runInternal();
134    } finally {
135    // cleanup execution context
136  3 cleanupExecutionContext();
137    }
138    }
139   
140    protected abstract void runInternal();
141    }