1 /* 2 * Copyright 2010 the original author or authors. 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 package org.openehealth.ipf.commons.core.config; 17 18 import org.slf4j.Logger; 19 import org.slf4j.LoggerFactory; 20 21 import java.util.Collection; 22 23 /** 24 * Facade to an active registry, providing static access to their registered 25 * beans. 26 * <p/> 27 * The ContextFacade is supposed to be used by Groovy Extension Modules that 28 * require a global piece of configuration or context. As Extension Modules 29 * are stateless, they have to obtain this information at runtime by means of 30 * statically access, i.e. calling the getBean methods of this class. Before 31 * this can be done, the registry must be set by calling {@link #setRegistry(Registry)}. 32 * 33 * @since 2.5 34 */ 35 public class ContextFacade { 36 37 private static Registry instance; 38 private static Logger LOG = LoggerFactory.getLogger(ContextFacade.class); 39 40 public static synchronized void setRegistry(Registry registry) { 41 if (instance != null && !registry.equals(instance)) 42 LOG.info("Re-initializing the registry"); 43 instance = registry; 44 } 45 46 /** 47 * @param requiredType required bean type 48 * @return bean of the required type 49 * @since 2.5 50 */ 51 public static <B> B getBean(Class<B> requiredType) { 52 return instance.bean(requiredType); 53 } 54 55 /** 56 * @param requiredType required bean type 57 * @return bean of the required type 58 * @since 3.5 59 */ 60 public static <B> Collection<B> getBeans(Class<B> requiredType) { 61 return instance.beans(requiredType).values(); 62 } 63 64 /** 65 * @param beanName name 66 * @return bean of the required type 67 * @since 2.5 68 */ 69 @SuppressWarnings("unchecked") 70 public static <B> B getBean(String beanName) { 71 return (B) instance.bean(beanName); 72 } 73 74 /** 75 * Empties the registry 76 */ 77 public static synchronized void clearRegistry() { 78 instance = null; 79 } 80 81 }