001 /**
002 * Copyright 2005-2013 The Kuali Foundation
003 *
004 * Licensed under the Educational Community License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 * http://www.opensource.org/licenses/ecl2.php
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 */
016 package org.kuali.rice.kcb.service;
017
018 import java.lang.reflect.Proxy;
019
020 import org.apache.log4j.Logger;
021 import org.kuali.rice.kcb.util.BeanFactoryInvocationHandler;
022 import org.springframework.beans.factory.BeanFactory;
023
024 /**
025 * Class that holds a singleton reference to KCBServiceLocator
026 * @author Kuali Rice Team (rice.collab@kuali.org)
027 */
028 public class GlobalKCBServiceLocator {
029 private static final Logger LOG = Logger.getLogger(GlobalKCBServiceLocator.class);
030
031 /**
032 * The KCBServiceLocator singleton
033 */
034 private static KCBServiceLocator locator;
035
036 /**
037 * The global initializer that constructs the KCBServiceLocator singleton
038 * @param beanFactory the beanFactory from which to construct the KCBServiceLocator
039 */
040 public static synchronized void init(BeanFactory beanFactory) {
041 // LOG.error("INITIALIZING", new Exception());
042 if (locator != null) {
043 throw new IllegalStateException("GlobalKCBServiceLocator already initialized");
044 }
045
046 locator = (KCBServiceLocator) Proxy.newProxyInstance(GlobalKCBServiceLocator.class.getClassLoader(),
047 new Class[] { KCBServiceLocator.class },
048 new BeanFactoryInvocationHandler(beanFactory));
049 }
050
051 /**
052 * Returns whether the GlobalKCBServiceLocator has already been initialized (classloader scoped)
053 * @return whether the GlobalKCBServiceLocator has already been initialized (classloader scoped)
054 */
055 public static synchronized boolean isInitialized() {
056 return locator != null;
057 }
058
059 /**
060 * Un-sets the KCBServiceLocator singleton, in order to fulfill a "lifecycle"
061 * contract (whereby init may be called again in the same class loader), specifically for
062 * unit tests.
063 */
064 public static synchronized void destroy() {
065 //LOG.debug("DESTROYING", new Exception());
066 locator = null;
067 }
068
069 /**
070 * Returns the KCBServiceLocator singleton
071 * @return the KCBServiceLocator singleton
072 */
073 public static synchronized KCBServiceLocator getInstance() {
074 return locator;
075 }
076 }