1 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 import com.sun.enterprise.admin.util.SOMLocalStringsManager; 31 32 35 36 final class AssertImpl 37 { 38 private int mExceptionType = 0; 39 private boolean mWantStackTrace = true; 40 private String mPreamble = null; 41 42 private static final String sDefaultPreamble = "Assertion Failure: "; 43 static final int sAssertError = 0; 44 static final int sIllegalArgument = 1; 45 46 private static SOMLocalStringsManager localizedStrMgr = 48 SOMLocalStringsManager.getManager( AssertImpl.class ); 49 50 AssertImpl(int exceptionType) 51 { 52 this("", exceptionType); 53 } 54 55 AssertImpl(String msg, int exceptionType) 56 { 57 mPreamble = msg; 58 mExceptionType = exceptionType; 59 60 if(mPreamble == null) 61 { 62 mPreamble = sDefaultPreamble; 63 } 64 65 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 mExceptionType = sAssertError; 78 } 79 } 80 81 void setWantStackTrace(boolean what) 82 { 83 mWantStackTrace = what; 84 } 85 86 void assertIt(boolean b, Object userMsg) 87 { 88 if (b) 89 { 90 return; 91 } 92 String 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 userMsg) 105 { 106 if (value < min || value > max) 107 { 108 final String rangeString = "[" + min + ", " + max + "]"; 109 String 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 object, String name, Validator validator) 120 { 121 final ValidatorResult result = validator.validate(object); 122 123 if (!result.isValid()) 124 { 125 final String msg = "Validation failed for " + name + 126 ": " + result.getString(); 127 toss(msg); 128 } 129 } 130 131 138 private void toss(String msg) throws IllegalArgumentException , AssertError 139 { 140 String s = mPreamble + msg; 141 Throwable t = null; 142 143 147 148 if(mExceptionType == sIllegalArgument) 149 { 150 IllegalArgumentException iae = new IllegalArgumentException (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 s) 176 { 177 Debug.println(s); 178 } 179 180 private void lowLevelAssert(String s) 181 { 182 185 String msg = localizedStrMgr.getString( "admin.util.fatal_error_in_setupexceptionconstructor", s ); 186 throw new AssertError( msg ); 187 } 188 } 189 | Popular Tags |