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