jdk/src/share/classes/com/sun/jmx/mbeanserver/Repository.java
author xdono
Wed, 02 Jul 2008 12:55:45 -0700
changeset 715 f16baef3a20e
parent 526 61ba2d5ea9da
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: 526
diff changeset
     2
 * Copyright 1999-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
package com.sun.jmx.mbeanserver;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
import com.sun.jmx.defaults.ServiceName;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
import static com.sun.jmx.defaults.JmxProperties.MBEANSERVER_LOGGER;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
import java.util.ArrayList;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
import java.util.HashMap;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
import java.util.HashSet;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
import java.util.List;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
import java.util.concurrent.locks.ReentrantReadWriteLock;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
import java.util.logging.Level;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
import java.util.Map;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
import java.util.Set;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
import javax.management.DynamicMBean;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
import javax.management.InstanceAlreadyExistsException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
import javax.management.InstanceNotFoundException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
import javax.management.MalformedObjectNameException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
import javax.management.ObjectName;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
import javax.management.QueryExp;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
import javax.management.RuntimeOperationsException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
 * The RepositorySupport implements the Repository interface.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
 * This repository does not support persistency.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
 * @since 1.5
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
public class Repository {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
    // Private fields -------------------------------------------->
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
     * The structure for storing the objects is very basic.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
     * A Hashtable is used for storing the different domains
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
     * For each domain, a hashtable contains the instances with
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
     * canonical key property list string as key and named object
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
     * aggregated from given object name and mbean instance as value.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
    private final Map<String,Map<String,NamedObject>> domainTb;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
     * Number of elements contained in the Repository
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
    private volatile int nbElements = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
     * Domain name of the server the repository is attached to.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
     * It is quicker to store the information in the repository rather
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
     * than querying the framework each time the info is required.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
    private final String domain;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
     * We use a global reentrant read write lock to protect the repository.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
     * This seems safer and more efficient: we are using Maps of Maps,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
     * Guaranteing consistency while using Concurent objects at each level
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
     * may be more difficult.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
     **/
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
    private final ReentrantReadWriteLock lock;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
    // Private fields <=============================================
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
    // Private methods --------------------------------------------->
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
    /* This class is used to match an ObjectName against a pattern. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
    private final static class ObjectNamePattern {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
        private final String[] keys;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
        private final String[] values;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
        private final String   properties;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
        private final boolean  isPropertyListPattern;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
        private final boolean  isPropertyValuePattern;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
         * The ObjectName pattern against which ObjectNames are matched.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
         **/
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
        public final ObjectName pattern;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
         * Builds a new ObjectNamePattern object from an ObjectName pattern.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
         * @param pattern The ObjectName pattern under examination.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
         **/
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
        public ObjectNamePattern(ObjectName pattern) {
526
61ba2d5ea9da 6701459: Synchronization bug pattern found in javax.management.relation.RelationService
emcmanus
parents: 287
diff changeset
   108
            this(pattern.isPropertyListPattern(),
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
                 pattern.isPropertyValuePattern(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
                 pattern.getCanonicalKeyPropertyListString(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
                 pattern.getKeyPropertyList(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
                 pattern);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
         * Builds a new ObjectNamePattern object from an ObjectName pattern
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
         * constituents.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
         * @param domain pattern.getDomain().
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
         * @param propertyListPattern pattern.isPropertyListPattern().
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
         * @param propertyValuePattern pattern.isPropertyValuePattern().
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
         * @param canonicalProps pattern.getCanonicalKeyPropertyListString().
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
         * @param keyPropertyList pattern.getKeyPropertyList().
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
         * @param pattern The ObjectName pattern under examination.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
         **/
526
61ba2d5ea9da 6701459: Synchronization bug pattern found in javax.management.relation.RelationService
emcmanus
parents: 287
diff changeset
   125
        ObjectNamePattern(boolean propertyListPattern,
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
                          boolean propertyValuePattern,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
                          String canonicalProps,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
                          Map<String,String> keyPropertyList,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
                          ObjectName pattern) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
            this.isPropertyListPattern = propertyListPattern;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
            this.isPropertyValuePattern = propertyValuePattern;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
            this.properties = canonicalProps;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
            final int len = keyPropertyList.size();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
            this.keys   = new String[len];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
            this.values = new String[len];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
            int i = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
            for (Map.Entry<String,String> entry : keyPropertyList.entrySet()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
                keys[i]   = entry.getKey();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
                values[i] = entry.getValue();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
                i++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
            this.pattern = pattern;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
         * Return true if the given ObjectName matches the ObjectName pattern
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
         * for which this object has been built.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
         * WARNING: domain name is not considered here because it is supposed
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
         *          not to be wildcard when called. PropertyList is also
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
         *          supposed not to be zero-length.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
         * @param name The ObjectName we want to match against the pattern.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
         * @return true if <code>name</code> matches the pattern.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
         **/
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
        public boolean matchKeys(ObjectName name) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
            // If key property value pattern but not key property list
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
            // pattern, then the number of key properties must be equal
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
            //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
            if (isPropertyValuePattern &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
                !isPropertyListPattern &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
                (name.getKeyPropertyList().size() != keys.length))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
                return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
            // If key property value pattern or key property list pattern,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
            // then every property inside pattern should exist in name
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
            //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
            if (isPropertyValuePattern || isPropertyListPattern) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
                for (int i = keys.length - 1; i >= 0 ; i--) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
                    // Find value in given object name for key at current
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
                    // index in receiver
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
                    //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
                    String v = name.getKeyProperty(keys[i]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
                    // Did we find a value for this key ?
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
                    //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
                    if (v == null) return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
                    // If this property is ok (same key, same value), go to next
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
                    //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
                    if (isPropertyValuePattern &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
                        pattern.isPropertyValuePattern(keys[i])) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
                        // wildmatch key property values
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
                        final char[] val_pattern = values[i].toCharArray();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
                        final char[] val_string  = v.toCharArray();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
                        if (wildmatch(val_string,val_pattern))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
                            continue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
                        else
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
                            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
                    if (v.equals(values[i])) continue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
                    return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
                return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
            // If no pattern, then canonical names must be equal
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
            //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
            final String p1 = name.getCanonicalKeyPropertyListString();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
            final String p2 = properties;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
            return (p1.equals(p2));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
     * Add all the matching objects from the given hashtable in the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
     * result set for the given ObjectNamePattern
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
     * Do not check whether the domains match (only check for matching
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
     * key property lists - see <i>matchKeys()</i>)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
     **/
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
    private void addAllMatching(final Map<String,NamedObject> moiTb,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
                                final Set<NamedObject> result,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
                                final ObjectNamePattern pattern) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
        synchronized (moiTb) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
            for (NamedObject no : moiTb.values()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
                final ObjectName on = no.getName();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
                // if all couples (property, value) are contained
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
                if (pattern.matchKeys(on)) result.add(no);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
    private void addNewDomMoi(final DynamicMBean object, final String dom,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
                              final ObjectName name) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
        final Map<String,NamedObject> moiTb =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
            new HashMap<String,NamedObject>();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
        moiTb.put(name.getCanonicalKeyPropertyListString(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
                  new NamedObject(name, object));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
        domainTb.put(dom, moiTb);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
        nbElements++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
    /** Match a string against a shell-style pattern.  The only pattern
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
        characters recognised are <code>?</code>, standing for any one
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
        character, and <code>*</code>, standing for any string of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
        characters, including the empty string.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
        @param str the string to match, as a character array.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
        @param pat the pattern to match the string against, as a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
        character array.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
        @return true if and only if the string matches the pattern.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
    */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
    /* The algorithm is a classical one.  We advance pointers in
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
       parallel through str and pat.  If we encounter a star in pat,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
       we remember its position and continue advancing.  If at any
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
       stage we get a mismatch between str and pat, we look to see if
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
       there is a remembered star.  If not, we fail.  If so, we
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
       retreat pat to just past that star and str to the position
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
       after the last one we tried, and we let the match advance
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
       again.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
       Even though there is only one remembered star position, the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
       algorithm works when there are several stars in the pattern.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
       When we encounter the second star, we forget the first one.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
       This is OK, because if we get to the second star in A*B*C
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
       (where A etc are arbitrary strings), we have already seen AXB.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
       We're therefore setting up a match of *C against the remainder
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
       of the string, which will match if that remainder looks like
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
       YC, so the whole string looks like AXBYC.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
    */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
    public static boolean wildmatch(char[] str, char[] pat) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
        int stri;     // index in str
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
        int pati;     // index in pat
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
        int starstri; // index for backtrack if "*" attempt fails
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
        int starpati; // index for backtrack if "*" attempt fails, +1
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
        final int strlen = str.length;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
        final int patlen = pat.length;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
        stri = pati = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
        starstri = starpati = -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
        /* On each pass through this loop, we either advance pati,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
           or we backtrack pati and advance starstri.  Since starstri
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
           is only ever assigned from pati, the loop must terminate.  */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
        while (true) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
            if (pati < patlen) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
                final char patc = pat[pati];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
                switch (patc) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
                case '?':
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
                    if (stri == strlen)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
                        break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
                    stri++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
                    pati++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
                    continue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
                case '*':
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
                    pati++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
                    starpati = pati;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
                    starstri = stri;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
                    continue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
                default:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
                    if (stri < strlen && str[stri] == patc) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
                        stri++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
                        pati++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
                        continue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
                    break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
            } else if (stri == strlen)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
                return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
            // Mismatched, can we backtrack to a "*"?
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
            if (starpati < 0 || starstri == strlen)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
                return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
            // Retry the match one position later in str
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
            pati = starpati;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
            starstri++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
            stri = starstri;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
     * Retrieves the named object contained in repository
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
     * from the given objectname.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
    private NamedObject retrieveNamedObject(ObjectName name) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
        // No patterns inside reposit
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
        if (name.isPattern()) return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
        // Extract the domain name.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
        String dom= name.getDomain().intern();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
        // Default domain case
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
        if (dom.length() == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
            dom = domain;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
        Map<String,NamedObject> moiTb = domainTb.get(dom);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
        if (moiTb == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
            return null; // No domain containing registered object names
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
        return moiTb.get(name.getCanonicalKeyPropertyListString());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
    // Private methods <=============================================
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
    // Protected methods --------------------------------------------->
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
    // Protected methods <=============================================
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
    // Public methods --------------------------------------------->
90ce3da70b43 Initial load
duke
parents:
diff changeset
   341
90ce3da70b43 Initial load
duke
parents:
diff changeset
   342
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
     * Construct a new repository with the given default domain.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   344
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   345
    public Repository(String domain) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   346
        this(domain,true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   347
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   348
90ce3da70b43 Initial load
duke
parents:
diff changeset
   349
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   350
     * Construct a new repository with the given default domain.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   351
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   352
    public Repository(String domain, boolean fairLock) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   353
        lock = new ReentrantReadWriteLock(fairLock);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   354
90ce3da70b43 Initial load
duke
parents:
diff changeset
   355
        domainTb = new HashMap<String,Map<String,NamedObject>>(5);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   356
90ce3da70b43 Initial load
duke
parents:
diff changeset
   357
        if (domain != null && domain.length() != 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   358
            this.domain = domain;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   359
        else
90ce3da70b43 Initial load
duke
parents:
diff changeset
   360
            this.domain = ServiceName.DOMAIN;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   361
90ce3da70b43 Initial load
duke
parents:
diff changeset
   362
        // Creates an new hastable for the default domain
90ce3da70b43 Initial load
duke
parents:
diff changeset
   363
        domainTb.put(this.domain.intern(), new HashMap<String,NamedObject>());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   364
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   365
90ce3da70b43 Initial load
duke
parents:
diff changeset
   366
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   367
     * Returns the list of domains in which any MBean is currently
90ce3da70b43 Initial load
duke
parents:
diff changeset
   368
     * registered.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   369
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   370
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   371
    public String[] getDomains() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   372
90ce3da70b43 Initial load
duke
parents:
diff changeset
   373
        lock.readLock().lock();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   374
        final List<String> result;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   375
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   376
            // Temporary list
90ce3da70b43 Initial load
duke
parents:
diff changeset
   377
            result = new ArrayList<String>(domainTb.size());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   378
            for (Map.Entry<String,Map<String,NamedObject>> entry :
90ce3da70b43 Initial load
duke
parents:
diff changeset
   379
                     domainTb.entrySet()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   380
                // Skip domains that are in the table but have no
90ce3da70b43 Initial load
duke
parents:
diff changeset
   381
                // MBean registered in them
90ce3da70b43 Initial load
duke
parents:
diff changeset
   382
                // in particular the default domain may be like this
90ce3da70b43 Initial load
duke
parents:
diff changeset
   383
                Map<String,NamedObject> t = entry.getValue();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   384
                if (t != null && t.size() != 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   385
                    result.add(entry.getKey());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   386
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   387
        } finally {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   388
            lock.readLock().unlock();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   389
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   390
90ce3da70b43 Initial load
duke
parents:
diff changeset
   391
        // Make an array from result.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   392
        return result.toArray(new String[result.size()]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   393
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   394
90ce3da70b43 Initial load
duke
parents:
diff changeset
   395
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   396
     * Stores an MBean associated with its object name in the repository.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   397
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   398
     * @param object MBean to be stored in the repository.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   399
     * @param name MBean object name.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   400
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   401
    public void addMBean(final DynamicMBean object, ObjectName name)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   402
        throws InstanceAlreadyExistsException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   403
90ce3da70b43 Initial load
duke
parents:
diff changeset
   404
        if (MBEANSERVER_LOGGER.isLoggable(Level.FINER)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   405
            MBEANSERVER_LOGGER.logp(Level.FINER, Repository.class.getName(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   406
                    "addMBean", "name = " + name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   407
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   408
90ce3da70b43 Initial load
duke
parents:
diff changeset
   409
        // Extract the domain name.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   410
        String dom = name.getDomain().intern();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   411
        boolean to_default_domain = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   412
90ce3da70b43 Initial load
duke
parents:
diff changeset
   413
        // Set domain to default if domain is empty and not already set
287
bff5501b2a02 6610917: Define a generic NotificationFilter
emcmanus
parents: 2
diff changeset
   414
        if (dom.length() == 0)
bff5501b2a02 6610917: Define a generic NotificationFilter
emcmanus
parents: 2
diff changeset
   415
            name = Util.newObjectName(domain + name.toString());
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   416
90ce3da70b43 Initial load
duke
parents:
diff changeset
   417
        // Do we have default domain ?
90ce3da70b43 Initial load
duke
parents:
diff changeset
   418
        if (dom == domain) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   419
            to_default_domain = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   420
            dom = domain;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   421
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   422
            to_default_domain = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   423
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   424
90ce3da70b43 Initial load
duke
parents:
diff changeset
   425
        // Validate name for an object
90ce3da70b43 Initial load
duke
parents:
diff changeset
   426
        if (name.isPattern()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   427
            throw new RuntimeOperationsException(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   428
             new IllegalArgumentException("Repository: cannot add mbean for " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   429
                                          "pattern name " + name.toString()));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   430
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   431
90ce3da70b43 Initial load
duke
parents:
diff changeset
   432
        lock.writeLock().lock();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   433
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   434
            // Domain cannot be JMImplementation if entry does not exists
90ce3da70b43 Initial load
duke
parents:
diff changeset
   435
            if ( !to_default_domain &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   436
                    dom.equals("JMImplementation") &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   437
                    domainTb.containsKey("JMImplementation")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   438
                throw new RuntimeOperationsException(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   439
                        new IllegalArgumentException(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   440
                        "Repository: domain name cannot be JMImplementation"));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   441
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   442
90ce3da70b43 Initial load
duke
parents:
diff changeset
   443
            // If domain not already exists, add it to the hash table
90ce3da70b43 Initial load
duke
parents:
diff changeset
   444
            final Map<String,NamedObject> moiTb = domainTb.get(dom);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   445
            if (moiTb == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   446
                addNewDomMoi(object, dom, name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   447
                return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   448
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   449
90ce3da70b43 Initial load
duke
parents:
diff changeset
   450
            // Add instance if not already present
90ce3da70b43 Initial load
duke
parents:
diff changeset
   451
            String cstr = name.getCanonicalKeyPropertyListString();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   452
            NamedObject elmt= moiTb.get(cstr);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   453
            if (elmt != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   454
                throw new InstanceAlreadyExistsException(name.toString());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   455
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   456
                nbElements++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   457
                moiTb.put(cstr, new NamedObject(name, object));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   458
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   459
90ce3da70b43 Initial load
duke
parents:
diff changeset
   460
        } finally {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   461
            lock.writeLock().unlock();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   462
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   463
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   464
90ce3da70b43 Initial load
duke
parents:
diff changeset
   465
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   466
     * Checks whether an MBean of the name specified is already stored in
90ce3da70b43 Initial load
duke
parents:
diff changeset
   467
     * the repository.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   468
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   469
     * @param name name of the MBean to find.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   470
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   471
     * @return  true if the MBean is stored in the repository,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   472
     *          false otherwise.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   473
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   474
    public boolean contains(ObjectName name) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   475
        if (MBEANSERVER_LOGGER.isLoggable(Level.FINER)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   476
            MBEANSERVER_LOGGER.logp(Level.FINER, Repository.class.getName(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   477
                    "contains", " name = " + name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   478
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   479
        lock.readLock().lock();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   480
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   481
            return (retrieveNamedObject(name) != null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   482
        } finally {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   483
            lock.readLock().unlock();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   484
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   485
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   486
90ce3da70b43 Initial load
duke
parents:
diff changeset
   487
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   488
     * Retrieves the MBean of the name specified from the repository. The
90ce3da70b43 Initial load
duke
parents:
diff changeset
   489
     * object name must match exactly.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   490
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   491
     * @param name name of the MBean to retrieve.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   492
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   493
     * @return  The retrieved MBean if it is contained in the repository,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   494
     *          null otherwise.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   495
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   496
    public DynamicMBean retrieve(ObjectName name) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   497
        if (MBEANSERVER_LOGGER.isLoggable(Level.FINER)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   498
            MBEANSERVER_LOGGER.logp(Level.FINER, Repository.class.getName(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   499
                    "retrieve", "name = " + name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   500
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   501
90ce3da70b43 Initial load
duke
parents:
diff changeset
   502
        // Calls internal retrieve method to get the named object
90ce3da70b43 Initial load
duke
parents:
diff changeset
   503
        lock.readLock().lock();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   504
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   505
            NamedObject no = retrieveNamedObject(name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   506
            if (no == null) return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   507
            else return no.getObject();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   508
        } finally {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   509
            lock.readLock().unlock();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   510
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   511
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   512
90ce3da70b43 Initial load
duke
parents:
diff changeset
   513
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   514
     * Selects and retrieves the list of MBeans whose names match the specified
90ce3da70b43 Initial load
duke
parents:
diff changeset
   515
     * object name pattern and which match the specified query expression
90ce3da70b43 Initial load
duke
parents:
diff changeset
   516
     * (optionally).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   517
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   518
     * @param pattern The name of the MBean(s) to retrieve - may be a specific
90ce3da70b43 Initial load
duke
parents:
diff changeset
   519
     * object or a name pattern allowing multiple MBeans to be selected.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   520
     * @param query query expression to apply when selecting objects - this
90ce3da70b43 Initial load
duke
parents:
diff changeset
   521
     * parameter will be ignored when the Repository Service does not
90ce3da70b43 Initial load
duke
parents:
diff changeset
   522
     * support filtering.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   523
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   524
     * @return  The list of MBeans selected. There may be zero, one or many
90ce3da70b43 Initial load
duke
parents:
diff changeset
   525
     *          MBeans returned in the set.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   526
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   527
    public Set<NamedObject> query(ObjectName pattern, QueryExp query) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   528
90ce3da70b43 Initial load
duke
parents:
diff changeset
   529
        final Set<NamedObject> result = new HashSet<NamedObject>();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   530
90ce3da70b43 Initial load
duke
parents:
diff changeset
   531
        // The following filter cases are considered:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   532
        // null, "", "*:*" : names in all domains
90ce3da70b43 Initial load
duke
parents:
diff changeset
   533
        // ":*", ":[key=value],*" : names in defaultDomain
90ce3da70b43 Initial load
duke
parents:
diff changeset
   534
        // "domain:*", "domain:[key=value],*" : names in the specified domain
90ce3da70b43 Initial load
duke
parents:
diff changeset
   535
90ce3da70b43 Initial load
duke
parents:
diff changeset
   536
        // Surely one of the most frequent case ... query on the whole world
526
61ba2d5ea9da 6701459: Synchronization bug pattern found in javax.management.relation.RelationService
emcmanus
parents: 287
diff changeset
   537
        ObjectName name;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   538
        if (pattern == null ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   539
            pattern.getCanonicalName().length() == 0 ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   540
            pattern.equals(ObjectName.WILDCARD))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   541
           name = ObjectName.WILDCARD;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   542
        else name = pattern;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   543
90ce3da70b43 Initial load
duke
parents:
diff changeset
   544
        lock.readLock().lock();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   545
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   546
90ce3da70b43 Initial load
duke
parents:
diff changeset
   547
            // If pattern is not a pattern, retrieve this mbean !
90ce3da70b43 Initial load
duke
parents:
diff changeset
   548
            if (!name.isPattern()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   549
                final NamedObject no;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   550
                no = retrieveNamedObject(name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   551
                if (no != null) result.add(no);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   552
                return result;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   553
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   554
90ce3da70b43 Initial load
duke
parents:
diff changeset
   555
            // All names in all domains
90ce3da70b43 Initial load
duke
parents:
diff changeset
   556
            if (name == ObjectName.WILDCARD) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   557
                for (Map<String,NamedObject> moiTb : domainTb.values()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   558
                    result.addAll(moiTb.values());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   559
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   560
                return result;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   561
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   562
90ce3da70b43 Initial load
duke
parents:
diff changeset
   563
            final String canonical_key_property_list_string =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   564
                    name.getCanonicalKeyPropertyListString();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   565
            final boolean allNames =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   566
                    (canonical_key_property_list_string.length()==0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   567
            final ObjectNamePattern namePattern =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   568
                (allNames?null:new ObjectNamePattern(name));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   569
90ce3da70b43 Initial load
duke
parents:
diff changeset
   570
            // All names in default domain
90ce3da70b43 Initial load
duke
parents:
diff changeset
   571
            if (name.getDomain().length() == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   572
                final Map<String,NamedObject> moiTb = domainTb.get(domain);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   573
                if (allNames)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   574
                    result.addAll(moiTb.values());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   575
                else
90ce3da70b43 Initial load
duke
parents:
diff changeset
   576
                    addAllMatching(moiTb, result, namePattern);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   577
                return result;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   578
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   579
90ce3da70b43 Initial load
duke
parents:
diff changeset
   580
            // Pattern matching in the domain name (*, ?)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   581
            char[] dom2Match = name.getDomain().toCharArray();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   582
            for (String domain : domainTb.keySet()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   583
                char[] theDom = domain.toCharArray();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   584
                if (wildmatch(theDom, dom2Match)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   585
                    final Map<String,NamedObject> moiTb = domainTb.get(domain);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   586
                    if (allNames)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   587
                        result.addAll(moiTb.values());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   588
                    else
90ce3da70b43 Initial load
duke
parents:
diff changeset
   589
                        addAllMatching(moiTb, result, namePattern);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   590
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   591
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   592
            return result;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   593
        } finally {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   594
            lock.readLock().unlock();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   595
        }
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
     * Removes an MBean from the repository.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   600
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   601
     * @param name name of the MBean to remove.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   602
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   603
     * @exception InstanceNotFoundException The MBean does not exist in
90ce3da70b43 Initial load
duke
parents:
diff changeset
   604
     *            the repository.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   605
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   606
    public void remove(final ObjectName name)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   607
        throws InstanceNotFoundException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   608
90ce3da70b43 Initial load
duke
parents:
diff changeset
   609
        // Debugging stuff
90ce3da70b43 Initial load
duke
parents:
diff changeset
   610
        if (MBEANSERVER_LOGGER.isLoggable(Level.FINER)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   611
            MBEANSERVER_LOGGER.logp(Level.FINER, Repository.class.getName(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   612
                    "remove", "name = " + name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   613
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   614
90ce3da70b43 Initial load
duke
parents:
diff changeset
   615
        // Extract domain name.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   616
        String dom= name.getDomain().intern();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   617
90ce3da70b43 Initial load
duke
parents:
diff changeset
   618
        // Default domain case
90ce3da70b43 Initial load
duke
parents:
diff changeset
   619
        if (dom.length() == 0) dom = domain;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   620
90ce3da70b43 Initial load
duke
parents:
diff changeset
   621
        lock.writeLock().lock();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   622
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   623
            // Find the domain subtable
90ce3da70b43 Initial load
duke
parents:
diff changeset
   624
            final Map<String,NamedObject> moiTb = domainTb.get(dom);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   625
            if (moiTb == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   626
                throw new InstanceNotFoundException(name.toString());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   627
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   628
90ce3da70b43 Initial load
duke
parents:
diff changeset
   629
            // Remove the corresponding element
90ce3da70b43 Initial load
duke
parents:
diff changeset
   630
            if (moiTb.remove(name.getCanonicalKeyPropertyListString())==null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   631
                throw new InstanceNotFoundException(name.toString());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   632
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   633
90ce3da70b43 Initial load
duke
parents:
diff changeset
   634
            // We removed it !
90ce3da70b43 Initial load
duke
parents:
diff changeset
   635
            nbElements--;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   636
90ce3da70b43 Initial load
duke
parents:
diff changeset
   637
            // No more object for this domain, we remove this domain hashtable
90ce3da70b43 Initial load
duke
parents:
diff changeset
   638
            if (moiTb.isEmpty()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   639
                domainTb.remove(dom);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   640
90ce3da70b43 Initial load
duke
parents:
diff changeset
   641
                // set a new default domain table (always present)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   642
                // need to reinstantiate a hashtable because of possible
90ce3da70b43 Initial load
duke
parents:
diff changeset
   643
                // big buckets array size inside table, never cleared,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   644
                // thus the new !
90ce3da70b43 Initial load
duke
parents:
diff changeset
   645
                if (dom == domain)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   646
                    domainTb.put(domain, new HashMap<String,NamedObject>());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   647
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   648
        } finally {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   649
            lock.writeLock().unlock();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   650
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   651
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   652
90ce3da70b43 Initial load
duke
parents:
diff changeset
   653
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   654
     * Gets the number of MBeans stored in the repository.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   655
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   656
     * @return  Number of MBeans.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   657
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   658
    public Integer getCount() {
526
61ba2d5ea9da 6701459: Synchronization bug pattern found in javax.management.relation.RelationService
emcmanus
parents: 287
diff changeset
   659
        return nbElements;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   660
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   661
90ce3da70b43 Initial load
duke
parents:
diff changeset
   662
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   663
     * Gets the name of the domain currently used by default in the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   664
     * repository.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   665
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   666
     * @return  A string giving the name of the default domain name.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   667
     */
526
61ba2d5ea9da 6701459: Synchronization bug pattern found in javax.management.relation.RelationService
emcmanus
parents: 287
diff changeset
   668
    public String getDefaultDomain() {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   669
        return domain;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   670
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   671
90ce3da70b43 Initial load
duke
parents:
diff changeset
   672
    // Public methods <=============================================
90ce3da70b43 Initial load
duke
parents:
diff changeset
   673
}