1
2
3
4 package org.rcfaces.core.internal.service;
5
6 import java.util.HashMap;
7 import java.util.Map;
8
9 import javax.faces.component.UIComponent;
10 import javax.faces.event.FacesListener;
11
12 import org.apache.commons.logging.Log;
13 import org.apache.commons.logging.LogFactory;
14 import org.rcfaces.core.component.ServiceComponent;
15 import org.rcfaces.core.event.IServiceEventListener;
16 import org.rcfaces.core.event.ServiceEvent;
17 import org.rcfaces.core.lang.ApplicationException;
18 import org.rcfaces.core.progressMonitor.IProgressMonitor;
19 import org.rcfaces.core.progressMonitor.SubProgressMonitor;
20
21
22
23
24
25
26 public class ClientServiceRegistryImpl implements IClientServiceRegistry {
27 private static final String REVISION = "$Revision: 1.18 $";
28
29 private static final Log LOG = LogFactory
30 .getLog(ClientServiceRegistryImpl.class);
31
32 private final Map clientServicesByRequestId = new HashMap(32);
33
34 public IClientService getClientServiceById(String requestId) {
35 return (IClientService) clientServicesByRequestId.get(requestId);
36 }
37
38 public IClientService createClientService(String requestId,
39 ServiceComponent component, Object parameter, int syncMode)
40 throws ClientServiceException {
41
42 IClientService clientService = new ClientServiceImpl(requestId,
43 component, parameter,
44 syncMode == IClientServiceRegistry.ASYNC_MODE);
45
46 return clientService;
47 }
48
49 public void startClientService(IClientService clientService) {
50 }
51
52 public Object waitClientService(IClientService clientService,
53 IProgressMonitor progressMonitor) {
54 ClientServiceImpl clientServiceImpl = (ClientServiceImpl) clientService;
55
56 ServiceComponent serviceComponent = clientServiceImpl.popComponent();
57
58 FacesListener fls[] = serviceComponent.listServiceEventListeners();
59
60 ClientServiceEventReturnValue event = new ClientServiceEventReturnValue(
61 serviceComponent, clientServiceImpl.popParameter(),
62 progressMonitor, fls.length);
63
64 Object returnValue = null;
65 for (int i = 0; i < fls.length; i++) {
66 IServiceEventListener serviceEventListener = (IServiceEventListener) fls[i];
67
68 event.beginListener(i);
69
70 serviceEventListener.processServiceEvent(event);
71
72 event.endListener(i);
73
74 ApplicationException applicationException = event
75 .getApplicationException();
76 if (applicationException != null) {
77
78 if (LOG.isDebugEnabled()) {
79 LOG.debug("Throw application exception.",
80 applicationException);
81 }
82
83 throw applicationException;
84 }
85
86 returnValue = event.getReturnValue();
87
88 if (LOG.isDebugEnabled()) {
89 LOG.debug("Return value of event ='" + returnValue + "'.");
90 }
91
92 if (returnValue != null) {
93 break;
94 }
95 }
96
97 event.listenersDone();
98
99 return returnValue;
100 }
101
102 public void releaseClientService(IClientService clientService) {
103 clientServicesByRequestId.remove(clientService.getClientServiceId());
104 }
105
106
107
108
109
110
111 private static class ClientServiceImpl implements IClientService {
112 private static final String REVISION = "$Revision: 1.18 $";
113
114 private String clientServiceId;
115
116 private int status;
117
118 private int progress;
119
120 private int errorCode;
121
122 private String errorMessage;
123
124 private Object parameter;
125
126 private boolean asyncMode;
127
128 private ServiceComponent component;
129
130 ClientServiceImpl(String requestId, ServiceComponent component,
131 Object parameter, boolean asyncMode) {
132 this.clientServiceId = requestId;
133 this.parameter = parameter;
134 this.asyncMode = asyncMode;
135 this.component = component;
136 }
137
138 public String getClientServiceId() {
139 return clientServiceId;
140 }
141
142 public int getStatus() {
143 return status;
144 }
145
146 public int getProgress() {
147 return progress;
148 }
149
150 public int getErrorCode() {
151 return errorCode;
152 }
153
154 public String getErrorMessage() {
155 return errorMessage;
156 }
157
158 public final Object popParameter() {
159 Object parameter = this.parameter;
160 this.parameter = null;
161
162 return parameter;
163 }
164
165 public final boolean isAsyncMode() {
166 return asyncMode;
167 }
168
169 public final ServiceComponent popComponent() {
170 ServiceComponent serviceComponent = this.component;
171 this.component = null;
172
173 return serviceComponent;
174 }
175
176 public void cancel() {
177
178
179 }
180
181 }
182
183
184
185
186
187
188 private static final class ClientServiceEventReturnValue extends
189 ServiceEvent implements IEventReturnValue,
190 IApplicationExceptionCapability {
191
192 private static final String REVISION = "$Revision: 1.18 $";
193
194 private static final long serialVersionUID = 8740362936333831513L;
195
196 private final int nbListeners;
197
198 private int listenerIndex = 0;
199
200 private Object returnValue;
201
202 private IProgressMonitor progressMonitor;
203
204 private ApplicationException applicationException;
205
206 public ClientServiceEventReturnValue(UIComponent component,
207 Object data, IProgressMonitor progressMonitor, int nbListeners) {
208 super(component, data);
209
210 if (progressMonitor != null) {
211 progressMonitor = new SubProgressMonitor(progressMonitor,
212 nbListeners);
213 }
214 this.progressMonitor = progressMonitor;
215 this.nbListeners = nbListeners;
216 }
217
218 public void listenersDone() {
219 if (progressMonitor == null) {
220 return;
221 }
222
223 progressMonitor.done();
224 }
225
226 public void beginListener(int i) {
227 listenerIndex = i;
228 }
229
230 public void endListener(int i) {
231 if (progressMonitor == null) {
232 return;
233 }
234
235 getProgressMonitor().done();
236
237 resetProgressMonitor();
238 }
239
240 public void setReturnValue(Object ret) {
241
242 if (LOG.isDebugEnabled()) {
243 LOG.debug("Set return value to '" + ret + "'.");
244 }
245
246 this.returnValue = ret;
247 }
248
249 public Object getReturnValue() {
250 return returnValue;
251 }
252
253 public ApplicationException getApplicationException() {
254 return applicationException;
255 }
256
257 public void setApplicationException(
258 ApplicationException applicationException) {
259 this.applicationException = applicationException;
260 }
261
262 protected IProgressMonitor createProgressMonitor() {
263 if (progressMonitor == null) {
264 return super.createProgressMonitor();
265 }
266
267 return new SubProgressMonitor(progressMonitor, 1);
268 }
269
270 }
271 }