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 2008 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.Serializable;
31 import java.beans.*;
32 
33 /**
34  * A record indicating that the DTrace {@code exit()} action is about to
35  * stop the source {@link Consumer}.  The exit status is whatever value
36  * was passed to the {@code exit()} action in the D program.
37  * <p>
38  * Immutable.  Supports persistence using {@link java.beans.XMLEncoder}.
39  *
40  * @author Tom Erickson
41  */
42 public final class ExitRecord implements Record, Serializable {
43     static final long serialVersionUID = -2062716683135961493L;
44 
45     static {
46 	try {
47 	    BeanInfo info = Introspector.getBeanInfo(ExitRecord.class);
48 	    PersistenceDelegate persistenceDelegate =
49 		    new DefaultPersistenceDelegate(
50 		    new String[] {"status"})
51 	    {
52 		/*
53 		 * Need to prevent DefaultPersistenceDelegate from using
54 		 * overridden equals() method, resulting in a
55 		 * StackOverFlowError.  Revert to PersistenceDelegate
56 		 * implementation.  See
57 		 * http://forum.java.sun.com/thread.jspa?threadID=
58 		 * 477019&tstart=135
59 		 */
60 		protected boolean
61 		mutatesTo(Object oldInstance, Object newInstance)
62 		{
63 		    return (newInstance != null && oldInstance != null &&
64 			    oldInstance.getClass() == newInstance.getClass());
65 		}
66 	    };
67 	    BeanDescriptor d = info.getBeanDescriptor();
68 	    d.setValue("persistenceDelegate", persistenceDelegate);
69 	} catch (IntrospectionException e) {
70 	    System.out.println(e);
71 	}
72     }
73 
74     /** @serial */
75     private final int status;
76 
77     /**
78      * Creates an exit record with the given status.
79      *
80      * @param exitStatus value passed to the D {@code exit()} action
81      */
82     public
ExitRecord(int exitStatus)83     ExitRecord(int exitStatus)
84     {
85 	status = exitStatus;
86     }
87 
88     /**
89      * Gets the exit status of a DTrace {@link Consumer}.
90      *
91      * @return the value passed to the D {@code exit()} action
92      */
93     public int
getStatus()94     getStatus()
95     {
96 	return status;
97     }
98 
99     /**
100      * Compares the specified object with this {@code ExitRecord} for
101      * equality. Returns {@code true} if and only if the specified
102      * object is also an {@code ExitRecord} and both records have the
103      * same status.
104      *
105      * @return {@code true} if and only if the specified object is also
106      * an {@code ExitRecord} and both records have the same status
107      */
108     @Override
109     public boolean
equals(Object o)110     equals(Object o)
111     {
112 	if (o instanceof ExitRecord) {
113 	    ExitRecord r = (ExitRecord)o;
114 	    return (status == r.status);
115 	}
116 	return false;
117     }
118 
119     /**
120      * Overridden to ensure that equal instances have equal hash codes.
121      */
122     @Override
123     public int
hashCode()124     hashCode()
125     {
126 	return status;
127     }
128 
129     /**
130      * Gets a string representation of the exit status.
131      *
132      * @return the string form of {@link #getStatus()} returned by
133      * {@link Integer#toString(int i)}
134      */
135     public String
toString()136     toString()
137     {
138 	return Integer.toString(status);
139     }
140 }
141