| Class | Line # | Actions | |||||
|---|---|---|---|---|---|---|---|
| ExecutionContextRunnable | 34 | 11 | 0% | 4 | 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.context.concurrent; | |
| 21 | ||
| 22 | import org.xwiki.component.manager.ComponentLookupException; | |
| 23 | import org.xwiki.component.manager.ComponentManager; | |
| 24 | import org.xwiki.context.Execution; | |
| 25 | import org.xwiki.context.ExecutionContext; | |
| 26 | import org.xwiki.context.ExecutionContextManager; | |
| 27 | ||
| 28 | /** | |
| 29 | * {@link Runnable} wrapper which initialize and clean the execution context. | |
| 30 | * | |
| 31 | * @version $Id: b7b0ff6d9b3c37911aedad3df91d1efb4ed304c0 $ | |
| 32 | * @since 5.1RC1 | |
| 33 | */ | |
| 34 | public class ExecutionContextRunnable implements Runnable | |
| 35 | { | |
| 36 | /** | |
| 37 | * Used to access the components needed to initialize and dispose the {@link ExecutionContext}. | |
| 38 | */ | |
| 39 | private ComponentManager componentManager; | |
| 40 | ||
| 41 | /** | |
| 42 | * The runnable to wrap. | |
| 43 | */ | |
| 44 | private Runnable runnable; | |
| 45 | ||
| 46 | /** | |
| 47 | * @param runnable used to access the components needed to initialize and dispose the {@link ExecutionContext}. | |
| 48 | * @param componentManager the runnable to wrap. | |
| 49 | */ | |
| 50 | 3 | public ExecutionContextRunnable(Runnable runnable, ComponentManager componentManager) |
| 51 | { | |
| 52 | 3 | this.runnable = runnable; |
| 53 | 3 | this.componentManager = componentManager; |
| 54 | } | |
| 55 | ||
| 56 | 3 | @Override |
| 57 | public void run() | |
| 58 | { | |
| 59 | // Create a clean Execution Context | |
| 60 | 3 | ExecutionContext context = new ExecutionContext(); |
| 61 | ||
| 62 | 3 | try { |
| 63 | 3 | this.componentManager.<ExecutionContextManager>getInstance(ExecutionContextManager.class).initialize( |
| 64 | context); | |
| 65 | } catch (Exception e) { | |
| 66 | 0 | throw new RuntimeException("Failed to initialize Runnable [" + this + "] execution context", e); |
| 67 | } | |
| 68 | ||
| 69 | 3 | try { |
| 70 | 3 | this.runnable.run(); |
| 71 | } finally { | |
| 72 | 3 | try { |
| 73 | 3 | this.componentManager.<Execution>getInstance(Execution.class).removeContext(); |
| 74 | } catch (ComponentLookupException e) { | |
| 75 | 0 | throw new RuntimeException("Failed to cleanup ExecutionContext after Runnable execution", e); |
| 76 | } | |
| 77 | } | |
| 78 | } | |
| 79 | } |