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 the state of a target process designated by {@link
35  * Consumer#createProcess(String command)} or {@link
36  * Consumer#grabProcess(int pid)} has changed.
37  *
38  * @see ConsumerListener#processStateChanged(ProcessEvent e)
39  *
40  * @author Tom Erickson
41  */
42 public class ProcessEvent extends EventObject {
43     static final long serialVersionUID = -3779443761929558702L;
44 
45     /** @serial */
46     private ProcessState processState;
47 
48     /**
49      * Creates a {@link ConsumerListener#processStateChanged(ProcessEvent e)
50      * processStateChanged()} event to notify listeners of a process
51      * state change.
52      *
53      * @param source the {@link Consumer} that is the source of this event
54      * @throws NullPointerException if the given process state is {@code
55      * null}
56      */
57     public
ProcessEvent(Object source, ProcessState p)58     ProcessEvent(Object source, ProcessState p)
59     {
60 	super(source);
61 	processState = p;
62 	validate();
63     }
64 
65     private final void
validate()66     validate()
67     {
68 	if (processState == null) {
69 	    throw new NullPointerException("process state is null");
70 	}
71     }
72 
73     /**
74      * Gets the process state.
75      *
76      * @return non-null process state
77      */
78     public ProcessState
getProcessState()79     getProcessState()
80     {
81 	return processState;
82     }
83 
84     private void
readObject(ObjectInputStream s)85     readObject(ObjectInputStream s)
86             throws IOException, ClassNotFoundException
87     {
88 	s.defaultReadObject();
89 	// check invariants
90 	try {
91 	    validate();
92 	} catch (Exception e) {
93 	    InvalidObjectException x = new InvalidObjectException(
94 		    e.getMessage());
95 	    x.initCause(e);
96 	    throw x;
97 	}
98     }
99 
100     /**
101      * Gets a string representation of this event useful for logging and
102      * not intended for display.  The exact details of the
103      * representation are unspecified and subject to change, but the
104      * following format may be regarded as typical:
105      * <pre><code>
106      * class-name[property1 = value1, property2 = value2]
107      * </code></pre>
108      */
109     public String
toString()110     toString()
111     {
112 	StringBuilder buf = new StringBuilder();
113 	buf.append(ProcessEvent.class.getName());
114 	buf.append("[source = ");
115 	buf.append(getSource());
116 	buf.append(", processState = ");
117 	buf.append(processState);
118 	buf.append(']');
119 	return buf.toString();
120     }
121 }
122