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.springframework.beans.factory.FactoryBean;
22 import org.springframework.beans.factory.InitializingBean;
23
24 import javax.xml.namespace.QName;
25
26
27
28
29
30
31
32 public class GlobalResourceLoaderServiceFactoryBean implements FactoryBean<Object>, InitializingBean {
33
34 private String serviceNamespace;
35 private String serviceName;
36 private boolean singleton;
37 private boolean mustExist;
38
39
40 private boolean isFetchingService = false;
41
42 public GlobalResourceLoaderServiceFactoryBean() {
43 this.mustExist = true;
44 }
45
46 public Object getObject() throws Exception {
47 if (isFetchingService) return null;
48 isFetchingService = true;
49 try {
50 Object service = null;
51 if (StringUtils.isBlank(getServiceNamespace())) {
52 service = GlobalResourceLoader.getService(this.getServiceName());
53 } else {
54 service = GlobalResourceLoader.getService(new QName(getServiceNamespace(), getServiceName()));
55 }
56 if (mustExist && service == null) {
57 throw new IllegalStateException("Service must exist and no service could be located with serviceNamespace='" + getServiceNamespace() + "' and name='" + this.getServiceName() + "'");
58 }
59 return service;
60 } finally {
61 isFetchingService = false;
62 }
63 }
64
65 public Class<?> getObjectType() {
66 return Object.class;
67 }
68
69 public boolean isSingleton() {
70 return singleton;
71 }
72
73 public String getServiceNamespace() {
74 return serviceNamespace;
75 }
76
77 public void setServiceNamespace(String serviceNamespace) {
78 this.serviceNamespace = serviceNamespace;
79 }
80
81 public String getServiceName() {
82 return serviceName;
83 }
84
85 public void setServiceName(String serviceName) {
86 this.serviceName = serviceName;
87 }
88
89 public void setSingleton(boolean singleton) {
90 this.singleton = singleton;
91 }
92
93 public boolean isMustExist() {
94 return mustExist;
95 }
96
97 public void setMustExist(boolean mustExist) {
98 this.mustExist = mustExist;
99 }
100
101
102 public void afterPropertiesSet() throws Exception {
103 if (StringUtils.isBlank(this.getServiceName())) {
104 throw new ConfigurationException("No serviceName given.");
105 }
106 }
107
108 }