KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > verifier > event > VerificationEvent


1 /*
2 * JBoss, Home of Professional Open Source
3 * Copyright 2005, JBoss Inc., and individual contributors as indicated
4 * by the @authors tag. See the copyright.txt in the distribution for a
5 * full listing of individual contributors.
6 *
7 * This is free software; you can redistribute it and/or modify it
8 * under the terms of the GNU Lesser General Public License as
9 * published by the Free Software Foundation; either version 2.1 of
10 * the License, or (at your option) any later version.
11 *
12 * This software is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
16 *
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this software; if not, write to the Free
19 * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
20 * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
21 */

22 package org.jboss.verifier.event;
23
24 /*
25  * Class org.jboss.verifier.event.VerificationEvent
26  * Copyright (C) 2000 Juha Lindfors
27  *
28  * This library is free software; you can redistribute it and/or
29  * modify it under the terms of the GNU Lesser General Public
30  * License as published by the Free Software Foundation; either
31  * version 2 of the License, or (at your option) any later version
32  *
33  * This library is distributed in the hope that it will be useful,
34  * but WITHOUT ANY WARRANTY; without even the implied warranty of
35  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
36  * Lesser General Public License for more details.
37  *
38  * You should have received a copy of the GNU Lesser General Public
39  * License along with this library; if not, write to the Free Software
40  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
41  *
42  * This package and its source code is available at www.jboss.org
43  * $Id: VerificationEvent.java 37459 2005-10-30 00:04:02Z starksm $
44  */

45
46
47 // standard imports
48
import java.util.EventObject JavaDoc;
49 import java.lang.reflect.Method JavaDoc;
50 import java.lang.reflect.Modifier JavaDoc;
51
52
53 // non-standard class dependencies
54
import org.jboss.verifier.Section;
55
56
57 /**
58  *
59  * @author Juha Lindfors (jplindfo@helsinki.fi)
60  * @version $Revision: 37459 $
61  * @since JDK 1.3
62  */

63 public class VerificationEvent extends EventObject JavaDoc {
64
65     public static final String JavaDoc WARNING = "WARNING";
66     public static final String JavaDoc OK = "OK";
67
68     private boolean isOk = false;
69     private boolean isWarning = false;
70
71     /*
72      * Contains a short, one line message for this event.
73      */

74     private String JavaDoc message = "<undefined>";
75     private String JavaDoc beanName = "<unnamed>";
76     private Method JavaDoc method = null;
77     private String JavaDoc section = null;
78     private String JavaDoc info = null;
79
80 /*
81  *************************************************************************
82  *
83  * PUBLIC INSTANCE METHODS
84  *
85  *************************************************************************
86  */

87
88    /*
89     * Constructor
90     */

91    public VerificationEvent( VerificationEventGenerator source )
92    {
93       super(source);
94    }
95
96    public VerificationEvent( VerificationEventGenerator source,
97       String JavaDoc message)
98    {
99       this(source);
100       setMessage(message);
101    }
102
103    public void setState(String JavaDoc 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 JavaDoc( 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 JavaDoc msg )
133    {
134       this.message = msg;
135    }
136
137    public void setName( String JavaDoc 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 JavaDoc method)
151    {
152       if( method == null )
153          return;
154
155       this.method = method;
156    }
157
158    public String JavaDoc getMessage()
159    {
160       return beanName + ": " + message;
161    }
162
163    public String JavaDoc getVerbose()
164    {
165       StringBuffer JavaDoc buf = new StringBuffer JavaDoc(512);
166       String JavaDoc linebreak = System.getProperty("line.separator");
167
168       buf.append(linebreak + "Bean : " + beanName + linebreak);
169
170       if( method != null )
171       {
172          String JavaDoc returnType = getShortClassName( method.getReturnType() );
173
174          Class JavaDoc[] arguments = method.getParameterTypes();
175          String JavaDoc arglist = getCommaSeparatedList( getShortClassNames(
176             arguments) );
177
178          Class JavaDoc[] exceptions = method.getExceptionTypes();
179          String JavaDoc 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 JavaDoc getName()
217    {
218       return beanName;
219    }
220
221 /*
222  *************************************************************************
223  *
224  * PRIVATE INSTANCE METHODS
225  *
226  *************************************************************************
227  */

228    private String JavaDoc[] getShortClassNames( Class JavaDoc[] c )
229    {
230       String JavaDoc[] names = new String JavaDoc[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    /*
239     * Returns class name without package path
240     */

241    private String JavaDoc getShortClassName( Class JavaDoc c )
242    {
243       String JavaDoc className = c.getName();
244       int len = className.length();
245       int offset = className.lastIndexOf( "." );
246
247       String JavaDoc 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    /*
258     * builds a comma separated string list of objects
259     */

260    private String JavaDoc getCommaSeparatedList( Object JavaDoc[] 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 JavaDoc buf = new StringBuffer JavaDoc( 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     /*
281      * String constants
282      */

283    private final static String JavaDoc STATE_NOT_RECOGNIZED = "Unknown event state";
284 }
285 /*
286 vim:ts=3:sw=3:et
287 */

288
Popular Tags