src/java.management/share/classes/javax/management/MatchQueryExp.java
author darcy
Wed, 23 Oct 2019 13:01:40 -0700
changeset 58766 54ffb15c4839
parent 47216 71c04702a3d5
permissions -rw-r--r--
8232442: Suppress warnings on non-serializable non-transient instance fields in java.management.* Reviewed-by: rriggs, mchung
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 715
diff changeset
     2
 * Copyright (c) 1999, 2008, Oracle and/or its affiliates. All rights reserved.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 715
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 715
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
2
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
 *
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 715
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 715
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 715
diff changeset
    23
 * questions.
2
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 javax.management;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
 * This class is used by the query-building mechanism to represent binary
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
 * relations.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
 * @serial include
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
 * @since 1.5
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
class MatchQueryExp extends QueryEval implements QueryExp {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
    /* Serial version */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
    private static final long serialVersionUID = -7156603696948215014L;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
     * @serial The attribute value to be matched
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
    private AttributeValueExp exp;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
     * @serial The pattern to be matched
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
    private String pattern;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
     * Basic Constructor.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
    public MatchQueryExp() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
     * Creates a new MatchQueryExp where the specified AttributeValueExp matches
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
     * the specified pattern StringValueExp.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
    public MatchQueryExp(AttributeValueExp a, StringValueExp s) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
        exp     = a;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
        pattern = s.getValue();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
     * Returns the attribute of the query.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
    public AttributeValueExp getAttribute()  {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
        return exp;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
     * Returns the pattern of the query.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
    public String getPattern()  {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
        return pattern;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
     * Applies the MatchQueryExp on a MBean.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
     * @param name The name of the MBean on which the MatchQueryExp will be applied.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
     * @return  True if the query was successfully applied to the MBean, false otherwise.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
     * @exception BadStringOperationException
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
     * @exception BadBinaryOpValueExpException
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
     * @exception BadAttributeValueExpException
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
     * @exception InvalidApplicationException
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
    public boolean apply(ObjectName name) throws
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
        BadStringOperationException,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
        BadBinaryOpValueExpException,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
        BadAttributeValueExpException,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
        InvalidApplicationException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
        ValueExp val = exp.apply(name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
        if (!(val instanceof StringValueExp)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
        return wildmatch(((StringValueExp)val).getValue(), pattern);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
     * Returns the string representing the object
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
    public String toString()  {
686
d0c74839e1bd 6701498: Change JMX query language to use * and ? as wildcards rather than % and _
emcmanus
parents: 34
diff changeset
   112
        return exp + " like " + new StringValueExp(pattern);
2
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
     * Tests whether string s is matched by pattern p.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
     * Supports "?", "*", "[", each of which may be escaped with "\";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
     * character classes may use "!" for negation and "-" for range.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
     * Not yet supported: internationalization; "\" inside brackets.<P>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
     * Wildcard matching routine by Karl Heuer.  Public Domain.<P>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
    private static boolean wildmatch(String s, String p) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
        char c;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
        int si = 0, pi = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
        int slen = s.length();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
        int plen = p.length();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
        while (pi < plen) { // While still string
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
            c = p.charAt(pi++);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
            if (c == '?') {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
                if (++si > slen)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
                    return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
            } else if (c == '[') { // Start of choice
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
                if (si >= slen)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
                    return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
                boolean wantit = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
                boolean seenit = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
                if (p.charAt(pi) == '!') {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
                    wantit = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
                    ++pi;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
                while ((c = p.charAt(pi)) != ']' && ++pi < plen) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
                    if (p.charAt(pi) == '-' &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
                        pi+1 < plen &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
                        p.charAt(pi+1) != ']') {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
                        if (s.charAt(si) >= p.charAt(pi-1) &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
                            s.charAt(si) <= p.charAt(pi+1)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
                            seenit = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
                        ++pi;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
                    } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
                        if (c == s.charAt(si)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
                            seenit = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
                if ((pi >= plen) || (wantit != seenit)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
                    return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
                ++pi;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
                ++si;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
            } else if (c == '*') { // Wildcard
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
                if (pi >= plen)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
                    return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
                do {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
                    if (wildmatch(s.substring(si), p.substring(pi)))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
                        return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
                } while (++si < slen);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
                return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
            } else if (c == '\\') {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
                if (pi >= plen || si >= slen ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
                    p.charAt(pi++) != s.charAt(si++))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
                    return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
                if (si >= slen || c != s.charAt(si++)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
                    return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
        return (si == slen);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
 }