01: /**
02: * Copyright 2006 Webmedia Group Ltd.
03: *
04: * Licensed under the Apache License, Version 2.0 (the "License");
05: * you may not use this file except in compliance with the License.
06: * You may obtain a copy of the License at
07: *
08: * http://www.apache.org/licenses/LICENSE-2.0
09: *
10: * Unless required by applicable law or agreed to in writing, software
11: * distributed under the License is distributed on an "AS IS" BASIS,
12: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13: * See the License for the specific language governing permissions and
14: * limitations under the License.
15: **/package org.araneaframework.framework.container;
16:
17: import org.araneaframework.InputData;
18: import org.araneaframework.Message;
19: import org.araneaframework.OutputData;
20: import org.araneaframework.Path;
21: import org.araneaframework.Service;
22: import org.araneaframework.core.BaseWidget;
23: import org.araneaframework.core.StandardPath;
24:
25: /**
26: * A widget that contains a child service. Calls the service's action only if it gets
27: * an event.
28: *
29: * @author "Toomas Römer" <toomas@webmedia.ee>
30: */
31: public class StandardServiceAdapterWidget extends BaseWidget {
32: public static final String ACTION_PATH_INPUT_DATA_PARAMETER = "widgetSubServiceActionId";
33:
34: private Service childService;
35: private boolean eventReceived;
36: private transient InputData input;
37:
38: /**
39: * Set the child service.
40: */
41: public void setChildService(Service service) {
42: childService = service;
43: }
44:
45: protected void init() throws Exception {
46: childService._getComponent().init(getScope(), getEnvironment());
47: }
48:
49: /**
50: * Returns the path of action from the InputData. Uses the
51: * {@link StandardServiceAdapterWidget#ACTION_PATH_INPUT_DATA_PARAMETER} to get the path.
52: */
53: protected Path getActionPath(InputData input) {
54: return new StandardPath((String) input.getGlobalData().get(
55: ACTION_PATH_INPUT_DATA_PARAMETER));
56: }
57:
58: public void update(InputData input) {
59: this .input = input;
60: eventReceived = false;
61: }
62:
63: protected void propagate(Message message) throws Exception {
64: message.send(null, childService);
65: }
66:
67: public void event(Path path, InputData input) {
68: if (!path.hasNext()) {
69: eventReceived = true;
70: }
71: }
72:
73: /**
74: * Calls child service's action only if an event was received. The action path
75: * is constructed via <code>getActionPath(InputData)</code>. The InputData is
76: * saved in the <code>update(InputData)</code> method.
77: *
78: * TODO: why is it in render and not in event() ?
79: */
80: public void render(OutputData output) throws Exception {
81: if (eventReceived) {
82: this .childService._getService().action(
83: getActionPath(input), input, output);
84: }
85: }
86:
87: protected void destroy() throws Exception {
88: childService._getComponent().destroy();
89: }
90: }
|