Clover coverage report - dom4j - 1.6.1
Coverage timestamp: ma mei 16 2005 14:23:01 GMT+01:00

file stats: | LOC: | 103 |  | Methods: | 4
| NCLOC: | 36 |  | Classes: | 1

Source file | Conditionals | Statements | Methods | TOTAL

PerThreadSingleton.java

100%

73,3%

75%

76,2%

1 |  | /*
2 |  | * Copyright 2001-2005 (C) MetaStuff, Ltd. All Rights Reserved.
3 |  | *
4 |  | * This software is open source.
5 |  | * See the bottom of this file for the licence.
6 |  | */
7 |  |
8 |  | package org.dom4j.util;
9 |  |
10 |  | import java.lang.ref.WeakReference;
11 |  |
12 |  | /**
13 |  | * <p>
14 |  | * <code>PerThreadSingleton</code> is an implementation of the
15 |  | * SingletonStrategy used to provide common factory access to a single object
16 |  | * instance based on an implementation strategy for one object instance per
17 |  | * thread. This is useful in replace of the ThreadLocal usage.
18 |  | * </p>
19 |  | *
20 |  | * @author <a href="mailto:ddlucas@users.sourceforge.net">David Lucas </a>
21 |  | * @version $Revision: 1.3 $
22 |  | */
23 |  |
24 |  | public class PerThreadSingleton implements SingletonStrategy {
25 |  | private String singletonClassName = null;
26 |  |
27 |  | private ThreadLocal perThreadCache = new ThreadLocal();
28 |  |
29 | 2 | public PerThreadSingleton() {
30 |  | }
31 |  |
32 | 0 | public void reset() {
33 | 0 | perThreadCache = new ThreadLocal();
34 |  | }
35 |  |
36 | 1003 | public Object instance() {
37 | 1003 | Object singletonInstancePerThread = null;
38 |  | // use weak reference to prevent cyclic reference during GC
39 | 1003 | WeakReference ref = (WeakReference) perThreadCache.get();
40 |  | // singletonInstancePerThread=perThreadCache.get();
41 |  | // if (singletonInstancePerThread==null) {
42 | 1003 | if (ref == null || ref.get() == null) {
43 | 6 | Class clazz = null;
44 | 6 | try {
45 | 6 | clazz = Thread.currentThread().getContextClassLoader().loadClass(
46 |  | singletonClassName);
47 | 6 | singletonInstancePerThread = clazz.newInstance();
48 |  | } catch (Exception ignore) {
49 | 0 | try {
50 | 0 | clazz = Class.forName(singletonClassName);
51 | 0 | singletonInstancePerThread = clazz.newInstance();
52 |  | } catch (Exception ignore2) {
53 |  | }
54 |  | }
55 | 6 | perThreadCache.set(new WeakReference(singletonInstancePerThread));
56 |  | } else {
57 | 997 | singletonInstancePerThread = ref.get();
58 |  | }
59 | 1003 | return singletonInstancePerThread;
60 |  | }
61 |  |
62 | 2 | public void setSingletonClassName(String singletonClassName) {
63 | 2 | this.singletonClassName = singletonClassName;
64 |  | }
65 |  |
66 |  | }
67 |  |
68 |  | /*
69 |  | * Redistribution and use of this software and associated documentation
70 |  | * ("Software"), with or without modification, are permitted provided that the
71 |  | * following conditions are met:
72 |  | *
73 |  | * 1. Redistributions of source code must retain copyright statements and
74 |  | * notices. Redistributions must also contain a copy of this document.
75 |  | *
76 |  | * 2. Redistributions in binary form must reproduce the above copyright notice,
77 |  | * this list of conditions and the following disclaimer in the documentation
78 |  | * and/or other materials provided with the distribution.
79 |  | *
80 |  | * 3. The name "DOM4J" must not be used to endorse or promote products derived
81 |  | * from this Software without prior written permission of MetaStuff, Ltd. For
82 |  | * written permission, please contact dom4j-info@metastuff.com.
83 |  | *
84 |  | * 4. Products derived from this Software may not be called "DOM4J" nor may
85 |  | * "DOM4J" appear in their names without prior written permission of MetaStuff,
86 |  | * Ltd. DOM4J is a registered trademark of MetaStuff, Ltd.
87 |  | *
88 |  | * 5. Due credit should be given to the DOM4J Project - http://www.dom4j.org
89 |  | *
90 |  | * THIS SOFTWARE IS PROVIDED BY METASTUFF, LTD. AND CONTRIBUTORS ``AS IS'' AND
91 |  | * ANY EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
92 |  | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
93 |  | * ARE DISCLAIMED. IN NO EVENT SHALL METASTUFF, LTD. OR ITS CONTRIBUTORS BE
94 |  | * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
95 |  | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
96 |  | * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
97 |  | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
98 |  | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
99 |  | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
100 |  | * POSSIBILITY OF SUCH DAMAGE.
101 |  | *
102 |  | * Copyright 2001-2005 (C) MetaStuff, Ltd. All Rights Reserved.
103 |  | */