File: QueryFactory.java

package info (click to toggle)
openjdk-11 11.0.4%2B11-1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 757,028 kB
  • sloc: java: 5,016,041; xml: 1,191,974; cpp: 934,731; ansic: 555,697; sh: 24,299; objc: 12,703; python: 3,602; asm: 3,415; makefile: 2,772; awk: 351; sed: 172; perl: 114; jsp: 24; csh: 3
file content (328 lines) | stat: -rw-r--r-- 14,949 bytes parent folder | download | duplicates (16)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
/*
 * Copyright (c) 2006, 2015, Oracle and/or its affiliates. All rights reserved.
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
 *
 * This code is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License version 2 only, as
 * published by the Free Software Foundation.
 *
 * This code is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
 * version 2 for more details (a copy is included in the LICENSE file that
 * accompanied this code).
 *
 * You should have received a copy of the GNU General Public License version
 * 2 along with this work; if not, write to the Free Software Foundation,
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
 *
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
 * or visit www.oracle.com if you need additional information or have any
 * questions.
 */

import java.util.ArrayList;

import javax.management.Query;
import javax.management.QueryExp;
import javax.management.ValueExp;

/**
 * Class used for building QueryExp instances of all every possible type
 * in terms of JMX API members; note that several JMX classes are private
 * and appears in the JDK API only by their serial form.
 * Comments in each case of the big switch in method getQuery() details which
 * API member we cover with a given query.
 */
public class QueryFactory extends QueryData {

    private String mbeanClassName = "";
    private String primitiveIntAttName = "IntAtt";
    private String primitiveLongAttName = "LongAtt";
    private String integerAttName = "IntegerAtt";
    private String primitiveBooleanAttName = "BooleanAtt";
    private String primitiveDoubleAttName = "DoubleAtt";
    private String primitiveFloatAttName = "FloatAtt";
    private String stringAttName = "StringAtt";
    private ArrayList<QueryExp> queries = new ArrayList<QueryExp>();

    /**
     * Creates a new instance of QueryFactory.
     * The name is the fully qualified class name of an MBean.
     * There is severe constraints on that MBean that must:
     * <ul>
     * <li>extend QueryData in order to inherit attribute values.
     * <li>define a RW attribute IntAtt of type int
     * initialized to QueryData.longValue
     * <li>define a RW attribute LongAtt of type long
     * initialized to QueryData.intValue
     * <li>define a RW attribute IntegerAtt of type Integer
     * initialized to QueryData.integerValue
     * <li>define a RW attribute BooleanAtt of type boolean
     * initialized to QueryData.booleanValue
     * <li>define a RW attribute DoubleAtt of type double
     * initialized to QueryData.doubleValue
     * <li>define a RW attribute FloatAtt of type float
     * initialized to QueryData.floatValue
     * <li>define a RW attribute StringAtt of type String
     * initialized to QueryData.stringValue
     * </ul>
     */
    public QueryFactory(String name) {
        this.mbeanClassName = name;
    }

    /**
     * Returns the highest index value the method getQuery supports.
     * WARNING : returns 0 if buildQueries haven't been called first !
     */
    public int getSize() {
        return queries.size();
    }

    /**
     * Populates an ArrayList of QueryExp.
     * Lowest index is 1.
     * Highest index is returned by getSize().
     * <br>The queries numbered 1 to 23 allow to cover all the underlying
     * Java classes of the JMX API used to build queries.
     */
    public void buildQueries() {
        if ( queries.size() == 0 ) {
            int smallerIntValue = intValue - 1;
            int biggerIntValue = intValue + 1;

            // case 1:
            // True if the MBean is of class mbeanClassName
            // We cover javax.management.InstanceOfQueryExp
            queries.add(Query.isInstanceOf(Query.value(mbeanClassName)));

            // case 2:
            // True if the MBean is of class mbeanClassName
            // We cover javax.management.MatchQueryExp and
            // javax.management.ClassAttributeValueExp
            queries.add(Query.match(Query.classattr(),
                    Query.value(mbeanClassName)));

            // case 3:
            // True if an attribute named primitiveIntAttName of type int has
            // the value intValue
            // We cover javax.management.BinaryRelQueryExp with
            // a relOp equal to EQ and javax.management.NumericValueExp
            queries.add(Query.eq(Query.attr(primitiveIntAttName),
                    Query.value(intValue)));

            // case 4:
            // True if an attribute named primitiveLongAttName of type long has
            // the value longValue
            // We cover javax.management.BinaryRelQueryExp with
            // a relOp equal to EQ and javax.management.NumericValueExp
            queries.add(Query.eq(Query.attr(primitiveLongAttName),
                    Query.value(longValue)));

            // case 5:
            // True if an attribute named primitiveDoubleAttName of type double
            // has the value doubleValue
            // We cover javax.management.BinaryRelQueryExp with
            // a relOp equal to EQ and javax.management.NumericValueExp
            queries.add(Query.eq(Query.attr(primitiveDoubleAttName),
                    Query.value(doubleValue)));

            // case 6:
            // True if an attribute named primitiveFloatAttName of type float
            // has the value floatValue
            // We cover javax.management.BinaryRelQueryExp with
            // a relOp equal to EQ and javax.management.NumericValueExp
            queries.add(Query.eq(Query.attr(primitiveFloatAttName),
                    Query.value(floatValue)));

            // case 7:
            // True if an attribute named primitiveIntAttName of type int is
            // hold by an MBean of class mbeanClassName and has
            // the value intValue
            // We cover javax.management.QualifiedAttributeValueExp
            queries.add(Query.eq(Query.attr(mbeanClassName, primitiveIntAttName),
                    Query.value(intValue)));

            // case 8:
            // True if an attribute named stringAttName of type String has
            // the value stringValue
            // We cover javax.management.BinaryRelQueryExp with
            // a relOp equal to EQ and javax.management.StringValueExp
            queries.add(Query.eq(Query.attr(stringAttName),
                    Query.value(stringValue)));

            // case 9:
            // True if an attribute named integerAttName of type Integer has
            // the value integerValue
            // We cover javax.management.BinaryRelQueryExp with
            // a relOp equal to EQ and javax.management.NumericValueExp
            queries.add(Query.eq(Query.attr(integerAttName),
                    Query.value(integerValue)));

            // case 10:
            // True if an attribute named primitiveBooleanAttName of type boolean
            // has the value booleanValue
            // We cover javax.management.BinaryRelQueryExp with
            // a relOp equal to EQ and javax.management.BooleanValueExp
            queries.add(Query.eq(Query.attr(primitiveBooleanAttName),
                    Query.value(booleanValue)));

            // case 11:
            // True if an attribute named primitiveIntAttName of type int has
            // not the value smallerIntValue
            // We cover javax.management.NotQueryExp
            queries.add(Query.not(Query.eq(Query.attr(primitiveIntAttName),
                    Query.value(smallerIntValue))));

            // case 12:
            // True if either
            // an attribute named primitiveIntAttName of type int has
            // the value intValue
            // or
            // an attribute named primitiveLongAttName of type long has
            // the value longValue
            // We cover javax.management.OrQueryExp
            queries.add(Query.or(
                    Query.eq(Query.attr(primitiveIntAttName),
                    Query.value(intValue)),
                    Query.eq(Query.attr(primitiveLongAttName),
                    Query.value(longValue))));

            // case 13:
            // True if
            // an attribute named primitiveIntAttName of type int has
            // the value intValue
            // and
            // an attribute named primitiveLongAttName of type long has
            // the value longValue
            // We cover javax.management.AndQueryExp
            queries.add(Query.and(
                    Query.eq(Query.attr(primitiveIntAttName),
                    Query.value(intValue)),
                    Query.eq(Query.attr(primitiveLongAttName),
                    Query.value(longValue))));

            // case 14:
            // True if an attribute named primitiveIntAttName of type int has
            // the value intValue
            // We cover javax.management.InQueryExp
            ValueExp[] inArray = {Query.value(intValue)};
            queries.add(Query.in(Query.attr(primitiveIntAttName), inArray));

            // case 15:
            // True if an attribute named primitiveIntAttName of type int has
            // its value in between smallerIntValue and biggerIntValue
            // We cover javax.management.BetweenRelQueryExp
            queries.add(Query.between(Query.attr(primitiveIntAttName),
                    Query.value(smallerIntValue),
                    Query.value(biggerIntValue)));

            // case 16:
            // True if an attribute named primitiveIntAttName of type int has
            // a value greater than smallerIntValue
            // We cover javax.management.BinaryRelQueryExp with
            // a relOp equal to GT
            queries.add(Query.gt(Query.attr(primitiveIntAttName),
                    Query.value(smallerIntValue)));

            // case 17:
            // True if an attribute named primitiveIntAttName of type int has
            // a value greater or equal to smallerIntValue
            // We cover javax.management.BinaryRelQueryExp with
            // a relOp equal to GE
            queries.add(Query.geq(Query.attr(primitiveIntAttName),
                    Query.value(smallerIntValue)));

            // case 18:
            // True if an attribute named primitiveIntAttName of type int has
            // a value smaller than biggerIntValue
            // We cover javax.management.BinaryRelQueryExp with
            // a relOp equal to LT
            queries.add(Query.lt(Query.attr(primitiveIntAttName),
                    Query.value(biggerIntValue)));

            // case 19:
            // True if an attribute named primitiveIntAttName of type int has
            // a value smaller or equal to biggerIntValue
            // We cover javax.management.BinaryRelQueryExp with
            // a relOp equal to LE
            queries.add(Query.leq(Query.attr(primitiveIntAttName),
                    Query.value(biggerIntValue)));

            // case 20:
            // True if an attribute named primitiveIntAttName of type int has
            // a value equal to intValue minus zero
            // We cover javax.management.BinaryRelQueryExp with
            // a relOp equal to MINUS
            queries.add(Query.eq(Query.attr(primitiveIntAttName),
                    Query.minus(Query.value(intValue), Query.value(0))));

            // case 21:
            // True if an attribute named primitiveIntAttName of type int has
            // a value equal to intValue plus zero
            // We cover javax.management.BinaryRelQueryExp with
            // a relOp equal to PLUS
            queries.add(Query.eq(Query.attr(primitiveIntAttName),
                    Query.plus(Query.value(intValue), Query.value(0))));

            // case 22:
            // True if an attribute named primitiveIntAttName of type int has
            // a value equal to intValue divided by one
            // We cover javax.management.BinaryRelQueryExp with
            // a relOp equal to DIV
            queries.add(Query.eq(Query.attr(primitiveIntAttName),
                    Query.div(Query.value(intValue), Query.value(1))));

            // case 23:
            // True if an attribute named primitiveIntAttName of type int has
            // a value equal to intValue multiplicated by one
            // We cover javax.management.BinaryRelQueryExp with
            // a relOp equal to TIMES
            queries.add(Query.eq(Query.attr(primitiveIntAttName),
                    Query.times(Query.value(intValue), Query.value(1))));

            // case 24:
            // That query is a complex one that combines within a big AND
            // queries with index 2 to 23 inclusive. But because a List is
            // zero based, we must decrement all indexes by 1 when retrieving
            // any previously stored query.
            QueryExp q2_3 = Query.and(queries.get(2-1), queries.get(3-1));
            QueryExp q4_5 = Query.and(queries.get(4-1), queries.get(5-1));
            QueryExp q6_7 = Query.and(queries.get(6-1), queries.get(7-1));
            QueryExp q8_9 = Query.and(queries.get(8-1), queries.get(9-1));
            QueryExp q10_11 = Query.and(queries.get(10-1), queries.get(11-1));
            QueryExp q12_13 = Query.and(queries.get(12-1), queries.get(13-1));
            QueryExp q14_15 = Query.and(queries.get(14-1), queries.get(15-1));
            QueryExp q16_17 = Query.and(queries.get(16-1), queries.get(17-1));
            QueryExp q18_19 = Query.and(queries.get(18-1), queries.get(19-1));
            QueryExp q20_21 = Query.and(queries.get(20-1), queries.get(21-1));
            QueryExp q22_23 = Query.and(queries.get(22-1), queries.get(23-1));
            QueryExp q2_5 = Query.and(q2_3, q4_5);
            QueryExp q6_9 = Query.and(q6_7, q8_9);
            QueryExp q10_13 = Query.and(q10_11, q12_13);
            QueryExp q14_17 = Query.and(q14_15, q16_17);
            QueryExp q18_21 = Query.and(q18_19, q20_21);
            QueryExp q2_9 = Query.and(q2_5, q6_9);
            QueryExp q10_17 = Query.and(q10_13, q14_17);
            QueryExp q18_23 = Query.and(q18_21, q22_23);
            QueryExp q2_17 = Query.and(q2_9, q10_17);
            queries.add(Query.and(q2_17, q18_23));

            // case 25:
            // Complex query mixing AND and OR.
            queries.add(Query.or(q6_9, q18_23));
        }
    }

    /**
     * Returns a QueryExp taken is the ArrayList populated by buildQueries().
     * Lowest index is 1.
     * Highest index is returned by getSize().
     * <br>The queries numbered 1 to 23 allow to cover all the underlying
     * Java classes of the JMX API used to build queries.
     */
    public QueryExp getQuery(int index) {
        return queries.get(index - 1);
    }
}