1 /*
2  * CDDL HEADER START
3  *
4  * The contents of this file are subject to the terms of the
5  * Common Development and Distribution License (the "License").
6  * You may not use this file except in compliance with the License.
7  *
8  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9  * or http://www.opensolaris.org/os/licensing.
10  * See the License for the specific language governing permissions
11  * and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL HEADER in each
14  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15  * If applicable, add the following below this CDDL HEADER, with the
16  * fields enclosed by brackets "[]" replaced with your own identifying
17  * information: Portions Copyright [yyyy] [name of copyright owner]
18  *
19  * CDDL HEADER END
20  */
21 
22 /*
23  * Copyright 2007 Sun Microsystems, Inc.  All rights reserved.
24  * Use is subject to license terms.
25  *
26  * ident	"%Z%%M%	%I%	%E% SMI"
27  */
28 package org.opensolaris.os.dtrace;
29 
30 import java.io.*;
31 import java.util.EventObject;
32 
33 /**
34  * Notification that DTrace has encountered an error.
35  *
36  * @see ConsumerListener#errorEncountered(ErrorEvent e)
37  *
38  * @author Tom Erickson
39  */
40 public class ErrorEvent extends EventObject {
41     static final long serialVersionUID = 2236600660422267215L;
42 
43     /** @serial */
44     private Error error;
45 
46     /**
47      * Creates an {@link ConsumerListener#errorEncountered(ErrorEvent e)
48      * errorEncountered()} event that reports an error encountered in
49      * the native DTrace library during tracing.
50      *
51      * @param source the {@link Consumer} that is the source of this event
52      * @param dtraceError the error encountered by DTrace
53      * @throws NullPointerException if the given error is {@code null}
54      */
55     public
ErrorEvent(Object source, Error dtraceError)56     ErrorEvent(Object source, Error dtraceError)
57     {
58 	super(source);
59 	error = dtraceError;
60 	validate();
61     }
62 
63     private final void
validate()64     validate()
65     {
66 	if (error == null) {
67 	    throw new NullPointerException("error is null");
68 	}
69     }
70 
71     /**
72      * Gets the error reported by DTrace.
73      *
74      * @return non-null error reported by DTrace
75      */
76     public Error
getError()77     getError()
78     {
79 	return error;
80     }
81 
82     private void
readObject(ObjectInputStream s)83     readObject(ObjectInputStream s)
84             throws IOException, ClassNotFoundException
85     {
86 	s.defaultReadObject();
87 	// check invariants
88 	try {
89 	    validate();
90 	} catch (Exception e) {
91 	    InvalidObjectException x = new InvalidObjectException(
92 		    e.getMessage());
93 	    x.initCause(e);
94 	    throw x;
95 	}
96     }
97 
98     /**
99      * Gets a string representation of this event useful for logging and
100      * not intended for display.  The exact details of the
101      * representation are unspecified and subject to change, but the
102      * following format may be regarded as typical:
103      * <pre><code>
104      * class-name[property1 = value1, property2 = value2]
105      * </code></pre>
106      */
107     public String
toString()108     toString()
109     {
110 	StringBuilder buf = new StringBuilder();
111 	buf.append(ErrorEvent.class.getName());
112 	buf.append("[source = ");
113 	buf.append(getSource());
114 	buf.append(", error = ");
115 	buf.append(error);
116 	buf.append(']');
117 	return buf.toString();
118     }
119 }
120