01: /**********************************************************************************
02: * $URL: * $URL: https://source.sakaiproject.org/svn/sam/trunk/component/src/java/org/sakaiproject/spring/SpringBeanLocator.java $
03: * $Id: SpringBeanLocator.java 13802 2006-08-16 23:13:50Z ktsao@stanford.edu $
04: ***********************************************************************************
05: *
06: * Copyright (c) 2005, 2006 The Sakai Foundation.
07: *
08: * Licensed under the Educational Community License, Version 1.0 (the"License");
09: * you may not use this file except in compliance with the License.
10: * You may obtain a copy of the License at
11: *
12: * http://www.opensource.org/licenses/ecl1.php
13: *
14: * Unless required by applicable law or agreed to in writing, software
15: * distributed under the License is distributed on an "AS IS" BASIS,
16: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17: * See the License for the specific language governing permissions and
18: * limitations under the License.
19: *
20: **********************************************************************************/package org.sakaiproject.spring;
21:
22: import org.springframework.web.context.WebApplicationContext;
23: import org.springframework.context.ConfigurableApplicationContext;
24:
25: public class SpringBeanLocator {
26:
27: //private static Log log = LogFactory.getLog(SpringBeanLocator.class);
28: private static WebApplicationContext waCtx = null;
29: private static ConfigurableApplicationContext caCtx = null;
30: private static boolean inWebContext = false;
31: private static SpringBeanLocator instance = null;
32:
33: public static SpringBeanLocator getInstance() {
34: if (instance != null) {
35: return instance;
36: } else {
37: return new SpringBeanLocator();
38: }
39: }
40:
41: /**
42: * For integration inside a web context
43: * @param context the WebApplicationContext
44: */
45: public static void setApplicationContext(
46: WebApplicationContext context) {
47: SpringBeanLocator.waCtx = context;
48: SpringBeanLocator.inWebContext = true;
49: }
50:
51: /**
52: * Support unit testing via a ConfigurableApplicationContext concrete subclass
53: * such as FileSystemXmlApplicationContext
54: * @param ca
55: */
56: public static void setConfigurableApplicationContext(
57: ConfigurableApplicationContext ca) {
58: SpringBeanLocator.caCtx = ca;
59: SpringBeanLocator.inWebContext = false;
60: }
61:
62: public Object getBean(String name) {
63: if (inWebContext) {
64: //log.info("** context in Locator " + waCtx);
65: return waCtx.getBean(name);
66: } else {
67: //log.info("** context in Locator " + caCtx);
68: return caCtx.getBean(name);
69: }
70:
71: }
72:
73: }
|