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   */
37  public class WriteException extends IOException {
38  
39      private static final long serialVersionUID = -4174407422754524197L;
40      
41      private final List<WriteRequest> requests;
42  
43      /**
44       * Creates a new exception.
45       */
46      public WriteException(WriteRequest request) {
47          super();
48          this.requests = asRequestList(request);
49      }
50  
51      /**
52       * Creates a new exception.
53       */
54      public WriteException(WriteRequest request, String s) {
55          super(s);
56          this.requests = asRequestList(request);
57      }
58  
59      /**
60       * Creates a new exception.
61       */
62      public WriteException(WriteRequest request, String message, Throwable cause) {
63          super(message);
64          initCause(cause);
65          this.requests = asRequestList(request);
66      }
67  
68      /**
69       * Creates a new exception.
70       */
71      public WriteException(WriteRequest request, Throwable cause) {
72          initCause(cause);
73          this.requests = asRequestList(request);
74      }
75  
76      /**
77       * Creates a new exception.
78       */
79      public WriteException(Collection<WriteRequest> requests) {
80          super();
81          this.requests = asRequestList(requests);
82      }
83  
84      /**
85       * Creates a new exception.
86       */
87      public WriteException(Collection<WriteRequest> requests, String s) {
88          super(s);
89          this.requests = asRequestList(requests);
90      }
91  
92      /**
93       * Creates a new exception.
94       */
95      public WriteException(Collection<WriteRequest> requests, String message, Throwable cause) {
96          super(message);
97          initCause(cause);
98          this.requests = asRequestList(requests);
99      }
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 NullPointerException("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 NullPointerException("request");
143         }
144         
145         List<WriteRequest> requests = new ArrayList<WriteRequest>(1);
146         requests.add(request.getOriginalRequest());
147         return Collections.unmodifiableList(requests);
148     }
149 }