1 /**
2 * $Id: BaseExceptionManagerImpl.java 17 2006-10-19 15:11:35Z maldito_orco $
3 * $Revision: 17 $
4 * $Date: 2006-10-19 12:11:35 -0300 (Thu, 19 Oct 2006) $
5 *
6 * =========================================================================
7 *
8 * Copyright 2005 Tubo
9 *
10 * Licensed under the Apache License, Version 2.0 (the "License");
11 * you may not use this file except in compliance with the License.
12 * You may obtain a copy of the License at
13 *
14 * http://www.apache.org/licenses/LICENSE-2.0
15 *
16 * Unless required by applicable law or agreed to in writing, software
17 * distributed under the License is distributed on an "AS IS" BASIS,
18 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
19 * See the License for the specific language governing permissions and
20 * limitations under the License.
21 */
22 package org.tubo.exception.baseimpl;
23
24
25 import org.apache.commons.logging.Log;
26 import org.apache.commons.logging.LogFactory;
27
28 import org.tubo.resource.ResourceManager;
29 import org.tubo.exception.TuboException;
30 import org.tubo.exception.ExceptionManager;
31 import org.tubo.configuration.def.Configuration;
32 import org.tubo.configuration.def.ExceptionDef;
33
34 import java.util.List;
35 import java.util.ArrayList;
36 import java.util.ResourceBundle;
37 import java.util.Map;
38 import java.util.Iterator;
39 import java.util.PropertyResourceBundle;
40 import java.text.MessageFormat;
41
42 /**
43 * Created: Sep 28, 2006, 12:43:19 PM
44 * Last Modification Date: $Date: 2006-10-19 12:11:35 -0300 (Thu, 19 Oct 2006) $
45 *
46 * @author maldito_orco (maldito_orco@users.sourceforge.net)
47 * @version $Revision: 17 $
48 */
49 public class BaseExceptionManagerImpl implements ExceptionManager {
50 public static final String RCS_ID = "$Id: BaseExceptionManagerImpl.java 17 2006-10-19 15:11:35Z maldito_orco $";
51 private static Log log = LogFactory.getLog(BaseExceptionManagerImpl.class);
52
53 private ResourceManager resourceManager = null;
54 private List ranks = new ArrayList();
55 private ResourceBundle[] bundles = null;
56
57
58 public void setResourceManager(ResourceManager resourceManager) throws TuboException {
59 this.resourceManager = resourceManager;
60
61
62 Configuration configuration = resourceManager.getConfiguration();
63
64
65 Map exceptionDefs = configuration.getExceptionDefs();
66
67
68 for (Iterator it=exceptionDefs.values().iterator(); it.hasNext();) {
69
70
71 ExceptionDef def = (ExceptionDef)it.next();
72
73
74 List _ranks = def.getRankErrorCodesList();
75
76
77 for (Iterator rit=_ranks.iterator(); rit.hasNext(); ) {
78
79
80 int[] arank = (int[])rit.next();
81
82
83 Rank rank = new Rank(arank[1],arank[0],def.getId());
84
85
86 addRank(rank);
87 }
88 }
89
90
91 List messageResources = configuration.getMessageResources();
92
93
94 bundles = new ResourceBundle[messageResources.size()];
95
96
97 int i=0;
98 for (Iterator it=messageResources.iterator(); it.hasNext(); i++) {
99 String resource = (String)it.next();
100
101
102 ResourceBundle bundle = null;
103 try {
104
105 bundle = PropertyResourceBundle.getBundle(resource);
106 } catch (Exception e) {
107 if(log.isErrorEnabled()) log.error("Error while load property file '"+resource+"'", e);
108 }
109
110
111 bundles[i]=bundle;
112 }
113 }
114
115
116 /**
117 * Add rank to global rank list
118 * TODO: check ranks overlaping
119 * @param rank
120 * @throws TuboException
121 */
122 protected void addRank(Rank rank) throws TuboException {
123 int i=0;
124 for(Iterator it=ranks.iterator(); it.hasNext(); i++) {
125 Rank irank = (Rank)it.next();
126 if(irank.getUpperLimit()>rank.getUpperLimit())
127 break;
128 }
129 ranks.add(i,rank);
130 }
131
132 public TuboException getException(int errorCode) {
133 return getException(errorCode, null, null);
134 }
135
136 public TuboException getException(int errorCode, Object arg1) {
137 return getException(errorCode, new Object[] {arg1}, null);
138 }
139
140 public TuboException getException(int errorCode, Object arg1, Object arg2) {
141 return getException(errorCode, new Object[] {arg1,arg2}, null);
142 }
143
144 public TuboException getException(int errorCode, Object[] args) {
145 return getException(errorCode, args, null);
146 }
147
148 public TuboException getException(int errorCode, Throwable cause) {
149 return getException(errorCode, null, cause);
150 }
151
152 public TuboException getException(int errorCode, Object arg1, Throwable cause) {
153 return getException(errorCode, new Object[] {arg1}, cause);
154 }
155
156 public TuboException getException(int errorCode, Object arg1, Object arg2, Throwable cause) {
157 return getException(errorCode, new Object[] {arg1,arg2}, cause);
158 }
159
160 public TuboException getException(int errorCode, Object[] args, Throwable cause) {
161
162
163 boolean foundRank = false;
164 String exceptionId = null;
165 for(Iterator it=ranks.iterator(); it.hasNext() && !foundRank; ) {
166 Rank rank = (Rank)it.next();
167 if(rank.getUpperLimit()>=errorCode && errorCode>=rank.getLowerLimit()) {
168 exceptionId = rank.getExceptionId();
169 foundRank = true;
170 }
171 }
172
173
174 TuboException exception = getException(exceptionId, (""+errorCode), args, cause);
175
176
177 return exception;
178 }
179
180 public TuboException getException(String exceptionId, String messageId, Object[] args, Throwable cause) {
181
182
183 String message = null;
184 for (int i=0; i<bundles.length; i++) {
185 try {
186 message = bundles[i].getString(messageId);
187 break;
188 } catch (Exception e) {
189 log.error("TODO: write error message!", e);
190 }
191 }
192
193
194 if (args!=null) {
195 MessageFormat mf = new MessageFormat(message);
196 message = mf.format(args);
197 }
198
199
200 TuboException exception = getException(exceptionId, message, cause);
201
202
203 return exception;
204 }
205
206 /**
207 * Hook method to create a new exception instance based on ID.
208 * This default implementation to create a TuboException
209 * @param exceptionId exception identifier
210 * @return by default returns TuboException for any exceptionId
211 */
212 public TuboException getException(String exceptionId, String message, Throwable cause) {
213 return new TuboException();
214 }
215
216 protected class Rank {
217 private int upperLimit = -1;
218 private int lowerLimit = -1;
219 private String exceptionId = null;
220
221 public Rank() { }
222
223 public Rank(int upperLimit, int lowerLimit, String exceptionId) {
224 this.upperLimit = upperLimit;
225 this.lowerLimit = lowerLimit;
226 this.exceptionId = exceptionId;
227 }
228
229 public int getUpperLimit() {
230 return upperLimit;
231 }
232
233 public void setUpperLimit(int upperLimit) {
234 this.upperLimit = upperLimit;
235 }
236
237 public int getLowerLimit() {
238 return lowerLimit;
239 }
240
241 public void setLowerLimit(int lowerLimit) {
242 this.lowerLimit = lowerLimit;
243 }
244
245 public String getExceptionId() {
246 return exceptionId;
247 }
248
249 public void setExceptionId(String exceptionId) {
250 this.exceptionId = exceptionId;
251 }
252 }
253 }