1 22 package org.jboss.verifier.event; 23 24 45 46 47 import java.util.EventObject ; 49 import java.lang.reflect.Method ; 50 import java.lang.reflect.Modifier ; 51 52 53 import org.jboss.verifier.Section; 55 56 57 63 public class VerificationEvent extends EventObject { 64 65 public static final String WARNING = "WARNING"; 66 public static final String OK = "OK"; 67 68 private boolean isOk = false; 69 private boolean isWarning = false; 70 71 74 private String message = "<undefined>"; 75 private String beanName = "<unnamed>"; 76 private Method method = null; 77 private String section = null; 78 private String info = null; 79 80 87 88 91 public VerificationEvent( VerificationEventGenerator source ) 92 { 93 super(source); 94 } 95 96 public VerificationEvent( VerificationEventGenerator source, 97 String message) 98 { 99 this(source); 100 setMessage(message); 101 } 102 103 public void setState(String state) 104 { 105 if( WARNING.equalsIgnoreCase(state) ) 106 { 107 isWarning = true; 108 isOk = false; 109 } 110 else if( OK.equalsIgnoreCase(state) ) 111 { 112 isOk = true; 113 isWarning = false; 114 } 115 else 116 { 117 throw new IllegalArgumentException ( STATE_NOT_RECOGNIZED + ": " 118 + state); 119 } 120 } 121 122 public boolean isOk() 123 { 124 return isOk; 125 } 126 127 public boolean isWarning() 128 { 129 return isWarning; 130 } 131 132 public void setMessage( String msg ) 133 { 134 this.message = msg; 135 } 136 137 public void setName( String name ) 138 { 139 this.beanName = name; 140 } 141 142 public void setSection( Section section ) 143 { 144 this.section = section.getSection(); 145 146 if( section.hasInfo() ) 147 this.info = section.getInfo(); 148 } 149 150 public void setMethod(Method method) 151 { 152 if( method == null ) 153 return; 154 155 this.method = method; 156 } 157 158 public String getMessage() 159 { 160 return beanName + ": " + message; 161 } 162 163 public String getVerbose() 164 { 165 StringBuffer buf = new StringBuffer (512); 166 String linebreak = System.getProperty("line.separator"); 167 168 buf.append(linebreak + "Bean : " + beanName + linebreak); 169 170 if( method != null ) 171 { 172 String returnType = getShortClassName( method.getReturnType() ); 173 174 Class [] arguments = method.getParameterTypes(); 175 String arglist = getCommaSeparatedList( getShortClassNames( 176 arguments) ); 177 178 Class [] exceptions = method.getExceptionTypes(); 179 String exclist = getCommaSeparatedList(getShortClassNames( 180 exceptions) ); 181 182 buf.append( "Method : " + Modifier.toString(method.getModifiers()) + 183 " " + returnType + " " + 184 method.getName() + "(" + 185 arglist + ")"); 186 187 if ( exclist.length() > 0 ) 188 buf.append(" throws " + exclist.toString()); 189 190 buf.append(linebreak); 191 } 192 193 int offset = section.lastIndexOf("."); 194 if ( !Character.isDigit(section.charAt(offset+1)) ) 195 buf.append("Section: " + section.substring(0, offset) + linebreak); 196 else 197 buf.append("Section: " + section + linebreak); 198 199 buf.append( "Warning: " ); 200 if( message != null ) 201 { 202 buf.append( message + linebreak ); 203 } 204 else 205 { 206 buf.append( "No warning message found, please file a Bug " + 207 "report." ); 208 } 209 210 if( info != null ) 211 buf.append("Info : " + info + linebreak ); 212 213 return buf.toString(); 214 } 215 216 public String getName() 217 { 218 return beanName; 219 } 220 221 228 private String [] getShortClassNames( Class [] c ) 229 { 230 String [] names = new String [c.length]; 231 232 for (int i = 0; i < c.length; ++i) 233 names[i] = getShortClassName( c[i] ); 234 235 return names; 236 } 237 238 241 private String getShortClassName( Class c ) 242 { 243 String className = c.getName(); 244 int len = className.length(); 245 int offset = className.lastIndexOf( "." ); 246 247 String name = ""; 248 249 if (offset == -1) 250 name = className; 251 else 252 name = className.substring(offset+1, len); 253 254 return name; 255 } 256 257 260 private String getCommaSeparatedList( Object [] list ) 261 { 262 if (list == null || list.length <= 0) 263 return ""; 264 265 if (list.length == 1) 266 return list[0].toString(); 267 268 StringBuffer buf = new StringBuffer ( 256 ); 269 buf.append( list[0] ); 270 271 for (int i = 1; i < list.length; ++i) 272 { 273 buf.append( ", " ); 274 buf.append( list[i] ); 275 } 276 277 return buf.toString(); 278 } 279 280 283 private final static String STATE_NOT_RECOGNIZED = "Unknown event state"; 284 } 285 288 | Popular Tags |