KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > enterprise > admin > util > AssertImpl


1 /*
2  * The contents of this file are subject to the terms
3  * of the Common Development and Distribution License
4  * (the License). You may not use this file except in
5  * compliance with the License.
6  *
7  * You can obtain a copy of the license at
8  * https://glassfish.dev.java.net/public/CDDLv1.0.html or
9  * glassfish/bootstrap/legal/CDDLv1.0.txt.
10  * See the License for the specific language governing
11  * permissions and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL
14  * Header Notice in each file and include the License file
15  * at glassfish/bootstrap/legal/CDDLv1.0.txt.
16  * If applicable, add the following below the CDDL Header,
17  * with the fields enclosed by brackets [] replaced by
18  * you own identifying information:
19  * "Portions Copyrighted [year] [name of copyright owner]"
20  *
21  * Copyright 2006 Sun Microsystems, Inc. All rights reserved.
22  */

23
24 package com.sun.enterprise.admin.util;
25
26 import com.sun.enterprise.admin.util.Validator;
27 import com.sun.enterprise.admin.util.ValidatorResult;
28
29 // i18n import
30
import com.sun.enterprise.admin.util.SOMLocalStringsManager;
31
32 /**
33  * Implementation class for Assert and CheckArgs
34  */

35
36 final class AssertImpl
37 {
38     private int mExceptionType = 0;
39     private boolean mWantStackTrace = true;
40     private String JavaDoc mPreamble = null;
41
42     private static final String JavaDoc sDefaultPreamble = "Assertion Failure: ";
43     static final int sAssertError = 0;
44     static final int sIllegalArgument = 1;
45     
46     // i18n SOMLocalStringsManager
47
private static SOMLocalStringsManager localizedStrMgr =
48         SOMLocalStringsManager.getManager( AssertImpl.class );
49
50     AssertImpl(int exceptionType)
51     {
52         this("", exceptionType);
53     }
54
55     AssertImpl(String JavaDoc msg, int exceptionType)
56     {
57         mPreamble = msg;
58         mExceptionType = exceptionType;
59
60         if(mPreamble == null)
61         {
62             mPreamble = sDefaultPreamble;
63         }
64
65         // add a ": " -- if it isn't an empty string
66
if(mPreamble.length() > 0 && !mPreamble.endsWith(": "))
67         {
68             mPreamble += ": ";
69         }
70
71         if(mExceptionType < sAssertError || mExceptionType > sIllegalArgument)
72         {
73             lowLevelAssert("Invalid exception type id. Must be 0 or 1");
74
75             // caller could swallow that assert and call assert() later
76
// so let's setup a reasonable value.
77
mExceptionType = sAssertError;
78         }
79     }
80     
81     void setWantStackTrace(boolean what)
82     {
83         mWantStackTrace = what;
84     }
85
86     void assertIt(boolean b, Object JavaDoc userMsg)
87     {
88         if (b)
89         {
90             return;
91         }
92         String JavaDoc msg = null;
93         if(userMsg != null)
94         {
95             msg = userMsg.toString();
96         }
97         else
98         {
99             msg = "boolean test was false";
100         }
101         toss(msg);
102     }
103
104     void assertRange(long value, long min, long max, Object JavaDoc userMsg)
105     {
106         if (value < min || value > max)
107         {
108             final String JavaDoc rangeString = "[" + min + ", " + max + "]";
109             String JavaDoc msg = "illegal integer value = " + value +
110                     " must be in range " + rangeString;
111             if (userMsg != null)
112             {
113                 msg += " ( " + userMsg.toString() + " )";
114             }
115             toss(msg);
116         }
117     }
118
119     void assertValid(Object JavaDoc object, String JavaDoc name, Validator validator)
120     {
121         final ValidatorResult result = validator.validate(object);
122
123         if (!result.isValid())
124         {
125             final String JavaDoc msg = "Validation failed for " + name +
126                                             ": " + result.getString();
127             toss(msg);
128         }
129     }
130
131     /**
132         An assertion has failed, do something with the message.
133
134         Our current implemention is to dump the stack trace
135         and throw an AssertError, so that the failure is obnoxious
136         and will be noticed.
137      */

138     private void toss(String JavaDoc msg) throws IllegalArgumentException JavaDoc, AssertError
139     {
140         String JavaDoc s = mPreamble + msg;
141         Throwable JavaDoc t = null;
142
143         /* yes -- the following is ugly. But there is NO WAY to throw the
144          * common superclass -- Throwable -- without making everyone on
145          * the call stack declare it!!
146          **/

147
148         if(mExceptionType == sIllegalArgument)
149         {
150             IllegalArgumentException JavaDoc iae = new IllegalArgumentException JavaDoc(s);
151
152             if(mWantStackTrace)
153             {
154                 Debug.printStackTrace(iae);
155             }
156             throw iae;
157         }
158         else if(mExceptionType == sAssertError)
159         {
160             AssertError ae = new AssertError(s);
161
162             if(mWantStackTrace)
163             {
164                 Debug.printStackTrace(ae);
165             }
166             throw ae;
167         }
168         else
169         {
170             lowLevelAssert("Impossible condition -- bad mExceptionType -- "
171                     + mExceptionType);
172         }
173     }
174
175     private void pr(String JavaDoc s)
176     {
177         Debug.println(s);
178     }
179
180     private void lowLevelAssert(String JavaDoc s)
181     {
182         // if there is a problem in this code -- we can't do a normal assert or
183
// we are going to enter an infinite loop - since Assert calls us!!
184

185         String JavaDoc msg = localizedStrMgr.getString( "admin.util.fatal_error_in_setupexceptionconstructor", s );
186         throw new AssertError( msg );
187     }
188 }
189
Popular Tags