1 | |
|
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
|
12 | |
|
13 | |
|
14 | |
|
15 | |
|
16 | |
|
17 | |
package org.kuali.rice.ksb.messaging; |
18 | |
|
19 | |
import java.io.ByteArrayInputStream; |
20 | |
import java.io.ByteArrayOutputStream; |
21 | |
import java.io.IOException; |
22 | |
import java.io.ObjectInputStream; |
23 | |
import java.io.ObjectOutput; |
24 | |
import java.io.ObjectOutputStream; |
25 | |
|
26 | |
import org.apache.log4j.Logger; |
27 | |
import org.kuali.rice.core.config.ConfigContext; |
28 | |
import org.kuali.rice.core.exception.RiceRuntimeException; |
29 | |
import org.kuali.rice.ksb.messaging.serviceconnectors.ServiceConnectorFactory; |
30 | |
|
31 | |
|
32 | |
|
33 | |
|
34 | |
|
35 | |
|
36 | |
|
37 | |
|
38 | |
public class RemotedServiceHolder implements ServiceHolder { |
39 | |
|
40 | |
|
41 | 0 | private static final Logger LOG = Logger.getLogger(RemotedServiceHolder.class); |
42 | |
|
43 | |
private Object service; |
44 | |
private ServiceInfo serviceInfo; |
45 | |
|
46 | 0 | public RemotedServiceHolder(ServiceInfo entry) { |
47 | 0 | this.setServiceInfo(entry); |
48 | 0 | } |
49 | |
|
50 | |
public ServiceInfo getServiceInfo() { |
51 | 0 | return this.serviceInfo; |
52 | |
} |
53 | |
|
54 | |
public void setServiceInfo(ServiceInfo entry) { |
55 | 0 | if (ConfigContext.getCurrentContextConfig().getDevMode()) { |
56 | 0 | this.serviceInfo = cloneServiceInfo(entry); |
57 | |
} else { |
58 | 0 | this.serviceInfo = entry; |
59 | |
} |
60 | 0 | } |
61 | |
|
62 | |
public Object getService() throws Exception { |
63 | 0 | synchronized (this) { |
64 | 0 | if (this.service == null) { |
65 | 0 | this.setService(ServiceConnectorFactory.getServiceConnector(serviceInfo).getService()); |
66 | |
} |
67 | 0 | } |
68 | 0 | return this.service; |
69 | |
} |
70 | |
|
71 | |
public void setService(Object service) { |
72 | 0 | this.service = service; |
73 | 0 | } |
74 | |
|
75 | |
|
76 | |
|
77 | |
|
78 | |
|
79 | |
|
80 | |
|
81 | |
|
82 | |
|
83 | |
|
84 | |
private static ServiceInfo cloneServiceInfo(final ServiceInfo serviceInfo) { |
85 | |
|
86 | 0 | ObjectInputStream in = null; |
87 | 0 | ObjectOutput out = null; |
88 | 0 | Object tempService = serviceInfo.getServiceDefinition().getService(); |
89 | |
|
90 | |
try { |
91 | 0 | serviceInfo.getServiceDefinition().setService(null); |
92 | 0 | ByteArrayOutputStream bos = new ByteArrayOutputStream(); |
93 | 0 | out = new ObjectOutputStream(bos); |
94 | 0 | out.writeObject(serviceInfo); |
95 | |
|
96 | 0 | in = new ObjectInputStream(new ByteArrayInputStream(bos.toByteArray())); |
97 | 0 | return (ServiceInfo) in.readObject(); |
98 | 0 | } catch (Exception e) { |
99 | 0 | throw new RiceRuntimeException(e); |
100 | |
} finally { |
101 | |
|
102 | 0 | serviceInfo.getServiceDefinition().setService(tempService); |
103 | 0 | if (in != null) { |
104 | |
try { |
105 | 0 | in.close(); |
106 | 0 | } catch (IOException ioe) { |
107 | 0 | LOG.info("failed to close InputStream", ioe); |
108 | 0 | } |
109 | |
} |
110 | 0 | if (out != null) { |
111 | |
try { |
112 | 0 | out.close(); |
113 | 0 | } catch (IOException ioe) { |
114 | 0 | LOG.info("Failed to close OutputStream", ioe); |
115 | 0 | } |
116 | |
} |
117 | |
|
118 | |
} |
119 | |
} |
120 | |
} |