jdk/test/javax/management/remote/mandatory/subjectDelegation/SimpleStandard.java
author ohair
Tue, 25 May 2010 15:58:33 -0700
changeset 5506 202f599c92aa
parent 1639 a97859015238
permissions -rw-r--r--
6943119: Rebrand source copyright notices Reviewed-by: darcy, weijun
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 1639
diff changeset
     2
 * Copyright (c) 2003, 2008, Oracle and/or its affiliates. All rights reserved.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
90ce3da70b43 Initial load
duke
parents:
diff changeset
     7
 * published by the Free Software Foundation.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 * accompanied this code).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 *
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 1639
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 1639
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 1639
diff changeset
    21
 * questions.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    22
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    23
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
 * Simple definition of a standard MBean, named "SimpleStandard".
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
 * The "SimpleStandard" standard MBean shows how to expose attributes and
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
 * operations for management by implementing its corresponding
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
 * "SimpleStandardMBean" management interface.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
 * This MBean has two attributes and one operation exposed
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
 * for management by a JMX agent:
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
 *      - the read/write "State" attribute,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
 *      - the read only "NbChanges" attribute,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
 *      - the "reset()" operation.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
 * This object also has one property and one method not exposed
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
 * for management by a JMX agent:
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
 *      - the "NbResets" property,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
 *      - the "getNbResets()" method.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
import java.security.AccessControlContext;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
import java.security.AccessController;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
import java.security.Principal;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
import java.util.Set;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
import javax.management.AttributeChangeNotification;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
import javax.management.NotificationBroadcasterSupport;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
import javax.management.remote.JMXPrincipal;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
import javax.security.auth.Subject;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
public class SimpleStandard
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
    extends NotificationBroadcasterSupport
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
    implements SimpleStandardMBean {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
     * -----------------------------------------------------
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
     * CONSTRUCTORS
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
     * -----------------------------------------------------
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
    public SimpleStandard(String principalName) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
        this.principalName = principalName;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
     * -----------------------------------------------------
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
     * IMPLEMENTATION OF THE SimpleStandardMBean INTERFACE
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
     * -----------------------------------------------------
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
     * Getter: get the "State" attribute of the "SimpleStandard" standard MBean.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
     * @return the current value of the "State" attribute.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
    public String getState() {
1570
4165709c91e3 5072267: A way to communicate client context such as locale to the JMX server
emcmanus
parents: 2
diff changeset
    78
        checkSubject("getState");
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
        return state;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
     * Setter: set the "State" attribute of the "SimpleStandard" standard MBean.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
     * @param <VAR>s</VAR> the new value of the "State" attribute.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
    public void setState(String s) {
1570
4165709c91e3 5072267: A way to communicate client context such as locale to the JMX server
emcmanus
parents: 2
diff changeset
    88
        checkSubject("setState");
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
        state = s;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
        nbChanges++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
     * Getter: get the "NbChanges" attribute of the "SimpleStandard" standard
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
     * MBean.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
     * @return the current value of the "NbChanges" attribute.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
    public int getNbChanges() {
1570
4165709c91e3 5072267: A way to communicate client context such as locale to the JMX server
emcmanus
parents: 2
diff changeset
   100
        checkSubject("getNbChanges");
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
        return nbChanges;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
     * Operation: reset to their initial values the "State" and "NbChanges"
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
     * attributes of the "SimpleStandard" standard MBean.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
    public void reset() {
1570
4165709c91e3 5072267: A way to communicate client context such as locale to the JMX server
emcmanus
parents: 2
diff changeset
   109
        checkSubject("reset");
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
        AttributeChangeNotification acn =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
            new AttributeChangeNotification(this,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
                                            0,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
                                            0,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
                                            "NbChanges reset",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
                                            "NbChanges",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
                                            "Integer",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
                                            new Integer(nbChanges),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
                                            new Integer(0));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
        state = "initial state";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
        nbChanges = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
        nbResets++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
        sendNotification(acn);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
     * -----------------------------------------------------
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
     * METHOD NOT EXPOSED FOR MANAGEMENT BY A JMX AGENT
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
     * -----------------------------------------------------
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
     * Return the "NbResets" property.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
     * This method is not a Getter in the JMX sense because
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
     * it is not exposed in the "SimpleStandardMBean" interface.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
     * @return the current value of the "NbResets" property.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
    public int getNbResets() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
        return nbResets;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
     * ---------------
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
     * PRIVATE METHODS
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
     * ---------------
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
     * Check that the principal contained in the Subject is of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
     * type JMXPrincipal and refers to the principalName identity.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
     */
1570
4165709c91e3 5072267: A way to communicate client context such as locale to the JMX server
emcmanus
parents: 2
diff changeset
   152
    private void checkSubject(String op) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
        AccessControlContext acc = AccessController.getContext();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
        Subject subject = Subject.getSubject(acc);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
        Set principals = subject.getPrincipals();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
        Principal principal = (Principal) principals.iterator().next();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
        if (!(principal instanceof JMXPrincipal))
1570
4165709c91e3 5072267: A way to communicate client context such as locale to the JMX server
emcmanus
parents: 2
diff changeset
   158
            throw new SecurityException(op+": Authenticated subject contains " +
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
                                        "invalid principal type = " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
                                        principal.getClass().getName());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
        String identity = principal.getName();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
        if (!identity.equals(principalName))
1570
4165709c91e3 5072267: A way to communicate client context such as locale to the JMX server
emcmanus
parents: 2
diff changeset
   163
            throw new SecurityException(op+": Authenticated subject contains " +
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
                                        "invalid principal name = " + identity);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
     * -----------------------------------------------------
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
     * ATTRIBUTES ACCESSIBLE FOR MANAGEMENT BY A JMX AGENT
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
     * -----------------------------------------------------
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
    private String state = "initial state";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
    private int nbChanges = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
     * -----------------------------------------------------
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
     * PROPERTY NOT ACCESSIBLE FOR MANAGEMENT BY A JMX AGENT
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
     * -----------------------------------------------------
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
    private int nbResets = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
    private String principalName;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
}