1fb3fb4f3Stomee /*
2fb3fb4f3Stomee  * CDDL HEADER START
3fb3fb4f3Stomee  *
4fb3fb4f3Stomee  * The contents of this file are subject to the terms of the
5fb3fb4f3Stomee  * Common Development and Distribution License (the "License").
6fb3fb4f3Stomee  * You may not use this file except in compliance with the License.
7fb3fb4f3Stomee  *
8fb3fb4f3Stomee  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9fb3fb4f3Stomee  * or http://www.opensolaris.org/os/licensing.
10fb3fb4f3Stomee  * See the License for the specific language governing permissions
11fb3fb4f3Stomee  * and limitations under the License.
12fb3fb4f3Stomee  *
13fb3fb4f3Stomee  * When distributing Covered Code, include this CDDL HEADER in each
14fb3fb4f3Stomee  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15fb3fb4f3Stomee  * If applicable, add the following below this CDDL HEADER, with the
16fb3fb4f3Stomee  * fields enclosed by brackets "[]" replaced with your own identifying
17fb3fb4f3Stomee  * information: Portions Copyright [yyyy] [name of copyright owner]
18fb3fb4f3Stomee  *
19fb3fb4f3Stomee  * CDDL HEADER END
20fb3fb4f3Stomee  */
21fb3fb4f3Stomee 
22fb3fb4f3Stomee /*
23*e77b06d2Stomee  * Copyright 2008 Sun Microsystems, Inc.  All rights reserved.
24fb3fb4f3Stomee  * Use is subject to license terms.
25fb3fb4f3Stomee  *
26fb3fb4f3Stomee  * ident	"%Z%%M%	%I%	%E% SMI"
27fb3fb4f3Stomee  */
28fb3fb4f3Stomee package org.opensolaris.os.dtrace;
29fb3fb4f3Stomee 
30fb3fb4f3Stomee import java.io.Serializable;
31fb3fb4f3Stomee import java.beans.*;
32fb3fb4f3Stomee 
33fb3fb4f3Stomee /**
34fb3fb4f3Stomee  * A record indicating that the DTrace {@code exit()} action is about to
35fb3fb4f3Stomee  * stop the source {@link Consumer}.  The exit status is whatever value
36fb3fb4f3Stomee  * was passed to the {@code exit()} action in the D program.
37fb3fb4f3Stomee  * <p>
38fb3fb4f3Stomee  * Immutable.  Supports persistence using {@link java.beans.XMLEncoder}.
39fb3fb4f3Stomee  *
40fb3fb4f3Stomee  * @author Tom Erickson
41fb3fb4f3Stomee  */
42fb3fb4f3Stomee public final class ExitRecord implements Record, Serializable {
43fb3fb4f3Stomee     static final long serialVersionUID = -2062716683135961493L;
44fb3fb4f3Stomee 
45fb3fb4f3Stomee     static {
46fb3fb4f3Stomee 	try {
47fb3fb4f3Stomee 	    BeanInfo info = Introspector.getBeanInfo(ExitRecord.class);
48fb3fb4f3Stomee 	    PersistenceDelegate persistenceDelegate =
49fb3fb4f3Stomee 		    new DefaultPersistenceDelegate(
50*e77b06d2Stomee 		    new String[] {"status"})
51*e77b06d2Stomee 	    {
52*e77b06d2Stomee 		/*
53*e77b06d2Stomee 		 * Need to prevent DefaultPersistenceDelegate from using
54*e77b06d2Stomee 		 * overridden equals() method, resulting in a
55*e77b06d2Stomee 		 * StackOverFlowError.  Revert to PersistenceDelegate
56*e77b06d2Stomee 		 * implementation.  See
57*e77b06d2Stomee 		 * http://forum.java.sun.com/thread.jspa?threadID=
58*e77b06d2Stomee 		 * 477019&tstart=135
59*e77b06d2Stomee 		 */
60*e77b06d2Stomee 		protected boolean
61*e77b06d2Stomee 		mutatesTo(Object oldInstance, Object newInstance)
62*e77b06d2Stomee 		{
63*e77b06d2Stomee 		    return (newInstance != null && oldInstance != null &&
64*e77b06d2Stomee 			    oldInstance.getClass() == newInstance.getClass());
65*e77b06d2Stomee 		}
66*e77b06d2Stomee 	    };
67fb3fb4f3Stomee 	    BeanDescriptor d = info.getBeanDescriptor();
68fb3fb4f3Stomee 	    d.setValue("persistenceDelegate", persistenceDelegate);
69fb3fb4f3Stomee 	} catch (IntrospectionException e) {
70fb3fb4f3Stomee 	    System.out.println(e);
71fb3fb4f3Stomee 	}
72fb3fb4f3Stomee     }
73fb3fb4f3Stomee 
74fb3fb4f3Stomee     /** @serial */
75fb3fb4f3Stomee     private final int status;
76fb3fb4f3Stomee 
77fb3fb4f3Stomee     /**
78fb3fb4f3Stomee      * Creates an exit record with the given status.
79fb3fb4f3Stomee      *
80fb3fb4f3Stomee      * @param exitStatus value passed to the D {@code exit()} action
81fb3fb4f3Stomee      */
82fb3fb4f3Stomee     public
ExitRecord(int exitStatus)83fb3fb4f3Stomee     ExitRecord(int exitStatus)
84fb3fb4f3Stomee     {
85fb3fb4f3Stomee 	status = exitStatus;
86fb3fb4f3Stomee     }
87fb3fb4f3Stomee 
88fb3fb4f3Stomee     /**
89fb3fb4f3Stomee      * Gets the exit status of a DTrace {@link Consumer}.
90fb3fb4f3Stomee      *
91fb3fb4f3Stomee      * @return the value passed to the D {@code exit()} action
92fb3fb4f3Stomee      */
93fb3fb4f3Stomee     public int
getStatus()94fb3fb4f3Stomee     getStatus()
95fb3fb4f3Stomee     {
96fb3fb4f3Stomee 	return status;
97fb3fb4f3Stomee     }
98fb3fb4f3Stomee 
99*e77b06d2Stomee     /**
100*e77b06d2Stomee      * Compares the specified object with this {@code ExitRecord} for
101*e77b06d2Stomee      * equality. Returns {@code true} if and only if the specified
102*e77b06d2Stomee      * object is also an {@code ExitRecord} and both records have the
103*e77b06d2Stomee      * same status.
104*e77b06d2Stomee      *
105*e77b06d2Stomee      * @return {@code true} if and only if the specified object is also
106*e77b06d2Stomee      * an {@code ExitRecord} and both records have the same status
107*e77b06d2Stomee      */
108*e77b06d2Stomee     @Override
109*e77b06d2Stomee     public boolean
equals(Object o)110*e77b06d2Stomee     equals(Object o)
111*e77b06d2Stomee     {
112*e77b06d2Stomee 	if (o instanceof ExitRecord) {
113*e77b06d2Stomee 	    ExitRecord r = (ExitRecord)o;
114*e77b06d2Stomee 	    return (status == r.status);
115*e77b06d2Stomee 	}
116*e77b06d2Stomee 	return false;
117*e77b06d2Stomee     }
118*e77b06d2Stomee 
119*e77b06d2Stomee     /**
120*e77b06d2Stomee      * Overridden to ensure that equal instances have equal hash codes.
121*e77b06d2Stomee      */
122*e77b06d2Stomee     @Override
123*e77b06d2Stomee     public int
hashCode()124*e77b06d2Stomee     hashCode()
125*e77b06d2Stomee     {
126*e77b06d2Stomee 	return status;
127*e77b06d2Stomee     }
128*e77b06d2Stomee 
129fb3fb4f3Stomee     /**
130fb3fb4f3Stomee      * Gets a string representation of the exit status.
131fb3fb4f3Stomee      *
132fb3fb4f3Stomee      * @return the string form of {@link #getStatus()} returned by
133fb3fb4f3Stomee      * {@link Integer#toString(int i)}
134fb3fb4f3Stomee      */
135fb3fb4f3Stomee     public String
toString()136fb3fb4f3Stomee     toString()
137fb3fb4f3Stomee     {
138fb3fb4f3Stomee 	return Integer.toString(status);
139fb3fb4f3Stomee     }
140fb3fb4f3Stomee }
141