ContextClassloaderLocal.java revision 524:dcaa586ab756
1193323Sed/*
2193323Sed * Copyright (c) 2014, Oracle and/or its affiliates. All rights reserved.
3193323Sed * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4193323Sed *
5193323Sed * This code is free software; you can redistribute it and/or modify it
6193323Sed * under the terms of the GNU General Public License version 2 only, as
7193323Sed * published by the Free Software Foundation.  Oracle designates this
8193323Sed * particular file as subject to the "Classpath" exception as provided
9193323Sed * by Oracle in the LICENSE file that accompanied this code.
10193323Sed *
11193323Sed * This code is distributed in the hope that it will be useful, but WITHOUT
12193323Sed * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13193323Sed * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
14193323Sed * version 2 for more details (a copy is included in the LICENSE file that
15193323Sed * accompanied this code).
16193323Sed *
17193323Sed * You should have received a copy of the GNU General Public License version
18249423Sdim * 2 along with this work; if not, write to the Free Software Foundation,
19193323Sed * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20193323Sed *
21193323Sed * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22193323Sed * or visit www.oracle.com if you need additional information or have any
23218893Sdim * questions.
24193323Sed */
25193323Sed
26193323Sedpackage com.sun.xml.internal.xsom.util;
27193323Sed
28193323Sedimport java.security.AccessController;
29218893Sdimimport java.security.PrivilegedAction;
30193323Sedimport java.text.MessageFormat;
31193323Sedimport java.util.ResourceBundle;
32193323Sedimport java.util.WeakHashMap;
33193323Sed
34193323Sed/**
35193323Sed * Simple utility ensuring that the value is cached only in case it is non-internal implementation
36193323Sed */
37193323Sedabstract class ContextClassloaderLocal<V> {
38234353Sdim
39193323Sed    private static final String FAILED_TO_CREATE_NEW_INSTANCE = "FAILED_TO_CREATE_NEW_INSTANCE";
40218893Sdim
41193323Sed    private WeakHashMap<ClassLoader, V> CACHE = new WeakHashMap<ClassLoader, V>();
42234353Sdim
43234353Sdim    public V get() throws Error {
44234353Sdim        ClassLoader tccl = getContextClassLoader();
45193323Sed        V instance = CACHE.get(tccl);
46193323Sed        if (instance == null) {
47193323Sed            instance = createNewInstance();
48193323Sed            CACHE.put(tccl, instance);
49193323Sed        }
50193323Sed        return instance;
51234353Sdim    }
52193323Sed
53193323Sed    public void set(V instance) {
54193323Sed        CACHE.put(getContextClassLoader(), instance);
55193323Sed    }
56193323Sed
57193323Sed    protected abstract V initialValue() throws Exception;
58193323Sed
59234353Sdim    private V createNewInstance() {
60193323Sed        try {
61193323Sed            return initialValue();
62193323Sed        } catch (Exception e) {
63193323Sed            throw new Error(format(FAILED_TO_CREATE_NEW_INSTANCE, getClass().getName()), e);
64239462Sdim        }
65193323Sed    }
66193323Sed
67198892Srdivacky    private static String format(String property, Object... args) {
68207618Srdivacky        String text = ResourceBundle.getBundle(ContextClassloaderLocal.class.getName()).getString(property);
69207618Srdivacky        return MessageFormat.format(text, args);
70193323Sed    }
71193323Sed
72193323Sed    private static ClassLoader getContextClassLoader() {
73193323Sed        return (ClassLoader)
74193323Sed                AccessController.doPrivileged(new PrivilegedAction() {
75234353Sdim                    public Object run() {
76193323Sed                        ClassLoader cl = null;
77193323Sed                        try {
78193323Sed                            cl = Thread.currentThread().getContextClassLoader();
79193323Sed                        } catch (SecurityException ex) {
80193323Sed                        }
81218893Sdim                        return cl;
82193323Sed                    }
83193323Sed                });
84193323Sed    }
85193323Sed}
86234353Sdim