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.http.core;
016:
017: import javax.servlet.http.HttpServletRequest;
018: import javax.servlet.http.HttpServletResponse;
019: import org.araneaframework.Component;
020: import org.araneaframework.Environment;
021: import org.araneaframework.InputData;
022: import org.araneaframework.OutputData;
023: import org.araneaframework.Service;
024: import org.araneaframework.core.BaseComponent;
025: import org.araneaframework.core.BaseEnvironment;
026: import org.araneaframework.http.HttpInputData;
027: import org.araneaframework.http.HttpOutputData;
028: import org.araneaframework.http.ServletServiceAdapterComponent;
029:
030: /**
031: * <p>
032: * Creates a StandardServletInputData and StandardServletOutputData from the
033: * HttpServletRequest and HttpServletResponse respectively and routes the
034: * request to the child services using a null Path.
035: * </p>
036: * <p>
037: * Since <emphasis>1.0.3</emphasis> this adapter makes {@link OutputData} and
038: * {@link InputData} accessible from {@link Component}'s {@link Environment}:
039: *
040: * <p>
041: * <code>
042: * InputData input = (InputData)getEnvironment().getEntry(InputData.class);<br>
043: * OutputData input = (OutputData)getEnvironment().getEntry(OutputData.class);
044: * </code>
045: * </p>
046: *
047: * which allows access to request from {@link BaseComponent}'s initialization
048: * callback—<code>init()</code>.
049: * </p>
050: *
051: * @author "Toomas Römer" <toomas@webmedia.ee>
052: */
053: public class StandardServletServiceAdapterComponent extends
054: BaseComponent implements ServletServiceAdapterComponent {
055:
056: private Service childService;
057:
058: private static final ThreadLocal localInput = new ThreadLocal();
059: private static final ThreadLocal localOutput = new ThreadLocal();
060:
061: protected void init() throws Exception {
062: childService._getComponent().init(getScope(),
063: new BaseEnvironment() {
064:
065: public Object getEntry(Object key) {
066: if (InputData.class.equals(key))
067: return localInput.get();
068: if (OutputData.class.equals(key))
069: return localOutput.get();
070: return getEnvironment().getEntry(key);
071: }
072: });
073: }
074:
075: public void setChildService(Service service) {
076: childService = service;
077: }
078:
079: protected void destroy() throws Exception {
080: childService._getComponent().destroy();
081: }
082:
083: public void service(HttpServletRequest request,
084: HttpServletResponse response) {
085: HttpInputData input = new StandardServletInputData(request);
086: localInput.set(input);
087: HttpOutputData output = new StandardServletOutputData(request,
088: response);
089: localOutput.set(output);
090:
091: try {
092: request.setAttribute(InputData.INPUT_DATA_KEY, input);
093: request.setAttribute(OutputData.OUTPUT_DATA_KEY, output);
094:
095: childService._getService().action(null, input, output);
096: } finally {
097: localInput.set(null);
098: localOutput.set(null);
099: }
100: }
101: }
|