001: /*
002: * Copyright 1999-2004 The Apache Software Foundation
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: */
016:
017: package org.apache.coyote.memory;
018:
019: import java.io.IOException;
020:
021: import org.apache.tomcat.util.buf.ByteChunk;
022:
023: import org.apache.coyote.Adapter;
024: import org.apache.coyote.InputBuffer;
025: import org.apache.coyote.OutputBuffer;
026: import org.apache.coyote.ProtocolHandler;
027: import org.apache.coyote.Request;
028: import org.apache.coyote.Response;
029:
030: /**
031: * Abstract the protocol implementation, including threading, etc.
032: * Processor is single threaded and specific to stream-based protocols,
033: * will not fit Jk protocols like JNI.
034: *
035: * @author Remy Maucherat
036: */
037: public class MemoryProtocolHandler implements ProtocolHandler {
038:
039: // ------------------------------------------------------------- Properties
040:
041: /**
042: * Pass config info.
043: */
044: public void setAttribute(String name, Object value) {
045: }
046:
047: public Object getAttribute(String name) {
048: return null;
049: }
050:
051: /**
052: * Associated adapter.
053: */
054: protected Adapter adapter = null;
055:
056: /**
057: * The adapter, used to call the connector.
058: */
059: public void setAdapter(Adapter adapter) {
060: this .adapter = adapter;
061: }
062:
063: public Adapter getAdapter() {
064: return (adapter);
065: }
066:
067: // ------------------------------------------------ ProtocolHandler Methods
068:
069: /**
070: * Init the protocol.
071: */
072: public void init() throws Exception {
073: }
074:
075: /**
076: * Start the protocol.
077: */
078: public void start() throws Exception {
079: }
080:
081: public void pause() throws Exception {
082: }
083:
084: public void resume() throws Exception {
085: }
086:
087: public void destroy() throws Exception {
088: }
089:
090: // --------------------------------------------------------- Public Methods
091:
092: /**
093: * Process specified request.
094: */
095: public void process(Request request, ByteChunk input,
096: Response response, ByteChunk output) throws Exception {
097:
098: InputBuffer inputBuffer = new ByteChunkInputBuffer(input);
099: OutputBuffer outputBuffer = new ByteChunkOutputBuffer(output);
100: request.setInputBuffer(inputBuffer);
101: response.setOutputBuffer(outputBuffer);
102:
103: adapter.service(request, response);
104:
105: }
106:
107: // --------------------------------------------- ByteChunkInputBuffer Class
108:
109: protected class ByteChunkInputBuffer implements InputBuffer {
110:
111: protected ByteChunk input = null;
112:
113: public ByteChunkInputBuffer(ByteChunk input) {
114: this .input = input;
115: }
116:
117: public int doRead(ByteChunk chunk, Request request)
118: throws IOException {
119: return input.substract(chunk);
120: }
121:
122: }
123:
124: // -------------------------------------------- ByteChunkOuptutBuffer Class
125:
126: protected class ByteChunkOutputBuffer implements OutputBuffer {
127:
128: protected ByteChunk output = null;
129:
130: public ByteChunkOutputBuffer(ByteChunk output) {
131: this .output = output;
132: }
133:
134: public int doWrite(ByteChunk chunk, Response response)
135: throws IOException {
136: output.append(chunk);
137: return chunk.getLength();
138: }
139:
140: }
141:
142: }
|