1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.kuali.rice.krad.config;
17
18 import org.apache.commons.lang.StringUtils;
19 import org.kuali.rice.core.api.config.ConfigurationException;
20 import org.kuali.rice.core.api.resourceloader.GlobalResourceLoader;
21 import org.kuali.rice.core.framework.util.ApplicationThreadLocal;
22 import org.springframework.beans.factory.FactoryBean;
23 import org.springframework.beans.factory.InitializingBean;
24
25 import javax.xml.namespace.QName;
26
27
28
29
30
31
32
33 public class GlobalResourceLoaderServiceFactoryBean implements FactoryBean<Object>, InitializingBean {
34
35 private String serviceNamespace;
36 private String serviceName;
37 private boolean singleton;
38 private boolean mustExist;
39
40
41 private ThreadLocal<Boolean> isFetchingService = new ApplicationThreadLocal<Boolean>() {
42 @Override
43 protected Boolean initialValue() {
44 return false;
45 }
46 };
47
48 public GlobalResourceLoaderServiceFactoryBean() {
49 this.mustExist = true;
50 }
51
52 public Object getObject() throws Exception {
53 if (isFetchingService.get()) return null;
54 isFetchingService.set(true);
55 try {
56 Object service = null;
57 if (StringUtils.isBlank(getServiceNamespace())) {
58 service = GlobalResourceLoader.getService(this.getServiceName());
59 } else {
60 service = GlobalResourceLoader.getService(new QName(getServiceNamespace(), getServiceName()));
61 }
62 if (mustExist && service == null) {
63 throw new IllegalStateException("Service must exist and no service could be located with serviceNamespace='" + getServiceNamespace() + "' and name='" + this.getServiceName() + "'");
64 }
65 return service;
66 } finally {
67 isFetchingService.remove();
68 }
69 }
70
71 public Class<?> getObjectType() {
72 return Object.class;
73 }
74
75 public boolean isSingleton() {
76 return singleton;
77 }
78
79 public String getServiceNamespace() {
80 return serviceNamespace;
81 }
82
83 public void setServiceNamespace(String serviceNamespace) {
84 this.serviceNamespace = serviceNamespace;
85 }
86
87 public String getServiceName() {
88 return serviceName;
89 }
90
91 public void setServiceName(String serviceName) {
92 this.serviceName = serviceName;
93 }
94
95 public void setSingleton(boolean singleton) {
96 this.singleton = singleton;
97 }
98
99 public boolean isMustExist() {
100 return mustExist;
101 }
102
103 public void setMustExist(boolean mustExist) {
104 this.mustExist = mustExist;
105 }
106
107
108 public void afterPropertiesSet() throws Exception {
109 if (StringUtils.isBlank(this.getServiceName())) {
110 throw new ConfigurationException("No serviceName given.");
111 }
112 }
113
114 }