|
|||||||||||||||||||
30 day Evaluation Version distributed via the Maven Jar Repository. Clover is not free. You have 30 days to evaluate it. Please visit http://www.thecortex.net/clover to obtain a licensed version of Clover | |||||||||||||||||||
Source file | Conditionals | Statements | Methods | TOTAL | |||||||||||||||
ThreadLocalCyclicDependencyGuard.java | 100% | 100% | 100% | 100% |
|
1 |
/*****************************************************************************
|
|
2 |
* Copyright (c) PicoContainer Organization. All rights reserved. *
|
|
3 |
* ------------------------------------------------------------------------- *
|
|
4 |
* The software in this package is published under the terms of the BSD *
|
|
5 |
* style license a copy of which has been included with this distribution in *
|
|
6 |
* the LICENSE.txt file. *
|
|
7 |
* *
|
|
8 |
* Original code by Joerg Schaible *
|
|
9 |
*****************************************************************************/
|
|
10 |
|
|
11 |
package org.picocontainer.defaults;
|
|
12 |
|
|
13 |
/**
|
|
14 |
* Abstract utility class to detect recursion cycles.
|
|
15 |
* Derive from this class and implement {@link ThreadLocalCyclicDependencyGuard#run}.
|
|
16 |
* The method will be called by {@link ThreadLocalCyclicDependencyGuard#observe}. Select
|
|
17 |
* an appropriate guard for your scope. Any {@link ObjectReference} can be
|
|
18 |
* used as long as it is initialized with <code>Boolean.FALSE</code>.
|
|
19 |
*
|
|
20 |
* @author Jörg Schaible
|
|
21 |
* @since 1.1
|
|
22 |
*/
|
|
23 |
public abstract class ThreadLocalCyclicDependencyGuard extends ThreadLocal implements CyclicDependencyGuard { |
|
24 |
|
|
25 |
/**
|
|
26 |
* {@inheritDoc}
|
|
27 |
* @see java.lang.ThreadLocal#initialValue()
|
|
28 |
*/
|
|
29 | 876 |
protected Object initialValue() {
|
30 | 876 |
return Boolean.FALSE;
|
31 |
} |
|
32 |
/**
|
|
33 |
* Derive from this class and implement this function with the functionality
|
|
34 |
* to observe for a dependency cycle.
|
|
35 |
*
|
|
36 |
* @return a value, if the functionality result in an expression,
|
|
37 |
* otherwise just return <code>null</code>
|
|
38 |
*/
|
|
39 |
public abstract Object run();
|
|
40 |
|
|
41 |
/**
|
|
42 |
* Call the observing function. The provided guard will hold the {@link Boolean} value.
|
|
43 |
* If the guard is already <code>Boolean.TRUE</code> a {@link CyclicDependencyException}
|
|
44 |
* will be thrown.
|
|
45 |
*
|
|
46 |
* @param stackFrame the current stack frame
|
|
47 |
* @return the result of the <code>run</code> method
|
|
48 |
*/
|
|
49 | 1054 |
public final Object observe(Class stackFrame) {
|
50 | 1054 |
if (Boolean.TRUE.equals(get())) {
|
51 | 20 |
throw new CyclicDependencyException(stackFrame); |
52 |
} |
|
53 | 1034 |
Object result = null;
|
54 | 1034 |
try {
|
55 | 1034 |
set(Boolean.TRUE); |
56 | 1034 |
result = run(); |
57 |
} catch (final CyclicDependencyException e) {
|
|
58 | 40 |
e.push(stackFrame); |
59 | 40 |
throw e;
|
60 |
} finally {
|
|
61 | 1034 |
set(Boolean.FALSE); |
62 |
} |
|
63 | 880 |
return result;
|
64 |
} |
|
65 |
} |
|
66 |
|
|