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         return contexts.get(Thread.currentThread().getThreadGroup());
 58     }
 59 
 60     private volatile boolean isDesignTime;
 61     private volatile Boolean isGuiAvailable;
 62 
 63     private Map<Class<?>, BeanInfo> beanInfoCache;
 64     private BeanInfoFinder beanInfoFinder;
 65     private PropertyEditorFinder propertyEditorFinder;
 66 
 67     private ThreadGroupContext() {
 68     }
 69 
 70     boolean isDesignTime() {
 71         return this.isDesignTime;
 72     }
 73 
 74     void setDesignTime(boolean isDesignTime) {
 75         this.isDesignTime = isDesignTime;
 76     }
 77 
 78 
 79     boolean isGuiAvailable() {
 80         Boolean isGuiAvailable = this.isGuiAvailable;
 81         return (isGuiAvailable != null)
 82                 ? isGuiAvailable.booleanValue()
 83                 : !GraphicsEnvironment.isHeadless();
 84     }
 85 
 86     void setGuiAvailable(boolean isGuiAvailable) {
 87         this.isGuiAvailable = Boolean.valueOf(isGuiAvailable);
 88     }
 89 
 90 
 91     synchronized BeanInfo getBeanInfo(Class<?> type) {
 92         return (this.beanInfoCache != null)
 93                 ? this.beanInfoCache.get(type)
 94                 : null;
 95     }
 96 
 97     synchronized BeanInfo putBeanInfo(Class<?> type, BeanInfo info) {
 98         if (this.beanInfoCache == null) {
 99             this.beanInfoCache = new WeakHashMap<>();
100         }
101         return this.beanInfoCache.put(type, info);
102     }
103 
104     synchronized void removeBeanInfo(Class<?> type) {
105         if (this.beanInfoCache != null) {
106             this.beanInfoCache.remove(type);
107         }
108     }
109 
110     synchronized void clearBeanInfoCache() {
111         if (this.beanInfoCache != null) {
112             this.beanInfoCache.clear();
113         }
114     }
115 
116 
117     synchronized BeanInfoFinder getBeanInfoFinder() {
118         if (this.beanInfoFinder == null) {
119             this.beanInfoFinder = new BeanInfoFinder();
120         }
121         return this.beanInfoFinder;
122     }
123 
124     synchronized PropertyEditorFinder getPropertyEditorFinder() {
125         if (this.propertyEditorFinder == null) {
126             this.propertyEditorFinder = new PropertyEditorFinder();
127         }
128         return this.propertyEditorFinder;
129     }
130 }