jdk/src/share/classes/javax/management/modelmbean/RequiredModelMBean.java
author xdono
Wed, 02 Jul 2008 12:55:45 -0700
changeset 715 f16baef3a20e
parent 623 97dd054b7f1f
child 834 dc74d4ddc28e
permissions -rw-r--r--
6719955: Update copyright year Summary: Update copyright year for files that have been modified in 2008 Reviewed-by: ohair, tbell
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
715
f16baef3a20e 6719955: Update copyright year
xdono
parents: 623
diff changeset
     2
 * Portions Copyright 2000-2008 Sun Microsystems, Inc.  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.  Sun designates this
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
90ce3da70b43 Initial load
duke
parents:
diff changeset
     9
 * by Sun in the LICENSE file that accompanied this code.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 * accompanied this code).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    20
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    21
 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    22
 * CA 95054 USA or visit www.sun.com if you need additional information or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    23
 * have any questions.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
 * @author    IBM Corp.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
 * Copyright IBM Corp. 1999-2000.  All rights reserved.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
package javax.management.modelmbean;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
/* java imports */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
import static com.sun.jmx.defaults.JmxProperties.MODELMBEAN_LOGGER;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
import java.io.FileOutputStream;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
import java.io.PrintStream;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
import java.lang.reflect.InvocationTargetException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
import java.lang.reflect.Method;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
import java.util.Date;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
import java.util.HashMap;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
import java.util.HashSet;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
import java.util.Iterator;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
import java.util.logging.Level;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
import java.util.Map;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
import java.util.Set;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
263
db2a63668df3 6675768: NoSuchElementException thrown in RequiredModelMBean when tracing enabled
emcmanus
parents: 2
diff changeset
    51
import java.util.Vector;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
import javax.management.Attribute;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
import javax.management.AttributeChangeNotification;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
import javax.management.AttributeChangeNotificationFilter;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
import javax.management.AttributeList;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
import javax.management.AttributeNotFoundException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
import javax.management.Descriptor;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
import javax.management.InstanceNotFoundException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
import javax.management.InvalidAttributeValueException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
import javax.management.ListenerNotFoundException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
import javax.management.MBeanAttributeInfo;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
import javax.management.MBeanConstructorInfo;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
import javax.management.MBeanException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
import javax.management.MBeanInfo;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
import javax.management.MBeanNotificationInfo;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
import javax.management.MBeanOperationInfo;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
import javax.management.MBeanRegistration;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
import javax.management.MBeanServer;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
import javax.management.MBeanServerFactory;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
import javax.management.Notification;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
import javax.management.NotificationBroadcasterSupport;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
import javax.management.NotificationEmitter;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
import javax.management.NotificationFilter;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
import javax.management.NotificationListener;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
import javax.management.ObjectName;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
import javax.management.ReflectionException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
import javax.management.RuntimeErrorException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
import javax.management.RuntimeOperationsException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
import javax.management.ServiceNotFoundException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
import javax.management.loading.ClassLoaderRepository;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
import sun.reflect.misc.MethodUtil;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
import sun.reflect.misc.ReflectUtil;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
 * This class is the implementation of a ModelMBean. An appropriate
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
 * implementation of a ModelMBean must be shipped with every JMX Agent
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
 * and the class must be named RequiredModelMBean.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
 * <P>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
 * Java resources wishing to be manageable instantiate the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
 * RequiredModelMBean using the MBeanServer's createMBean method.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
 * The resource then sets the MBeanInfo and Descriptors for the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
 * RequiredModelMBean instance. The attributes and operations exposed
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
 * via the ModelMBeanInfo for the ModelMBean are accessible
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
 * from MBeans, connectors/adaptors like other MBeans. Through the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
 * Descriptors, values and methods in the managed application can be
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
 * defined and mapped to attributes and operations of the ModelMBean.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
 * This mapping can be defined in an XML formatted file or dynamically and
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
 * programmatically at runtime.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
 * <P>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
 * Every RequiredModelMBean which is instantiated in the MBeanServer
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
 * becomes manageable:<br>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
 * its attributes and operations become remotely accessible through the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
 * connectors/adaptors connected to that MBeanServer.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
 * <P>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
 * A Java object cannot be registered in the MBeanServer unless it is a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
 * JMX compliant MBean. By instantiating a RequiredModelMBean, resources
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
 * are guaranteed that the MBean is valid.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
 * MBeanException and RuntimeOperationsException must be thrown on every
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
 * public method.  This allows for wrapping exceptions from distributed
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
 * communications (RMI, EJB, etc.)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
 * @since 1.5
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
public class RequiredModelMBean
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
    implements ModelMBean, MBeanRegistration, NotificationEmitter {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
    /*************************************/
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
    /* attributes                        */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
    /*************************************/
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
    ModelMBeanInfo modelMBeanInfo;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
    /* Notification broadcaster for any notification to be sent
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
     * from the application through the RequiredModelMBean.  */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
    private NotificationBroadcasterSupport generalBroadcaster = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
    /* Notification broadcaster for attribute change notifications */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
    private NotificationBroadcasterSupport attributeBroadcaster = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
    /* handle, name, or reference for instance on which the actual invoke
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
     * and operations will be executed */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
    private Object managedResource = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
    /* records the registering in MBeanServer */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
    private boolean registered = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
    private transient MBeanServer server = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
    /*************************************/
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
    /* constructors                      */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
    /*************************************/
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
     * Constructs an <CODE>RequiredModelMBean</CODE> with an empty
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
     * ModelMBeanInfo.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
     * <P>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
     * The RequiredModelMBean's MBeanInfo and Descriptors
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
     * can be customized using the {@link #setModelMBeanInfo} method.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
     * After the RequiredModelMBean's MBeanInfo and Descriptors are
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
     * customized, the RequiredModelMBean can be registered with
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
     * the MBeanServer.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
     * @exception MBeanException Wraps a distributed communication Exception.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
     * @exception RuntimeOperationsException Wraps a {@link
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
     * RuntimeException} during the construction of the object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
     **/
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
    public RequiredModelMBean()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
        throws MBeanException, RuntimeOperationsException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
        if (MODELMBEAN_LOGGER.isLoggable(Level.FINER)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
            MODELMBEAN_LOGGER.logp(Level.FINER,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
                    RequiredModelMBean.class.getName(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
                    "RequiredModelMBean()", "Entry");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
        modelMBeanInfo = createDefaultModelMBeanInfo();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
        if (MODELMBEAN_LOGGER.isLoggable(Level.FINER)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
            MODELMBEAN_LOGGER.logp(Level.FINER,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
                    RequiredModelMBean.class.getName(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
                    "RequiredModelMBean()", "Exit");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
     * Constructs a RequiredModelMBean object using ModelMBeanInfo passed in.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
     * As long as the RequiredModelMBean is not registered
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
     * with the MBeanServer yet, the RequiredModelMBean's MBeanInfo and
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
     * Descriptors can be customized using the {@link #setModelMBeanInfo}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
     * method.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
     * After the RequiredModelMBean's MBeanInfo and Descriptors are
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
     * customized, the RequiredModelMBean can be registered with the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
     * MBeanServer.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
     * @param mbi The ModelMBeanInfo object to be used by the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
     *            RequiredModelMBean. The given ModelMBeanInfo is cloned
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
     *            and modified as specified by {@link #setModelMBeanInfo}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
     * @exception MBeanException Wraps a distributed communication Exception.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
     * @exception RuntimeOperationsException Wraps an
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
     *    {link java.lang.IllegalArgumentException}:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
     *          The MBeanInfo passed in parameter is null.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
     **/
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
    public RequiredModelMBean(ModelMBeanInfo mbi)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
        throws MBeanException, RuntimeOperationsException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
        if (MODELMBEAN_LOGGER.isLoggable(Level.FINER)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
            MODELMBEAN_LOGGER.logp(Level.FINER,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
                    RequiredModelMBean.class.getName(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
                    "RequiredModelMBean(MBeanInfo)", "Entry");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
        setModelMBeanInfo(mbi);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
        if (MODELMBEAN_LOGGER.isLoggable(Level.FINER)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
            MODELMBEAN_LOGGER.logp(Level.FINER,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
                    RequiredModelMBean.class.getName(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
                    "RequiredModelMBean(MBeanInfo)", "Exit");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
    /*************************************/
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
    /* initializers                      */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
    /*************************************/
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
     * Initializes a ModelMBean object using ModelMBeanInfo passed in.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
     * This method makes it possible to set a customized ModelMBeanInfo on
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
     * the ModelMBean as long as it is not registered with the MBeanServer.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
     * <br>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
     * Once the ModelMBean's ModelMBeanInfo (with Descriptors) are
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
     * customized and set on the ModelMBean, the  ModelMBean be
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
     * registered with the MBeanServer.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
     * <P>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
     * If the ModelMBean is currently registered, this method throws
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
     * a {@link javax.management.RuntimeOperationsException} wrapping an
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
     * {@link IllegalStateException}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
     * <P>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
     * If the given <var>inModelMBeanInfo</var> does not contain any
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
     * {@link ModelMBeanNotificationInfo} for the <code>GENERIC</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
     * or <code>ATTRIBUTE_CHANGE</code> notifications, then the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
     * RequiredModelMBean will supply its own default
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
     * {@link ModelMBeanNotificationInfo ModelMBeanNotificationInfo}s for
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
     * those missing notifications.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
     * @param mbi The ModelMBeanInfo object to be used
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
     *        by the ModelMBean.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
     * @exception MBeanException Wraps a distributed communication
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
     *        Exception.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
     * @exception RuntimeOperationsException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
     * <ul><li>Wraps an {@link IllegalArgumentException} if
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
     *         the MBeanInfo passed in parameter is null.</li>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
     *     <li>Wraps an {@link IllegalStateException} if the ModelMBean
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
     *         is currently registered in the MBeanServer.</li>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
     * </ul>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
     **/
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
    public void setModelMBeanInfo(ModelMBeanInfo mbi)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
        throws MBeanException, RuntimeOperationsException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
        if (MODELMBEAN_LOGGER.isLoggable(Level.FINER)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
            MODELMBEAN_LOGGER.logp(Level.FINER,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
                    RequiredModelMBean.class.getName(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
                "setModelMBeanInfo(ModelMBeanInfo)","Entry");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
        if (mbi == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
            if (MODELMBEAN_LOGGER.isLoggable(Level.FINER)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
                MODELMBEAN_LOGGER.logp(Level.FINER,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
                        RequiredModelMBean.class.getName(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
                    "setModelMBeanInfo(ModelMBeanInfo)",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
                    "ModelMBeanInfo is null: Raising exception.");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
            final RuntimeException x = new
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
                IllegalArgumentException("ModelMBeanInfo must not be null");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
            final String exceptionText =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
                "Exception occurred trying to initialize the " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
                "ModelMBeanInfo of the RequiredModelMBean";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
            throw new RuntimeOperationsException(x,exceptionText);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
        if (registered) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
            if (MODELMBEAN_LOGGER.isLoggable(Level.FINER)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
                MODELMBEAN_LOGGER.logp(Level.FINER,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
                        RequiredModelMBean.class.getName(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
                    "setModelMBeanInfo(ModelMBeanInfo)",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
                    "RequiredMBean is registered: Raising exception.");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
            final String exceptionText =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
                "Exception occurred trying to set the " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
                "ModelMBeanInfo of the RequiredModelMBean";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
            final RuntimeException x = new IllegalStateException(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
             "cannot call setModelMBeanInfo while ModelMBean is registered");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
            throw new RuntimeOperationsException(x,exceptionText);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
        if (MODELMBEAN_LOGGER.isLoggable(Level.FINER)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
            MODELMBEAN_LOGGER.logp(Level.FINER,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
                    RequiredModelMBean.class.getName(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
                "setModelMBeanInfo(ModelMBeanInfo)",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
                "Setting ModelMBeanInfo to " + printModelMBeanInfo(mbi));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
            MODELMBEAN_LOGGER.logp(Level.FINER,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
                    RequiredModelMBean.class.getName(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
                "setModelMBeanInfo(ModelMBeanInfo)",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
                "ModelMBeanInfo notifications has " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
                (mbi.getNotifications()).length + " elements");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
        modelMBeanInfo = (ModelMBeanInfo)mbi.clone();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
        if (MODELMBEAN_LOGGER.isLoggable(Level.FINER)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
            MODELMBEAN_LOGGER.logp(Level.FINER,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
                    RequiredModelMBean.class.getName(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
                "setModelMBeanInfo(ModelMBeanInfo)","set mbeanInfo to: "+
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
                 printModelMBeanInfo(modelMBeanInfo));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
            MODELMBEAN_LOGGER.logp(Level.FINER,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
                    RequiredModelMBean.class.getName(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
                "setModelMBeanInfo(ModelMBeanInfo)","Exit");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
     * Sets the instance handle of the object against which to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
     * execute all methods in this ModelMBean management interface
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
     * (MBeanInfo and Descriptors).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
     * @param mr Object that is the managed resource
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
     * @param mr_type The type of reference for the managed resource.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
     *     <br>Can be: "ObjectReference", "Handle", "IOR", "EJBHandle",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
     *         or "RMIReference".
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
     *     <br>In this implementation only "ObjectReference" is supported.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
     * @exception MBeanException The initializer of the object has
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
     *            thrown an exception.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
     * @exception InstanceNotFoundException The managed resource
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
     *            object could not be found
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
     * @exception InvalidTargetObjectTypeException The managed
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
     *            resource type should be "ObjectReference".
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
     * @exception RuntimeOperationsException Wraps a {@link
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
     *            RuntimeException} when setting the resource.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
     **/
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
    public void setManagedResource(Object mr, String mr_type)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
        throws MBeanException, RuntimeOperationsException,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
               InstanceNotFoundException, InvalidTargetObjectTypeException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
        if (MODELMBEAN_LOGGER.isLoggable(Level.FINER)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
            MODELMBEAN_LOGGER.logp(Level.FINER,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
                    RequiredModelMBean.class.getName(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
                "setManagedResource(Object,String)","Entry");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   341
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   342
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
        // check that the mr_type is supported by this JMXAgent
90ce3da70b43 Initial load
duke
parents:
diff changeset
   344
        // only "objectReference" is supported
90ce3da70b43 Initial load
duke
parents:
diff changeset
   345
        if ((mr_type == null) ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   346
            (! mr_type.equalsIgnoreCase("objectReference"))) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   347
            if (MODELMBEAN_LOGGER.isLoggable(Level.FINER)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   348
                MODELMBEAN_LOGGER.logp(Level.FINER,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   349
                        RequiredModelMBean.class.getName(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   350
                    "setManagedResource(Object,String)",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   351
                    "Managed Resouce Type is not supported: " + mr_type);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   352
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   353
            throw new InvalidTargetObjectTypeException(mr_type);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   354
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   355
90ce3da70b43 Initial load
duke
parents:
diff changeset
   356
        if (MODELMBEAN_LOGGER.isLoggable(Level.FINER)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   357
            MODELMBEAN_LOGGER.logp(Level.FINER,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   358
                    RequiredModelMBean.class.getName(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   359
                "setManagedResource(Object,String)",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   360
                "Managed Resouce is valid");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   361
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   362
        managedResource = mr;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   363
90ce3da70b43 Initial load
duke
parents:
diff changeset
   364
        if (MODELMBEAN_LOGGER.isLoggable(Level.FINER)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   365
            MODELMBEAN_LOGGER.logp(Level.FINER,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   366
                    RequiredModelMBean.class.getName(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   367
                "setManagedResource(Object, String)", "Exit");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   368
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   369
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   370
90ce3da70b43 Initial load
duke
parents:
diff changeset
   371
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   372
     * <p>Instantiates this MBean instance with the data found for
90ce3da70b43 Initial load
duke
parents:
diff changeset
   373
     * the MBean in the persistent store.  The data loaded could include
90ce3da70b43 Initial load
duke
parents:
diff changeset
   374
     * attribute and operation values.</p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   375
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   376
     * <p>This method should be called during construction or
90ce3da70b43 Initial load
duke
parents:
diff changeset
   377
     * initialization of this instance, and before the MBean is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   378
     * registered with the MBeanServer.</p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   379
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   380
     * <p>If the implementation of this class does not support
90ce3da70b43 Initial load
duke
parents:
diff changeset
   381
     * persistence, an {@link MBeanException} wrapping a {@link
90ce3da70b43 Initial load
duke
parents:
diff changeset
   382
     * ServiceNotFoundException} is thrown.</p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   383
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   384
     * @exception MBeanException Wraps another exception, or
90ce3da70b43 Initial load
duke
parents:
diff changeset
   385
     * persistence is not supported
90ce3da70b43 Initial load
duke
parents:
diff changeset
   386
     * @exception RuntimeOperationsException Wraps exceptions from the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   387
     * persistence mechanism
90ce3da70b43 Initial load
duke
parents:
diff changeset
   388
     * @exception InstanceNotFoundException Could not find or load
90ce3da70b43 Initial load
duke
parents:
diff changeset
   389
     * this MBean from persistent storage
90ce3da70b43 Initial load
duke
parents:
diff changeset
   390
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   391
    public void load()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   392
        throws MBeanException, RuntimeOperationsException,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   393
               InstanceNotFoundException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   394
        final ServiceNotFoundException x = new ServiceNotFoundException(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   395
                                "Persistence not supported for this MBean");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   396
        throw new MBeanException(x, x.getMessage());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   397
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   398
90ce3da70b43 Initial load
duke
parents:
diff changeset
   399
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   400
     * <p>Captures the current state of this MBean instance and writes
90ce3da70b43 Initial load
duke
parents:
diff changeset
   401
     * it out to the persistent store.  The state stored could include
90ce3da70b43 Initial load
duke
parents:
diff changeset
   402
     * attribute and operation values.</p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   403
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   404
     * <p>If the implementation of this class does not support
90ce3da70b43 Initial load
duke
parents:
diff changeset
   405
     * persistence, an {@link MBeanException} wrapping a {@link
90ce3da70b43 Initial load
duke
parents:
diff changeset
   406
     * ServiceNotFoundException} is thrown.</p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   407
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   408
     * <p>Persistence policy from the MBean and attribute descriptor
90ce3da70b43 Initial load
duke
parents:
diff changeset
   409
     * is used to guide execution of this method. The MBean should be
90ce3da70b43 Initial load
duke
parents:
diff changeset
   410
     * stored if 'persistPolicy' field is:</p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   411
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   412
     * <PRE>  != "never"
90ce3da70b43 Initial load
duke
parents:
diff changeset
   413
     *   = "always"
90ce3da70b43 Initial load
duke
parents:
diff changeset
   414
     *   = "onTimer" and now > 'lastPersistTime' + 'persistPeriod'
90ce3da70b43 Initial load
duke
parents:
diff changeset
   415
     *   = "NoMoreOftenThan" and now > 'lastPersistTime' + 'persistPeriod'
90ce3da70b43 Initial load
duke
parents:
diff changeset
   416
     *   = "onUnregister"
90ce3da70b43 Initial load
duke
parents:
diff changeset
   417
     * </PRE>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   418
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   419
     * <p>Do not store the MBean if 'persistPolicy' field is:</p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   420
     * <PRE>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   421
     *    = "never"
90ce3da70b43 Initial load
duke
parents:
diff changeset
   422
     *    = "onUpdate"
90ce3da70b43 Initial load
duke
parents:
diff changeset
   423
     *    = "onTimer" && now < 'lastPersistTime' + 'persistPeriod'
90ce3da70b43 Initial load
duke
parents:
diff changeset
   424
     * </PRE>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   425
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   426
     * @exception MBeanException Wraps another exception, or
90ce3da70b43 Initial load
duke
parents:
diff changeset
   427
     * persistence is not supported
90ce3da70b43 Initial load
duke
parents:
diff changeset
   428
     * @exception RuntimeOperationsException Wraps exceptions from the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   429
     * persistence mechanism
90ce3da70b43 Initial load
duke
parents:
diff changeset
   430
     * @exception InstanceNotFoundException Could not find/access the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   431
     * persistent store
90ce3da70b43 Initial load
duke
parents:
diff changeset
   432
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   433
    public void store()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   434
        throws MBeanException, RuntimeOperationsException,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   435
               InstanceNotFoundException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   436
        final ServiceNotFoundException x = new ServiceNotFoundException(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   437
                                "Persistence not supported for this MBean");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   438
        throw new MBeanException(x, x.getMessage());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   439
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   440
90ce3da70b43 Initial load
duke
parents:
diff changeset
   441
    /*************************************/
90ce3da70b43 Initial load
duke
parents:
diff changeset
   442
    /* DynamicMBean Interface            */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   443
    /*************************************/
90ce3da70b43 Initial load
duke
parents:
diff changeset
   444
90ce3da70b43 Initial load
duke
parents:
diff changeset
   445
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   446
     * The resolveForCacheValue method checks the descriptor passed in to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   447
     * see if there is a valid cached value in the descriptor.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   448
     * The valid value will be in the 'value' field if there is one.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   449
     * If the 'currencyTimeLimit' field in the descriptor is:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   450
     * <ul>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   451
     *   <li><b>&lt;0</b> Then the value is not cached and is never valid.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   452
     *         Null is returned. The 'value' and 'lastUpdatedTimeStamp'
90ce3da70b43 Initial load
duke
parents:
diff changeset
   453
     *         fields are cleared.</li>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   454
     *   <li><b>=0</b> Then the value is always cached and always valid.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   455
     *         The 'value' field is returned.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   456
     *         The 'lastUpdatedTimeStamp' field is not checked.</li>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   457
     *   <li><b>&gt;0</b> Represents the number of seconds that the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   458
     *         'value' field is valid.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   459
     *         The 'value' field is no longer valid when
90ce3da70b43 Initial load
duke
parents:
diff changeset
   460
     *         'lastUpdatedTimeStamp' + 'currencyTimeLimit' &gt; Now.</li>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   461
     * </ul>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   462
     * <li>When 'value' is valid, 'valid' is returned.</li>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   463
     * <li>When 'value' is no longer valid then null is returned and
90ce3da70b43 Initial load
duke
parents:
diff changeset
   464
     *     'value' and 'lastUpdatedTimeStamp' fields are cleared.</li>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   465
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   466
     **/
90ce3da70b43 Initial load
duke
parents:
diff changeset
   467
    private Object resolveForCacheValue(Descriptor descr)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   468
        throws MBeanException, RuntimeOperationsException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   469
90ce3da70b43 Initial load
duke
parents:
diff changeset
   470
        final boolean tracing = MODELMBEAN_LOGGER.isLoggable(Level.FINER);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   471
        final String mth = "resolveForCacheValue(Descriptor)";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   472
        if (tracing) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   473
            MODELMBEAN_LOGGER.logp(Level.FINER,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   474
                    RequiredModelMBean.class.getName(),mth,"Entry");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   475
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   476
90ce3da70b43 Initial load
duke
parents:
diff changeset
   477
        Object response = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   478
        boolean resetValue = false, returnCachedValue = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   479
        long currencyPeriod = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   480
90ce3da70b43 Initial load
duke
parents:
diff changeset
   481
        if (descr == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   482
            if (tracing) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   483
                MODELMBEAN_LOGGER.logp(Level.FINER,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   484
                        RequiredModelMBean.class.getName(),mth,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   485
                    "Input Descriptor is null");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   486
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   487
            return response;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   488
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   489
90ce3da70b43 Initial load
duke
parents:
diff changeset
   490
        if (tracing) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   491
            MODELMBEAN_LOGGER.logp(Level.FINER,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   492
                    RequiredModelMBean.class.getName(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   493
                    mth, "descriptor is " + descr);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   494
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   495
90ce3da70b43 Initial load
duke
parents:
diff changeset
   496
        final Descriptor mmbDescr = modelMBeanInfo.getMBeanDescriptor();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   497
        if (mmbDescr == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   498
            if (tracing) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   499
                MODELMBEAN_LOGGER.logp(Level.FINER,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   500
                    RequiredModelMBean.class.getName(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   501
                        mth,"MBean Descriptor is null");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   502
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   503
            //return response;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   504
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   505
90ce3da70b43 Initial load
duke
parents:
diff changeset
   506
        Object objExpTime = descr.getFieldValue("currencyTimeLimit");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   507
90ce3da70b43 Initial load
duke
parents:
diff changeset
   508
        String expTime;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   509
        if (objExpTime != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   510
            expTime = objExpTime.toString();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   511
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   512
            expTime = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   513
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   514
90ce3da70b43 Initial load
duke
parents:
diff changeset
   515
        if ((expTime == null) && (mmbDescr != null)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   516
            objExpTime = mmbDescr.getFieldValue("currencyTimeLimit");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   517
            if (objExpTime != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   518
                expTime = objExpTime.toString();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   519
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   520
                expTime = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   521
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   522
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   523
90ce3da70b43 Initial load
duke
parents:
diff changeset
   524
        if (expTime != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   525
            if (tracing) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   526
                MODELMBEAN_LOGGER.logp(Level.FINER,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   527
                    RequiredModelMBean.class.getName(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   528
                        mth,"currencyTimeLimit: " + expTime);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   529
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   530
90ce3da70b43 Initial load
duke
parents:
diff changeset
   531
            // convert seconds to milliseconds for time comparison
90ce3da70b43 Initial load
duke
parents:
diff changeset
   532
            currencyPeriod = ((new Long(expTime)).longValue()) * 1000;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   533
            if (currencyPeriod < 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   534
                /* if currencyTimeLimit is -1 then value is never cached */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   535
                returnCachedValue = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   536
                resetValue = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   537
                if (tracing) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   538
                    MODELMBEAN_LOGGER.logp(Level.FINER,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   539
                            RequiredModelMBean.class.getName(),mth,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   540
                        currencyPeriod + ": never Cached");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   541
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   542
            } else if (currencyPeriod == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   543
                /* if currencyTimeLimit is 0 then value is always cached */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   544
                returnCachedValue = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   545
                resetValue = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   546
                if (tracing) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   547
                    MODELMBEAN_LOGGER.logp(Level.FINER,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   548
                            RequiredModelMBean.class.getName(),mth,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   549
                        "always valid Cache");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   550
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   551
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   552
                Object objtStamp =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   553
                    descr.getFieldValue("lastUpdatedTimeStamp");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   554
90ce3da70b43 Initial load
duke
parents:
diff changeset
   555
                String tStamp;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   556
                if (objtStamp != null) tStamp = objtStamp.toString();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   557
                else tStamp = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   558
90ce3da70b43 Initial load
duke
parents:
diff changeset
   559
                if (tracing) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   560
                    MODELMBEAN_LOGGER.logp(Level.FINER,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   561
                            RequiredModelMBean.class.getName(),mth,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   562
                        "lastUpdatedTimeStamp: " + tStamp);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   563
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   564
90ce3da70b43 Initial load
duke
parents:
diff changeset
   565
                if (tStamp == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   566
                    tStamp = "0";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   567
90ce3da70b43 Initial load
duke
parents:
diff changeset
   568
                long lastTime = (new Long(tStamp)).longValue();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   569
90ce3da70b43 Initial load
duke
parents:
diff changeset
   570
                if (tracing) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   571
                    MODELMBEAN_LOGGER.logp(Level.FINER,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   572
                            RequiredModelMBean.class.getName(),mth,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   573
                        "currencyPeriod:" + currencyPeriod +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   574
                        " lastUpdatedTimeStamp:" + lastTime);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   575
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   576
90ce3da70b43 Initial load
duke
parents:
diff changeset
   577
                long now = (new Date()).getTime();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   578
90ce3da70b43 Initial load
duke
parents:
diff changeset
   579
                if (now < (lastTime + currencyPeriod)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   580
                    returnCachedValue = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   581
                    resetValue = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   582
                    if (tracing) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   583
                        MODELMBEAN_LOGGER.logp(Level.FINER,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   584
                                RequiredModelMBean.class.getName(),mth,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   585
                            " timed valid Cache for " + now + " < " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   586
                            (lastTime + currencyPeriod));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   587
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   588
                } else { /* value is expired */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   589
                    returnCachedValue = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   590
                    resetValue = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   591
                    if (tracing) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   592
                        MODELMBEAN_LOGGER.logp(Level.FINER,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   593
                                RequiredModelMBean.class.getName(),mth,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   594
                            "timed expired cache for " + now + " > " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   595
                            (lastTime + currencyPeriod));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   596
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   597
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   598
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   599
            if (tracing) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   600
                MODELMBEAN_LOGGER.logp(Level.FINER,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   601
                        RequiredModelMBean.class.getName(),mth,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   602
                    "returnCachedValue:" + returnCachedValue +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   603
                    " resetValue: " + resetValue);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   604
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   605
90ce3da70b43 Initial load
duke
parents:
diff changeset
   606
            if (returnCachedValue == true) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   607
                Object currValue = descr.getFieldValue("value");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   608
                if (currValue != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   609
                    /* error/validity check return value here */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   610
                    response = currValue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   611
                    /* need to cast string cached value to type */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   612
                    if (tracing) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   613
                        MODELMBEAN_LOGGER.logp(Level.FINER,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   614
                                RequiredModelMBean.class.getName(),mth,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   615
                            "valid Cache value: " + currValue);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   616
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   617
90ce3da70b43 Initial load
duke
parents:
diff changeset
   618
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   619
                    response = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   620
                    if (tracing) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   621
                        MODELMBEAN_LOGGER.logp(Level.FINER,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   622
                            RequiredModelMBean.class.getName(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   623
                                mth,"no Cached value");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   624
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   625
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   626
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   627
90ce3da70b43 Initial load
duke
parents:
diff changeset
   628
            if (resetValue == true) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   629
                /* value is not current, so remove it */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   630
                descr.removeField("lastUpdatedTimeStamp");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   631
                descr.removeField("value");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   632
                response = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   633
                modelMBeanInfo.setDescriptor(descr,null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   634
                if (tracing) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   635
                    MODELMBEAN_LOGGER.logp(Level.FINER,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   636
                        RequiredModelMBean.class.getName(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   637
                            mth,"reset cached value to null");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   638
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   639
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   640
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   641
90ce3da70b43 Initial load
duke
parents:
diff changeset
   642
        if (tracing) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   643
            MODELMBEAN_LOGGER.logp(Level.FINER,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   644
                    RequiredModelMBean.class.getName(),mth,"Exit");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   645
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   646
90ce3da70b43 Initial load
duke
parents:
diff changeset
   647
        return response;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   648
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   649
90ce3da70b43 Initial load
duke
parents:
diff changeset
   650
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   651
     * Returns the attributes, operations, constructors and notifications
90ce3da70b43 Initial load
duke
parents:
diff changeset
   652
     * that this RequiredModelMBean exposes for management.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   653
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   654
     * @return  An instance of ModelMBeanInfo allowing retrieval all
90ce3da70b43 Initial load
duke
parents:
diff changeset
   655
     *          attributes, operations, and Notifications of this MBean.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   656
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   657
     **/
90ce3da70b43 Initial load
duke
parents:
diff changeset
   658
    public MBeanInfo getMBeanInfo() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   659
90ce3da70b43 Initial load
duke
parents:
diff changeset
   660
        if (MODELMBEAN_LOGGER.isLoggable(Level.FINER)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   661
            MODELMBEAN_LOGGER.logp(Level.FINER,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   662
                    RequiredModelMBean.class.getName(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   663
                    "getMBeanInfo()","Entry");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   664
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   665
90ce3da70b43 Initial load
duke
parents:
diff changeset
   666
        if (modelMBeanInfo == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   667
            if (MODELMBEAN_LOGGER.isLoggable(Level.FINER)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   668
                MODELMBEAN_LOGGER.logp(Level.FINER,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   669
                        RequiredModelMBean.class.getName(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   670
                    "getMBeanInfo()","modelMBeanInfo is null");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   671
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   672
            modelMBeanInfo = createDefaultModelMBeanInfo();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   673
            //return new ModelMBeanInfo(" ", "", null, null, null, null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   674
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   675
90ce3da70b43 Initial load
duke
parents:
diff changeset
   676
        if (MODELMBEAN_LOGGER.isLoggable(Level.FINER)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   677
            MODELMBEAN_LOGGER.logp(Level.FINER,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   678
                    RequiredModelMBean.class.getName(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   679
                "getMBeanInfo()","ModelMBeanInfo is " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   680
                  modelMBeanInfo.getClassName() + " for " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   681
                  modelMBeanInfo.getDescription());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   682
            MODELMBEAN_LOGGER.logp(Level.FINER,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   683
                    RequiredModelMBean.class.getName(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   684
                "getMBeanInfo()",printModelMBeanInfo(modelMBeanInfo));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   685
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   686
90ce3da70b43 Initial load
duke
parents:
diff changeset
   687
        return((MBeanInfo) modelMBeanInfo.clone());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   688
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   689
90ce3da70b43 Initial load
duke
parents:
diff changeset
   690
    private String printModelMBeanInfo(ModelMBeanInfo info) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   691
        final StringBuilder retStr = new StringBuilder();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   692
        if (info == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   693
            if (MODELMBEAN_LOGGER.isLoggable(Level.FINER)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   694
                MODELMBEAN_LOGGER.logp(Level.FINER,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   695
                        RequiredModelMBean.class.getName(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   696
                        "printModelMBeanInfo(ModelMBeanInfo)",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   697
                        "ModelMBeanInfo to print is null, " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   698
                        "printing local ModelMBeanInfo");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   699
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   700
            info = modelMBeanInfo;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   701
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   702
90ce3da70b43 Initial load
duke
parents:
diff changeset
   703
        retStr.append("\nMBeanInfo for ModelMBean is:");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   704
        retStr.append("\nCLASSNAME: \t"+ info.getClassName());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   705
        retStr.append("\nDESCRIPTION: \t"+ info.getDescription());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   706
90ce3da70b43 Initial load
duke
parents:
diff changeset
   707
90ce3da70b43 Initial load
duke
parents:
diff changeset
   708
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   709
            retStr.append("\nMBEAN DESCRIPTOR: \t"+
90ce3da70b43 Initial load
duke
parents:
diff changeset
   710
                          info.getMBeanDescriptor());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   711
        } catch (Exception e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   712
            retStr.append("\nMBEAN DESCRIPTOR: \t" + " is invalid");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   713
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   714
90ce3da70b43 Initial load
duke
parents:
diff changeset
   715
        retStr.append("\nATTRIBUTES");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   716
90ce3da70b43 Initial load
duke
parents:
diff changeset
   717
        final MBeanAttributeInfo[] attrInfo = info.getAttributes();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   718
        if ((attrInfo != null) && (attrInfo.length>0)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   719
            for (int i=0; i<attrInfo.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   720
                final ModelMBeanAttributeInfo attInfo =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   721
                    (ModelMBeanAttributeInfo)attrInfo[i];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   722
                retStr.append(" ** NAME: \t"+ attInfo.getName());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   723
                retStr.append("    DESCR: \t"+ attInfo.getDescription());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   724
                retStr.append("    TYPE: \t"+ attInfo.getType() +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   725
                              "    READ: \t"+ attInfo.isReadable() +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   726
                              "    WRITE: \t"+ attInfo.isWritable());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   727
                retStr.append("    DESCRIPTOR: " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   728
                              attInfo.getDescriptor().toString());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   729
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   730
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   731
            retStr.append(" ** No attributes **");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   732
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   733
90ce3da70b43 Initial load
duke
parents:
diff changeset
   734
        retStr.append("\nCONSTRUCTORS");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   735
        final MBeanConstructorInfo[] constrInfo = info.getConstructors();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   736
        if ((constrInfo != null) && (constrInfo.length > 0 )) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   737
            for (int i=0; i<constrInfo.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   738
                final ModelMBeanConstructorInfo ctorInfo =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   739
                    (ModelMBeanConstructorInfo)constrInfo[i];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   740
                retStr.append(" ** NAME: \t"+ ctorInfo.getName());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   741
                retStr.append("    DESCR: \t"+
90ce3da70b43 Initial load
duke
parents:
diff changeset
   742
                              ctorInfo.getDescription());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   743
                retStr.append("    PARAM: \t"+
90ce3da70b43 Initial load
duke
parents:
diff changeset
   744
                              ctorInfo.getSignature().length +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   745
                              " parameter(s)");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   746
                retStr.append("    DESCRIPTOR: " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   747
                              ctorInfo.getDescriptor().toString());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   748
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   749
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   750
            retStr.append(" ** No Constructors **");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   751
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   752
90ce3da70b43 Initial load
duke
parents:
diff changeset
   753
        retStr.append("\nOPERATIONS");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   754
        final MBeanOperationInfo[] opsInfo = info.getOperations();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   755
        if ((opsInfo != null) && (opsInfo.length>0)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   756
            for (int i=0; i<opsInfo.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   757
                final ModelMBeanOperationInfo operInfo =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   758
                    (ModelMBeanOperationInfo)opsInfo[i];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   759
                retStr.append(" ** NAME: \t"+ operInfo.getName());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   760
                retStr.append("    DESCR: \t"+ operInfo.getDescription());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   761
                retStr.append("    PARAM: \t"+
90ce3da70b43 Initial load
duke
parents:
diff changeset
   762
                              operInfo.getSignature().length +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   763
                              " parameter(s)");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   764
                retStr.append("    DESCRIPTOR: " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   765
                              operInfo.getDescriptor().toString());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   766
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   767
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   768
            retStr.append(" ** No operations ** ");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   769
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   770
90ce3da70b43 Initial load
duke
parents:
diff changeset
   771
        retStr.append("\nNOTIFICATIONS");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   772
90ce3da70b43 Initial load
duke
parents:
diff changeset
   773
        MBeanNotificationInfo[] notifInfo = info.getNotifications();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   774
        if ((notifInfo != null) && (notifInfo.length>0)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   775
            for (int i=0; i<notifInfo.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   776
                final ModelMBeanNotificationInfo nInfo =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   777
                    (ModelMBeanNotificationInfo)notifInfo[i];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   778
                retStr.append(" ** NAME: \t"+ nInfo.getName());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   779
                retStr.append("    DESCR: \t"+ nInfo.getDescription());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   780
                retStr.append("    DESCRIPTOR: " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   781
                              nInfo.getDescriptor().toString());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   782
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   783
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   784
            retStr.append(" ** No notifications **");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   785
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   786
90ce3da70b43 Initial load
duke
parents:
diff changeset
   787
        retStr.append(" ** ModelMBean: End of MBeanInfo ** ");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   788
90ce3da70b43 Initial load
duke
parents:
diff changeset
   789
        return retStr.toString();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   790
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   791
90ce3da70b43 Initial load
duke
parents:
diff changeset
   792
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   793
     * Invokes a method on or through a RequiredModelMBean and returns
90ce3da70b43 Initial load
duke
parents:
diff changeset
   794
     * the result of the method execution.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   795
     * <P>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   796
     * If the given method to be invoked, together with the provided
90ce3da70b43 Initial load
duke
parents:
diff changeset
   797
     * signature, matches one of RequiredModelMbean
90ce3da70b43 Initial load
duke
parents:
diff changeset
   798
     * accessible methods, this one will be call. Otherwise the call to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   799
     * the given method will be tried on the managed resource.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   800
     * <P>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   801
     * The last value returned by an operation may be cached in
90ce3da70b43 Initial load
duke
parents:
diff changeset
   802
     * the operation's descriptor which
90ce3da70b43 Initial load
duke
parents:
diff changeset
   803
     * is in the ModelMBeanOperationInfo's descriptor.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   804
     * The valid value will be in the 'value' field if there is one.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   805
     * If the 'currencyTimeLimit' field in the descriptor is:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   806
     * <UL>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   807
     * <LI><b>&lt;0</b> Then the value is not cached and is never valid.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   808
     *      The operation method is invoked.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   809
     *      The 'value' and 'lastUpdatedTimeStamp' fields are cleared.</LI>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   810
     * <LI><b>=0</b> Then the value is always cached and always valid.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   811
     *      The 'value' field is returned. If there is no 'value' field
90ce3da70b43 Initial load
duke
parents:
diff changeset
   812
     *      then the operation method is invoked for the attribute.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   813
     *      The 'lastUpdatedTimeStamp' field and `value' fields are set to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   814
     *      the operation's return value and the current time stamp.</LI>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   815
     * <LI><b>&gt;0</b> Represents the number of seconds that the 'value'
90ce3da70b43 Initial load
duke
parents:
diff changeset
   816
     *      field is valid.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   817
     *      The 'value' field is no longer valid when
90ce3da70b43 Initial load
duke
parents:
diff changeset
   818
     *      'lastUpdatedTimeStamp' + 'currencyTimeLimit' &gt; Now.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   819
     *      <UL>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   820
     *         <LI>When 'value' is valid, 'value' is returned.</LI>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   821
     *         <LI>When 'value' is no longer valid then the operation
90ce3da70b43 Initial load
duke
parents:
diff changeset
   822
     *             method is invoked. The 'lastUpdatedTimeStamp' field
90ce3da70b43 Initial load
duke
parents:
diff changeset
   823
     *             and `value' fields are updated.</lI>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   824
     *      </UL>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   825
     * </LI>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   826
     * </UL>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   827
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   828
     * <p><b>Note:</b> because of inconsistencies in previous versions of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   829
     * this specification, it is recommended not to use negative or zero
90ce3da70b43 Initial load
duke
parents:
diff changeset
   830
     * values for <code>currencyTimeLimit</code>.  To indicate that a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   831
     * cached value is never valid, omit the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   832
     * <code>currencyTimeLimit</code> field.  To indicate that it is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   833
     * always valid, use a very large number for this field.</p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   834
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   835
     * @param opName The name of the method to be invoked. The
90ce3da70b43 Initial load
duke
parents:
diff changeset
   836
     *     name can be the fully qualified method name including the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   837
     *     classname, or just the method name if the classname is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   838
     *     defined in the 'class' field of the operation descriptor.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   839
     * @param opArgs An array containing the parameters to be set
90ce3da70b43 Initial load
duke
parents:
diff changeset
   840
     *     when the operation is invoked
90ce3da70b43 Initial load
duke
parents:
diff changeset
   841
     * @param sig An array containing the signature of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   842
     *     operation. The class objects will be loaded using the same
90ce3da70b43 Initial load
duke
parents:
diff changeset
   843
     *     class loader as the one used for loading the MBean on which
90ce3da70b43 Initial load
duke
parents:
diff changeset
   844
     *     the operation was invoked.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   845
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   846
     * @return  The object returned by the method, which represents the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   847
     *     result of invoking the method on the specified managed resource.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   848
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   849
     * @exception MBeanException  Wraps one of the following Exceptions:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   850
     * <UL>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   851
     * <LI> An Exception thrown by the managed object's invoked method.</LI>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   852
     * <LI> {@link ServiceNotFoundException}: No ModelMBeanOperationInfo or
90ce3da70b43 Initial load
duke
parents:
diff changeset
   853
     *      no descriptor defined for the specified operation or the managed
90ce3da70b43 Initial load
duke
parents:
diff changeset
   854
     *      resource is null.</LI>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   855
     * <LI> {@link InvalidTargetObjectTypeException}: The 'targetType'
90ce3da70b43 Initial load
duke
parents:
diff changeset
   856
     *      field value is not 'objectReference'.</LI>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   857
     * </UL>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   858
     * @exception ReflectionException  Wraps an {@link java.lang.Exception}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   859
     *      thrown while trying to invoke the method.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   860
     * @exception RuntimeOperationsException Wraps an
90ce3da70b43 Initial load
duke
parents:
diff changeset
   861
     *      {@link IllegalArgumentException} Method name is null.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   862
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   863
     **/
90ce3da70b43 Initial load
duke
parents:
diff changeset
   864
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   865
      The requirement to be able to invoke methods on the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   866
      RequiredModelMBean class itself makes this method considerably
90ce3da70b43 Initial load
duke
parents:
diff changeset
   867
      more complicated than it might otherwise be.  Note that, unlike
90ce3da70b43 Initial load
duke
parents:
diff changeset
   868
      earlier versions, we do not allow you to invoke such methods if
90ce3da70b43 Initial load
duke
parents:
diff changeset
   869
      they are not explicitly mentioned in the ModelMBeanInfo.  Doing
90ce3da70b43 Initial load
duke
parents:
diff changeset
   870
      so was potentially a security problem, and certainly very
90ce3da70b43 Initial load
duke
parents:
diff changeset
   871
      surprising.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   872
90ce3da70b43 Initial load
duke
parents:
diff changeset
   873
      We do not look for the method in the RequiredModelMBean class
90ce3da70b43 Initial load
duke
parents:
diff changeset
   874
      itself if:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   875
      (a) there is a "targetObject" field in the Descriptor for the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   876
      operation; or
90ce3da70b43 Initial load
duke
parents:
diff changeset
   877
      (b) there is a "class" field in the Descriptor for the operation
90ce3da70b43 Initial load
duke
parents:
diff changeset
   878
      and the named class is not RequiredModelMBean or one of its
90ce3da70b43 Initial load
duke
parents:
diff changeset
   879
      superinterfaces; or
90ce3da70b43 Initial load
duke
parents:
diff changeset
   880
      (c) the name of the operation is not the name of a method in
90ce3da70b43 Initial load
duke
parents:
diff changeset
   881
      RequiredModelMBean (this is just an optimization).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   882
90ce3da70b43 Initial load
duke
parents:
diff changeset
   883
      In cases (a) and (b), if you have gone to the trouble of adding
90ce3da70b43 Initial load
duke
parents:
diff changeset
   884
      those fields specifically for this operation then presumably you
90ce3da70b43 Initial load
duke
parents:
diff changeset
   885
      do not want RequiredModelMBean's methods to be called.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   886
90ce3da70b43 Initial load
duke
parents:
diff changeset
   887
      We have to pay attention to class loading issues.  If the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   888
      "class" field is present, the named class has to be resolved
90ce3da70b43 Initial load
duke
parents:
diff changeset
   889
      relative to RequiredModelMBean's class loader to test the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   890
      condition (b) above, and relative to the managed resource's
90ce3da70b43 Initial load
duke
parents:
diff changeset
   891
      class loader to ensure that the managed resource is in fact of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   892
      the named class (or a subclass).  The class names in the sig
90ce3da70b43 Initial load
duke
parents:
diff changeset
   893
      array likewise have to be resolved, first against
90ce3da70b43 Initial load
duke
parents:
diff changeset
   894
      RequiredModelMBean's class loader, then against the managed
90ce3da70b43 Initial load
duke
parents:
diff changeset
   895
      resource's class loader.  There is no point in using any other
90ce3da70b43 Initial load
duke
parents:
diff changeset
   896
      loader because when we call Method.invoke we must call it on
90ce3da70b43 Initial load
duke
parents:
diff changeset
   897
      a Method that is implemented by the target object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   898
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   899
    public Object invoke(String opName, Object[] opArgs, String[] sig)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   900
            throws MBeanException, ReflectionException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   901
90ce3da70b43 Initial load
duke
parents:
diff changeset
   902
        final boolean tracing = MODELMBEAN_LOGGER.isLoggable(Level.FINER);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   903
        final String mth = "invoke(String, Object[], String[])";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   904
90ce3da70b43 Initial load
duke
parents:
diff changeset
   905
        if (tracing) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   906
            MODELMBEAN_LOGGER.logp(Level.FINER,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   907
                    RequiredModelMBean.class.getName(), mth, "Entry");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   908
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   909
90ce3da70b43 Initial load
duke
parents:
diff changeset
   910
        if (opName == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   911
            final RuntimeException x =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   912
                new IllegalArgumentException("Method name must not be null");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   913
            throw new RuntimeOperationsException(x,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   914
                      "An exception occurred while trying to " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   915
                      "invoke a method on a RequiredModelMBean");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   916
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   917
90ce3da70b43 Initial load
duke
parents:
diff changeset
   918
        String opClassName = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   919
        String opMethodName;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   920
90ce3da70b43 Initial load
duke
parents:
diff changeset
   921
        // Parse for class name and method
90ce3da70b43 Initial load
duke
parents:
diff changeset
   922
        int opSplitter = opName.lastIndexOf(".");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   923
        if (opSplitter > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   924
            opClassName = opName.substring(0,opSplitter);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   925
            opMethodName = opName.substring(opSplitter+1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   926
        } else
90ce3da70b43 Initial load
duke
parents:
diff changeset
   927
            opMethodName = opName;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   928
90ce3da70b43 Initial load
duke
parents:
diff changeset
   929
        /* Ignore anything after a left paren.  We keep this for
90ce3da70b43 Initial load
duke
parents:
diff changeset
   930
           compatibility but it isn't specified.  */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   931
        opSplitter = opMethodName.indexOf("(");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   932
        if (opSplitter > 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   933
            opMethodName = opMethodName.substring(0,opSplitter);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   934
90ce3da70b43 Initial load
duke
parents:
diff changeset
   935
        if (tracing) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   936
            MODELMBEAN_LOGGER.logp(Level.FINER,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   937
                    RequiredModelMBean.class.getName(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   938
                    mth, "Finding operation " + opName + " as " + opMethodName);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   939
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   940
90ce3da70b43 Initial load
duke
parents:
diff changeset
   941
        ModelMBeanOperationInfo opInfo =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   942
            modelMBeanInfo.getOperation(opMethodName);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   943
        if (opInfo == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   944
            final String msg =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   945
                "Operation " + opName + " not in ModelMBeanInfo";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   946
            throw new MBeanException(new ServiceNotFoundException(msg), msg);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   947
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   948
90ce3da70b43 Initial load
duke
parents:
diff changeset
   949
        final Descriptor opDescr = opInfo.getDescriptor();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   950
        if (opDescr == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   951
            final String msg = "Operation descriptor null";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   952
            throw new MBeanException(new ServiceNotFoundException(msg), msg);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   953
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   954
90ce3da70b43 Initial load
duke
parents:
diff changeset
   955
        final Object cached = resolveForCacheValue(opDescr);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   956
        if (cached != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   957
            if (tracing) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   958
                MODELMBEAN_LOGGER.logp(Level.FINER,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   959
                        RequiredModelMBean.class.getName(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   960
                        mth,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   961
                        "Returning cached value");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   962
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   963
            return cached;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   964
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   965
90ce3da70b43 Initial load
duke
parents:
diff changeset
   966
        if (opClassName == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   967
            opClassName = (String) opDescr.getFieldValue("class");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   968
        // may still be null now
90ce3da70b43 Initial load
duke
parents:
diff changeset
   969
90ce3da70b43 Initial load
duke
parents:
diff changeset
   970
        opMethodName = (String) opDescr.getFieldValue("name");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   971
        if (opMethodName == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   972
            final String msg =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   973
                "Method descriptor must include `name' field";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   974
            throw new MBeanException(new ServiceNotFoundException(msg), msg);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   975
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   976
90ce3da70b43 Initial load
duke
parents:
diff changeset
   977
        final String targetTypeField = (String)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   978
            opDescr.getFieldValue("targetType");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   979
        if (targetTypeField != null
90ce3da70b43 Initial load
duke
parents:
diff changeset
   980
            && !targetTypeField.equalsIgnoreCase("objectReference")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   981
            final String msg =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   982
                "Target type must be objectReference: " + targetTypeField;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   983
            throw new MBeanException(new InvalidTargetObjectTypeException(msg),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   984
                                     msg);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   985
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   986
90ce3da70b43 Initial load
duke
parents:
diff changeset
   987
        final Object targetObjectField = opDescr.getFieldValue("targetObject");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   988
        if (tracing && targetObjectField != null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   989
                MODELMBEAN_LOGGER.logp(Level.FINER,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   990
                    RequiredModelMBean.class.getName(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   991
                        mth, "Found target object in descriptor");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   992
90ce3da70b43 Initial load
duke
parents:
diff changeset
   993
        /* Now look for the method, either in RequiredModelMBean itself
90ce3da70b43 Initial load
duke
parents:
diff changeset
   994
           or in the target object.  Set "method" and "targetObject"
90ce3da70b43 Initial load
duke
parents:
diff changeset
   995
           appropriately.  */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   996
        Method method;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   997
        Object targetObject;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   998
90ce3da70b43 Initial load
duke
parents:
diff changeset
   999
        method = findRMMBMethod(opMethodName, targetObjectField,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1000
                                opClassName, sig);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1001
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1002
        if (method != null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1003
            targetObject = this;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1004
        else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1005
            if (tracing) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1006
                MODELMBEAN_LOGGER.logp(Level.FINER,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1007
                    RequiredModelMBean.class.getName(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1008
                        mth, "looking for method in managedResource class");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1009
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1010
            if (targetObjectField != null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1011
                targetObject = targetObjectField;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1012
            else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1013
                targetObject = managedResource;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1014
                if (targetObject == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1015
                    final String msg =
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1016
                        "managedResource for invoke " + opName +
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1017
                        " is null";
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1018
                    Exception snfe = new ServiceNotFoundException(msg);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1019
                    throw new MBeanException(snfe);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1020
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1021
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1022
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1023
            final Class targetClass;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1024
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1025
            if (opClassName != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1026
                try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1027
                    final ClassLoader targetClassLoader =
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1028
                        targetObject.getClass().getClassLoader();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1029
                    targetClass = Class.forName(opClassName, false,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1030
                                                targetClassLoader);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1031
                } catch (ClassNotFoundException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1032
                    final String msg =
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1033
                        "class for invoke " + opName + " not found";
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1034
                    throw new ReflectionException(e, msg);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1035
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1036
            } else
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1037
                targetClass = targetObject.getClass();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1038
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1039
            method = resolveMethod(targetClass, opMethodName, sig);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1040
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1041
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1042
        if (tracing) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1043
            MODELMBEAN_LOGGER.logp(Level.FINER,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1044
                RequiredModelMBean.class.getName(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1045
                    mth, "found " + opMethodName + ", now invoking");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1046
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1047
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1048
        final Object result =
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1049
            invokeMethod(opName, method, targetObject, opArgs);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1050
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1051
        if (tracing) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1052
            MODELMBEAN_LOGGER.logp(Level.FINER,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1053
                RequiredModelMBean.class.getName(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1054
                    mth, "successfully invoked method");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1055
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1056
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1057
        if (result != null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1058
            cacheResult(opInfo, opDescr, result);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1059
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1060
        return result;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1061
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1062
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1063
    private static Method resolveMethod(Class<?> targetClass,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1064
                                        String opMethodName,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1065
                                        String[] sig)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1066
            throws ReflectionException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1067
        final boolean tracing = MODELMBEAN_LOGGER.isLoggable(Level.FINER);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1068
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1069
        if (tracing) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1070
            MODELMBEAN_LOGGER.logp(Level.FINER,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1071
                RequiredModelMBean.class.getName(),"resolveMethod",
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1072
                  "resolving " + targetClass + "." + opMethodName);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1073
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1074
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1075
        final Class[] argClasses;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1076
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1077
        if (sig == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1078
            argClasses = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1079
        else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1080
            final ClassLoader targetClassLoader = targetClass.getClassLoader();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1081
            argClasses = new Class[sig.length];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1082
            for (int i = 0; i < sig.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1083
                if (tracing) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1084
                    MODELMBEAN_LOGGER.logp(Level.FINER,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1085
                        RequiredModelMBean.class.getName(),"resolveMethod",
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1086
                            "resolve type " + sig[i]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1087
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1088
                argClasses[i] = (Class) primitiveClassMap.get(sig[i]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1089
                if (argClasses[i] == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1090
                    try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1091
                        argClasses[i] =
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1092
                            Class.forName(sig[i], false, targetClassLoader);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1093
                    } catch (ClassNotFoundException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1094
                        if (tracing) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1095
                            MODELMBEAN_LOGGER.logp(Level.FINER,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1096
                                    RequiredModelMBean.class.getName(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1097
                                    "resolveMethod",
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1098
                                    "class not found");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1099
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1100
                        final String msg = "Parameter class not found";
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1101
                        throw new ReflectionException(e, msg);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1102
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1103
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1104
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1105
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1106
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1107
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1108
            return targetClass.getMethod(opMethodName, argClasses);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1109
        } catch (NoSuchMethodException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1110
            final String msg =
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1111
                "Target method not found: " + targetClass.getName() + "." +
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1112
                opMethodName;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1113
            throw new ReflectionException(e, msg);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1114
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1115
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1116
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1117
    /* Map e.g. "int" to int.class.  Goodness knows how many time this
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1118
       particular wheel has been reinvented.  */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1119
    private static final Class[] primitiveClasses = {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1120
        int.class, long.class, boolean.class, double.class,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1121
        float.class, short.class, byte.class, char.class,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1122
    };
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1123
    private static final Map<String,Class<?>> primitiveClassMap =
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1124
        new HashMap<String,Class<?>>();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1125
    static {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1126
        for (int i = 0; i < primitiveClasses.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1127
            final Class c = primitiveClasses[i];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1128
            primitiveClassMap.put(c.getName(), c);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1129
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1130
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1131
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1132
    /* Find a method in RequiredModelMBean as determined by the given
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1133
       parameters.  Return null if there is none, or if the parameters
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1134
       exclude using it.  Called from invoke. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1135
    private static Method findRMMBMethod(String opMethodName,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1136
                                         Object targetObjectField,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1137
                                         String opClassName,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1138
                                         String[] sig) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1139
        final boolean tracing = MODELMBEAN_LOGGER.isLoggable(Level.FINER);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1140
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1141
        if (tracing) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1142
            MODELMBEAN_LOGGER.logp(Level.FINER,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1143
                RequiredModelMBean.class.getName(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1144
                    "invoke(String, Object[], String[])",
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1145
                  "looking for method in RequiredModelMBean class");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1146
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1147
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1148
        if (!isRMMBMethodName(opMethodName))
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1149
            return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1150
        if (targetObjectField != null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1151
            return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1152
        final Class<RequiredModelMBean> rmmbClass = RequiredModelMBean.class;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1153
        final Class<?> targetClass;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1154
        if (opClassName == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1155
            targetClass = rmmbClass;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1156
        else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1157
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1158
                final ClassLoader targetClassLoader =
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1159
                    rmmbClass.getClassLoader();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1160
                targetClass = Class.forName(opClassName, false,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1161
                                            targetClassLoader);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1162
                if (!rmmbClass.isAssignableFrom(targetClass))
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1163
                    return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1164
            } catch (ClassNotFoundException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1165
                return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1166
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1167
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1168
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1169
            return resolveMethod(targetClass, opMethodName, sig);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1170
        } catch (ReflectionException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1171
            return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1172
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1173
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1174
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1175
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1176
     * Invoke the given method, and throw the somewhat unpredictable
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1177
     * appropriate exception if the method itself gets an exception.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1178
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1179
    private Object invokeMethod(String opName, Method method,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1180
                                Object targetObject, Object[] opArgs)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1181
            throws MBeanException, ReflectionException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1182
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1183
            ReflectUtil.checkPackageAccess(method.getDeclaringClass());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1184
            return MethodUtil.invoke(method, targetObject, opArgs);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1185
        } catch (RuntimeErrorException ree) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1186
            throw new RuntimeOperationsException(ree,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1187
                      "RuntimeException occurred in RequiredModelMBean "+
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1188
                      "while trying to invoke operation " + opName);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1189
        } catch (RuntimeException re) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1190
            throw new RuntimeOperationsException(re,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1191
                      "RuntimeException occurred in RequiredModelMBean "+
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1192
                      "while trying to invoke operation " + opName);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1193
        } catch (IllegalAccessException iae) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1194
            throw new ReflectionException(iae,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1195
                      "IllegalAccessException occurred in " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1196
                      "RequiredModelMBean while trying to " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1197
                      "invoke operation " + opName);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1198
        } catch (InvocationTargetException ite) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1199
            Throwable mmbTargEx = ite.getTargetException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1200
            if (mmbTargEx instanceof RuntimeException) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1201
                throw new MBeanException ((RuntimeException)mmbTargEx,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1202
                      "RuntimeException thrown in RequiredModelMBean "+
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1203
                      "while trying to invoke operation " + opName);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1204
            } else if (mmbTargEx instanceof Error) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1205
                throw new RuntimeErrorException((Error)mmbTargEx,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1206
                      "Error occurred in RequiredModelMBean while trying "+
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1207
                      "to invoke operation " + opName);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1208
            } else if (mmbTargEx instanceof ReflectionException) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1209
                throw (ReflectionException) mmbTargEx;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1210
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1211
                throw new MBeanException ((Exception)mmbTargEx,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1212
                      "Exception thrown in RequiredModelMBean "+
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1213
                      "while trying to invoke operation " + opName);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1214
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1215
        } catch (Error err) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1216
            throw new RuntimeErrorException(err,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1217
                  "Error occurred in RequiredModelMBean while trying "+
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1218
                  "to invoke operation " + opName);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1219
        } catch (Exception e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1220
            throw new ReflectionException(e,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1221
                  "Exception occurred in RequiredModelMBean while " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1222
                  "trying to invoke operation " + opName);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1223
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1224
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1225
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1226
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1227
     * Cache the result of an operation in the descriptor, if that is
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1228
     * called for by the descriptor's configuration.  Note that we
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1229
     * don't remember operation parameters when caching the result, so
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1230
     * this is unlikely to be useful if there are any.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1231
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1232
    private void cacheResult(ModelMBeanOperationInfo opInfo,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1233
                             Descriptor opDescr, Object result)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1234
            throws MBeanException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1235
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1236
        Descriptor mmbDesc =
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1237
            modelMBeanInfo.getMBeanDescriptor();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1238
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1239
        Object objctl =
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1240
            opDescr.getFieldValue("currencyTimeLimit");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1241
        String ctl;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1242
        if (objctl != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1243
            ctl = objctl.toString();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1244
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1245
            ctl = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1246
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1247
        if ((ctl == null) && (mmbDesc != null)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1248
            objctl =
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1249
                mmbDesc.getFieldValue("currencyTimeLimit");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1250
            if (objctl != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1251
                ctl = objctl.toString();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1252
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1253
                ctl = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1254
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1255
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1256
        if ((ctl != null) && !(ctl.equals("-1"))) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1257
            opDescr.setField("value", result);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1258
            opDescr.setField("lastUpdatedTimeStamp",
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1259
                    String.valueOf((new Date()).getTime()));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1260
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1261
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1262
            modelMBeanInfo.setDescriptor(opDescr,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1263
                                         "operation");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1264
            if (MODELMBEAN_LOGGER.isLoggable(Level.FINER)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1265
                MODELMBEAN_LOGGER.logp(Level.FINER,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1266
                        RequiredModelMBean.class.getName(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1267
                        "invoke(String,Object[],Object[])",
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1268
                        "new descriptor is " + opDescr);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1269
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1270
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1271
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1272
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1273
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1274
     * Determine whether the given name is the name of a public method
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1275
     * in this class.  This is only an optimization: it prevents us
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1276
     * from trying to do argument type lookups and reflection on a
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1277
     * method that will obviously fail because it has the wrong name.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1278
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1279
     * The first time this method is called we do the reflection, and
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1280
     * every other time we reuse the remembered values.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1281
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1282
     * It's conceivable that the (possibly malicious) first caller
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1283
     * doesn't have the required permissions to do reflection, in
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1284
     * which case we don't touch anything so as not to interfere
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1285
     * with a later permissionful caller.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1286
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1287
    private static Set<String> rmmbMethodNames;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1288
    private static synchronized boolean isRMMBMethodName(String name) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1289
        if (rmmbMethodNames == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1290
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1291
                Set<String> names = new HashSet<String>();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1292
                Method[] methods = RequiredModelMBean.class.getMethods();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1293
                for (int i = 0; i < methods.length; i++)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1294
                    names.add(methods[i].getName());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1295
                rmmbMethodNames = names;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1296
            } catch (Exception e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1297
                return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1298
                // This is only an optimization so we'll go on to discover
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1299
                // whether the name really is an RMMB method.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1300
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1301
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1302
        return rmmbMethodNames.contains(name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1303
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1304
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1305
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1306
     * Returns the value of a specific attribute defined for this
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1307
     * ModelMBean.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1308
     * The last value returned by an attribute may be cached in the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1309
     * attribute's descriptor.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1310
     * The valid value will be in the 'value' field if there is one.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1311
     * If the 'currencyTimeLimit' field in the descriptor is:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1312
     * <UL>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1313
     * <LI>  <b>&lt;0</b> Then the value is not cached and is never valid.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1314
     *       The getter method is invoked for the attribute.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1315
     *       The 'value' and 'lastUpdatedTimeStamp' fields are cleared.</LI>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1316
     * <LI>  <b>=0</b> Then the value is always cached and always valid.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1317
     *       The 'value' field is returned. If there is no'value' field
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1318
     *       then the getter method is invoked for the attribute.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1319
     *       The 'lastUpdatedTimeStamp' field and `value' fields are set
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1320
     *       to the attribute's value and the current time stamp.</LI>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1321
     * <LI>  <b>&gt;0</b> Represents the number of seconds that the 'value'
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1322
     *       field is valid.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1323
     *       The 'value' field is no longer valid when
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1324
     *       'lastUpdatedTimeStamp' + 'currencyTimeLimit' &gt; Now.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1325
     *   <UL>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1326
     *        <LI>When 'value' is valid, 'value' is returned.</LI>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1327
     *        <LI>When 'value' is no longer valid then the getter
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1328
     *            method is invoked for the attribute.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1329
     *            The 'lastUpdatedTimeStamp' field and `value' fields
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1330
     *            are updated.</LI>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1331
     *   </UL></LI>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1332
     * </UL>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1333
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1334
     * <p><b>Note:</b> because of inconsistencies in previous versions of
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1335
     * this specification, it is recommended not to use negative or zero
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1336
     * values for <code>currencyTimeLimit</code>.  To indicate that a
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1337
     * cached value is never valid, omit the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1338
     * <code>currencyTimeLimit</code> field.  To indicate that it is
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1339
     * always valid, use a very large number for this field.</p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1340
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1341
     * <p>If the 'getMethod' field contains the name of a valid
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1342
     * operation descriptor, then the method described by the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1343
     * operation descriptor is executed.  The response from the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1344
     * method is returned as the value of the attribute.  If the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1345
     * operation fails or the returned value is not compatible with
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1346
     * the declared type of the attribute, an exception will be thrown.</p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1347
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1348
     * <p>If no 'getMethod' field is defined then the default value of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1349
     * attribute is returned. If the returned value is not compatible with
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1350
     * the declared type of the attribute, an exception will be thrown.</p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1351
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1352
     * <p>The declared type of the attribute is the String returned by
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1353
     * {@link ModelMBeanAttributeInfo#getType()}.  A value is compatible
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1354
     * with this type if one of the following is true:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1355
     * <ul>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1356
     * <li>the value is null;</li>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1357
     * <li>the declared name is a primitive type name (such as "int")
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1358
     *     and the value is an instance of the corresponding wrapper
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1359
     *     type (such as java.lang.Integer);</li>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1360
     * <li>the name of the value's class is identical to the declared name;</li>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1361
     * <li>the declared name can be loaded by the value's class loader and
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1362
     *     produces a class to which the value can be assigned.</li>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1363
     * </ul>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1364
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1365
     * <p>In this implementation, in every case where the getMethod needs to
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1366
     * be called, because the method is invoked through the standard "invoke"
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1367
     * method and thus needs operationInfo, an operation must be specified
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1368
     * for that getMethod so that the invocation works correctly.</p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1369
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1370
     * @param attrName A String specifying the name of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1371
     * attribute to be retrieved. It must match the name of a
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1372
     * ModelMBeanAttributeInfo.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1373
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1374
     * @return The value of the retrieved attribute from the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1375
     * descriptor 'value' field or from the invocation of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1376
     * operation in the 'getMethod' field of the descriptor.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1377
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1378
     * @exception AttributeNotFoundException The specified attribute is
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1379
     *    not accessible in the MBean.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1380
     *    The following cases may result in an AttributeNotFoundException:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1381
     *    <UL>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1382
     *      <LI> No ModelMBeanInfo was found for the Model MBean.</LI>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1383
     *      <LI> No ModelMBeanAttributeInfo was found for the specified
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1384
     *           attribute name.</LI>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1385
     *      <LI> The ModelMBeanAttributeInfo isReadable method returns
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1386
     *           'false'.</LI>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1387
     *    </UL>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1388
     * @exception MBeanException  Wraps one of the following Exceptions:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1389
     *    <UL>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1390
     *      <LI> {@link InvalidAttributeValueException}: A wrong value type
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1391
     *           was received from the attribute's getter method or
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1392
     *           no 'getMethod' field defined in the descriptor for
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1393
     *           the attribute and no default value exists.</LI>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1394
     *      <LI> {@link ServiceNotFoundException}: No
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1395
     *           ModelMBeanOperationInfo defined for the attribute's
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1396
     *           getter method or no descriptor associated with the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1397
     *           ModelMBeanOperationInfo or the managed resource is
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1398
     *           null.</LI>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1399
     *      <LI> {@link InvalidTargetObjectTypeException} The 'targetType'
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1400
     *           field value is not 'objectReference'.</LI>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1401
     *      <LI> An Exception thrown by the managed object's getter.</LI>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1402
     *    </UL>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1403
     * @exception ReflectionException  Wraps an {@link java.lang.Exception}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1404
     *    thrown while trying to invoke the getter.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1405
     * @exception RuntimeOperationsException Wraps an
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1406
     *    {@link IllegalArgumentException}: The attribute name in
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1407
     *    parameter is null.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1408
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1409
     * @see #setAttribute(javax.management.Attribute)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1410
     **/
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1411
    public Object getAttribute(String attrName)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1412
        throws AttributeNotFoundException, MBeanException,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1413
               ReflectionException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1414
        if (attrName == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1415
            throw new RuntimeOperationsException(new
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1416
                IllegalArgumentException("attributeName must not be null"),
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1417
                "Exception occurred trying to get attribute of a " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1418
                "RequiredModelMBean");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1419
        final String mth = "getAttribute(String)";
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1420
        final boolean tracing = MODELMBEAN_LOGGER.isLoggable(Level.FINER);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1421
        if (tracing) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1422
            MODELMBEAN_LOGGER.logp(Level.FINER,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1423
                    RequiredModelMBean.class.getName(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1424
                mth, "Entry with " + attrName);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1425
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1426
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1427
        /* Check attributeDescriptor for getMethod */
526
61ba2d5ea9da 6701459: Synchronization bug pattern found in javax.management.relation.RelationService
emcmanus
parents: 263
diff changeset
  1428
        Object response;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1429
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1430
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1431
            if (modelMBeanInfo == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1432
                throw new AttributeNotFoundException(
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1433
                      "getAttribute failed: ModelMBeanInfo not found for "+
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1434
                      attrName);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1435
526
61ba2d5ea9da 6701459: Synchronization bug pattern found in javax.management.relation.RelationService
emcmanus
parents: 263
diff changeset
  1436
            ModelMBeanAttributeInfo attrInfo = modelMBeanInfo.getAttribute(attrName);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1437
            Descriptor mmbDesc = modelMBeanInfo.getMBeanDescriptor();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1438
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1439
            if (attrInfo == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1440
                throw new AttributeNotFoundException("getAttribute failed:"+
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1441
                      " ModelMBeanAttributeInfo not found for " + attrName);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1442
526
61ba2d5ea9da 6701459: Synchronization bug pattern found in javax.management.relation.RelationService
emcmanus
parents: 263
diff changeset
  1443
            Descriptor attrDescr = attrInfo.getDescriptor();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1444
            if (attrDescr != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1445
                if (!attrInfo.isReadable())
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1446
                    throw new AttributeNotFoundException(
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1447
                          "getAttribute failed: " + attrName +
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1448
                          " is not readable ");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1449
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1450
                response = resolveForCacheValue(attrDescr);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1451
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1452
                /* return current cached value */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1453
                if (tracing) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1454
                    MODELMBEAN_LOGGER.logp(Level.FINER,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1455
                            RequiredModelMBean.class.getName(), mth,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1456
                        "*** cached value is " + response);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1457
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1458
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1459
                if (response == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1460
                    /* no cached value, run getMethod */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1461
                    if (tracing) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1462
                        MODELMBEAN_LOGGER.logp(Level.FINER,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1463
                                RequiredModelMBean.class.getName(), mth,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1464
                            "**** cached value is null - getting getMethod");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1465
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1466
                    String attrGetMethod =
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1467
                        (String)(attrDescr.getFieldValue("getMethod"));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1468
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1469
                    if (attrGetMethod != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1470
                        /* run method from operations descriptor */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1471
                        if (tracing) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1472
                            MODELMBEAN_LOGGER.logp(Level.FINER,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1473
                                    RequiredModelMBean.class.getName(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1474
                                mth, "invoking a getMethod for " +  attrName);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1475
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1476
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1477
                        Object getResponse =
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1478
                            invoke(attrGetMethod, new Object[] {},
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1479
                                   new String[] {});
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1480
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1481
                        if (getResponse != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1482
                            // error/validity check return value here
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1483
                            if (tracing) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1484
                                MODELMBEAN_LOGGER.logp(Level.FINER,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1485
                                        RequiredModelMBean.class.getName(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1486
                                        mth, "got a non-null response " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1487
                                        "from getMethod\n");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1488
                            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1489
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1490
                            response = getResponse;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1491
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1492
                            // change cached value in attribute descriptor
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1493
                            Object objctl =
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1494
                                attrDescr.getFieldValue("currencyTimeLimit");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1495
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1496
                            String ctl;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1497
                            if (objctl != null) ctl = objctl.toString();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1498
                            else ctl = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1499
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1500
                            if ((ctl == null) && (mmbDesc != null)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1501
                                objctl = mmbDesc.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1502
                                    getFieldValue("currencyTimeLimit");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1503
                                if (objctl != null) ctl = objctl.toString();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1504
                                else ctl = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1505
                            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1506
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1507
                            if ((ctl != null) && !(ctl.equals("-1"))) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1508
                                if (tracing) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1509
                                    MODELMBEAN_LOGGER.logp(Level.FINER,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1510
                                            RequiredModelMBean.class.getName(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1511
                                            mth,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1512
                                            "setting cached value and " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1513
                                            "lastUpdatedTime in descriptor");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1514
                                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1515
                                attrDescr.setField("value", response);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1516
                                final String stamp = String.valueOf(
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1517
                                    (new Date()).getTime());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1518
                                attrDescr.setField("lastUpdatedTimeStamp",
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1519
                                                   stamp);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1520
                                attrInfo.setDescriptor(attrDescr);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1521
                                modelMBeanInfo.setDescriptor(attrDescr,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1522
                                                             "attribute");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1523
                                if (tracing) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1524
                                    MODELMBEAN_LOGGER.logp(Level.FINER,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1525
                                            RequiredModelMBean.class.getName(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1526
                                        mth,"new descriptor is " +attrDescr);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1527
                                    MODELMBEAN_LOGGER.logp(Level.FINER,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1528
                                            RequiredModelMBean.class.getName(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1529
                                        mth,"AttributeInfo descriptor is " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1530
                                            attrInfo.getDescriptor());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1531
                                    final String attStr = modelMBeanInfo.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1532
                                        getDescriptor(attrName,"attribute").
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1533
                                            toString();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1534
                                    MODELMBEAN_LOGGER.logp(Level.FINER,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1535
                                            RequiredModelMBean.class.getName(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1536
                                            mth,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1537
                                            "modelMBeanInfo: AttributeInfo " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1538
                                            "descriptor is " + attStr);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1539
                                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1540
                            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1541
                        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1542
                            // response was invalid or really returned null
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1543
                            if (tracing) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1544
                                MODELMBEAN_LOGGER.logp(Level.FINER,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1545
                                        RequiredModelMBean.class.getName(), mth,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1546
                                    "got a null response from getMethod\n");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1547
                            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1548
                            response = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1549
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1550
                    } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1551
                        // not getMethod so return descriptor (default) value
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1552
                        String qualifier="";
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1553
                        response = attrDescr.getFieldValue("value");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1554
                        if (response == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1555
                            qualifier="default ";
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1556
                            response = attrDescr.getFieldValue("default");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1557
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1558
                        if (tracing) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1559
                            MODELMBEAN_LOGGER.logp(Level.FINER,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1560
                                    RequiredModelMBean.class.getName(), mth,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1561
                                "could not find getMethod for " +attrName +
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1562
                                ", returning descriptor " +qualifier + "value");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1563
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1564
                        // !! cast response to right class
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1565
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1566
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1567
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1568
                // make sure response class matches type field
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1569
                String respType = attrInfo.getType();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1570
                if (response != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1571
                    String responseClass = response.getClass().getName();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1572
                    if (!respType.equals(responseClass)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1573
                        boolean wrongType = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1574
                        boolean primitiveType = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1575
                        boolean correspondingTypes = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1576
                        for (int i = 0; i < primitiveTypes.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1577
                            if (respType.equals(primitiveTypes[i])) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1578
                                primitiveType = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1579
                                if (responseClass.equals(primitiveWrappers[i]))
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1580
                                    correspondingTypes = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1581
                                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1582
                            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1583
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1584
                        if (primitiveType) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1585
                            // inequality may come from primitive/wrapper class
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1586
                            if (!correspondingTypes)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1587
                                wrongType = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1588
                        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1589
                            // inequality may come from type subclassing
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1590
                            boolean subtype;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1591
                            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1592
                                ClassLoader cl =
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1593
                                    response.getClass().getClassLoader();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1594
                                Class c = Class.forName(respType, true, cl);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1595
                                subtype = c.isInstance(response);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1596
                            } catch (Exception e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1597
                                subtype = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1598
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1599
                                if (tracing) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1600
                                    MODELMBEAN_LOGGER.logp(Level.FINER,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1601
                                            RequiredModelMBean.class.getName(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1602
                                        mth, "Exception: ",e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1603
                                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1604
                            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1605
                            if (!subtype)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1606
                                wrongType = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1607
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1608
                        if (wrongType) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1609
                            if (tracing) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1610
                                MODELMBEAN_LOGGER.logp(Level.FINER,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1611
                                        RequiredModelMBean.class.getName(), mth,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1612
                                    "Wrong response type '" + respType + "'");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1613
                            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1614
                            // throw exception, didn't get
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1615
                            // back right attribute type
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1616
                            throw new MBeanException(
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1617
                              new InvalidAttributeValueException(
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1618
                                "Wrong value type received for get attribute"),
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1619
                              "An exception occurred while trying to get an " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1620
                              "attribute value through a RequiredModelMBean");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1621
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1622
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1623
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1624
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1625
                if (tracing) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1626
                    MODELMBEAN_LOGGER.logp(Level.FINER,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1627
                            RequiredModelMBean.class.getName(), mth,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1628
                            "getMethod failed " + attrName +
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1629
                            " not in attributeDescriptor\n");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1630
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1631
                throw new MBeanException(new
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1632
                    InvalidAttributeValueException(
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1633
                    "Unable to resolve attribute value, " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1634
                    "no getMethod defined in descriptor for attribute"),
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1635
                    "An exception occurred while trying to get an "+
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1636
                    "attribute value through a RequiredModelMBean");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1637
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1638
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1639
        } catch (MBeanException mbe) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1640
            throw mbe;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1641
        } catch (AttributeNotFoundException t) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1642
            throw t;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1643
        } catch (Exception e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1644
            if (tracing) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1645
                MODELMBEAN_LOGGER.logp(Level.FINER,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1646
                        RequiredModelMBean.class.getName(), mth,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1647
                        "getMethod failed with " + e.getMessage() +
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1648
                        " exception type " + (e.getClass()).toString());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1649
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1650
            throw new MBeanException(e,"An exception occurred while trying "+
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1651
                      "to get an attribute value: " + e.getMessage());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1652
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1653
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1654
        if (tracing) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1655
            MODELMBEAN_LOGGER.logp(Level.FINER,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1656
                    RequiredModelMBean.class.getName(), mth, "Exit");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1657
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1658
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1659
        return response;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1660
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1661
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1662
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1663
     * Returns the values of several attributes in the ModelMBean.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1664
     * Executes a getAttribute for each attribute name in the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1665
     * attrNames array passed in.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1666
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1667
     * @param attrNames A String array of names of the attributes
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1668
     * to be retrieved.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1669
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1670
     * @return The array of the retrieved attributes.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1671
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1672
     * @exception RuntimeOperationsException Wraps an
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1673
     * {@link IllegalArgumentException}: The object name in parameter is
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1674
     * null or attributes in parameter is null.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1675
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1676
     * @see #setAttributes(javax.management.AttributeList)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1677
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1678
    public AttributeList getAttributes(String[] attrNames)      {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1679
        if (MODELMBEAN_LOGGER.isLoggable(Level.FINER)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1680
            MODELMBEAN_LOGGER.logp(Level.FINER,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1681
                    RequiredModelMBean.class.getName(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1682
            "getAttributes(String[])","Entry");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1683
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1684
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1685
        if (attrNames == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1686
            throw new RuntimeOperationsException(new
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1687
                IllegalArgumentException("attributeNames must not be null"),
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1688
                "Exception occurred trying to get attributes of a "+
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1689
                "RequiredModelMBean");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1690
526
61ba2d5ea9da 6701459: Synchronization bug pattern found in javax.management.relation.RelationService
emcmanus
parents: 263
diff changeset
  1691
        AttributeList responseList = new AttributeList();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1692
        for (int i = 0; i < attrNames.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1693
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1694
                responseList.add(new Attribute(attrNames[i],
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1695
                                     getAttribute(attrNames[i])));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1696
            } catch (Exception e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1697
                // eat exceptions because interface doesn't have an
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1698
                // exception on it
623
97dd054b7f1f 6592586: RequiredModelMBean prints a WARNING message when calling getAttributes() for a non-existing attr
dfuchs
parents: 526
diff changeset
  1699
                if (MODELMBEAN_LOGGER.isLoggable(Level.FINER)) {
97dd054b7f1f 6592586: RequiredModelMBean prints a WARNING message when calling getAttributes() for a non-existing attr
dfuchs
parents: 526
diff changeset
  1700
                    MODELMBEAN_LOGGER.logp(Level.FINER,
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1701
                            RequiredModelMBean.class.getName(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1702
                        "getAttributes(String[])",
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1703
                            "Failed to get \"" + attrNames[i] + "\": ", e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1704
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1705
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1706
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1707
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1708
        if (MODELMBEAN_LOGGER.isLoggable(Level.FINER)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1709
            MODELMBEAN_LOGGER.logp(Level.FINER,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1710
                RequiredModelMBean.class.getName(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1711
                    "getAttributes(String[])","Exit");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1712
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1713
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1714
        return responseList;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1715
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1716
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1717
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1718
     * Sets the value of a specific attribute of a named ModelMBean.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1719
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1720
     * If the 'setMethod' field of the attribute's descriptor
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1721
     * contains the name of a valid operation descriptor, then the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1722
     * method described by the operation descriptor is executed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1723
     * In this implementation, the operation descriptor must be specified
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1724
     * correctly and assigned to the modelMBeanInfo so that the 'setMethod'
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1725
     * works correctly.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1726
     * The response from the method is set as the value of the attribute
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1727
     * in the descriptor.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1728
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1729
     * <p>If currencyTimeLimit is &gt; 0, then the new value for the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1730
     * attribute is cached in the attribute descriptor's
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1731
     * 'value' field and the 'lastUpdatedTimeStamp' field is set to
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1732
     * the current time stamp.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1733
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1734
     * <p>If the persist field of the attribute's descriptor is not null
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1735
     * then Persistence policy from the attribute descriptor is used to
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1736
     * guide storing the attribute in a persistent store.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1737
     * <br>Store the MBean if 'persistPolicy' field is:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1738
     * <UL>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1739
     * <Li> != "never"</Li>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1740
     * <Li> = "always"</Li>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1741
     * <Li> = "onUpdate"</Li>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1742
     * <Li> = "onTimer" and now &gt; 'lastPersistTime' + 'persistPeriod'</Li>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1743
     * <Li> = "NoMoreOftenThan" and now &gt; 'lastPersistTime' +
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1744
     *         'persistPeriod'</Li>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1745
     * </UL>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1746
     * Do not store the MBean if 'persistPolicy' field is:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1747
     * <UL>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1748
     * <Li> = "never"</Li>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1749
     * <Li> = "onTimer" && now &lt; 'lastPersistTime' + 'persistPeriod'</Li>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1750
     * <Li> = "onUnregister"</Li>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1751
     * <Li> = "NoMoreOftenThan" and now &lt; 'lastPersistTime' +
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1752
     *        'persistPeriod'</Li>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1753
     * </UL>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1754
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1755
     * <p>The ModelMBeanInfo of the Model MBean is stored in a file.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1756
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1757
     * @param attribute The Attribute instance containing the name of
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1758
     *        the attribute to be set and the value it is to be set to.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1759
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1760
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1761
     * @exception AttributeNotFoundException The specified attribute is
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1762
     *   not accessible in the MBean.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1763
     *   <br>The following cases may result in an AttributeNotFoundException:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1764
     *   <UL>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1765
     *     <LI> No ModelMBeanAttributeInfo is found for the specified
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1766
     *          attribute.</LI>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1767
     *     <LI> The ModelMBeanAttributeInfo's isWritable method returns
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1768
     *          'false'.</LI>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1769
     *   </UL>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1770
     * @exception InvalidAttributeValueException No descriptor is defined
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1771
     *   for the specified attribute.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1772
     * @exception MBeanException Wraps one of the following Exceptions:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1773
     *   <UL>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1774
     *     <LI> An Exception thrown by the managed object's setter.</LI>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1775
     *     <LI> A {@link ServiceNotFoundException} if a setMethod field is
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1776
     *          defined in the descriptor for the attribute and the managed
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1777
     *          resource is null; or if no setMethod field is defined and
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1778
     *          caching is not enabled for the attribute.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1779
     *          Note that if there is no getMethod field either, then caching
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1780
     *          is automatically enabled.</LI>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1781
     *     <LI> {@link InvalidTargetObjectTypeException} The 'targetType'
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1782
     *          field value is not 'objectReference'.</LI>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1783
     *     <LI> An Exception thrown by the managed object's getter.</LI>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1784
     *   </UL>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1785
     * @exception ReflectionException  Wraps an {@link java.lang.Exception}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1786
     *   thrown while trying to invoke the setter.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1787
     * @exception RuntimeOperationsException Wraps an
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1788
     *   {@link IllegalArgumentException}: The attribute in parameter is
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1789
     *   null.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1790
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1791
     * @see #getAttribute(java.lang.String)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1792
     **/
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1793
    public void setAttribute(Attribute attribute)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1794
        throws AttributeNotFoundException, InvalidAttributeValueException,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1795
               MBeanException, ReflectionException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1796
        final boolean tracing = MODELMBEAN_LOGGER.isLoggable(Level.FINER);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1797
        if (tracing) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1798
            MODELMBEAN_LOGGER.logp(Level.FINER,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1799
                    RequiredModelMBean.class.getName(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1800
                "setAttribute()","Entry");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1801
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1802
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1803
        if (attribute == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1804
            throw new RuntimeOperationsException(new
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1805
                IllegalArgumentException("attribute must not be null"),
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1806
                "Exception occurred trying to set an attribute of a "+
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1807
                "RequiredModelMBean");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1808
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1809
        /* run setMethod if there is one */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1810
        /* return cached value if its current */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1811
        /* set cached value in descriptor and set date/time */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1812
        /* send attribute change Notification */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1813
        /* check persistence policy and persist if need be */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1814
        String attrName = attribute.getName();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1815
        Object attrValue = attribute.getValue();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1816
        boolean updateDescriptor = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1817
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1818
        ModelMBeanAttributeInfo attrInfo =
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1819
            modelMBeanInfo.getAttribute(attrName);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1820
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1821
        if (attrInfo == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1822
            throw new AttributeNotFoundException("setAttribute failed: " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1823
                                               attrName + " is not found ");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1824
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1825
        Descriptor mmbDesc = modelMBeanInfo.getMBeanDescriptor();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1826
        Descriptor attrDescr = attrInfo.getDescriptor();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1827
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1828
        if (attrDescr != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1829
            if (!attrInfo.isWritable())
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1830
                throw new AttributeNotFoundException("setAttribute failed: "
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1831
                                          + attrName + " is not writable ");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1832
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1833
            String attrSetMethod = (String)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1834
                (attrDescr.getFieldValue("setMethod"));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1835
            String attrGetMethod = (String)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1836
                (attrDescr.getFieldValue("getMethod"));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1837
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1838
            String attrType = attrInfo.getType();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1839
            Object currValue = "Unknown";
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1840
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1841
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1842
                currValue = this.getAttribute(attrName);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1843
            } catch (Throwable t) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1844
                // OK: Default "Unknown" value used for unknown attribute
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1845
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1846
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1847
            Attribute oldAttr = new Attribute(attrName, currValue);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1848
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1849
            /* run method from operations descriptor */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1850
            if (attrSetMethod == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1851
                if (attrValue != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1852
                    try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1853
                        final Class  clazz    = loadClass(attrType);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1854
                        if (! clazz.isInstance(attrValue))  throw new
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1855
                            InvalidAttributeValueException(clazz.getName() +
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1856
                                                           " expected, "   +
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1857
                                            attrValue.getClass().getName() +
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1858
                                                           " received.");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1859
                    } catch (ClassNotFoundException x) {
623
97dd054b7f1f 6592586: RequiredModelMBean prints a WARNING message when calling getAttributes() for a non-existing attr
dfuchs
parents: 526
diff changeset
  1860
                        if (MODELMBEAN_LOGGER.isLoggable(Level.FINER)) {
97dd054b7f1f 6592586: RequiredModelMBean prints a WARNING message when calling getAttributes() for a non-existing attr
dfuchs
parents: 526
diff changeset
  1861
                            MODELMBEAN_LOGGER.logp(Level.FINER,
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1862
                                    RequiredModelMBean.class.getName(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1863
                                "setAttribute(Attribute)","Class " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1864
                                    attrType + " for attribute "
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1865
                                + attrName + " not found: ", x);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1866
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1867
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1868
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1869
                updateDescriptor = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1870
            } else {
526
61ba2d5ea9da 6701459: Synchronization bug pattern found in javax.management.relation.RelationService
emcmanus
parents: 263
diff changeset
  1871
                invoke(attrSetMethod,
61ba2d5ea9da 6701459: Synchronization bug pattern found in javax.management.relation.RelationService
emcmanus
parents: 263
diff changeset
  1872
                       (new Object[] {attrValue}),
61ba2d5ea9da 6701459: Synchronization bug pattern found in javax.management.relation.RelationService
emcmanus
parents: 263
diff changeset
  1873
                       (new String[] {attrType}) );
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1874
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1875
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1876
            /* change cached value */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1877
            Object objctl = attrDescr.getFieldValue("currencyTimeLimit");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1878
            String ctl;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1879
            if (objctl != null) ctl = objctl.toString();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1880
            else ctl = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1881
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1882
            if ((ctl == null) && (mmbDesc != null)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1883
                objctl = mmbDesc.getFieldValue("currencyTimeLimit");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1884
                if (objctl != null) ctl = objctl.toString();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1885
                else ctl = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1886
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1887
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1888
            final boolean updateCache = ((ctl != null) && !(ctl.equals("-1")));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1889
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1890
             if(attrSetMethod == null  && !updateCache && attrGetMethod != null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1891
                throw new MBeanException(new ServiceNotFoundException("No " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1892
                        "setMethod field is defined in the descriptor for " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1893
                        attrName + " attribute and caching is not enabled " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1894
                        "for it"));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1895
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1896
            if (updateCache || updateDescriptor) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1897
                if (tracing) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1898
                    MODELMBEAN_LOGGER.logp(Level.FINER,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1899
                        RequiredModelMBean.class.getName(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1900
                            "setAttribute(Attribute)",
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1901
                            "setting cached value of " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1902
                            attrName + " to " + attrValue);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1903
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1904
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1905
                attrDescr.setField("value", attrValue);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1906
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1907
                if (updateCache) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1908
                    final String currtime = String.valueOf(
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1909
                        (new Date()).getTime());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1910
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1911
                    attrDescr.setField("lastUpdatedTimeStamp", currtime);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1912
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1913
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1914
                attrInfo.setDescriptor(attrDescr);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1915
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1916
                modelMBeanInfo.setDescriptor(attrDescr,"attribute");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1917
                if (tracing) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1918
                    final StringBuilder strb = new StringBuilder()
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1919
                    .append("new descriptor is ").append(attrDescr)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1920
                    .append(". AttributeInfo descriptor is ")
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1921
                    .append(attrInfo.getDescriptor())
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1922
                    .append(". AttributeInfo descriptor is ")
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1923
                    .append(modelMBeanInfo.getDescriptor(attrName,"attribute"));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1924
                    MODELMBEAN_LOGGER.logp(Level.FINER,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1925
                            RequiredModelMBean.class.getName(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1926
                            "setAttribute(Attribute)",strb.toString());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1927
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1928
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1929
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1930
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1931
            if (tracing) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1932
                MODELMBEAN_LOGGER.logp(Level.FINER,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1933
                        RequiredModelMBean.class.getName(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1934
                "setAttribute(Attribute)","sending sendAttributeNotification");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1935
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1936
            sendAttributeChangeNotification(oldAttr,attribute);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1937
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1938
        } else { // if descriptor ... else no descriptor
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1939
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1940
            if (tracing) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1941
                    MODELMBEAN_LOGGER.logp(Level.FINER,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1942
                            RequiredModelMBean.class.getName(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1943
                        "setAttribute(Attribute)","setMethod failed "+attrName+
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1944
                        " not in attributeDescriptor\n");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1945
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1946
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1947
            throw new InvalidAttributeValueException(
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1948
                      "Unable to resolve attribute value, "+
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1949
                      "no defined in descriptor for attribute");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1950
        } // else no descriptor
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1951
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1952
        if (tracing) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1953
            MODELMBEAN_LOGGER.logp(Level.FINER,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1954
                    RequiredModelMBean.class.getName(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1955
                "setAttribute(Attribute)", "Exit");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1956
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1957
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1958
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1959
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1960
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1961
     * Sets the values of an array of attributes of this ModelMBean.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1962
     * Executes the setAttribute() method for each attribute in the list.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1963
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1964
     * @param attributes A list of attributes: The identification of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1965
     * attributes to be set and  the values they are to be set to.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1966
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1967
     * @return  The array of attributes that were set, with their new
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1968
     *    values in Attribute instances.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1969
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1970
     * @exception RuntimeOperationsException Wraps an
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1971
     *   {@link IllegalArgumentException}: The object name in parameter
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1972
     *   is null or attributes in parameter is null.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1973
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1974
     * @see #getAttributes
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1975
     **/
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1976
    public AttributeList setAttributes(AttributeList attributes) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1977
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1978
        if (MODELMBEAN_LOGGER.isLoggable(Level.FINER)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1979
            MODELMBEAN_LOGGER.logp(Level.FINER,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1980
                    RequiredModelMBean.class.getName(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1981
                "setAttribute(Attribute)", "Entry");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1982
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1983
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1984
        if (attributes == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1985
            throw new RuntimeOperationsException(new
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1986
                IllegalArgumentException("attributes must not be null"),
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1987
                "Exception occurred trying to set attributes of a "+
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1988
                "RequiredModelMBean");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1989
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1990
        final AttributeList responseList = new AttributeList();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1991
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1992
        // Go through the list of attributes
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1993
        for (Iterator i = attributes.iterator(); i.hasNext();) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1994
            final Attribute attr = (Attribute) i.next();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1995
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1996
                setAttribute(attr);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1997
                responseList.add(attr);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1998
            } catch (Exception excep) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1999
                responseList.remove(attr);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2000
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2001
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2002
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2003
        return responseList;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2004
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2005
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2006
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2007
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2008
    private ModelMBeanInfo createDefaultModelMBeanInfo() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2009
        return(new ModelMBeanInfoSupport((this.getClass().getName()),
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2010
                   "Default ModelMBean", null, null, null, null));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2011
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2012
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2013
    /*************************************/
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2014
    /* NotificationBroadcaster Interface */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2015
    /*************************************/
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2016
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2017
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2018
    private synchronized void writeToLog(String logFileName,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2019
                                         String logEntry) throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2020
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2021
        if (MODELMBEAN_LOGGER.isLoggable(Level.FINER)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2022
            MODELMBEAN_LOGGER.logp(Level.FINER,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2023
                    RequiredModelMBean.class.getName(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2024
                "writeToLog(String, String)",
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2025
                "Notification Logging to " + logFileName + ": " + logEntry);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2026
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2027
        if ((logFileName == null) || (logEntry == null)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2028
            if (MODELMBEAN_LOGGER.isLoggable(Level.FINER)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2029
                MODELMBEAN_LOGGER.logp(Level.FINER,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2030
                        RequiredModelMBean.class.getName(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2031
                    "writeToLog(String, String)",
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2032
                    "Bad input parameters, will not log this entry.");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2033
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2034
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2035
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2036
526
61ba2d5ea9da 6701459: Synchronization bug pattern found in javax.management.relation.RelationService
emcmanus
parents: 263
diff changeset
  2037
        FileOutputStream fos = new FileOutputStream(logFileName, true);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2038
        try {
526
61ba2d5ea9da 6701459: Synchronization bug pattern found in javax.management.relation.RelationService
emcmanus
parents: 263
diff changeset
  2039
            PrintStream logOut = new PrintStream(fos);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2040
            logOut.println(logEntry);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2041
            logOut.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2042
            if (MODELMBEAN_LOGGER.isLoggable(Level.FINER)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2043
                MODELMBEAN_LOGGER.logp(Level.FINER,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2044
                        RequiredModelMBean.class.getName(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2045
                    "writeToLog(String, String)","Successfully opened log " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2046
                        logFileName);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2047
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2048
        } catch (Exception e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2049
            if (MODELMBEAN_LOGGER.isLoggable(Level.FINER)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2050
                MODELMBEAN_LOGGER.logp(Level.FINER,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2051
                    RequiredModelMBean.class.getName(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2052
                        "writeToLog(String, String)",
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2053
                        "Exception " + e.toString() +
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2054
                        " trying to write to the Notification log file " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2055
                        logFileName);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2056
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2057
            throw e;
526
61ba2d5ea9da 6701459: Synchronization bug pattern found in javax.management.relation.RelationService
emcmanus
parents: 263
diff changeset
  2058
        } finally {
61ba2d5ea9da 6701459: Synchronization bug pattern found in javax.management.relation.RelationService
emcmanus
parents: 263
diff changeset
  2059
            fos.close();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2060
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2061
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2062
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2063
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2064
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2065
     * Registers an object which implements the NotificationListener
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2066
     * interface as a listener.  This
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2067
     * object's 'handleNotification()' method will be invoked when any
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2068
     * notification is issued through or by the ModelMBean.  This does
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2069
     * not include attributeChangeNotifications.  They must be registered
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2070
     * for independently.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2071
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2072
     * @param listener The listener object which will handles
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2073
     *        notifications emitted by the registered MBean.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2074
     * @param filter The filter object. If null, no filtering will be
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2075
     *        performed before handling notifications.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2076
     * @param handback The context to be sent to the listener with
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2077
     *        the notification when a notification is emitted.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2078
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2079
     * @exception IllegalArgumentException The listener cannot be null.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2080
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2081
     * @see #removeNotificationListener
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2082
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2083
    public void addNotificationListener(NotificationListener listener,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2084
                                        NotificationFilter filter,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2085
                                        Object handback)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2086
        throws java.lang.IllegalArgumentException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2087
        final String mth = "addNotificationListener(" +
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2088
                "NotificationListener, NotificationFilter, Object)";
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2089
        if (MODELMBEAN_LOGGER.isLoggable(Level.FINER)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2090
                MODELMBEAN_LOGGER.logp(Level.FINER,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2091
                        RequiredModelMBean.class.getName(), mth, "Entry");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2092
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2093
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2094
        if (listener == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2095
            throw new IllegalArgumentException(
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2096
                  "notification listener must not be null");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2097
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2098
        if (generalBroadcaster == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2099
            generalBroadcaster = new NotificationBroadcasterSupport();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2100
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2101
        generalBroadcaster.addNotificationListener(listener, filter,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2102
                                                   handback);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2103
        if (MODELMBEAN_LOGGER.isLoggable(Level.FINER)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2104
                MODELMBEAN_LOGGER.logp(Level.FINER,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2105
                        RequiredModelMBean.class.getName(), mth,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2106
                    "NotificationListener added");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2107
                MODELMBEAN_LOGGER.logp(Level.FINER,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2108
                        RequiredModelMBean.class.getName(), mth, "Exit");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2109
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2110
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2111
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2112
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2113
     * Removes a listener for Notifications from the RequiredModelMBean.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2114
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2115
     * @param listener The listener name which was handling notifications
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2116
     *    emitted by the registered MBean.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2117
     *    This method will remove all information related to this listener.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2118
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2119
     * @exception ListenerNotFoundException The listener is not registered
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2120
     *    in the MBean or is null.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2121
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2122
     * @see #addNotificationListener
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2123
     **/
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2124
    public void removeNotificationListener(NotificationListener listener)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2125
        throws ListenerNotFoundException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2126
        if (listener == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2127
            throw new ListenerNotFoundException(
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2128
                      "Notification listener is null");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2129
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2130
        final String mth="removeNotificationListener(NotificationListener)";
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2131
        if (MODELMBEAN_LOGGER.isLoggable(Level.FINER)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2132
                MODELMBEAN_LOGGER.logp(Level.FINER,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2133
                    RequiredModelMBean.class.getName(), mth, "Entry");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2134
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2135
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2136
        if (generalBroadcaster == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2137
            throw new ListenerNotFoundException(
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2138
                  "No notification listeners registered");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2139
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2140
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2141
        generalBroadcaster.removeNotificationListener(listener);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2142
        if (MODELMBEAN_LOGGER.isLoggable(Level.FINER)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2143
            MODELMBEAN_LOGGER.logp(Level.FINER,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2144
                    RequiredModelMBean.class.getName(), mth, "Exit");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2145
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2146
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2147
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2148
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2149
    public void removeNotificationListener(NotificationListener listener,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2150
                                           NotificationFilter filter,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2151
                                           Object handback)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2152
        throws ListenerNotFoundException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2153
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2154
        if (listener == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2155
            throw new ListenerNotFoundException(
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2156
                      "Notification listener is null");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2157
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2158
        final String mth = "removeNotificationListener(" +
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2159
                "NotificationListener, NotificationFilter, Object)";
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2160
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2161
        if (MODELMBEAN_LOGGER.isLoggable(Level.FINER)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2162
            MODELMBEAN_LOGGER.logp(Level.FINER,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2163
                    RequiredModelMBean.class.getName(), mth, "Entry");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2164
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2165
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2166
        if (generalBroadcaster == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2167
            throw new ListenerNotFoundException(
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2168
                  "No notification listeners registered");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2169
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2170
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2171
        generalBroadcaster.removeNotificationListener(listener,filter,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2172
                                                      handback);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2173
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2174
        if (MODELMBEAN_LOGGER.isLoggable(Level.FINER)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2175
            MODELMBEAN_LOGGER.logp(Level.FINER,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2176
                    RequiredModelMBean.class.getName(), mth, "Exit");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2177
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2178
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2179
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2180
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2181
    public void sendNotification(Notification ntfyObj)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2182
        throws MBeanException, RuntimeOperationsException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2183
        if (MODELMBEAN_LOGGER.isLoggable(Level.FINER)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2184
            MODELMBEAN_LOGGER.logp(Level.FINER,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2185
                    RequiredModelMBean.class.getName(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2186
                "sendNotification(Notification)", "Entry");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2187
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2188
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2189
        if (ntfyObj == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2190
            throw new RuntimeOperationsException(new
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2191
                IllegalArgumentException("notification object must not be "+
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2192
                                         "null"),
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2193
                "Exception occurred trying to send a notification from a "+
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2194
                "RequiredModelMBean");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2195
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2196
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2197
        // log notification if specified in descriptor
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2198
        Descriptor ntfyDesc =
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2199
            modelMBeanInfo.getDescriptor(ntfyObj.getType(),"notification");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2200
        Descriptor mmbDesc = modelMBeanInfo.getMBeanDescriptor();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2201
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2202
        if (ntfyDesc != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2203
            String logging = (String) ntfyDesc.getFieldValue("log");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2204
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2205
            if (logging == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2206
                if (mmbDesc != null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2207
                    logging = (String) mmbDesc.getFieldValue("log");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2208
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2209
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2210
            if ((logging != null) &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2211
                (logging.equalsIgnoreCase("t") ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2212
                 logging.equalsIgnoreCase("true"))) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2213
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2214
                String logfile = (String) ntfyDesc.getFieldValue("logfile");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2215
                if (logfile == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2216
                    if (mmbDesc != null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2217
                        logfile = (String)mmbDesc.getFieldValue("logfile");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2218
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2219
                if (logfile != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2220
                    try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2221
                        writeToLog(logfile,"LogMsg: " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2222
                            ((new Date(ntfyObj.getTimeStamp())).toString())+
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2223
                            " " + ntfyObj.getType() + " " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2224
                            ntfyObj.getMessage() + " Severity = " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2225
                            (String)ntfyDesc.getFieldValue("severity"));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2226
                    } catch (Exception e) {
623
97dd054b7f1f 6592586: RequiredModelMBean prints a WARNING message when calling getAttributes() for a non-existing attr
dfuchs
parents: 526
diff changeset
  2227
                        if (MODELMBEAN_LOGGER.isLoggable(Level.FINE)) {
97dd054b7f1f 6592586: RequiredModelMBean prints a WARNING message when calling getAttributes() for a non-existing attr
dfuchs
parents: 526
diff changeset
  2228
                            MODELMBEAN_LOGGER.logp(Level.FINE,
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2229
                                    RequiredModelMBean.class.getName(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2230
                                    "sendNotification(Notification)",
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2231
                                    "Failed to log " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2232
                                    ntfyObj.getType() + " notification: ", e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2233
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2234
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2235
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2236
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2237
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2238
        if (generalBroadcaster != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2239
            generalBroadcaster.sendNotification(ntfyObj);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2240
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2241
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2242
        if (MODELMBEAN_LOGGER.isLoggable(Level.FINER)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2243
            MODELMBEAN_LOGGER.logp(Level.FINER,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2244
                RequiredModelMBean.class.getName(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2245
                    "sendNotification(Notification)",
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2246
                    "sendNotification sent provided notification object");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2247
            MODELMBEAN_LOGGER.logp(Level.FINER,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2248
                RequiredModelMBean.class.getName(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2249
                    "sendNotification(Notification)"," Exit");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2250
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2251
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2252
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2253
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2254
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2255
    public void sendNotification(String ntfyText)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2256
        throws MBeanException, RuntimeOperationsException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2257
        if (MODELMBEAN_LOGGER.isLoggable(Level.FINER)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2258
            MODELMBEAN_LOGGER.logp(Level.FINER,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2259
                    RequiredModelMBean.class.getName(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2260
                "sendNotification(String)","Entry");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2261
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2262
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2263
        if (ntfyText == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2264
            throw new RuntimeOperationsException(new
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2265
                IllegalArgumentException("notification message must not "+
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2266
                                         "be null"),
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2267
                "Exception occurred trying to send a text notification "+
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2268
                "from a ModelMBean");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2269
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2270
        Notification myNtfyObj = new Notification("jmx.modelmbean.generic",
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2271
                                                  this, 1, ntfyText);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2272
        sendNotification(myNtfyObj);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2273
        if (MODELMBEAN_LOGGER.isLoggable(Level.FINER)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2274
            MODELMBEAN_LOGGER.logp(Level.FINER,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2275
                    RequiredModelMBean.class.getName(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2276
                "sendNotification(String)","Notification sent");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2277
            MODELMBEAN_LOGGER.logp(Level.FINER,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2278
                    RequiredModelMBean.class.getName(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2279
                "sendNotification(String)","Exit");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2280
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2281
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2282
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2283
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2284
     * Returns `true' if the notification `notifName' is found
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2285
     * in `info'. (bug 4744667)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2286
     **/
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2287
    private static final
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2288
        boolean hasNotification(final ModelMBeanInfo info,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2289
                                final String notifName) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2290
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2291
            if (info == null) return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2292
            else return (info.getNotification(notifName)!=null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2293
        } catch (MBeanException x) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2294
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2295
        } catch (RuntimeOperationsException r) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2296
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2297
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2298
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2299
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2300
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2301
     * Creates a default ModelMBeanNotificationInfo for GENERIC
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2302
     * notification.  (bug 4744667)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2303
     **/
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2304
    private static final ModelMBeanNotificationInfo makeGenericInfo() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2305
        final Descriptor genericDescriptor = new DescriptorSupport( new
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2306
            String[] {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2307
                "name=GENERIC",
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2308
                "descriptorType=notification",
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2309
                "log=T",
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2310
                "severity=6",
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2311
                "displayName=jmx.modelmbean.generic"} );
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2312
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2313
        return new ModelMBeanNotificationInfo(new
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2314
            String[] {"jmx.modelmbean.generic"},
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2315
            "GENERIC",
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2316
            "A text notification has been issued by the managed resource",
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2317
            genericDescriptor);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2318
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2319
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2320
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2321
     * Creates a default ModelMBeanNotificationInfo for ATTRIBUTE_CHANGE
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2322
     * notification.  (bug 4744667)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2323
     **/
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2324
    private static final
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2325
        ModelMBeanNotificationInfo makeAttributeChangeInfo() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2326
        final Descriptor attributeDescriptor = new DescriptorSupport(new
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2327
            String[] {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2328
                "name=ATTRIBUTE_CHANGE",
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2329
                "descriptorType=notification",
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2330
                "log=T",
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2331
                "severity=6",
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2332
                "displayName=jmx.attribute.change"});
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2333
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2334
        return new ModelMBeanNotificationInfo(new
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2335
            String[] {"jmx.attribute.change"},
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2336
            "ATTRIBUTE_CHANGE",
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2337
            "Signifies that an observed MBean attribute value has changed",
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2338
            attributeDescriptor );
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2339
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2340
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2341
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2342
     * Returns the array of Notifications always generated by the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2343
     * RequiredModelMBean.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2344
     * <P>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2345
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2346
     * RequiredModelMBean may always send also two additional notifications:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2347
     * <UL>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2348
     *   <LI> One with descriptor <code>"name=GENERIC,descriptorType=notification,log=T,severity=6,displayName=jmx.modelmbean.generic"</code></LI>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2349
     *   <LI> Second is a standard attribute change notification
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2350
     *        with descriptor <code>"name=ATTRIBUTE_CHANGE,descriptorType=notification,log=T,severity=6,displayName=jmx.attribute.change"</code></LI>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2351
     * </UL>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2352
     * Thus these two notifications are always added to those specified
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2353
     * by the application.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2354
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2355
     * @return MBeanNotificationInfo[]
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2356
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2357
     **/
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2358
    public MBeanNotificationInfo[] getNotificationInfo() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2359
        if (MODELMBEAN_LOGGER.isLoggable(Level.FINER)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2360
            MODELMBEAN_LOGGER.logp(Level.FINER,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2361
                    RequiredModelMBean.class.getName(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2362
                "getNotificationInfo()","Entry");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2363
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2364
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2365
        // Using hasNotification() is not optimal, but shouldn't really
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2366
        // matter in this context...
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2367
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2368
        // hasGeneric==true if GENERIC notification is present.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2369
        // (bug 4744667)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2370
        final boolean hasGeneric = hasNotification(modelMBeanInfo,"GENERIC");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2371
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2372
        // hasAttributeChange==true if ATTRIBUTE_CHANGE notification is
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2373
        // present.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2374
        // (bug 4744667)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2375
        final boolean hasAttributeChange =
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2376
           hasNotification(modelMBeanInfo,"ATTRIBUTE_CHANGE");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2377
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2378
        // User supplied list of notification infos.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2379
        //
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2380
        final ModelMBeanNotificationInfo[] currInfo =
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2381
           (ModelMBeanNotificationInfo[])modelMBeanInfo.getNotifications();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2382
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2383
        // Length of the returned list of notification infos:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2384
        //    length of user suplied list + possibly 1 for GENERIC, +
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2385
        //    possibly 1 for ATTRIBUTE_CHANGE
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2386
        //    (bug 4744667)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2387
        final int len = ((currInfo==null?0:currInfo.length) +
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2388
                         (hasGeneric?0:1) + (hasAttributeChange?0:1));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2389
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2390
        // Returned list of notification infos:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2391
        //
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2392
        final ModelMBeanNotificationInfo[] respInfo =
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2393
           new ModelMBeanNotificationInfo[len];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2394
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2395
        // Preserve previous ordering (JMX 1.1)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2396
        //
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2397
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2398
        // Counter of "standard" notification inserted before user
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2399
        // supplied notifications.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2400
        //
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2401
        int inserted=0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2402
        if (!hasGeneric)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2403
            // We need to add description for GENERIC notification
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2404
            // (bug 4744667)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2405
            respInfo[inserted++] = makeGenericInfo();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2406
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2407
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2408
        if (!hasAttributeChange)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2409
            // We need to add description for ATTRIBUTE_CHANGE notification
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2410
            // (bug 4744667)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2411
            respInfo[inserted++] = makeAttributeChangeInfo();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2412
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2413
        // Now copy user supplied list in returned list.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2414
        //
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2415
        final int count  = currInfo.length;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2416
        final int offset = inserted;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2417
        for (int j=0; j < count; j++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2418
            respInfo[offset+j] = currInfo[j];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2419
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2420
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2421
        if (MODELMBEAN_LOGGER.isLoggable(Level.FINER)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2422
            MODELMBEAN_LOGGER.logp(Level.FINER,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2423
                    RequiredModelMBean.class.getName(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2424
                "getNotificationInfo()","Exit");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2425
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2426
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2427
        return respInfo;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2428
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2429
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2430
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2431
    public void addAttributeChangeNotificationListener(NotificationListener
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2432
                                                       inlistener,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2433
                                                       String
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2434
                                                       inAttributeName,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2435
                                                       Object inhandback)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2436
        throws MBeanException, RuntimeOperationsException,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2437
               IllegalArgumentException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2438
        final String mth="addAttributeChangeNotificationListener(" +
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2439
                "NotificationListener, String, Object)";
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2440
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2441
        if (MODELMBEAN_LOGGER.isLoggable(Level.FINER)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2442
            MODELMBEAN_LOGGER.logp(Level.FINER,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2443
                    RequiredModelMBean.class.getName(),mth,"Entry");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2444
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2445
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2446
        if (inlistener == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2447
            throw new IllegalArgumentException(
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2448
                  "Listener to be registered must not be null");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2449
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2450
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2451
        if (attributeBroadcaster == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2452
            attributeBroadcaster = new NotificationBroadcasterSupport();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2453
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2454
        AttributeChangeNotificationFilter currFilter =
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2455
            new AttributeChangeNotificationFilter();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2456
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2457
        MBeanAttributeInfo[] attrInfo = modelMBeanInfo.getAttributes();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2458
        boolean found = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2459
        if (inAttributeName == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2460
            if ((attrInfo != null) && (attrInfo.length>0)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2461
                for (int i=0; i<attrInfo.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2462
                    currFilter.enableAttribute(attrInfo[i].getName());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2463
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2464
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2465
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2466
            if ((attrInfo != null) && (attrInfo.length>0)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2467
                for (int i=0; i<attrInfo.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2468
                    if (inAttributeName.equals(attrInfo[i].getName())) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2469
                        found = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2470
                        currFilter.enableAttribute(inAttributeName);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2471
                        break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2472
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2473
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2474
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2475
            if (!found) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2476
                throw new RuntimeOperationsException(new
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2477
                    IllegalArgumentException(
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2478
                    "The attribute name does not exist"),
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2479
                    "Exception occurred trying to add an "+
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2480
                    "AttributeChangeNotification listener");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2481
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2482
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2483
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2484
        if (MODELMBEAN_LOGGER.isLoggable(Level.FINER)) {
263
db2a63668df3 6675768: NoSuchElementException thrown in RequiredModelMBean when tracing enabled
emcmanus
parents: 2
diff changeset
  2485
            Vector<String> enabledAttrs = currFilter.getEnabledAttributes();
db2a63668df3 6675768: NoSuchElementException thrown in RequiredModelMBean when tracing enabled
emcmanus
parents: 2
diff changeset
  2486
            String s = (enabledAttrs.size() > 1) ?
db2a63668df3 6675768: NoSuchElementException thrown in RequiredModelMBean when tracing enabled
emcmanus
parents: 2
diff changeset
  2487
                        "[" + enabledAttrs.firstElement() + ", ...]" :
db2a63668df3 6675768: NoSuchElementException thrown in RequiredModelMBean when tracing enabled
emcmanus
parents: 2
diff changeset
  2488
                        enabledAttrs.toString();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2489
            MODELMBEAN_LOGGER.logp(Level.FINER,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2490
                    RequiredModelMBean.class.getName(), mth,
263
db2a63668df3 6675768: NoSuchElementException thrown in RequiredModelMBean when tracing enabled
emcmanus
parents: 2
diff changeset
  2491
                "Set attribute change filter to " + s);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2492
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2493
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2494
        attributeBroadcaster.addNotificationListener(inlistener,currFilter,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2495
                                                     inhandback);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2496
        if (MODELMBEAN_LOGGER.isLoggable(Level.FINER)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2497
            MODELMBEAN_LOGGER.logp(Level.FINER,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2498
                    RequiredModelMBean.class.getName(),mth,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2499
                    "Notification listener added for " + inAttributeName);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2500
            MODELMBEAN_LOGGER.logp(Level.FINER,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2501
                    RequiredModelMBean.class.getName(),mth,"Exit");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2502
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2503
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2504
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2505
    public void removeAttributeChangeNotificationListener(
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2506
            NotificationListener inlistener, String inAttributeName)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2507
        throws MBeanException, RuntimeOperationsException,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2508
               ListenerNotFoundException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2509
        if (inlistener == null) throw new
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2510
            ListenerNotFoundException("Notification listener is null");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2511
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2512
        final String mth = "removeAttributeChangeNotificationListener(" +
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2513
                "NotificationListener, String)";
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2514
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2515
        if (MODELMBEAN_LOGGER.isLoggable(Level.FINER)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2516
            MODELMBEAN_LOGGER.logp(Level.FINER,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2517
                    RequiredModelMBean.class.getName(),mth,"Entry");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2518
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2519
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2520
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2521
        if (attributeBroadcaster == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2522
            throw new ListenerNotFoundException(
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2523
                  "No attribute change notification listeners registered");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2524
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2525
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2526
        MBeanAttributeInfo[] attrInfo = modelMBeanInfo.getAttributes();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2527
        boolean found = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2528
        if ((attrInfo != null) && (attrInfo.length>0)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2529
            for (int i=0; i<attrInfo.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2530
                if (attrInfo[i].getName().equals(inAttributeName)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2531
                    found = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2532
                    break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2533
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2534
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2535
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2536
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2537
        if ((!found) && (inAttributeName != null)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2538
            throw new RuntimeOperationsException(new
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2539
                IllegalArgumentException("Invalid attribute name"),
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2540
                "Exception occurred trying to remove "+
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2541
                "attribute change notification listener");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2542
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2543
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2544
        /* note: */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2545
        /* this may be a problem if the same listener is registered for
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2546
           multiple attributes with multiple filters and/or handback
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2547
           objects.  It may remove all of them */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2548
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2549
        attributeBroadcaster.removeNotificationListener(inlistener);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2550
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2551
        if (MODELMBEAN_LOGGER.isLoggable(Level.FINER)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2552
            MODELMBEAN_LOGGER.logp(Level.FINER,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2553
                    RequiredModelMBean.class.getName(),mth,"Exit");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2554
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2555
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2556
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2557
    public void sendAttributeChangeNotification(AttributeChangeNotification
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2558
                                                ntfyObj)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2559
        throws MBeanException, RuntimeOperationsException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2560
        final String mth = "sendAttributeChangeNotification(" +
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2561
                "AttributeChangeNotification)";
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2562
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2563
        if (MODELMBEAN_LOGGER.isLoggable(Level.FINER)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2564
            MODELMBEAN_LOGGER.logp(Level.FINER,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2565
                    RequiredModelMBean.class.getName(),mth,"Entry");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2566
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2567
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2568
        if (ntfyObj == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2569
            throw new RuntimeOperationsException(new
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2570
                IllegalArgumentException(
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2571
                "attribute change notification object must not be null"),
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2572
                "Exception occurred trying to send "+
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2573
                "attribute change notification of a ModelMBean");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2574
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2575
        Object oldv = ntfyObj.getOldValue();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2576
        Object newv =  ntfyObj.getNewValue();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2577
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2578
        if (oldv == null) oldv = "null";
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2579
        if (newv == null) newv = "null";
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2580
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2581
        if (MODELMBEAN_LOGGER.isLoggable(Level.FINER)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2582
            MODELMBEAN_LOGGER.logp(Level.FINER,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2583
                    RequiredModelMBean.class.getName(),mth,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2584
                "Sending AttributeChangeNotification with " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2585
                ntfyObj.getAttributeName() + ntfyObj.getAttributeType() +
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2586
                ntfyObj.getNewValue() + ntfyObj.getOldValue());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2587
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2588
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2589
        // log notification if specified in descriptor
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2590
        Descriptor ntfyDesc =
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2591
            modelMBeanInfo.getDescriptor(ntfyObj.getType(),"notification");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2592
        Descriptor mmbDesc = modelMBeanInfo.getMBeanDescriptor();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2593
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2594
        String logging, logfile;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2595
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2596
        if (ntfyDesc != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2597
            logging =(String)  ntfyDesc.getFieldValue("log");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2598
            if (logging == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2599
                if (mmbDesc != null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2600
                    logging = (String) mmbDesc.getFieldValue("log");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2601
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2602
            if ((logging != null) &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2603
                ( logging.equalsIgnoreCase("t") ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2604
                  logging.equalsIgnoreCase("true"))) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2605
                logfile = (String) ntfyDesc.getFieldValue("logfile");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2606
                if (logfile == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2607
                    if (mmbDesc != null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2608
                        logfile = (String)mmbDesc.getFieldValue("logfile");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2609
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2610
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2611
                if (logfile != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2612
                    try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2613
                        writeToLog(logfile,"LogMsg: " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2614
                           ((new Date(ntfyObj.getTimeStamp())).toString())+
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2615
                           " " + ntfyObj.getType() + " " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2616
                           ntfyObj.getMessage() +
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2617
                           " Name = " + ntfyObj.getAttributeName() +
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2618
                           " Old value = " + oldv +
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2619
                           " New value = " + newv);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2620
                    } catch (Exception e) {
623
97dd054b7f1f 6592586: RequiredModelMBean prints a WARNING message when calling getAttributes() for a non-existing attr
dfuchs
parents: 526
diff changeset
  2621
                        if (MODELMBEAN_LOGGER.isLoggable(Level.FINE)) {
97dd054b7f1f 6592586: RequiredModelMBean prints a WARNING message when calling getAttributes() for a non-existing attr
dfuchs
parents: 526
diff changeset
  2622
                            MODELMBEAN_LOGGER.logp(Level.FINE,
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2623
                                    RequiredModelMBean.class.getName(),mth,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2624
                                "Failed to log " + ntfyObj.getType() +
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2625
                                    " notification: ", e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2626
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2627
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2628
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2629
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2630
        } else if (mmbDesc != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2631
            logging = (String) mmbDesc.getFieldValue("log");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2632
            if ((logging != null) &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2633
                ( logging.equalsIgnoreCase("t") ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2634
                  logging.equalsIgnoreCase("true") )) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2635
                logfile = (String) mmbDesc.getFieldValue("logfile");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2636
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2637
                if (logfile != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2638
                    try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2639
                        writeToLog(logfile,"LogMsg: " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2640
                           ((new Date(ntfyObj.getTimeStamp())).toString())+
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2641
                           " " + ntfyObj.getType() + " " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2642
                           ntfyObj.getMessage() +
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2643
                           " Name = " + ntfyObj.getAttributeName() +
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2644
                           " Old value = " + oldv +
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2645
                           " New value = " + newv);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2646
                    } catch (Exception e) {
623
97dd054b7f1f 6592586: RequiredModelMBean prints a WARNING message when calling getAttributes() for a non-existing attr
dfuchs
parents: 526
diff changeset
  2647
                        if (MODELMBEAN_LOGGER.isLoggable(Level.FINE)) {
97dd054b7f1f 6592586: RequiredModelMBean prints a WARNING message when calling getAttributes() for a non-existing attr
dfuchs
parents: 526
diff changeset
  2648
                            MODELMBEAN_LOGGER.logp(Level.FINE,
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2649
                                    RequiredModelMBean.class.getName(),mth,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2650
                                "Failed to log " + ntfyObj.getType() +
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2651
                                    " notification: ", e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2652
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2653
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2654
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2655
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2656
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2657
        if (attributeBroadcaster != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2658
            attributeBroadcaster.sendNotification(ntfyObj);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2659
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2660
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2661
        // XXX Revisit: This is a quickfix: it would be better to have a
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2662
        //     single broadcaster. However, it is not so simple because
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2663
        //     removeAttributeChangeNotificationListener() should
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2664
        //     remove only listeners whose filter is an instanceof
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2665
        //     AttributeChangeNotificationFilter.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2666
        //
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2667
        if (generalBroadcaster != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2668
            generalBroadcaster.sendNotification(ntfyObj);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2669
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2670
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2671
        if (MODELMBEAN_LOGGER.isLoggable(Level.FINER)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2672
            MODELMBEAN_LOGGER.logp(Level.FINER,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2673
                    RequiredModelMBean.class.getName(),mth,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2674
                "sent notification");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2675
            MODELMBEAN_LOGGER.logp(Level.FINER,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2676
                    RequiredModelMBean.class.getName(),mth,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2677
                "Exit");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2678
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2679
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2680
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2681
    public void sendAttributeChangeNotification(Attribute inOldVal,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2682
                                                Attribute inNewVal)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2683
        throws MBeanException, RuntimeOperationsException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2684
        final String mth =
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2685
            "sendAttributeChangeNotification(Attribute, Attribute)";
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2686
        if (MODELMBEAN_LOGGER.isLoggable(Level.FINER)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2687
            MODELMBEAN_LOGGER.logp(Level.FINER,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2688
                    RequiredModelMBean.class.getName(),mth,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2689
                "Entry");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2690
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2691
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2692
        // do we really want to do this?
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2693
        if ((inOldVal == null) || (inNewVal == null))
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2694
            throw new RuntimeOperationsException(new
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2695
               IllegalArgumentException("Attribute object must not be null"),
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2696
               "Exception occurred trying to send " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2697
               "attribute change notification of a ModelMBean");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2698
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2699
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2700
        if (!(inOldVal.getName().equals(inNewVal.getName())))
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2701
            throw new RuntimeOperationsException(new
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2702
                IllegalArgumentException("Attribute names are not the same"),
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2703
                "Exception occurred trying to send " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2704
                "attribute change notification of a ModelMBean");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2705
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2706
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2707
        Object newVal = inNewVal.getValue();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2708
        Object oldVal = inOldVal.getValue();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2709
        String className = "unknown";
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2710
        if (newVal != null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2711
            className = newVal.getClass().getName();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2712
        if (oldVal != null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2713
            className = oldVal.getClass().getName();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2714
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2715
        AttributeChangeNotification myNtfyObj = new
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2716
            AttributeChangeNotification(this,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2717
                                        1,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2718
                                        ((new Date()).getTime()),
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2719
                                        "AttributeChangeDetected",
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2720
                                        inOldVal.getName(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2721
                                        className,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2722
                                        inOldVal.getValue(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2723
                                        inNewVal.getValue());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2724
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2725
        sendAttributeChangeNotification(myNtfyObj);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2726
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2727
        if (MODELMBEAN_LOGGER.isLoggable(Level.FINER)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2728
            MODELMBEAN_LOGGER.logp(Level.FINER,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2729
                    RequiredModelMBean.class.getName(),mth,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2730
                "Exit");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2731
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2732
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2733
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2734
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2735
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2736
     * Return the Class Loader Repository used to perform class loading.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2737
     * Subclasses may wish to redefine this method in order to return
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2738
     * the appropriate {@link javax.management.loading.ClassLoaderRepository}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2739
     * that should be used in this object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2740
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2741
     * @return the Class Loader Repository.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2742
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2743
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2744
    protected ClassLoaderRepository getClassLoaderRepository() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2745
        return MBeanServerFactory.getClassLoaderRepository(server);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2746
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2747
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2748
    private  Class loadClass(String className)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2749
        throws ClassNotFoundException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2750
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2751
            return Class.forName(className);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2752
        } catch (ClassNotFoundException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2753
            final ClassLoaderRepository clr =
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2754
                getClassLoaderRepository();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2755
            if (clr == null) throw new ClassNotFoundException(className);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2756
            return clr.loadClass(className);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2757
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2758
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2759
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2760
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2761
    /*************************************/
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2762
    /* MBeanRegistration Interface       */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2763
    /*************************************/
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2764
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2765
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2766
     * Allows the MBean to perform any operations it needs before
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2767
     * being registered in the MBean server.  If the name of the MBean
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2768
     * is not specified, the MBean can provide a name for its
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2769
     * registration.  If any exception is raised, the MBean will not be
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2770
     * registered in the MBean server.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2771
     * <P>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2772
     * In order to ensure proper run-time semantics of RequireModelMBean,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2773
     * Any subclass of RequiredModelMBean overloading or overriding this
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2774
     * method should call <code>super.preRegister(server, name)</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2775
     * in its own <code>preRegister</code> implementation.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2776
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2777
     * @param server The MBean server in which the MBean will be registered.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2778
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2779
     * @param name The object name of the MBean.  This name is null if
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2780
     * the name parameter to one of the <code>createMBean</code> or
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2781
     * <code>registerMBean</code> methods in the {@link MBeanServer}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2782
     * interface is null.  In that case, this method must return a
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2783
     * non-null ObjectName for the new MBean.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2784
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2785
     * @return The name under which the MBean is to be registered.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2786
     * This value must not be null.  If the <code>name</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2787
     * parameter is not null, it will usually but not necessarily be
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2788
     * the returned value.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2789
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2790
     * @exception java.lang.Exception This exception will be caught by
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2791
     * the MBean server and re-thrown as an
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2792
     * {@link javax.management.MBeanRegistrationException
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2793
     * MBeanRegistrationException}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2794
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2795
    public ObjectName preRegister(MBeanServer server,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2796
                                  ObjectName name)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2797
        throws java.lang.Exception  {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2798
        // Since ModelMbeanInfo cannot be null (otherwise exception
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2799
        // thrown at creation)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2800
        // no exception thrown on ModelMBeanInfo not set.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2801
        if (name == null) throw new NullPointerException(
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2802
                     "name of RequiredModelMBean to registered is null");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2803
        this.server = server;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2804
        return name;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2805
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2806
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2807
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2808
     * Allows the MBean to perform any operations needed after having been
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2809
     * registered in the MBean server or after the registration has failed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2810
     * <P>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2811
     * In order to ensure proper run-time semantics of RequireModelMBean,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2812
     * Any subclass of RequiredModelMBean overloading or overriding this
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2813
     * method should call <code>super.postRegister(registrationDone)</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2814
     * in its own <code>postRegister</code> implementation.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2815
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2816
     * @param registrationDone Indicates whether or not the MBean has
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2817
     * been successfully registered in the MBean server. The value
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2818
     * false means that the registration phase has failed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2819
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2820
    public void postRegister(Boolean registrationDone) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2821
        registered = registrationDone.booleanValue();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2822
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2823
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2824
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2825
     * Allows the MBean to perform any operations it needs before
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2826
     * being unregistered by the MBean server.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2827
     * <P>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2828
     * In order to ensure proper run-time semantics of RequireModelMBean,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2829
     * Any subclass of RequiredModelMBean overloading or overriding this
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2830
     * method should call <code>super.preDeregister()</code> in its own
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2831
     * <code>preDeregister</code> implementation.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2832
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2833
     * @exception java.lang.Exception This exception will be caught by
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2834
     * the MBean server and re-thrown as an
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2835
     * {@link javax.management.MBeanRegistrationException
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2836
     * MBeanRegistrationException}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2837
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2838
    public void preDeregister() throws java.lang.Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2839
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2840
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2841
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2842
     * Allows the MBean to perform any operations needed after having been
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2843
     * unregistered in the MBean server.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2844
     * <P>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2845
     * In order to ensure proper run-time semantics of RequireModelMBean,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2846
     * Any subclass of RequiredModelMBean overloading or overriding this
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2847
     * method should call <code>super.postDeregister()</code> in its own
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2848
     * <code>postDeregister</code> implementation.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2849
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2850
    public void postDeregister() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2851
        registered = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2852
        this.server=null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2853
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2854
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2855
    private static final String[] primitiveTypes;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2856
    private static final String[] primitiveWrappers;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2857
    static {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2858
        primitiveTypes = new String[] {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2859
            Boolean.TYPE.getName(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2860
            Byte.TYPE.getName(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2861
            Character.TYPE.getName(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2862
            Short.TYPE.getName(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2863
            Integer.TYPE.getName(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2864
            Long.TYPE.getName(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2865
            Float.TYPE.getName(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2866
            Double.TYPE.getName(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2867
            Void.TYPE.getName()
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2868
        };
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2869
        primitiveWrappers = new String[] {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2870
            Boolean.class.getName(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2871
            Byte.class.getName(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2872
            Character.class.getName(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2873
            Short.class.getName(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2874
            Integer.class.getName(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2875
            Long.class.getName(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2876
            Float.class.getName(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2877
            Double.class.getName(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2878
            Void.class.getName()
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2879
        };
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2880
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2881
}