001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one
003: * or more contributor license agreements. See the NOTICE file
004: * distributed with this work for additional information
005: * regarding copyright ownership. The ASF licenses this file
006: * to you under the Apache License, Version 2.0 (the
007: * "License"); you may not use this file except in compliance
008: * with the License. You may obtain a copy of the License at
009: *
010: * http://www.apache.org/licenses/LICENSE-2.0
011: *
012: * Unless required by applicable law or agreed to in writing,
013: * software distributed under the License is distributed on an
014: * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
015: * KIND, either express or implied. See the License for the
016: * specific language governing permissions and limitations
017: * under the License.
018: *
019: */
020: package org.apache.mina.filter.codec.statemachine;
021:
022: import java.util.ArrayList;
023: import java.util.List;
024:
025: import org.apache.mina.common.IoBuffer;
026: import org.apache.mina.filter.codec.ProtocolDecoderOutput;
027: import org.slf4j.Logger;
028: import org.slf4j.LoggerFactory;
029:
030: /**
031: *
032: * @author The Apache MINA Project (dev@mina.apache.org)
033: * @version $Rev: 602837 $, $Date: 2007-12-10 03:03:42 -0700 (Mon, 10 Dec 2007) $
034: */
035: public abstract class DecodingStateMachine implements DecodingState {
036: private final Logger log = LoggerFactory
037: .getLogger(DecodingStateMachine.class);
038:
039: private final List<Object> childProducts = new ArrayList<Object>();
040:
041: private final ProtocolDecoderOutput childOutput = new ProtocolDecoderOutput() {
042: public void flush() {
043: }
044:
045: public void write(Object message) {
046: childProducts.add(message);
047: }
048: };
049:
050: private DecodingState currentState;
051: private boolean initialized;
052:
053: protected abstract DecodingState init() throws Exception;
054:
055: protected abstract DecodingState finishDecode(
056: List<Object> childProducts, ProtocolDecoderOutput out)
057: throws Exception;
058:
059: protected abstract void destroy() throws Exception;
060:
061: public DecodingState decode(IoBuffer in, ProtocolDecoderOutput out)
062: throws Exception {
063: DecodingState state = getCurrentState();
064:
065: final int limit = in.limit();
066: int pos = in.position();
067:
068: try {
069: for (;;) {
070: // Wait for more data if all data is consumed.
071: if (pos == limit) {
072: break;
073: }
074:
075: DecodingState oldState = state;
076: state = state.decode(in, childOutput);
077:
078: // If finished, call finishDecode
079: if (state == null) {
080: return finishDecode(childProducts, out);
081: }
082:
083: int newPos = in.position();
084:
085: // Wait for more data if nothing is consumed and state didn't change.
086: if (newPos == pos && oldState == state) {
087: break;
088: }
089: pos = newPos;
090: }
091:
092: return this ;
093: } catch (Exception e) {
094: state = null;
095: throw e;
096: } finally {
097: this .currentState = state;
098:
099: // Destroy if decoding is finished or failed.
100: if (state == null) {
101: cleanup();
102: }
103: }
104: }
105:
106: public DecodingState finishDecode(ProtocolDecoderOutput out)
107: throws Exception {
108: DecodingState nextState;
109: DecodingState state = getCurrentState();
110: try {
111: for (;;) {
112: DecodingState oldState = state;
113: state = state.finishDecode(childOutput);
114: if (state == null) {
115: // Finished
116: break;
117: }
118:
119: // Exit if state didn't change.
120: if (oldState == state) {
121: break;
122: }
123: }
124: } catch (Exception e) {
125: state = null;
126: log
127: .debug(
128: "Ignoring the exception caused by a closed session.",
129: e);
130: } finally {
131: this .currentState = state;
132: nextState = finishDecode(childProducts, out);
133: if (state == null) {
134: cleanup();
135: }
136: }
137: return nextState;
138: }
139:
140: private void cleanup() {
141: if (!initialized) {
142: throw new IllegalStateException();
143: }
144:
145: initialized = false;
146: childProducts.clear();
147: try {
148: destroy();
149: } catch (Exception e2) {
150: log.warn("Failed to destroy a decoding state machine.", e2);
151: }
152: }
153:
154: private DecodingState getCurrentState() throws Exception {
155: DecodingState state = this .currentState;
156: if (state == null) {
157: state = init();
158: initialized = true;
159: }
160: return state;
161: }
162: }
|