1 /*
  2  * Copyright (c) 2011, 2019, Oracle and/or its affiliates. All rights reserved.
  3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
  4  *
  5  * This code is free software; you can redistribute it and/or modify it
  6  * under the terms of the GNU General Public License version 2 only, as
  7  * published by the Free Software Foundation.  Oracle designates this
  8  * particular file as subject to the "Classpath" exception as provided
  9  * by Oracle in the LICENSE file that accompanied this code.
 10  *
 11  * This code is distributed in the hope that it will be useful, but WITHOUT
 12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
 14  * version 2 for more details (a copy is included in the LICENSE file that
 15  * accompanied this code).
 16  *
 17  * You should have received a copy of the GNU General Public License version
 18  * 2 along with this work; if not, write to the Free Software Foundation,
 19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
 20  *
 21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
 22  * or visit www.oracle.com if you need additional information or have any
 23  * questions.
 24  */
 25 
 26 package java.beans;
 27 
 28 import com.sun.beans.finder.BeanInfoFinder;
 29 import com.sun.beans.finder.PropertyEditorFinder;
 30 
 31 import java.awt.GraphicsEnvironment;
 32 import java.util.Map;
 33 import java.util.WeakHashMap;
 34 
 35 /**
 36  * The {@code ThreadGroupContext} is an application-dependent
 37  * context referenced by the specific {@link ThreadGroup}.
 38  * This is a replacement for the {@link sun.awt.AppContext}.
 39  *
 40  * @author  Sergey Malenkov
 41  */
 42 final class ThreadGroupContext {
 43 
 44     private static final WeakIdentityMap<ThreadGroupContext> contexts = new WeakIdentityMap<ThreadGroupContext>() {
 45         protected ThreadGroupContext create(Object key) {
 46             return new ThreadGroupContext();
 47         }
 48     };
 49 
 50     /**
 51      * Returns the appropriate {@code ThreadGroupContext} for the caller,
 52      * as determined by its {@code ThreadGroup}.
 53      *
 54      * @return  the application-dependent context
 55      */
 56     static ThreadGroupContext getContext() {
 57         @SuppressWarnings("deprecation")
 58         ThreadGroup group = Thread.currentThread().getThreadGroup();
 59         return contexts.get(group);
 60     }
 61 
 62     private volatile boolean isDesignTime;
 63     private volatile Boolean isGuiAvailable;
 64 
 65     private Map<Class<?>, BeanInfo> beanInfoCache;
 66     private BeanInfoFinder beanInfoFinder;
 67     private PropertyEditorFinder propertyEditorFinder;
 68 
 69     private ThreadGroupContext() {
 70     }
 71 
 72     boolean isDesignTime() {
 73         return this.isDesignTime;
 74     }
 75 
 76     void setDesignTime(boolean isDesignTime) {
 77         this.isDesignTime = isDesignTime;
 78     }
 79 
 80 
 81     boolean isGuiAvailable() {
 82         Boolean isGuiAvailable = this.isGuiAvailable;
 83         return (isGuiAvailable != null)
 84                 ? isGuiAvailable.booleanValue()
 85                 : !GraphicsEnvironment.isHeadless();
 86     }
 87 
 88     void setGuiAvailable(boolean isGuiAvailable) {
 89         this.isGuiAvailable = Boolean.valueOf(isGuiAvailable);
 90     }
 91 
 92 
 93     synchronized BeanInfo getBeanInfo(Class<?> type) {
 94         return (this.beanInfoCache != null)
 95                 ? this.beanInfoCache.get(type)
 96                 : null;
 97     }
 98 
 99     synchronized BeanInfo putBeanInfo(Class<?> type, BeanInfo info) {
100         if (this.beanInfoCache == null) {
101             this.beanInfoCache = new WeakHashMap<>();
102         }
103         return this.beanInfoCache.put(type, info);
104     }
105 
106     synchronized void removeBeanInfo(Class<?> type) {
107         if (this.beanInfoCache != null) {
108             this.beanInfoCache.remove(type);
109         }
110     }
111 
112     synchronized void clearBeanInfoCache() {
113         if (this.beanInfoCache != null) {
114             this.beanInfoCache.clear();
115         }
116     }
117 
118 
119     synchronized BeanInfoFinder getBeanInfoFinder() {
120         if (this.beanInfoFinder == null) {
121             this.beanInfoFinder = new BeanInfoFinder();
122         }
123         return this.beanInfoFinder;
124     }
125 
126     synchronized PropertyEditorFinder getPropertyEditorFinder() {
127         if (this.propertyEditorFinder == null) {
128             this.propertyEditorFinder = new PropertyEditorFinder();
129         }
130         return this.propertyEditorFinder;
131     }
132 }