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.kuali.rice.core.api.config.ConfigurationException;
19 import org.kuali.rice.core.api.resourceloader.GlobalResourceLoader;
20 import org.springframework.beans.factory.FactoryBean;
21 import org.springframework.beans.factory.InitializingBean;
22
23
24
25
26
27
28
29 public class GlobalResourceLoaderServiceFactoryBean implements FactoryBean<Object>, InitializingBean {
30
31 private String serviceName;
32 private boolean singleton;
33 private boolean mustExist;
34
35
36 private boolean isFetchingService = false;
37
38 public GlobalResourceLoaderServiceFactoryBean() {
39 this.mustExist = true;
40 }
41
42 public Object getObject() throws Exception {
43 if (isFetchingService) return null;
44 isFetchingService = true;
45 try {
46 Object service = GlobalResourceLoader.getService(this.getServiceName());
47 if (mustExist && service == null) {
48 throw new IllegalStateException("Service must exist and no service could be located with name: " + this.getServiceName());
49 }
50 return service;
51 } finally {
52 isFetchingService = false;
53 }
54 }
55
56 public Class<?> getObjectType() {
57 return Object.class;
58 }
59
60 public boolean isSingleton() {
61 return singleton;
62 }
63
64 public String getServiceName() {
65 return serviceName;
66 }
67
68 public void setServiceName(String serviceName) {
69 this.serviceName = serviceName;
70 }
71
72 public void setSingleton(boolean singleton) {
73 this.singleton = singleton;
74 }
75
76 public boolean isMustExist() {
77 return mustExist;
78 }
79
80 public void setMustExist(boolean mustExist) {
81 this.mustExist = mustExist;
82 }
83
84
85 public void afterPropertiesSet() throws Exception {
86 if (this.getServiceName() == null) {
87 throw new ConfigurationException("No serviceName given.");
88 }
89 }
90
91 }