Docjar: A Java Source and Docuemnt Enginecom.*    java.*    javax.*    org.*    all    new    plug-in

Quick Search    Search Deep

Source code: com/opencms/core/CmsException.java


1   /*
2   * File   : $Source: /usr/local/cvs/opencms/src/com/opencms/core/CmsException.java,v $
3   * Date   : $Date: 2002/11/17 16:42:56 $
4   * Version: $Revision: 1.48 $
5   *
6   * This library is part of OpenCms -
7   * the Open Source Content Mananagement System
8   *
9   * Copyright (C) 2001  The OpenCms Group
10  *
11  * This library is free software; you can redistribute it and/or
12  * modify it under the terms of the GNU Lesser General Public
13  * License as published by the Free Software Foundation; either
14  * version 2.1 of the License, or (at your option) any later version.
15  *
16  * This library is distributed in the hope that it will be useful,
17  * but WITHOUT ANY WARRANTY; without even the implied warranty of
18  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
19  * Lesser General Public License for more details.
20  *
21  * For further information about OpenCms, please see the
22  * OpenCms Website: http://www.opencms.org
23  *
24  * You should have received a copy of the GNU Lesser General Public
25  * License along with this library; if not, write to the Free Software
26  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
27  */
28  
29  package com.opencms.core;
30  
31  import java.io.*;
32  import java.util.*;
33  
34  /**
35   * <p>This class provides OpenCms interal Exception handling.
36   * A CmsExeption thrown will result in a default OpenCms error dialog box.</p> 
37   * <p>Note: Most Exceptions thrown in templates and other classes will be converted
38   * to a CmsException by the template launcher.</p>
39   * 
40   * @author Alexander Kandzior (a.kandzior@alkacon.com)
41   * @author Michael Emmerich
42   * 
43   * @version $Revision: 1.48 $ $Date: 2002/11/17 16:42:56 $
44   */
45  public class CmsException extends Exception {
46  
47      /** Stores the error code of the CmsException */
48      protected int m_type = 0;
49      
50      /** A string message describing the CmsEception */
51      protected String m_message = "NO MESSAGE";
52  
53      /** Stores a forwared exception */
54      protected Throwable m_rootCause = null;
55      
56      /** Flag to set processing of a saved forwared root exception */
57      protected boolean m_useRootCause = false;
58      
59      /** Definition of error code for unknown exception */
60      public final static int C_UNKNOWN_EXCEPTION = 0;
61  
62      /** Definition of error code for access denied exception for non file resources */
63      public final static int C_NO_ACCESS = 1;
64  
65      /** Definition of error code for not found exception */
66      public final static int C_NOT_FOUND = 2;
67  
68      /** Definition of error code for bad name exception */
69      public final static int C_BAD_NAME = 3;
70  
71      /** Definition of error code for sql exception */
72      public final static int C_SQL_ERROR = 4;
73  
74      /** Definition of error code for not empty exception */
75      public final static int C_NOT_EMPTY = 5;
76  
77      /** Definition of error code for no admin exception */
78      public final static int C_NOT_ADMIN = 6;
79  
80      /** Definition of error code for serialization exception */
81      public final static int C_SERIALIZATION = 7;
82  
83      /** Definition of error code for no group exception */
84      public final static int C_NO_GROUP = 8;
85  
86      /** Definition of error code for group not empty exception */
87      public final static int C_GROUP_NOT_EMPTY = 9;
88  
89      /** Definition of error code for no user exception */
90      public final static int C_NO_USER = 10;
91  
92      /** Definition of error code for no default group exception */
93      public final static int C_NO_DEFAULT_GROUP = 11;
94  
95      /** Definition of error code for file exists exception */
96      public final static int C_FILE_EXISTS = 12;
97  
98      /** Definition of error code for locked resource */
99      public final static int C_LOCKED = 13;
100 
101     /** Definition of error code filesystem error */
102     public final static int C_FILESYSTEM_ERROR = 14;
103 
104     /** Definition of error code internal file */
105     public final static int C_INTERNAL_FILE = 15;
106 
107     /** Definition of error code service unavailable */
108     public final static int C_SERVICE_UNAVAILABLE = 17;
109 
110     /** Definition of error code for unknown XML datablocks */
111     public final static int C_XML_UNKNOWN_DATA = 18;
112 
113     /** Definition of error code for corrupt internal structure */
114     public final static int C_XML_CORRUPT_INTERNAL_STRUCTURE = 19;
115 
116     /** Definition of error code for wrong XML content type */
117     public final static int C_XML_WRONG_CONTENT_TYPE = 20;
118 
119     /** Definition of error code for XML parsing error */
120     public final static int C_XML_PARSING_ERROR = 21;
121 
122     /** Definition of error code for XML processing error */
123     public final static int C_XML_PROCESS_ERROR = 22;
124 
125     /** Definition of error code for XML user method not found */
126     public final static int C_XML_NO_USER_METHOD = 23;
127 
128     /** Definition of error code for XML process method not found */
129     public final static int C_XML_NO_PROCESS_METHOD = 24;
130 
131     /** Definition of error code for missing XML tag */
132     public final static int C_XML_TAG_MISSING = 25;
133 
134     /** Definition of error code for wrong XML template class */
135     public final static int C_XML_WRONG_TEMPLATE_CLASS = 26;
136 
137     /** Definition of error code for no XML template class */
138     public final static int C_XML_NO_TEMPLATE_CLASS = 27;
139 
140     /** Definition of error code for launcher errors */
141     public final static int C_LAUNCH_ERROR = 28;
142 
143     /** Definition of error code for launcher errors */
144     public final static int C_CLASSLOADER_ERROR = 29;
145 
146     /** Definition of error code for error"Password too short" */
147     public final static int C_SHORT_PASSWORD = 30;
148 
149     /** 
150      * Definition of error code for error"Password not valid".
151      * For comptibility reasons the same like for short password.
152      */
153     public final static int C_INVALID_PASSWORD = C_SHORT_PASSWORD;
154 
155     /**
156      * Definition of error code for access denied exception for file resources.
157      * This exception causes a login-screen.
158      */
159     public final static int C_ACCESS_DENIED = 31;
160 
161     /** Definition of error code for accessing a deleted resource */
162     public final static int C_RESOURCE_DELETED = 32;
163 
164     /** Definition of error code for RB-INIT-ERRORS */
165     public final static int C_RB_INIT_ERROR = 33;
166 
167     /** Definition of error code for Registry exception */
168     public final static int C_REGISTRY_ERROR = 34;
169 
170     /** Definition of error code for user exists */
171     public final static int C_USER_EXISTS = 35;
172     
173     /** Definition of error code for HTTP sreaming error */
174     public final static int C_STREAMING_ERROR = 36;
175 
176     /** Definition of error code for HTTP sreaming error */
177     public final static int C_HTTPS_PAGE_ERROR = 37;
178 
179     /** Definition of error code for HTTP sreaming error */
180     public final static int C_HTTPS_REQUEST_ERROR = 38;
181 
182     /** Error code for Flex cache */
183     public final static int C_FLEX_CACHE = 39;
184     
185     /** Error code for Flex loader */
186     public final static int C_FLEX_LOADER = 40;
187 
188     /** Unspecified Flex error code */
189     public final static int C_FLEX_OTHER = 41;
190 
191     /** Default prefix for a CmsException message */
192     public static final String C_CMS_EXCEPTION_PREFIX = "com.opencms.core.CmsException";
193 
194     /**
195      * This array provides descriptions for the error codes stored as
196      * constants in the CmsExeption class.
197      */
198     public final static String C_EXTXT[] =  {
199         "Unknown exception", 
200         "Access denied", 
201         "Not found",
202         "Bad name", 
203         "Sql exception", 
204         "Folder not empty", 
205         "Admin access required",
206         "Serialization/Deserialization failed", 
207         "Unknown User Group",
208         "Group not empty", 
209         "Unknown User", 
210         "No removal from Default Group",
211         "Resource already exists", 
212         "Locked Resource", 
213         "Filesystem exception",
214         "Internal use only", 
215         "Deprecated exception: File-property is mandatory",
216         "Service unavailable", 
217         "Unknown XML datablock", 
218         "Corrupt internal structure",
219         "Wrong XML content type", 
220         "XML parsing error", 
221         "Could not process OpenCms special XML tag",
222         "Could not call user method", 
223         "Could not call process method",
224         "XML tag missing", 
225         "Wrong XML template class", 
226         "No XML template class",
227         "Error while launching template class", 
228         "OpenCms class loader error",
229         "New password is too short", 
230         "Access denied to resource",
231         "Resource deleted", 
232         "Resourcebroker-init error", 
233         "Registry error",
234         "User already exists", 
235         "HTTP streaming error",
236         "Wrong scheme for http resource", 
237         "Wrong scheme for https resource",
238         "Error in Flex cache", 
239         "Error in Flex loader", 
240         "Error in Flex engine"
241     };
242 
243     /**
244      * Constructs a simple CmsException
245      */
246     public CmsException() {
247         this("", 0, null, false);
248     }
249 
250     /**
251      * Contructs a CmsException with the provided error code, 
252      * The error codes used should be the constants from the CmsEception class.
253      *
254      * @param i Exception error code
255      */
256     public CmsException(int type) {
257         this("CmsException ID: " + type, type, null, false);
258     }
259 
260     /**
261      * Contructs a CmsException with the provided error code and
262      * a given root cause.
263      * The error codes used should be the constants from the CmsEception class.
264      *
265      * @param i Exception code
266      * @param e Forwarded root cause exception
267      */
268     public CmsException(int type, Throwable rootCause) {
269         this("CmsException ID: " + type, type, rootCause, false);
270     }
271 
272     /**
273      * Constructs a CmsException with the provided description.
274      *
275      * @param s Exception message
276      */
277     public CmsException(String message) {
278         this(message, 0, null, false);
279     }
280 
281     /**
282      * Contructs a CmsException with the provided description and error code.
283      * 
284      * @param s Exception message
285      * @param i Exception code
286      */
287     public CmsException(String message, int type) {
288         this(message, type, null, false);
289     }
290 
291     /**
292      * Construtcs a CmsException with a detail message and a forwarded 
293      * root cause exception
294      *
295      * @param s Exception message
296      * @param e Forwarded root cause exception
297      */
298     public CmsException(String message, Throwable rootCause) {
299         this(message, 0, rootCause, false);
300     }
301 
302     /**
303      * Creates a CmsException with the provided error code, 
304      * a forwarded root cause exception and a detail message.
305      *
306      * @param s Exception message
307      * @param i Exception code
308      * @param e Forwarded root cause exception
309      */
310     public CmsException(String message, int type, Throwable rootCause) {
311         this(message, type, rootCause, false);
312     }
313     
314     /**
315      * Creates a CmsException with a provided error code, 
316      * a forwarded root cause exception and a detail message.
317      * The further processing of the exception can be controlled 
318      * with the <code>useRoot</code> parameter.
319      *
320      * @param s Exception message
321      * @param i Exception code
322      * @param e Forwarded root cause exception
323      * @param useRoot If true, use 
324      */    
325     public CmsException(String message, int type, Throwable rootCause, boolean useRoot) {
326         super(C_CMS_EXCEPTION_PREFIX + ": " + message);
327         this.m_message = message;
328         this.m_type = type;
329         this.m_rootCause = rootCause;
330         this.m_useRootCause = useRoot;
331     }
332         
333     /**
334      * Get the root cause Exception which was provided
335      * when this exception was thrown.
336      *
337      * @return The root cause Exception.
338      */
339     public Exception getException() {        
340         if (m_useRootCause) return null;
341         try {
342             return (Exception)getRootCause();
343         } catch (ClassCastException e) {
344             return null;
345         }
346     }
347 
348     /**
349      * Get the root cause Throwable which was provided
350      * when this exception was thrown.
351      *
352      * @return The root cause Throwable.
353      */
354     public Throwable getRootCause() {
355         return m_rootCause;
356     }
357 
358     /**
359      * Set the root cause Exception value.
360      *
361      * @param value The root cause Exception
362      */
363     public void setException(Throwable value) {
364         m_rootCause = value;
365     }
366         
367     /**
368      * Get the throwable.
369      *
370      * @return Exception.
371      */
372     public Throwable getThrowable() {
373         return m_rootCause;
374     }    
375 
376     /**
377      * Get the exeption message
378      *
379      * @return Exception messge.
380      */
381     public String getMessage() {
382         return C_CMS_EXCEPTION_PREFIX + ": " + m_message;
383     }
384 
385     /**
386      * Get the exeption message
387      *
388      * @return Exception messge.
389      */
390     public String getShortException() {
391         return C_CMS_EXCEPTION_PREFIX + ": " + getType() + " " + C_EXTXT[getType()]
392                 + ". Detailed Error: " + m_message + ".";
393     }
394 
395     /**
396    * Return a string with the stacktrace. for this exception
397    * and for all encapsulated exceptions.
398    * Creation date: (10/23/00 %r)
399    * @return java.lang.String
400    */
401   public String getStackTraceAsString() {
402       java.io.StringWriter sw = new java.io.StringWriter();
403       java.io.PrintWriter pw = new java.io.PrintWriter(sw);
404     
405       if (m_useRootCause && (m_rootCause != null)) {
406           // use stack trace of root cause
407           m_rootCause.printStackTrace(pw);
408       } else {            
409           // use stack trace of this eception and add the root case 
410           super.printStackTrace(pw);
411   
412           // if there are any encapsulated exceptions, write them also.
413           if(m_rootCause != null) {
414               StringWriter _sw = new StringWriter();
415               PrintWriter _pw = new PrintWriter(_sw);
416                 _pw.println("-----------");                
417                 _pw.println("Root cause:");                
418               m_rootCause.printStackTrace(_pw);
419               _pw.close();
420               try {
421                   _sw.close();
422               }
423               catch(Exception exc) {
424       
425               // ignore the exception
426               }
427               StringTokenizer st = new StringTokenizer(_sw.toString(), "\n");
428               while(st.hasMoreElements()) {
429                     String s = ">" + (String)st.nextElement();
430                     while ( (s != null) && (! "".equals(s)) && ((s.endsWith("\r") || s.endsWith("\n") || s.endsWith(">"))) ) {
431                         s = s.substring(0, s.length()-1);
432                     } 
433                     if ((s != null) && (! "".equals(s))) pw.println(s);
434               }
435           }
436       }
437       pw.close();
438       try {
439           sw.close();
440       } catch(Exception exc) {
441           // ignore the exception
442       }
443       return sw.toString();
444   }
445 
446     /**
447      * Get the type of the CmsException.
448      *
449      * @return Type of CmsException
450      */
451     public int getType() {
452         return m_type;
453     }
454 
455     /**
456      * Gets the exception type as text.
457      *
458      * @return Exception type in a text-version.
459      */
460     public String getTypeText() {
461         return C_CMS_EXCEPTION_PREFIX + ": " + getType() + " " + C_EXTXT[getType()];
462     }
463 
464     /**
465      * Print the exception stack trace to System.out.
466      */
467     public void printStackTrace() {
468         printStackTrace(System.out);
469     }
470 
471     /**
472      * Prints this <code>Throwable</code> and its backtrace to the
473      * specified print stream.
474      */
475     public void printStackTrace(java.io.PrintStream s) {
476         s.println(getStackTraceAsString());
477     }
478 
479     /**
480      * Prints this <code>Throwable</code> and its backtrace to the specified
481      * print writer.
482      */
483     public void printStackTrace(java.io.PrintWriter s) {
484         s.println(getStackTraceAsString());
485     }
486     
487     /**
488      * Overwrites the standart toString method.
489      */
490     public String toString() {
491         StringBuffer output = new StringBuffer();
492         output.append(C_CMS_EXCEPTION_PREFIX + ": ");
493         output.append(m_type + " ");
494         output.append(CmsException.C_EXTXT[m_type] + ". ");
495         if (m_message != null && (!"".equals(m_message))) {
496             output.append("Detailed error: ");
497             output.append(m_message + ". ");
498         }
499         if(m_rootCause != null) {
500             output.append("\nroot cause was ");
501             output.append(m_rootCause);
502         }
503         return output.toString();
504     }
505 }