2N/A/*
2N/A * CDDL HEADER START
2N/A *
2N/A * The contents of this file are subject to the terms of the
2N/A * Common Development and Distribution License (the "License").
2N/A * You may not use this file except in compliance with the License.
2N/A *
2N/A * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
2N/A * or http://www.opensolaris.org/os/licensing.
2N/A * See the License for the specific language governing permissions
2N/A * and limitations under the License.
2N/A *
2N/A * When distributing Covered Code, include this CDDL HEADER in each
2N/A * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
2N/A * If applicable, add the following below this CDDL HEADER, with the
2N/A * fields enclosed by brackets "[]" replaced with your own identifying
2N/A * information: Portions Copyright [yyyy] [name of copyright owner]
2N/A *
2N/A * CDDL HEADER END
2N/A */
2N/A
2N/A/*
2N/A * Copyright 2008 Sun Microsystems, Inc. All rights reserved.
2N/A * Use is subject to license terms.
2N/A *
2N/A * ident "%Z%%M% %I% %E% SMI"
2N/A */
2N/Apackage org.opensolaris.os.dtrace;
2N/A
2N/Aimport java.io.*;
2N/Aimport java.beans.*;
2N/Aimport java.util.*;
2N/A
2N/A/**
2N/A * A formatted string generated by the DTrace {@code printf()} action.
2N/A * <p>
2N/A * Immutable. Supports persistence using {@link java.beans.XMLEncoder}.
2N/A *
2N/A * @author Tom Erickson
2N/A */
2N/Apublic final class PrintfRecord implements Record, Serializable,
2N/A Comparable <PrintfRecord> {
2N/A static final long serialVersionUID = 727237355963977675L;
2N/A
2N/A static {
2N/A try {
2N/A BeanInfo info = Introspector.getBeanInfo(PrintfRecord.class);
2N/A PersistenceDelegate persistenceDelegate =
2N/A new DefaultPersistenceDelegate(
2N/A new String[] {"records", "formattedString"})
2N/A {
2N/A /*
2N/A * Need to prevent DefaultPersistenceDelegate from using
2N/A * overridden equals() method, resulting in a
2N/A * StackOverFlowError. Revert to PersistenceDelegate
2N/A * implementation. See
2N/A * http://forum.java.sun.com/thread.jspa?threadID=
2N/A * 477019&tstart=135
2N/A */
2N/A protected boolean
2N/A mutatesTo(Object oldInstance, Object newInstance)
2N/A {
2N/A return (newInstance != null && oldInstance != null &&
2N/A oldInstance.getClass() == newInstance.getClass());
2N/A }
2N/A };
2N/A BeanDescriptor d = info.getBeanDescriptor();
2N/A d.setValue("persistenceDelegate", persistenceDelegate);
2N/A } catch (IntrospectionException e) {
2N/A System.out.println(e);
2N/A }
2N/A }
2N/A
2N/A /** @serial */
2N/A private List <ValueRecord> records;
2N/A /** @serial */
2N/A private String formattedString;
2N/A
2N/A // package-level access, called by ProbeData
2N/A PrintfRecord()
2N/A {
2N/A records = new ArrayList <ValueRecord> ();
2N/A }
2N/A
2N/A /**
2N/A * Creates a record with the unformatted elements passed to the
2N/A * DTrace {@code printf()} action and the resulting formatted
2N/A * output. Supports XML persistence.
2N/A *
2N/A * @param v variable number of unformatted elements passed to the
2N/A * DTrace {@code printf()} action
2N/A * @param s formatted {@code printf()} output
2N/A * @throws NullPointerException if the given list or any of its
2N/A * elements is {@code null}, or if the given formatted string is
2N/A * {@code null}
2N/A */
2N/A public
2N/A PrintfRecord(List <ValueRecord> v, String s)
2N/A {
2N/A formattedString = s;
2N/A records = new ArrayList <ValueRecord> (v.size());
2N/A records.addAll(v);
2N/A validate();
2N/A }
2N/A
2N/A private final void
2N/A validate()
2N/A {
2N/A if (formattedString == null) {
2N/A throw new NullPointerException("formatted string is null");
2N/A }
2N/A if (records == null) {
2N/A throw new NullPointerException("list of format args is null");
2N/A }
2N/A for (ValueRecord r : records) {
2N/A if (r == null) {
2N/A throw new NullPointerException("format arg is null");
2N/A }
2N/A }
2N/A }
2N/A
2N/A /**
2N/A * Called by ProbeData code to populate record list.
2N/A *
2N/A * @throws NullPointerException if o is null
2N/A */
2N/A void
2N/A addUnformattedElement(ScalarRecord rec)
2N/A {
2N/A records.add(rec);
2N/A }
2N/A
2N/A /**
2N/A * Gets the formatted string output of the DTrace {@code printf()}
2N/A * action.
2N/A *
2N/A * @return non-null formatted string output of the DTrace {@code
2N/A * printf()} action
2N/A */
2N/A public String
2N/A getFormattedString()
2N/A {
2N/A return formattedString;
2N/A }
2N/A
2N/A /**
2N/A * Package level access; called by ProbeData
2N/A */
2N/A void
2N/A setFormattedString(String s)
2N/A {
2N/A if (s == null) {
2N/A throw new NullPointerException("formatted string is null");
2N/A }
2N/A formattedString = s;
2N/A }
2N/A
2N/A /**
2N/A * Gets the unfomatted elements passed to the DTrace {@code
2N/A * printf()} action after the format string.
2N/A *
2N/A * @return non-null, unmodifiable list of unformatted elements
2N/A * passed to the DTrace {@code printf()} action that generated this
2N/A * record, in the order they appear in the argument list after the
2N/A * format string
2N/A */
2N/A public List <ValueRecord>
2N/A getRecords()
2N/A {
2N/A return Collections. <ValueRecord> unmodifiableList(records);
2N/A }
2N/A
2N/A /**
2N/A * Gets the number of DTrace {@code printf()} unformatted elements
2N/A * (arguments following the format string). For example, the
2N/A * following action
2N/A * <pre><code>
2N/A * printf("%s %d\n", "cat", 9);
2N/A * </code></pre>
2N/A * generates a {@code PrintfRecord} with a record count of two.
2N/A *
2N/A * @return the number of unformatted elements passed to the DTrace
2N/A * {@code printf()} action that generated this record.
2N/A */
2N/A public int
2N/A getRecordCount()
2N/A {
2N/A return records.size();
2N/A }
2N/A
2N/A /**
2N/A * Gets the unformatted element passed to the DTrace {@code
2N/A * printf()} action at the given offset in the {@code printf()}
2N/A * argument list after the format string, starting at offset zero
2N/A * for the first unformatted element.
2N/A *
2N/A * @return non-null record representing the unformatted {@code
2N/A * printf()} element at the given index (using the same order that
2N/A * they appear in the {@code printf()} argument list)
2N/A * @throws ArrayIndexOutOfBoundsException if the given index is
2N/A * out of range (index < 0 || index >= getRecordCount())
2N/A */
2N/A public ValueRecord
2N/A getRecord(int i)
2N/A {
2N/A return records.get(i);
2N/A }
2N/A
2N/A /**
2N/A * Compares the specified object with this {@code PrintfRecord} for
2N/A * equality. Returns {@code true} if and only if the specified
2N/A * object is also a {@code PrintfRecord} and both records have the
2N/A * same formatted string and underlying data elements.
2N/A *
2N/A * @return {@code true} if and only if the specified object is also
2N/A * a {@code PrintfRecord} and both the formatted strings <i>and</i>
2N/A * the underlying data elements of both records are equal
2N/A */
2N/A @Override
2N/A public boolean
2N/A equals(Object o)
2N/A {
2N/A if (o instanceof PrintfRecord) {
2N/A PrintfRecord r = (PrintfRecord)o;
2N/A return (records.equals(r.records) &&
2N/A formattedString.equals(r.formattedString));
2N/A }
2N/A return false;
2N/A }
2N/A
2N/A /**
2N/A * Overridden to ensure that equal instances have equal hash codes.
2N/A */
2N/A @Override
2N/A public int
2N/A hashCode()
2N/A {
2N/A int hash = 17;
2N/A hash = (37 * hash) + records.hashCode();
2N/A hash = (37 * hash) + formattedString.hashCode();
2N/A return hash;
2N/A }
2N/A
2N/A /**
2N/A * Compares the formatted string value of this record with that of
2N/A * the given record. Note that ordering {@code printf} records by
2N/A * their string values is incompatible with {@link #equals(Object o)
2N/A * equals()}, which also checks the underlying data elements for
2N/A * equality.
2N/A *
2N/A * @return a negative number, 0, or a positive number as this
2N/A * record's formatted string is lexicographically less than, equal
2N/A * to, or greater than the given record's formatted string
2N/A */
2N/A public int
2N/A compareTo(PrintfRecord r)
2N/A {
2N/A return formattedString.compareTo(r.formattedString);
2N/A }
2N/A
2N/A private void
2N/A readObject(ObjectInputStream s)
2N/A throws IOException, ClassNotFoundException
2N/A {
2N/A s.defaultReadObject();
2N/A // Defensively copy record list before validating
2N/A if (records == null) {
2N/A throw new InvalidObjectException("record list is null");
2N/A }
2N/A List <ValueRecord> copy = new ArrayList <ValueRecord> (records.size());
2N/A copy.addAll(records);
2N/A records = copy;
2N/A // check invariants
2N/A try {
2N/A validate();
2N/A } catch (Exception e) {
2N/A InvalidObjectException x = new InvalidObjectException(
2N/A e.getMessage());
2N/A x.initCause(e);
2N/A throw x;
2N/A }
2N/A }
2N/A
2N/A /**
2N/A * Gets the formatted string output of the DTrace {@code printf()}
2N/A * action.
2N/A */
2N/A public String
2N/A toString()
2N/A {
2N/A return formattedString;
2N/A }
2N/A}