001: /*
002: * JBoss, Home of Professional Open Source.
003: * Copyright 2006, Red Hat Middleware LLC, and individual contributors
004: * as indicated by the @author tags. See the copyright.txt file in the
005: * distribution for a full listing of individual contributors.
006: *
007: * This is free software; you can redistribute it and/or modify it
008: * under the terms of the GNU Lesser General Public License as
009: * published by the Free Software Foundation; either version 2.1 of
010: * the License, or (at your option) any later version.
011: *
012: * This software is distributed in the hope that it will be useful,
013: * but WITHOUT ANY WARRANTY; without even the implied warranty of
014: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
015: * Lesser General Public License for more details.
016: *
017: * You should have received a copy of the GNU Lesser General Public
018: * License along with this software; if not, write to the Free
019: * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
020: * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
021: */
022: package org.jboss.verifier.event;
023:
024: /*
025: * Class org.jboss.verifier.event.VerificationEvent
026: * Copyright (C) 2000 Juha Lindfors
027: *
028: * This library is free software; you can redistribute it and/or
029: * modify it under the terms of the GNU Lesser General Public
030: * License as published by the Free Software Foundation; either
031: * version 2 of the License, or (at your option) any later version
032: *
033: * This library is distributed in the hope that it will be useful,
034: * but WITHOUT ANY WARRANTY; without even the implied warranty of
035: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
036: * Lesser General Public License for more details.
037: *
038: * You should have received a copy of the GNU Lesser General Public
039: * License along with this library; if not, write to the Free Software
040: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
041: *
042: * This package and its source code is available at www.jboss.org
043: * $Id: VerificationEvent.java 57209 2006-09-26 12:21:57Z dimitris@jboss.org $
044: */
045:
046: // standard imports
047: import java.util.EventObject;
048: import java.lang.reflect.Method;
049: import java.lang.reflect.Modifier;
050:
051: // non-standard class dependencies
052: import org.jboss.verifier.Section;
053:
054: /**
055: *
056: * @author Juha Lindfors (jplindfo@helsinki.fi)
057: * @version $Revision: 57209 $
058: * @since JDK 1.3
059: */
060: public class VerificationEvent extends EventObject {
061:
062: public static final String WARNING = "WARNING";
063: public static final String OK = "OK";
064:
065: private boolean isOk = false;
066: private boolean isWarning = false;
067:
068: /*
069: * Contains a short, one line message for this event.
070: */
071: private String message = "<undefined>";
072: private String beanName = "<unnamed>";
073: private Method method = null;
074: private String section = null;
075: private String info = null;
076:
077: /*
078: *************************************************************************
079: *
080: * PUBLIC INSTANCE METHODS
081: *
082: *************************************************************************
083: */
084:
085: /*
086: * Constructor
087: */
088: public VerificationEvent(VerificationEventGenerator source) {
089: super (source);
090: }
091:
092: public VerificationEvent(VerificationEventGenerator source,
093: String message) {
094: this (source);
095: setMessage(message);
096: }
097:
098: public void setState(String state) {
099: if (WARNING.equalsIgnoreCase(state)) {
100: isWarning = true;
101: isOk = false;
102: } else if (OK.equalsIgnoreCase(state)) {
103: isOk = true;
104: isWarning = false;
105: } else {
106: throw new IllegalArgumentException(STATE_NOT_RECOGNIZED
107: + ": " + state);
108: }
109: }
110:
111: public boolean isOk() {
112: return isOk;
113: }
114:
115: public boolean isWarning() {
116: return isWarning;
117: }
118:
119: public void setMessage(String msg) {
120: this .message = msg;
121: }
122:
123: public void setName(String name) {
124: this .beanName = name;
125: }
126:
127: public void setSection(Section section) {
128: this .section = section.getSection();
129:
130: if (section.hasInfo())
131: this .info = section.getInfo();
132: }
133:
134: public void setMethod(Method method) {
135: if (method == null)
136: return;
137:
138: this .method = method;
139: }
140:
141: public String getMessage() {
142: return beanName + ": " + message;
143: }
144:
145: public String getVerbose() {
146: StringBuffer buf = new StringBuffer(512);
147: String linebreak = System.getProperty("line.separator");
148:
149: buf.append(linebreak + "Bean : " + beanName + linebreak);
150:
151: if (method != null) {
152: String returnType = getShortClassName(method
153: .getReturnType());
154:
155: Class[] arguments = method.getParameterTypes();
156: String arglist = getCommaSeparatedList(getShortClassNames(arguments));
157:
158: Class[] exceptions = method.getExceptionTypes();
159: String exclist = getCommaSeparatedList(getShortClassNames(exceptions));
160:
161: buf.append("Method : "
162: + Modifier.toString(method.getModifiers()) + " "
163: + returnType + " " + method.getName() + "("
164: + arglist + ")");
165:
166: if (exclist.length() > 0)
167: buf.append(" throws " + exclist.toString());
168:
169: buf.append(linebreak);
170: }
171:
172: int offset = section.lastIndexOf(".");
173: if (!Character.isDigit(section.charAt(offset + 1)))
174: buf.append("Section: " + section.substring(0, offset)
175: + linebreak);
176: else
177: buf.append("Section: " + section + linebreak);
178:
179: buf.append("Warning: ");
180: if (message != null) {
181: buf.append(message + linebreak);
182: } else {
183: buf.append("No warning message found, please file a Bug "
184: + "report.");
185: }
186:
187: if (info != null)
188: buf.append("Info : " + info + linebreak);
189:
190: return buf.toString();
191: }
192:
193: public String getName() {
194: return beanName;
195: }
196:
197: /*
198: *************************************************************************
199: *
200: * PRIVATE INSTANCE METHODS
201: *
202: *************************************************************************
203: */
204: private String[] getShortClassNames(Class[] c) {
205: String[] names = new String[c.length];
206:
207: for (int i = 0; i < c.length; ++i)
208: names[i] = getShortClassName(c[i]);
209:
210: return names;
211: }
212:
213: /*
214: * Returns class name without package path
215: */
216: private String getShortClassName(Class c) {
217: String className = c.getName();
218: int len = className.length();
219: int offset = className.lastIndexOf(".");
220:
221: String name = "";
222:
223: if (offset == -1)
224: name = className;
225: else
226: name = className.substring(offset + 1, len);
227:
228: return name;
229: }
230:
231: /*
232: * builds a comma separated string list of objects
233: */
234: private String getCommaSeparatedList(Object[] list) {
235: if (list == null || list.length <= 0)
236: return "";
237:
238: if (list.length == 1)
239: return list[0].toString();
240:
241: StringBuffer buf = new StringBuffer(256);
242: buf.append(list[0]);
243:
244: for (int i = 1; i < list.length; ++i) {
245: buf.append(", ");
246: buf.append(list[i]);
247: }
248:
249: return buf.toString();
250: }
251:
252: /*
253: * String constants
254: */
255: private final static String STATE_NOT_RECOGNIZED = "Unknown event state";
256: }
257: /*
258: vim:ts=3:sw=3:et
259: */
|