001: /**
002: * Copyright 2006 Webmedia Group Ltd.
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License");
005: * you may not use this file except in compliance with the License.
006: * You may obtain a copy of the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License.
015: **/package org.araneaframework.framework.container;
016:
017: import org.apache.commons.logging.Log;
018: import org.apache.commons.logging.LogFactory;
019: import org.araneaframework.InputData;
020: import org.araneaframework.OutputData;
021: import org.araneaframework.Path;
022: import org.araneaframework.core.ApplicationService;
023: import org.araneaframework.core.ApplicationWidget;
024: import org.araneaframework.core.StandardPath;
025: import org.araneaframework.framework.core.BaseFilterWidget;
026:
027: /**
028: * Service that contains a widget.
029: *
030: * @author "Toomas Römer" <toomas@webmedia.ee>
031: */
032: public class StandardWidgetAdapterService extends BaseFilterWidget {
033: private static final Log log = LogFactory
034: .getLog(StandardWidgetAdapterService.class);
035:
036: /**
037: * If <code>propagateAsAction(InputData)</code> returns true then the action is
038: * propagated to the child. Otherwise if the request is the first one then:
039: * <ul>
040: * <il><code>update(input)</code></il>
041: * <il><code>event(path, input)</code></il>
042: * </ul>
043: * are called on the child, if not then just <code>render(output).</code>
044: */
045: protected void action(Path path, InputData input, OutputData output)
046: throws Exception {
047: if (hasAction(input)) {
048: Path actionPath = getActionPath(input);
049: if (log.isDebugEnabled())
050: log.debug("Routing action to widget '"
051: + actionPath.toString() + "'");
052: childWidget._getService().action(actionPath, input, output);
053: } else {
054: if (log.isDebugEnabled())
055: log
056: .debug("Translating action() call to widget update()/event()/render() calls.");
057:
058: childWidget._getWidget().update(input);
059: if (hasEvent(input)) {
060: Path eventPath = getEventPath(input);
061: if (log.isDebugEnabled())
062: log.debug("Routing event to widget '"
063: + eventPath.toString() + "'");
064: childWidget._getWidget().event(eventPath, input);
065: }
066: childWidget._getWidget().render(output);
067: }
068: }
069:
070: /**
071: * Extracts the path from the input and returns it. This implementation uses
072: * the {@link ApplicationWidget#EVENT_PATH_KEY} parameter in the request and expects the event path to be
073: * a dot-separated string.
074: *
075: * @since 1.1
076: */
077: protected Path getEventPath(InputData input) {
078: return new StandardPath((String) input.getGlobalData().get(
079: ApplicationWidget.EVENT_PATH_KEY));
080: }
081:
082: /**
083: * Returns true if the request contains an event.
084: *
085: * @since 1.1
086: */
087: protected boolean hasEvent(InputData input) {
088: return input.getGlobalData().get(
089: ApplicationWidget.EVENT_PATH_KEY) != null;
090: }
091:
092: /**
093: * Extracts the path from the input and returns it. This implementation uses
094: * the {@link ApplicationService#ACTION_PATH_KEY} parameter in the request and expects the action path to be
095: * a dot-separated string.
096: *
097: * @since 1.1
098: */
099: protected Path getActionPath(InputData input) {
100: return new StandardPath((String) input.getGlobalData().get(
101: ApplicationService.ACTION_PATH_KEY));
102: }
103:
104: /**
105: * Returns true if the request contains an action.
106: *
107: * @since 1.1
108: */
109: protected boolean hasAction(InputData input) {
110: return input.getGlobalData().get(
111: ApplicationService.ACTION_PATH_KEY) != null;
112: }
113:
114: }
|