author | xdono |
Wed, 02 Jul 2008 12:55:45 -0700 | |
changeset 715 | f16baef3a20e |
parent 686 | d0c74839e1bd |
child 5506 | 202f599c92aa |
permissions | -rw-r--r-- |
2 | 1 |
/* |
715 | 2 |
* Copyright 1999-2008 Sun Microsystems, Inc. All Rights Reserved. |
2 | 3 |
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
4 |
* |
|
5 |
* This code is free software; you can redistribute it and/or modify it |
|
6 |
* under the terms of the GNU General Public License version 2 only, as |
|
7 |
* published by the Free Software Foundation. Sun designates this |
|
8 |
* particular file as subject to the "Classpath" exception as provided |
|
9 |
* by Sun in the LICENSE file that accompanied this code. |
|
10 |
* |
|
11 |
* This code is distributed in the hope that it will be useful, but WITHOUT |
|
12 |
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
|
13 |
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
|
14 |
* version 2 for more details (a copy is included in the LICENSE file that |
|
15 |
* accompanied this code). |
|
16 |
* |
|
17 |
* You should have received a copy of the GNU General Public License version |
|
18 |
* 2 along with this work; if not, write to the Free Software Foundation, |
|
19 |
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
|
20 |
* |
|
21 |
* Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara, |
|
22 |
* CA 95054 USA or visit www.sun.com if you need additional information or |
|
23 |
* have any questions. |
|
24 |
*/ |
|
25 |
||
26 |
package javax.management; |
|
27 |
||
28 |
||
29 |
||
30 |
/** |
|
31 |
* This class is used by the query-building mechanism to represent binary |
|
32 |
* relations. |
|
33 |
* @serial include |
|
34 |
* |
|
35 |
* @since 1.5 |
|
36 |
*/ |
|
37 |
class MatchQueryExp extends QueryEval implements QueryExp { |
|
38 |
||
39 |
/* Serial version */ |
|
40 |
private static final long serialVersionUID = -7156603696948215014L; |
|
41 |
||
42 |
/** |
|
43 |
* @serial The attribute value to be matched |
|
44 |
*/ |
|
45 |
private AttributeValueExp exp; |
|
46 |
||
47 |
/** |
|
48 |
* @serial The pattern to be matched |
|
49 |
*/ |
|
50 |
private String pattern; |
|
51 |
||
52 |
||
53 |
/** |
|
54 |
* Basic Constructor. |
|
55 |
*/ |
|
56 |
public MatchQueryExp() { |
|
57 |
} |
|
58 |
||
59 |
/** |
|
60 |
* Creates a new MatchQueryExp where the specified AttributeValueExp matches |
|
61 |
* the specified pattern StringValueExp. |
|
62 |
*/ |
|
63 |
public MatchQueryExp(AttributeValueExp a, StringValueExp s) { |
|
64 |
exp = a; |
|
65 |
pattern = s.getValue(); |
|
66 |
} |
|
67 |
||
68 |
||
69 |
/** |
|
70 |
* Returns the attribute of the query. |
|
71 |
*/ |
|
72 |
public AttributeValueExp getAttribute() { |
|
73 |
return exp; |
|
74 |
} |
|
75 |
||
76 |
/** |
|
77 |
* Returns the pattern of the query. |
|
78 |
*/ |
|
79 |
public String getPattern() { |
|
80 |
return pattern; |
|
81 |
} |
|
82 |
||
83 |
/** |
|
84 |
* Applies the MatchQueryExp on a MBean. |
|
85 |
* |
|
86 |
* @param name The name of the MBean on which the MatchQueryExp will be applied. |
|
87 |
* |
|
88 |
* @return True if the query was successfully applied to the MBean, false otherwise. |
|
89 |
* |
|
90 |
* @exception BadStringOperationException |
|
91 |
* @exception BadBinaryOpValueExpException |
|
92 |
* @exception BadAttributeValueExpException |
|
93 |
* @exception InvalidApplicationException |
|
94 |
*/ |
|
95 |
public boolean apply(ObjectName name) throws |
|
96 |
BadStringOperationException, |
|
97 |
BadBinaryOpValueExpException, |
|
98 |
BadAttributeValueExpException, |
|
99 |
InvalidApplicationException { |
|
100 |
||
101 |
ValueExp val = exp.apply(name); |
|
102 |
if (!(val instanceof StringValueExp)) { |
|
103 |
return false; |
|
104 |
} |
|
105 |
return wildmatch(((StringValueExp)val).getValue(), pattern); |
|
106 |
} |
|
107 |
||
108 |
/** |
|
109 |
* Returns the string representing the object |
|
110 |
*/ |
|
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 | 113 |
} |
114 |
||
115 |
/* |
|
116 |
* Tests whether string s is matched by pattern p. |
|
117 |
* Supports "?", "*", "[", each of which may be escaped with "\"; |
|
118 |
* character classes may use "!" for negation and "-" for range. |
|
119 |
* Not yet supported: internationalization; "\" inside brackets.<P> |
|
120 |
* Wildcard matching routine by Karl Heuer. Public Domain.<P> |
|
121 |
*/ |
|
122 |
private static boolean wildmatch(String s, String p) { |
|
123 |
char c; |
|
124 |
int si = 0, pi = 0; |
|
125 |
int slen = s.length(); |
|
126 |
int plen = p.length(); |
|
127 |
||
128 |
while (pi < plen) { // While still string |
|
129 |
c = p.charAt(pi++); |
|
130 |
if (c == '?') { |
|
131 |
if (++si > slen) |
|
132 |
return false; |
|
133 |
} else if (c == '[') { // Start of choice |
|
134 |
if (si >= slen) |
|
135 |
return false; |
|
136 |
boolean wantit = true; |
|
137 |
boolean seenit = false; |
|
138 |
if (p.charAt(pi) == '!') { |
|
139 |
wantit = false; |
|
140 |
++pi; |
|
141 |
} |
|
142 |
while ((c = p.charAt(pi)) != ']' && ++pi < plen) { |
|
143 |
if (p.charAt(pi) == '-' && |
|
144 |
pi+1 < plen && |
|
145 |
p.charAt(pi+1) != ']') { |
|
146 |
if (s.charAt(si) >= p.charAt(pi-1) && |
|
147 |
s.charAt(si) <= p.charAt(pi+1)) { |
|
148 |
seenit = true; |
|
149 |
} |
|
150 |
++pi; |
|
151 |
} else { |
|
152 |
if (c == s.charAt(si)) { |
|
153 |
seenit = true; |
|
154 |
} |
|
155 |
} |
|
156 |
} |
|
157 |
if ((pi >= plen) || (wantit != seenit)) { |
|
158 |
return false; |
|
159 |
} |
|
160 |
++pi; |
|
161 |
++si; |
|
162 |
} else if (c == '*') { // Wildcard |
|
163 |
if (pi >= plen) |
|
164 |
return true; |
|
165 |
do { |
|
166 |
if (wildmatch(s.substring(si), p.substring(pi))) |
|
167 |
return true; |
|
168 |
} while (++si < slen); |
|
169 |
return false; |
|
170 |
} else if (c == '\\') { |
|
171 |
if (pi >= plen || si >= slen || |
|
172 |
p.charAt(pi++) != s.charAt(si++)) |
|
173 |
return false; |
|
174 |
} else { |
|
175 |
if (si >= slen || c != s.charAt(si++)) { |
|
176 |
return false; |
|
177 |
} |
|
178 |
} |
|
179 |
} |
|
180 |
return (si == slen); |
|
181 |
} |
|
182 |
} |