KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > slide > common > ServiceAccessException


1 /*
2  * $Header: /home/cvs/jakarta-slide/src/share/org/apache/slide/common/ServiceAccessException.java,v 1.12 2004/07/28 09:38:15 ib Exp $
3  * $Revision: 1.12 $
4  * $Date: 2004/07/28 09:38:15 $
5  *
6  * ====================================================================
7  *
8  * Copyright 1999-2002 The Apache Software Foundation
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  */

23
24 package org.apache.slide.common;
25
26 import java.io.PrintWriter JavaDoc;
27 import java.io.StringWriter JavaDoc;
28 import org.apache.slide.common.Domain;
29 import org.apache.slide.util.Messages;
30 import org.apache.slide.util.logger.Logger;
31
32 /**
33  * Service access exception.
34  *
35  * @version $Revision: 1.12 $
36  */

37 public class ServiceAccessException extends SlideException {
38
39     private static final String JavaDoc CHANNEL = "org.apache.slide.common.ServiceAccessException";
40     private static final int DEBUG = Logger.DEBUG;
41     private static Logger LOGGER = Domain.getLogger();
42
43     /* hold the cause exception, if supplied */
44     private Throwable JavaDoc nestedException = null;
45
46     // ----------------------------------------------------------- Constructors
47

48     /**
49      * Constructor.
50      *
51      * @param service Service from which the exception was thrown
52      * @param cause Cause of the exception
53      */

54     public ServiceAccessException(Service service, String JavaDoc cause) {
55         super(Messages.format
56               (ServiceAccessException.class.getName(), service, computeCause(cause)), false);
57         
58         if (LOGGER.isEnabled(CHANNEL, DEBUG)) {
59             StringWriter JavaDoc sw = new StringWriter JavaDoc();
60             printStackTrace( new PrintWriter JavaDoc(sw, true) ); //autoFlush=true
61
LOGGER.log( sw.toString(), CHANNEL, DEBUG );
62         }
63     }
64
65     /**
66      * Constructor.
67      *
68      * @param service Service from which the exception was thrown
69      * @param e Exception which is the initial cause of the problem
70      */

71     public ServiceAccessException(Service service, Throwable JavaDoc e) {
72         this(service, computeCause(e));
73         nestedException = e;
74     }
75
76     /**
77      * computeCause.
78      *
79      * @param delieveredCause the cause as a string, if null or empty e is used
80      * @param e the exception stacktrace is shown, if cause is not supplied
81      */

82     private static String JavaDoc computeCause(String JavaDoc delieveredCause, Throwable JavaDoc e) {
83         String JavaDoc result = delieveredCause;
84         if (delieveredCause == null || delieveredCause.equals("")) {
85             StringWriter JavaDoc sw = new StringWriter JavaDoc();
86             e.printStackTrace(new PrintWriter JavaDoc(sw, true)); //autoFlush=true
87
result = sw.toString();
88         }
89         return result;
90     }
91
92     /**
93      * computeCause.
94      *
95      * @param delieveredCause the cause as a string, if null or empty the current stack is used
96      */

97     private static String JavaDoc computeCause(String JavaDoc delieveredCause) {
98         if (delieveredCause == null || delieveredCause.equals("")) {
99             return "cause is empty";
100         } else {
101             return delieveredCause;
102         }
103     }
104
105     /**
106      * computeCause.
107      *
108      * @param e if getMessage is empty the stack trace of e is used
109      */

110     private static String JavaDoc computeCause(Throwable JavaDoc e) {
111         return computeCause(e.getMessage(), e);
112     }
113
114     /**
115      * return the cause exception, if supplied, else null.
116      */

117     public Throwable JavaDoc getCauseException() {
118         return nestedException;
119     }
120
121 }
122
Popular Tags