001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017: package org.apache.catalina.tribes;
018:
019: import java.util.ArrayList;
020:
021: /**
022: * Channel Exception<br>
023: * A channel exception is thrown when an internal error happens
024: * somewhere in the channel. <br>
025: * When a global error happens, the cause can be retrieved using <code>getCause()</code><br><br>
026: * If an application is sending a message and some of the recipients fail to receive it,
027: * the application can retrieve what recipients failed by using the <code>getFaultyMembers()</code>
028: * method. This way, an application will always know if a message was delivered successfully or not.
029: * @author Filip Hanik
030: * @version $Revision: 532400 $, $Date: 2007-04-25 18:16:37 +0200 (mer., 25 avr. 2007) $
031: */
032:
033: public class ChannelException extends Exception {
034: /**
035: * Empty list to avoid reinstatiating lists
036: */
037: protected static final FaultyMember[] EMPTY_LIST = new FaultyMember[0];
038: /*
039: * Holds a list of faulty members
040: */
041: private ArrayList faultyMembers = null;
042:
043: /**
044: * Constructor, creates a ChannelException
045: * @see java.lang.Exception#Exception()
046: */
047: public ChannelException() {
048: super ();
049: }
050:
051: /**
052: * Constructor, creates a ChannelException with an error message
053: * @see java.lang.Exception#Exception(String)
054: */
055: public ChannelException(String message) {
056: super (message);
057: }
058:
059: /**
060: * Constructor, creates a ChannelException with an error message and a cause
061: * @param message String
062: * @param cause Throwable
063: * @see java.lang.Exception#Exception(String,Throwable)
064: */
065: public ChannelException(String message, Throwable cause) {
066: super (message, cause);
067: }
068:
069: /**
070: * Constructor, creates a ChannelException with a cause
071: * @param cause Throwable
072: * @see java.lang.Exception#Exception(Throwable)
073: */
074: public ChannelException(Throwable cause) {
075: super (cause);
076: }
077:
078: /**
079: * Returns the message for this exception
080: * @return String
081: * @see java.lang.Exception#getMessage()
082: */
083: public String getMessage() {
084: StringBuffer buf = new StringBuffer(super .getMessage());
085: if (faultyMembers == null || faultyMembers.size() == 0) {
086: buf.append("; No faulty members identified.");
087: } else {
088: buf.append("; Faulty members:");
089: for (int i = 0; i < faultyMembers.size(); i++) {
090: FaultyMember mbr = (FaultyMember) faultyMembers.get(i);
091: buf.append(mbr.getMember().getName());
092: buf.append("; ");
093: }
094: }
095: return buf.toString();
096: }
097:
098: /**
099: * Adds a faulty member, and the reason the member failed.
100: * @param mbr Member
101: * @param x Exception
102: */
103: public boolean addFaultyMember(Member mbr, Exception x) {
104: return addFaultyMember(new FaultyMember(mbr, x));
105: }
106:
107: /**
108: * Adds a list of faulty members
109: * @param mbrs FaultyMember[]
110: */
111: public int addFaultyMember(FaultyMember[] mbrs) {
112: int result = 0;
113: for (int i = 0; mbrs != null && i < mbrs.length; i++) {
114: if (addFaultyMember(mbrs[i]))
115: result++;
116: }
117: return result;
118: }
119:
120: /**
121: * Adds a faulty member
122: * @param mbr FaultyMember
123: */
124: public boolean addFaultyMember(FaultyMember mbr) {
125: if (this .faultyMembers == null)
126: this .faultyMembers = new ArrayList();
127: if (!faultyMembers.contains(mbr))
128: return faultyMembers.add(mbr);
129: else
130: return false;
131: }
132:
133: /**
134: * Returns an array of members that failed and the reason they failed.
135: * @return FaultyMember[]
136: */
137: public FaultyMember[] getFaultyMembers() {
138: if (this .faultyMembers == null)
139: return EMPTY_LIST;
140: return (FaultyMember[]) faultyMembers
141: .toArray(new FaultyMember[faultyMembers.size()]);
142: }
143:
144: /**
145: *
146: * <p>Title: FaultyMember class</p>
147: *
148: * <p>Description: Represent a failure to a specific member when a message was sent
149: * to more than one member</p>
150: *
151: * @author Filip Hanik
152: * @version 1.0
153: */
154: public static class FaultyMember {
155: protected Exception cause;
156: protected Member member;
157:
158: public FaultyMember(Member mbr, Exception x) {
159: this .member = mbr;
160: this .cause = x;
161: }
162:
163: public Member getMember() {
164: return member;
165: }
166:
167: public Exception getCause() {
168: return cause;
169: }
170:
171: public String toString() {
172: return "FaultyMember:" + member.toString();
173: }
174:
175: public int hashCode() {
176: return (member != null) ? member.hashCode() : 0;
177: }
178:
179: public boolean equals(Object o) {
180: if (member == null || (!(o instanceof FaultyMember))
181: || (((FaultyMember) o).member == null))
182: return false;
183: return member.equals(((FaultyMember) o).member);
184: }
185: }
186:
187: }
|