001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017:
018: package org.apache.coyote.memory;
019:
020: import java.io.IOException;
021: import java.util.Iterator;
022: import org.apache.tomcat.util.buf.ByteChunk;
023:
024: import org.apache.coyote.Adapter;
025: import org.apache.coyote.InputBuffer;
026: import org.apache.coyote.OutputBuffer;
027: import org.apache.coyote.ProtocolHandler;
028: import org.apache.coyote.Request;
029: import org.apache.coyote.Response;
030:
031: /**
032: * Abstract the protocol implementation, including threading, etc.
033: * Processor is single threaded and specific to stream-based protocols,
034: * will not fit Jk protocols like JNI.
035: *
036: * @author Remy Maucherat
037: */
038: public class MemoryProtocolHandler implements ProtocolHandler {
039:
040: // ------------------------------------------------------------- Properties
041:
042: /**
043: * Pass config info.
044: */
045: public void setAttribute(String name, Object value) {
046: }
047:
048: public Object getAttribute(String name) {
049: return null;
050: }
051:
052: public Iterator getAttributeNames() {
053: return null;
054: }
055:
056: /**
057: * Associated adapter.
058: */
059: protected Adapter adapter = null;
060:
061: /**
062: * The adapter, used to call the connector.
063: */
064: public void setAdapter(Adapter adapter) {
065: this .adapter = adapter;
066: }
067:
068: public Adapter getAdapter() {
069: return (adapter);
070: }
071:
072: // ------------------------------------------------ ProtocolHandler Methods
073:
074: /**
075: * Init the protocol.
076: */
077: public void init() throws Exception {
078: }
079:
080: /**
081: * Start the protocol.
082: */
083: public void start() throws Exception {
084: }
085:
086: public void pause() throws Exception {
087: }
088:
089: public void resume() throws Exception {
090: }
091:
092: public void destroy() throws Exception {
093: }
094:
095: // --------------------------------------------------------- Public Methods
096:
097: /**
098: * Process specified request.
099: */
100: public void process(Request request, ByteChunk input,
101: Response response, ByteChunk output) throws Exception {
102:
103: InputBuffer inputBuffer = new ByteChunkInputBuffer(input);
104: OutputBuffer outputBuffer = new ByteChunkOutputBuffer(output);
105: request.setInputBuffer(inputBuffer);
106: response.setOutputBuffer(outputBuffer);
107:
108: adapter.service(request, response);
109:
110: }
111:
112: // --------------------------------------------- ByteChunkInputBuffer Class
113:
114: protected class ByteChunkInputBuffer implements InputBuffer {
115:
116: protected ByteChunk input = null;
117:
118: public ByteChunkInputBuffer(ByteChunk input) {
119: this .input = input;
120: }
121:
122: public int doRead(ByteChunk chunk, Request request)
123: throws IOException {
124: return input.substract(chunk);
125: }
126:
127: }
128:
129: // -------------------------------------------- ByteChunkOuptutBuffer Class
130:
131: protected class ByteChunkOutputBuffer implements OutputBuffer {
132:
133: protected ByteChunk output = null;
134:
135: public ByteChunkOutputBuffer(ByteChunk output) {
136: this .output = output;
137: }
138:
139: public int doWrite(ByteChunk chunk, Response response)
140: throws IOException {
141: output.append(chunk);
142: return chunk.getLength();
143: }
144:
145: }
146:
147: }
|