1 | /* |
2 | * Copyright (c) 2003, 2004, 2005, 2006 Trustees of Indiana University. |
3 | * |
4 | * Licensed under the Educational Community License Version 1.0 (the "License"); By obtaining, |
5 | * using and/or copying this Original Work, you agree that you have read, understand, and will |
6 | * comply with the terms and conditions of the Educational Community License. |
7 | * |
8 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING |
9 | * BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE |
10 | * AND NONINFRINGEMENT. |
11 | * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES |
12 | * OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
13 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
14 | */ |
15 | package edu.iu.uis.sit.portal; |
16 | |
17 | import org.springframework.beans.factory.BeanFactory; |
18 | import org.springframework.beans.factory.access.BeanFactoryLocator; |
19 | import org.springframework.beans.factory.access.BeanFactoryReference; |
20 | import org.springframework.beans.factory.access.SingletonBeanFactoryLocator; |
21 | import org.springframework.context.ApplicationContext; |
22 | import org.springframework.context.ConfigurableApplicationContext; |
23 | |
24 | import edu.iu.uis.sit.portal.cache.service.CacheService; |
25 | import edu.iu.uis.sit.portal.portlet.personalize.services.PersonalizeService; |
26 | import edu.iu.uis.sit.portal.user.service.AclService; |
27 | import edu.iu.uis.sit.portal.user.service.UserService; |
28 | |
29 | public class SpringServiceLocator { |
30 | |
31 | private static ApplicationContext appContext; |
32 | |
33 | private static boolean initializationBegan = false; |
34 | |
35 | public static final String ACL_SRV = "myAclService"; |
36 | |
37 | public static final String USER_SRV = "myUserService"; |
38 | |
39 | public static final String PERSONALIZE_SRV = "myPersonalizeService"; |
40 | |
41 | public static final String CACHE_SRV = "myCacheService"; |
42 | |
43 | public static Object getService(String serviceName) { |
44 | if (appContext == null && !initializationBegan) { |
45 | initializationBegan = true; |
46 | initializeAppContexts("Spring.xml"); |
47 | } else if (appContext == null && initializationBegan) { |
48 | throw new RuntimeException("Spring not initialized properly. Initialization has begun and the application context is null." + "Probably spring loaded bean is trying to use SpringServiceLocator before the application context is initialized."); |
49 | } |
50 | |
51 | return appContext.getBean(serviceName); |
52 | } |
53 | |
54 | public static void close() { |
55 | if (appContext instanceof ConfigurableApplicationContext) { |
56 | ((ConfigurableApplicationContext) appContext).close(); |
57 | } |
58 | } |
59 | |
60 | public static void setAppContext(ApplicationContext newAppContext) { |
61 | appContext = newAppContext; |
62 | } |
63 | |
64 | public static ApplicationContext getAppContext() { |
65 | return appContext; |
66 | } |
67 | |
68 | protected static void initializeAppContexts(String rootContextFile) { |
69 | BeanFactoryLocator bfLocator = SingletonBeanFactoryLocator.getInstance(rootContextFile); |
70 | BeanFactoryReference bfReference = bfLocator.useBeanFactory("appContext"); |
71 | BeanFactory factory = bfReference.getFactory(); |
72 | appContext = (ApplicationContext) factory; |
73 | } |
74 | |
75 | public static AclService getAclService() { |
76 | return (AclService) getService(ACL_SRV); |
77 | } |
78 | |
79 | public static UserService getUserService() { |
80 | return (UserService) getService(USER_SRV); |
81 | } |
82 | |
83 | public static PersonalizeService getPersonalizeService() { |
84 | return (PersonalizeService) getService(PERSONALIZE_SRV); |
85 | } |
86 | |
87 | public static CacheService getCacheService() { |
88 | return (CacheService) getService(CACHE_SRV); |
89 | } |
90 | } |