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.common;
021:
022: import java.io.IOException;
023: import java.util.ArrayList;
024: import java.util.Collection;
025: import java.util.Collections;
026: import java.util.LinkedHashMap;
027: import java.util.List;
028: import java.util.Set;
029:
030: import org.apache.mina.util.MapBackedSet;
031:
032: /**
033: * An exception which is thrown when one or more write operations were failed.
034: *
035: * @author The Apache MINA Project (dev@mina.apache.org)
036: * @version $Rev: 594773 $, $Date: 2007-11-13 22:27:14 -0700 (Tue, 13 Nov 2007) $,
037: */
038: public class WriteException extends IOException {
039:
040: private static final long serialVersionUID = -4174407422754524197L;
041:
042: private final List<WriteRequest> requests;
043:
044: /**
045: * Creates a new exception.
046: */
047: public WriteException(WriteRequest request) {
048: super ();
049: this .requests = asRequestList(request);
050: }
051:
052: /**
053: * Creates a new exception.
054: */
055: public WriteException(WriteRequest request, String s) {
056: super (s);
057: this .requests = asRequestList(request);
058: }
059:
060: /**
061: * Creates a new exception.
062: */
063: public WriteException(WriteRequest request, String message,
064: Throwable cause) {
065: super (message);
066: initCause(cause);
067: this .requests = asRequestList(request);
068: }
069:
070: /**
071: * Creates a new exception.
072: */
073: public WriteException(WriteRequest request, Throwable cause) {
074: initCause(cause);
075: this .requests = asRequestList(request);
076: }
077:
078: /**
079: * Creates a new exception.
080: */
081: public WriteException(Collection<WriteRequest> requests) {
082: super ();
083: this .requests = asRequestList(requests);
084: }
085:
086: /**
087: * Creates a new exception.
088: */
089: public WriteException(Collection<WriteRequest> requests, String s) {
090: super (s);
091: this .requests = asRequestList(requests);
092: }
093:
094: /**
095: * Creates a new exception.
096: */
097: public WriteException(Collection<WriteRequest> requests,
098: String message, Throwable cause) {
099: super (message);
100: initCause(cause);
101: this .requests = asRequestList(requests);
102: }
103:
104: /**
105: * Creates a new exception.
106: */
107: public WriteException(Collection<WriteRequest> requests,
108: Throwable cause) {
109: initCause(cause);
110: this .requests = asRequestList(requests);
111: }
112:
113: /**
114: * Returns the list of the failed {@link WriteRequest}, in the order of occurrance.
115: */
116: public List<WriteRequest> getRequests() {
117: return requests;
118: }
119:
120: /**
121: * Returns the firstly failed {@link WriteRequest}.
122: */
123: public WriteRequest getRequest() {
124: return requests.get(0);
125: }
126:
127: private static List<WriteRequest> asRequestList(
128: Collection<WriteRequest> requests) {
129: if (requests == null) {
130: throw new NullPointerException("requests");
131: }
132: if (requests.isEmpty()) {
133: throw new IllegalArgumentException("requests is empty.");
134: }
135:
136: // Create a list of requests removing duplicates.
137: Set<WriteRequest> newRequests = new MapBackedSet<WriteRequest>(
138: new LinkedHashMap<WriteRequest, Boolean>());
139: for (WriteRequest r : requests) {
140: newRequests.add(r.getOriginalRequest());
141: }
142:
143: return Collections
144: .unmodifiableList(new ArrayList<WriteRequest>(
145: newRequests));
146: }
147:
148: private static List<WriteRequest> asRequestList(WriteRequest request) {
149: if (request == null) {
150: throw new NullPointerException("request");
151: }
152:
153: List<WriteRequest> requests = new ArrayList<WriteRequest>(1);
154: requests.add(request.getOriginalRequest());
155: return Collections.unmodifiableList(requests);
156: }
157: }
|