1 | |
|
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
|
12 | |
|
13 | |
|
14 | |
|
15 | |
|
16 | |
package org.kuali.rice.core.impl.lifecycle; |
17 | |
|
18 | |
import javax.xml.namespace.QName; |
19 | |
|
20 | |
import org.kuali.rice.core.api.lifecycle.BaseLifecycle; |
21 | |
import org.kuali.rice.core.api.lifecycle.Lifecycle; |
22 | |
import org.kuali.rice.core.api.resourceloader.GlobalResourceLoader; |
23 | |
|
24 | |
|
25 | |
|
26 | |
|
27 | |
|
28 | |
|
29 | |
|
30 | |
public class ServiceDelegatingLifecycle extends BaseLifecycle { |
31 | |
|
32 | 0 | private static final org.apache.log4j.Logger LOG = org.apache.log4j.Logger.getLogger(BaseLifecycle.class); |
33 | |
|
34 | |
private QName serviceName; |
35 | |
|
36 | 0 | public ServiceDelegatingLifecycle(QName serviceName) { |
37 | 0 | this.serviceName = serviceName; |
38 | 0 | } |
39 | |
|
40 | |
public ServiceDelegatingLifecycle(String serviceName) { |
41 | 0 | this(new QName(serviceName)); |
42 | 0 | } |
43 | |
|
44 | |
public void start() throws Exception { |
45 | 0 | if (!isStarted()) { |
46 | 0 | loadService(this.serviceName).start(); |
47 | |
} |
48 | 0 | super.start(); |
49 | 0 | } |
50 | |
|
51 | |
public void stop() throws Exception { |
52 | 0 | if (isStarted()) { |
53 | |
try { |
54 | 0 | Lifecycle lifecycle = loadService(this.serviceName); |
55 | 0 | if (lifecycle == null) { |
56 | 0 | LOG.warn("Couldn't stop service, failed to locate service with name " + serviceName); |
57 | |
} else { |
58 | 0 | lifecycle.stop(); |
59 | |
} |
60 | 0 | } catch (Exception e) { |
61 | 0 | LOG.warn("couldn't stop service " + this.serviceName); |
62 | 0 | throw e; |
63 | 0 | } |
64 | |
} |
65 | 0 | super.stop(); |
66 | 0 | } |
67 | |
|
68 | |
protected Lifecycle loadService(QName serviceName) { |
69 | 0 | Object service = GlobalResourceLoader.getService(serviceName); |
70 | 0 | if (service == null) { |
71 | 0 | return null; |
72 | |
} |
73 | 0 | if (!(service instanceof Lifecycle)) { |
74 | 0 | throw new RuntimeException("Service with name " + serviceName + " does not implement the Lifecycle interface!"); |
75 | |
} |
76 | 0 | return (Lifecycle) service; |
77 | |
} |
78 | |
|
79 | |
} |