View Javadoc

1   /*
2    *  Licensed to the Apache Software Foundation (ASF) under one
3    *  or more contributor license agreements.  See the NOTICE file
4    *  distributed with this work for additional information
5    *  regarding copyright ownership.  The ASF licenses this file
6    *  to you under the Apache License, Version 2.0 (the
7    *  "License"); you may not use this file except in compliance
8    *  with the License.  You may obtain a copy of the License at
9    *
10   *    http://www.apache.org/licenses/LICENSE-2.0
11   *
12   *  Unless required by applicable law or agreed to in writing,
13   *  software distributed under the License is distributed on an
14   *  "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   *  KIND, either express or implied.  See the License for the
16   *  specific language governing permissions and limitations
17   *  under the License.
18   *
19   */
20  package org.apache.mina.core.write;
21  
22  import java.io.IOException;
23  import java.util.ArrayList;
24  import java.util.Collection;
25  import java.util.Collections;
26  import java.util.LinkedHashMap;
27  import java.util.List;
28  import java.util.Set;
29  
30  import org.apache.mina.util.MapBackedSet;
31  
32  /**
33   * An exception which is thrown when one or more write operations were failed.
34   *
35   * @author The Apache MINA Project (dev@mina.apache.org)
36   * @version $Rev: 671827 $, $Date: 2008-06-26 10:49:48 +0200 (jeu, 26 jun 2008) $,
37   */
38  public class WriteException extends IOException {
39  
40      private static final long serialVersionUID = -4174407422754524197L;
41      
42      private final List<WriteRequest> requests;
43  
44      /**
45       * Creates a new exception.
46       */
47      public WriteException(WriteRequest request) {
48          super();
49          this.requests = asRequestList(request);
50      }
51  
52      /**
53       * Creates a new exception.
54       */
55      public WriteException(WriteRequest request, String s) {
56          super(s);
57          this.requests = asRequestList(request);
58      }
59  
60      /**
61       * Creates a new exception.
62       */
63      public WriteException(WriteRequest request, String message, Throwable cause) {
64          super(message);
65          initCause(cause);
66          this.requests = asRequestList(request);
67      }
68  
69      /**
70       * Creates a new exception.
71       */
72      public WriteException(WriteRequest request, Throwable cause) {
73          initCause(cause);
74          this.requests = asRequestList(request);
75      }
76  
77      /**
78       * Creates a new exception.
79       */
80      public WriteException(Collection<WriteRequest> requests) {
81          super();
82          this.requests = asRequestList(requests);
83      }
84  
85      /**
86       * Creates a new exception.
87       */
88      public WriteException(Collection<WriteRequest> requests, String s) {
89          super(s);
90          this.requests = asRequestList(requests);
91      }
92  
93      /**
94       * Creates a new exception.
95       */
96      public WriteException(Collection<WriteRequest> requests, String message, Throwable cause) {
97          super(message);
98          initCause(cause);
99          this.requests = asRequestList(requests);
100     }
101 
102     /**
103      * Creates a new exception.
104      */
105     public WriteException(Collection<WriteRequest> requests, Throwable cause) {
106         initCause(cause);
107         this.requests = asRequestList(requests);
108     }
109 
110     /**
111      * Returns the list of the failed {@link WriteRequest}, in the order of occurrance.
112      */
113     public List<WriteRequest> getRequests() {
114         return requests;
115     }
116 
117     /**
118      * Returns the firstly failed {@link WriteRequest}. 
119      */
120     public WriteRequest getRequest() {
121         return requests.get(0);
122     }
123     
124     private static List<WriteRequest> asRequestList(Collection<WriteRequest> requests) {
125         if (requests == null) {
126             throw new NullPointerException("requests");
127         }
128         if (requests.isEmpty()) {
129             throw new IllegalArgumentException("requests is empty.");
130         }
131 
132         // Create a list of requests removing duplicates.
133         Set<WriteRequest> newRequests = new MapBackedSet<WriteRequest>(new LinkedHashMap<WriteRequest, Boolean>());
134         for (WriteRequest r: requests) {
135             newRequests.add(r.getOriginalRequest());
136         }
137         
138         return Collections.unmodifiableList(new ArrayList<WriteRequest>(newRequests));
139     }
140 
141     private static List<WriteRequest> asRequestList(WriteRequest request) {
142         if (request == null) {
143             throw new NullPointerException("request");
144         }
145         
146         List<WriteRequest> requests = new ArrayList<WriteRequest>(1);
147         requests.add(request.getOriginalRequest());
148         return Collections.unmodifiableList(requests);
149     }
150 }