查看本类的 API文档回源码主页即时通讯网 - 即时通讯开发者社区!
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 failed.
34   *
35   * @author <a href="http://mina.apache.org">Apache MINA Project</a>
36   */
37  public class WriteException extends IOException {
38      /** The mandatory serialVersionUUID */
39      private static final long serialVersionUID = -4174407422754524197L;
40  
41      /** The list of WriteRequest stored in this exception */
42      private final List<WriteRequest> requests;
43  
44      /**
45       * Creates a new exception.
46       * 
47       * @param request The associated {@link WriteRequest}
48       */
49      public WriteException(WriteRequest request) {
50          super();
51          this.requests = asRequestList(request);
52      }
53  
54      /**
55       * Creates a new exception.
56       * 
57       * @param request The associated {@link WriteRequest}
58       * @param message The detail message
59       */
60      public WriteException(WriteRequest request, String message) {
61          super(message);
62          this.requests = asRequestList(request);
63      }
64  
65      /**
66       * Creates a new exception.
67       * 
68       * @param request The associated {@link WriteRequest}
69       * @param message The detail message
70       * @param cause The Exception's cause
71       */
72      public WriteException(WriteRequest request, String message, Throwable cause) {
73          super(message);
74          initCause(cause);
75          this.requests = asRequestList(request);
76      }
77  
78      /**
79       * Creates a new exception.
80       * 
81       * @param request The associated {@link WriteRequest}
82       * @param cause The Exception's cause
83       */
84      public WriteException(WriteRequest request, Throwable cause) {
85          initCause(cause);
86          this.requests = asRequestList(request);
87      }
88  
89      /**
90       * Creates a new exception.
91       * 
92       * @param requests The collection of {@link WriteRequest}s
93       */
94      public WriteException(Collection<WriteRequest> requests) {
95          super();
96          this.requests = asRequestList(requests);
97      }
98  
99      /**
100      * Creates a new exception.
101      * 
102      * @param requests The collection of {@link WriteRequest}s
103      * @param message The detail message
104      */
105     public WriteException(Collection<WriteRequest> requests, String message) {
106         super(message);
107         this.requests = asRequestList(requests);
108     }
109 
110     /**
111      * Creates a new exception.
112      * 
113      * @param requests The collection of {@link WriteRequest}s
114      * @param message The detail message
115      * @param cause The Exception's cause
116      */
117     public WriteException(Collection<WriteRequest> requests, String message, Throwable cause) {
118         super(message);
119         initCause(cause);
120         this.requests = asRequestList(requests);
121     }
122 
123     /**
124      * Creates a new exception.
125      * 
126      * @param requests The collection of {@link WriteRequest}s
127      * @param cause The Exception's cause
128      */
129     public WriteException(Collection<WriteRequest> requests, Throwable cause) {
130         initCause(cause);
131         this.requests = asRequestList(requests);
132     }
133 
134     /**
135      * @return the list of the failed {@link WriteRequest}, in the order of occurrence.
136      */
137     public List<WriteRequest> getRequests() {
138         return requests;
139     }
140 
141     /**
142      * @return the firstly failed {@link WriteRequest}. 
143      */
144     public WriteRequest getRequest() {
145         return requests.get(0);
146     }
147 
148     private static List<WriteRequest> asRequestList(Collection<WriteRequest> requests) {
149         if (requests == null) {
150             throw new IllegalArgumentException("requests");
151         }
152         
153         if (requests.isEmpty()) {
154             throw new IllegalArgumentException("requests is empty.");
155         }
156 
157         // Create a list of requests removing duplicates.
158         Set<WriteRequest> newRequests = new MapBackedSet<WriteRequest>(new LinkedHashMap<WriteRequest, Boolean>());
159         
160         for (WriteRequest r : requests) {
161             newRequests.add(r.getOriginalRequest());
162         }
163 
164         return Collections.unmodifiableList(new ArrayList<WriteRequest>(newRequests));
165     }
166 
167     private static List<WriteRequest> asRequestList(WriteRequest request) {
168         if (request == null) {
169             throw new IllegalArgumentException("request");
170         }
171 
172         List<WriteRequest> requests = new ArrayList<WriteRequest>(1);
173         requests.add(request.getOriginalRequest());
174         
175         return Collections.unmodifiableList(requests);
176     }
177 }