Skip to content

Package: ContextClassloaderLocal

ContextClassloaderLocal

nameinstructionbranchcomplexitylinemethod
ContextClassloaderLocal()
M: 0 C: 8
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 2
100%
M: 0 C: 1
100%
createNewInstance()
M: 16 C: 3
16%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 2 C: 1
33%
M: 0 C: 1
100%
format(String, Object[])
M: 10 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 2 C: 0
0%
M: 1 C: 0
0%
get()
M: 0 C: 20
100%
M: 0 C: 2
100%
M: 0 C: 2
100%
M: 0 C: 6
100%
M: 0 C: 1
100%
getContextClassLoader()
M: 0 C: 6
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 1
100%
M: 0 C: 1
100%
set(Object)
M: 7 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 2 C: 0
0%
M: 1 C: 0
0%

Coverage

1: /*
2: * Copyright (c) 2014, 2022 Oracle and/or its affiliates. All rights reserved.
3: *
4: * This program and the accompanying materials are made available under the
5: * terms of the Eclipse Distribution License v. 1.0, which is available at
6: * http://www.eclipse.org/org/documents/edl-v10.php.
7: *
8: * SPDX-License-Identifier: BSD-3-Clause
9: */
10:
11: package com.sun.xml.messaging.saaj.soap;
12:
13: import java.security.AccessController;
14: import java.security.PrivilegedAction;
15: import java.text.MessageFormat;
16: import java.util.ResourceBundle;
17: import java.util.WeakHashMap;
18:
19: /**
20: * Simple utility ensuring that the value is cached only in case it is non-internal implementation
21: */
22: abstract class ContextClassloaderLocal<V> {
23:
24: private static final String FAILED_TO_CREATE_NEW_INSTANCE = "FAILED_TO_CREATE_NEW_INSTANCE";
25:
26: private WeakHashMap<ClassLoader, V> CACHE = new WeakHashMap<>();
27:
28: public V get() throws Error {
29: ClassLoader tccl = getContextClassLoader();
30: V instance = CACHE.get(tccl);
31:• if (instance == null) {
32: instance = createNewInstance();
33: CACHE.put(tccl, instance);
34: }
35: return instance;
36: }
37:
38: public void set(V instance) {
39: CACHE.put(getContextClassLoader(), instance);
40: }
41:
42: protected abstract V initialValue() throws Exception;
43:
44: private V createNewInstance() {
45: try {
46: return initialValue();
47: } catch (Exception e) {
48: throw new Error(format(FAILED_TO_CREATE_NEW_INSTANCE, getClass().getName()), e);
49: }
50: }
51:
52: private static String format(String property, Object... args) {
53: String text = ResourceBundle.getBundle(ContextClassloaderLocal.class.getName()).getString(property);
54: return MessageFormat.format(text, args);
55: }
56:
57: private static ClassLoader getContextClassLoader() {
58: return AccessController.doPrivileged(new PrivilegedAction<ClassLoader>() {
59: @Override
60: public ClassLoader run() {
61: ClassLoader cl = null;
62: try {
63: cl = Thread.currentThread().getContextClassLoader();
64: } catch (SecurityException ex) {
65: }
66: return cl;
67: }
68: });
69: }
70: }
71: