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.axis2.transport.http.server;
021:
022: import org.apache.commons.logging.Log;
023: import org.apache.commons.logging.LogFactory;
024: import org.apache.http.ConnectionClosedException;
025: import org.apache.http.HttpException;
026: import org.apache.http.protocol.HttpContext;
027: import org.apache.http.protocol.HttpExecutionContext;
028:
029: import edu.emory.mathcs.backport.java.util.concurrent.atomic.AtomicBoolean;
030: import edu.emory.mathcs.backport.java.util.concurrent.atomic.AtomicLong;
031:
032: import java.io.IOException;
033: import java.net.SocketException;
034: import java.net.SocketTimeoutException;
035:
036: /**
037: * I/O processor intended to process requests and fill in responses.
038: *
039: * @author Chuck Williams
040: */
041: public class HttpServiceProcessor implements IOProcessor {
042:
043: private static final Log LOG = LogFactory
044: .getLog(HttpServiceProcessor.class);
045:
046: /** Counter used to create unique IDs. */
047: private static AtomicLong counter = new AtomicLong(0L);
048:
049: private AtomicBoolean terminated;
050:
051: private final AxisHttpService httpservice;
052:
053: private final AxisHttpConnection conn;
054:
055: private final IOProcessorCallback callback;
056:
057: /**
058: * Unique identifier used by {@linkplain #equals(Object)} and
059: * {@linkplain #hashCode()}.
060: * <p>
061: * This field is needed to allow the equals method to work properly when this
062: * HttpServiceProcessor has to be removed from the list of processors.
063: *
064: * @see DefaultHttpConnectionManager
065: */
066: private final long id;
067:
068: public HttpServiceProcessor(final AxisHttpService httpservice,
069: final AxisHttpConnection conn,
070: final IOProcessorCallback callback) {
071: super ();
072: this .httpservice = httpservice;
073: this .conn = conn;
074: this .callback = callback;
075: this .terminated = new AtomicBoolean(false);
076:
077: id = counter.incrementAndGet();
078: }
079:
080: public void run() {
081: LOG.debug("New connection thread");
082: HttpContext context = new HttpExecutionContext(null);
083: try {
084: while (!Thread.interrupted() && !isDestroyed()
085: && this .conn.isOpen()) {
086: this .httpservice.handleRequest(this .conn, context);
087: }
088: } catch (ConnectionClosedException ex) {
089: LOG.debug("Client closed connection");
090: } catch (IOException ex) {
091: if (ex instanceof SocketTimeoutException) {
092: LOG.debug(ex.getMessage());
093: } else if (ex instanceof SocketException) {
094: LOG.debug(ex.getMessage());
095: } else {
096: LOG.warn(ex.getMessage(), ex);
097: }
098: } catch (HttpException ex) {
099: if (LOG.isWarnEnabled()) {
100: LOG.warn("HTTP protocol error: " + ex.getMessage());
101: }
102: } finally {
103: destroy();
104: if (this .callback == null) {
105: throw new NullPointerException(
106: "The callback object can't be null");
107: }
108: this .callback.completed(this );
109: }
110: }
111:
112: public void close() throws IOException {
113: this .conn.close();
114: }
115:
116: public void destroy() {
117: if (this .terminated.compareAndSet(false, true)) {
118: try {
119: // this.conn.shutdown();
120: close();
121: } catch (IOException ex) {
122: LOG.debug("I/O error shutting down connection");
123: }
124: }
125: }
126:
127: public boolean isDestroyed() {
128: return this .terminated.get();
129: }
130:
131: // -------------------------------------------------- Methods from Object
132:
133: /**
134: * Returns the unique ID of this HttpServiceProcessor.
135: *
136: * @return The unique ID of this HttpServiceProcessor.
137: */
138: public int hashCode() {
139: final int PRIME = 31;
140: int result = 1;
141: result = PRIME * result + (int) (id ^ (id >>> 32));
142: return result;
143: }
144:
145: /**
146: * Indicates whether some other object is "equal to" this one.
147: *
148: * @return <code>true</code> if this HttpServiceProcessor refere to the same
149: * object as obj or they have the same {@linkplain #id}, <code>false</code> otherwise.
150: */
151: public boolean equals(Object obj) {
152: if (this == obj)
153: return true;
154: if (obj == null)
155: return false;
156: if (getClass() != obj.getClass())
157: return false;
158: final HttpServiceProcessor other = (HttpServiceProcessor) obj;
159: if (id != other.id)
160: return false;
161: return true;
162: }
163:
164: }
|