KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > mr > core > util > ExceptionMonitor


1 /*
2  * Copyright 2002 by
3  * <a HREF="http://www.coridan.com">Coridan</a>
4  * <a HREF="mailto: support@coridan.com ">support@coridan.com</a>
5  *
6  * The contents of this file are subject to the Mozilla Public License Version
7  * 1.1 (the "License"); you may not use this file except in compliance with the
8  * License. You may obtain a copy of the License at
9  * http://www.mozilla.org/MPL/
10  *
11  * Software distributed under the License is distributed on an "AS IS" basis,
12  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
13  * for the specific language governing rights and limitations under the
14  * License.
15  *
16  * The Original Code is "MantaRay" (TM).
17  *
18  * The Initial Developer of the Original Code is - Nimo 23-MAR-2004.
19  * Portions created by the Initial Developer are Copyright (C) 2006
20  * Coridan Inc. All Rights Reserved.
21  *
22  * Contributor(s): all the names of the contributors are added in the source
23  * code where applicable.
24  *
25  * Alternatively, the contents of this file may be used under the terms of the
26  * LGPL license (the "GNU LESSER GENERAL PUBLIC LICENSE"), in which case the
27  * provisions of LGPL are applicable instead of those above. If you wish to
28  * allow use of your version of this file only under the terms of the LGPL
29  * License and not to allow others to use your version of this file under
30  * the MPL, indicate your decision by deleting the provisions above and
31  * replace them with the notice and other provisions required by the LGPL.
32  * If you do not delete the provisions above, a recipient may use your version
33  * of this file under either the MPL or the GNU LESSER GENERAL PUBLIC LICENSE.
34  
35  *
36  * This library is free software; you can redistribute it and/or modify it
37  * under the terms of the MPL as stated above or under the terms of the GNU
38  * Lesser General Public License as published by the Free Software Foundation;
39  * either version 2.1 of the License, or any later version.
40  *
41  * This library is distributed in the hope that it will be useful, but WITHOUT
42  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
43  * FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
44  * License for more details.
45  */

46
47 package org.mr.core.util;
48 /**
49  * @author - Nimo 23-MAR-2004
50  *
51  * This class is supposed to be a singleton, that handles all exceptions
52  * From the various classes.
53  *
54  */

55 import java.util.Hashtable JavaDoc;
56 import javax.jms.IllegalStateException JavaDoc;
57 import javax.jms.InvalidDestinationException JavaDoc;
58 import javax.jms.JMSException JavaDoc;
59 import javax.jms.MessageEOFException JavaDoc;
60 import javax.jms.MessageFormatException JavaDoc;
61 import javax.jms.MessageNotReadableException JavaDoc;
62 import javax.jms.MessageNotWriteableException JavaDoc;
63 import javax.jms.TransactionInProgressException JavaDoc;
64
65 public class ExceptionMonitor {
66    
67    //The length of the numeric part of the message code: MNTA99999E
68
private final static int NUMBER_LENGTH = 5;
69    //This is the singleton instance.
70
private static ExceptionMonitor instance = null;
71   
72    //the default constructor is private.
73
private ExceptionMonitor() { }
74    
75    //The messages repository and the initialization.
76
static Hashtable JavaDoc messages;
77    static {
78       messages = new Hashtable JavaDoc();
79       
80       //JMSException constants
81
messages.put(new Integer JavaDoc(0),"CONNECTION CAN NOT BE INSTANTIATED WITHOUT A FACTORY");
82       messages.put(new Integer JavaDoc(1),"CONNECTION COULD NOT BE CLOSED");
83       messages.put(new Integer JavaDoc(2),"TEMPORARY TOPIC DOES NOT EXIST");
84       messages.put(new Integer JavaDoc(3),"TEMPORARY DESTINATION DOES NOT EXIST");
85       messages.put(new Integer JavaDoc(4),"QUEUE DOES NOT EXIST");
86       messages.put(new Integer JavaDoc(5),"CAN NOT CREATE A SESSION ON A NULL CONNECTION");
87       messages.put(new Integer JavaDoc(6),"CAN NOT USE A SESSION_TRANSACTED ACK MODE ON A NON-TRANSACTED SESSION");
88       messages.put(new Integer JavaDoc(7),"DESTINATION DOES NOT EXIST");
89       messages.put(new Integer JavaDoc(8),"AGENT ERROR ON REGISTRATION");
90       messages.put(new Integer JavaDoc(9),"OPERATION NOT SUPPORTED");
91       messages.put(new Integer JavaDoc(10),"CAN NOT CREATE A CONSUMER ON A NULL SESSION");
92       
93       messages.put(new Integer JavaDoc(12),"ILLEGAL DELIVERY MODE SUPPLIED");
94       messages.put(new Integer JavaDoc(13),"ILLEGAL PRIORITY SUPPLIED");
95       messages.put(new Integer JavaDoc(14),"MESSAGE TIME TO LIVE MUST NOT BE NEGATIVE");
96       messages.put(new Integer JavaDoc(15),"CAN NOT DELIVER A NULL MESSAGE");
97       messages.put(new Integer JavaDoc(16),"LISTENER FOR SERVER SESSION IS NULL");
98       messages.put(new Integer JavaDoc(17),"METHOD NOT AVAILABLE FOR A SERVER SESSION");
99       messages.put(new Integer JavaDoc(18),"AN IO ERROR OCCURED");
100       messages.put(new Integer JavaDoc(19),"CAN NOT DELETE TEMPORARY DESTINATION");
101       messages.put(new Integer JavaDoc(20),"THE APPLICATION SERVER HAD AN INTERNAL ERROR");
102       messages.put(new Integer JavaDoc(21),"CAN NOT SEND MESSAGES FROM A CLOSED PRODUCER");
103       messages.put(new Integer JavaDoc(22),"FAILED TO PRODUCE MESSAGES ON DESTINATION");
104       messages.put(new Integer JavaDoc(23),"PRIORITY OUT OF VALID RANGE");
105       
106       
107       //IllegalStateException constants
108
messages.put(new Integer JavaDoc(201),"CAN NOT CHANGE A CLIENT ID. IT IS SYSTEM CONFIGURED");
109       messages.put(new Integer JavaDoc(202),"ILLEGAL OPERATION - CONNECTION IS CLOSED");
110       messages.put(new Integer JavaDoc(203),"INVALID ACK MODE SUPPLIED");
111       messages.put(new Integer JavaDoc(204),"CAN NOT COMMIT A NON-TRANSACTED SESSION");
112       messages.put(new Integer JavaDoc(205),"CAN NOT ROLLBACK A NON TRANSACTED SESSION");
113       messages.put(new Integer JavaDoc(206),"CAN NOT RECOVER A TRANSACTED SESSION");
114       messages.put(new Integer JavaDoc(207),"OPERATION UNALLOWED ON A CLOSED SESSION");
115       messages.put(new Integer JavaDoc(208),"MESSAGE CAN NOT BE ACKED ON A NULL SESSION");
116       messages.put(new Integer JavaDoc(209),"MESSAGE WILL NOT BE ACKED - WRONG ACK MODE IN USE");
117       messages.put(new Integer JavaDoc(210),"CAN NOT CREATE A DURABLE SUBSCRIPTION ON A QUEUE");
118       messages.put(new Integer JavaDoc(211),"CAN NOT CREATE A TOPIC SUBSCRIBER ON A QUEUE SESSION");
119       messages.put(new Integer JavaDoc(212),"CAN NOT CREATE A TOPIC FROM A QUEUE SESSION");
120       messages.put(new Integer JavaDoc(213),"CAN NOT UNSUBSCRIBE OFF A QUEUE SESSION");
121       messages.put(new Integer JavaDoc(215),"PRODUCER IS CLOSED. OPERATION UNALLOWED");
122       messages.put(new Integer JavaDoc(216),"CONSUMER IS CLOSED. OPERATION UNALLOWED");
123       //IllegalArgumentException
124
messages.put(new Integer JavaDoc(250),"PROPERTY NAME ILLEGAL (MUST NOT BE NULL OR EMPTY)");
125       
126       //InvalidDestinationException constants. or Unsupported
127
messages.put(new Integer JavaDoc(299),"CAN NOT SWITCH DESTINATION ON A FIXED-DESTINATION PRODUCER");
128       messages.put(new Integer JavaDoc(300),"A NULL DESTINATION WAS SUPPLIED");
129       messages.put(new Integer JavaDoc(302),"QUEUE DOES NOT EXIST");
130       messages.put(new Integer JavaDoc(303),"CAN NOT REGISTER QUEUE, TOPIC MIGHT EXIST WITH SAME NAME");
131       messages.put(new Integer JavaDoc(304),"CAN NOT REGISTER TOPIC, QUEUE MIGHT EXIST WITH SAME NAME");
132       messages.put(new Integer JavaDoc(305),"CAN NOT PUBLISH TO A QUEUE USING JMS1.0.2 TopicPublisher. DESTINATION NAME IS");
133       messages.put(new Integer JavaDoc(306),"CAN NOT SEND MESSAGE TO A TOPIC USING JMS1.0.2 QueueSender. DESTINATION NAME IS");
134       //MessageNotReadableException constants
135
messages.put(new Integer JavaDoc(401),"INVALID OPERATION. MESSAGE IS MARKED AS WRITE-ONLY");
136       messages.put(new Integer JavaDoc(402),"WRONG MESSAGE FORMAT");
137       messages.put(new Integer JavaDoc(403),"MESSAGE DOES NOT SUPPORT OBJECT OF TYPE");
138      
139       //MessageNotWritableException
140
messages.put(new Integer JavaDoc(454),"THE MESSAGE IS NOT IN A WRITEABLE STATE");
141       messages.put(new Integer JavaDoc(455),"ERROR IN OBJECT");
142       messages.put(new Integer JavaDoc(456),"WRONG MESSAGE FORMAT");
143       //MessageEofException
144
messages.put(new Integer JavaDoc(457),"MESSAGE AT END OF STREAM");
145       
146       //Type conversions
147
messages.put(new Integer JavaDoc(458),"CAN NOT CONVERT TO BOOLEAN. SOURCE OBJECT IS");
148       messages.put(new Integer JavaDoc(459),"CAN NOT CONVERT TO BYTE. SOURCE OBJECT IS");
149       messages.put(new Integer JavaDoc(460),"CAN NOT CONVERT TO SHORT. SOURCE OBJECT IS");
150       messages.put(new Integer JavaDoc(461),"CAN NOT CONVERT TO CHAR. SOURCE OBJECT IS");
151       messages.put(new Integer JavaDoc(462),"CAN NOT CONVERT TO INTEGER. SOURCE OBJECT IS");
152       messages.put(new Integer JavaDoc(463),"CAN NOT CONVERT TO LONG. SOURCE OBJECT IS");
153       messages.put(new Integer JavaDoc(464),"CAN NOT CONVERT TO FLOAT. SOURCE OBJECT IS");
154       messages.put(new Integer JavaDoc(465),"CAN NOT CONVERT TO DOUBLE. SOURCE OBJECT IS");
155       messages.put(new Integer JavaDoc(466),"CAN NOT CONVERT A BYTE ARRAY TO STRING");
156       messages.put(new Integer JavaDoc(467),"CAN NOT GET A BYTE ARRAY FROM");
157       messages.put(new Integer JavaDoc(468),"INVALID OBJECT TYPE SUPPLIED");
158       messages.put(new Integer JavaDoc(470),"MUST FINISH READING BYTES.");
159       //TransactionInProgressExceptions
160
messages.put(new Integer JavaDoc(481),"CAN NOT CALL COMMIT ON AN XA SESSION");
161       messages.put(new Integer JavaDoc(482),"CAN NOT CALL ROLLBACK ON AN XA SESSION");
162      
163    }
164
165    //Get the singleton instance of the monitor.
166
public synchronized static ExceptionMonitor getInstance() {
167       if (instance == null) {
168          instance = new ExceptionMonitor();
169       }
170       return instance;
171          
172    }
173    
174    //This method is used to throw the relevant exception, using the
175
//message's text from the repository, and the text that was puted
176
//by the caller.
177
public void shout(int key, String JavaDoc msg) throws JMSException JavaDoc {
178       
179      StringBuffer JavaDoc text = new StringBuffer JavaDoc((String JavaDoc) messages.get(new Integer JavaDoc(key)));
180      if (text == null)
181         text = new StringBuffer JavaDoc("99999E : UNKNOWN ERROR. CONTACT ADMINISTRATOR");
182      else
183         text = new StringBuffer JavaDoc("MNTA").append(stringOf(key)).append("E : ").append(text);
184      
185      if (msg!=null)
186         text.append(" : <").append(msg).append(">");
187      //differentiate the types of exceptions.
188
if (key<=200)
189         throw new JMSException JavaDoc(text.toString());
190      else if (key<250)
191         throw new IllegalStateException JavaDoc(text.toString());
192      else if (key==250)
193          throw new IllegalArgumentException JavaDoc(text.toString());
194      else if (key<=300)
195          throw new UnsupportedOperationException JavaDoc(text.toString());
196      else if (key<=400)
197         throw new InvalidDestinationException JavaDoc(text.toString());
198      else if (key<=450)
199         throw new MessageNotReadableException JavaDoc(text.toString());
200      else if (key==451)
201         throw new IndexOutOfBoundsException JavaDoc(text.toString());
202      else if (key==452)
203         throw new NullPointerException JavaDoc(text.toString());
204      else if (key==453||key==455||key==456)
205         throw new MessageFormatException JavaDoc(text.toString());
206      else if (key==454)
207         throw new MessageNotWriteableException JavaDoc(text.toString());
208      else if (key==457)
209         throw new MessageEOFException JavaDoc(text.toString());
210      else if (key<=480)
211         throw new MessageFormatException JavaDoc(text.toString());
212      else if (key<=485)
213         throw new TransactionInProgressException JavaDoc(text.toString());
214      
215           
216   }
217   
218   private StringBuffer JavaDoc stringOf(int key) {
219      
220      StringBuffer JavaDoc num = new StringBuffer JavaDoc(String.valueOf(key));
221      for (int i = num.length();i<NUMBER_LENGTH;i++)
222         num.insert(0,'0');
223      
224      return num;
225   }
226 }
227
228
Popular Tags