View Javadoc
1   /**
2    * Copyright 2005-2016 The Kuali Foundation
3    *
4    * Licensed under the Educational Community 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.opensource.org/licenses/ecl2.php
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.kuali.rice.kcb.service;
17  
18  import java.lang.reflect.Proxy;
19  
20  import org.apache.log4j.Logger;
21  import org.kuali.rice.kcb.util.BeanFactoryInvocationHandler;
22  import org.springframework.beans.factory.BeanFactory;
23  
24  /**
25   * Class that holds a singleton reference to KCBServiceLocator 
26   * @author Kuali Rice Team (rice.collab@kuali.org)
27   */
28  public class GlobalKCBServiceLocator {
29      private static final Logger LOG = Logger.getLogger(GlobalKCBServiceLocator.class);
30  
31      /**
32       * The KCBServiceLocator singleton
33       */
34      private static KCBServiceLocator locator;
35  
36      /**
37       * The global initializer that constructs the KCBServiceLocator singleton
38       * @param beanFactory the beanFactory from which to construct the KCBServiceLocator
39       */
40      public static synchronized void init(BeanFactory beanFactory) {
41  //        LOG.error("INITIALIZING", new Exception());
42          if (locator != null) {
43              throw new IllegalStateException("GlobalKCBServiceLocator already initialized");
44          }
45  
46          locator = (KCBServiceLocator) Proxy.newProxyInstance(GlobalKCBServiceLocator.class.getClassLoader(),
47                                                               new Class[] { KCBServiceLocator.class },
48                                                               new BeanFactoryInvocationHandler(beanFactory));
49      }
50  
51      /**
52       * Returns whether the GlobalKCBServiceLocator has already been initialized (classloader scoped)
53       * @return whether the GlobalKCBServiceLocator has already been initialized (classloader scoped)
54       */
55      public static synchronized boolean isInitialized() {
56          return locator != null;
57      }
58  
59      /**
60       * Un-sets the KCBServiceLocator singleton, in order to fulfill a "lifecycle"
61       * contract (whereby init may be called again in the same class loader), specifically for
62       * unit tests.
63       */
64      public static synchronized void destroy() {
65          //LOG.debug("DESTROYING", new Exception());
66          locator = null;
67      }
68  
69      /**
70       * Returns the KCBServiceLocator singleton
71       * @return the KCBServiceLocator singleton
72       */
73      public static synchronized KCBServiceLocator getInstance() {
74          return locator;
75      }
76  }