Path: blob/master/test/jdk/javax/management/query/QueryFactory.java
41149 views
/*1* Copyright (c) 2006, 2015, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223import java.util.ArrayList;2425import javax.management.Query;26import javax.management.QueryExp;27import javax.management.ValueExp;2829/**30* Class used for building QueryExp instances of all every possible type31* in terms of JMX API members; note that several JMX classes are private32* and appears in the JDK API only by their serial form.33* Comments in each case of the big switch in method getQuery() details which34* API member we cover with a given query.35*/36public class QueryFactory extends QueryData {3738private String mbeanClassName = "";39private String primitiveIntAttName = "IntAtt";40private String primitiveLongAttName = "LongAtt";41private String integerAttName = "IntegerAtt";42private String primitiveBooleanAttName = "BooleanAtt";43private String primitiveDoubleAttName = "DoubleAtt";44private String primitiveFloatAttName = "FloatAtt";45private String stringAttName = "StringAtt";46private ArrayList<QueryExp> queries = new ArrayList<QueryExp>();4748/**49* Creates a new instance of QueryFactory.50* The name is the fully qualified class name of an MBean.51* There is severe constraints on that MBean that must:52* <ul>53* <li>extend QueryData in order to inherit attribute values.54* <li>define a RW attribute IntAtt of type int55* initialized to QueryData.longValue56* <li>define a RW attribute LongAtt of type long57* initialized to QueryData.intValue58* <li>define a RW attribute IntegerAtt of type Integer59* initialized to QueryData.integerValue60* <li>define a RW attribute BooleanAtt of type boolean61* initialized to QueryData.booleanValue62* <li>define a RW attribute DoubleAtt of type double63* initialized to QueryData.doubleValue64* <li>define a RW attribute FloatAtt of type float65* initialized to QueryData.floatValue66* <li>define a RW attribute StringAtt of type String67* initialized to QueryData.stringValue68* </ul>69*/70public QueryFactory(String name) {71this.mbeanClassName = name;72}7374/**75* Returns the highest index value the method getQuery supports.76* WARNING : returns 0 if buildQueries haven't been called first !77*/78public int getSize() {79return queries.size();80}8182/**83* Populates an ArrayList of QueryExp.84* Lowest index is 1.85* Highest index is returned by getSize().86* <br>The queries numbered 1 to 23 allow to cover all the underlying87* Java classes of the JMX API used to build queries.88*/89public void buildQueries() {90if ( queries.size() == 0 ) {91int smallerIntValue = intValue - 1;92int biggerIntValue = intValue + 1;9394// case 1:95// True if the MBean is of class mbeanClassName96// We cover javax.management.InstanceOfQueryExp97queries.add(Query.isInstanceOf(Query.value(mbeanClassName)));9899// case 2:100// True if the MBean is of class mbeanClassName101// We cover javax.management.MatchQueryExp and102// javax.management.ClassAttributeValueExp103queries.add(Query.match(Query.classattr(),104Query.value(mbeanClassName)));105106// case 3:107// True if an attribute named primitiveIntAttName of type int has108// the value intValue109// We cover javax.management.BinaryRelQueryExp with110// a relOp equal to EQ and javax.management.NumericValueExp111queries.add(Query.eq(Query.attr(primitiveIntAttName),112Query.value(intValue)));113114// case 4:115// True if an attribute named primitiveLongAttName of type long has116// the value longValue117// We cover javax.management.BinaryRelQueryExp with118// a relOp equal to EQ and javax.management.NumericValueExp119queries.add(Query.eq(Query.attr(primitiveLongAttName),120Query.value(longValue)));121122// case 5:123// True if an attribute named primitiveDoubleAttName of type double124// has the value doubleValue125// We cover javax.management.BinaryRelQueryExp with126// a relOp equal to EQ and javax.management.NumericValueExp127queries.add(Query.eq(Query.attr(primitiveDoubleAttName),128Query.value(doubleValue)));129130// case 6:131// True if an attribute named primitiveFloatAttName of type float132// has the value floatValue133// We cover javax.management.BinaryRelQueryExp with134// a relOp equal to EQ and javax.management.NumericValueExp135queries.add(Query.eq(Query.attr(primitiveFloatAttName),136Query.value(floatValue)));137138// case 7:139// True if an attribute named primitiveIntAttName of type int is140// hold by an MBean of class mbeanClassName and has141// the value intValue142// We cover javax.management.QualifiedAttributeValueExp143queries.add(Query.eq(Query.attr(mbeanClassName, primitiveIntAttName),144Query.value(intValue)));145146// case 8:147// True if an attribute named stringAttName of type String has148// the value stringValue149// We cover javax.management.BinaryRelQueryExp with150// a relOp equal to EQ and javax.management.StringValueExp151queries.add(Query.eq(Query.attr(stringAttName),152Query.value(stringValue)));153154// case 9:155// True if an attribute named integerAttName of type Integer has156// the value integerValue157// We cover javax.management.BinaryRelQueryExp with158// a relOp equal to EQ and javax.management.NumericValueExp159queries.add(Query.eq(Query.attr(integerAttName),160Query.value(integerValue)));161162// case 10:163// True if an attribute named primitiveBooleanAttName of type boolean164// has the value booleanValue165// We cover javax.management.BinaryRelQueryExp with166// a relOp equal to EQ and javax.management.BooleanValueExp167queries.add(Query.eq(Query.attr(primitiveBooleanAttName),168Query.value(booleanValue)));169170// case 11:171// True if an attribute named primitiveIntAttName of type int has172// not the value smallerIntValue173// We cover javax.management.NotQueryExp174queries.add(Query.not(Query.eq(Query.attr(primitiveIntAttName),175Query.value(smallerIntValue))));176177// case 12:178// True if either179// an attribute named primitiveIntAttName of type int has180// the value intValue181// or182// an attribute named primitiveLongAttName of type long has183// the value longValue184// We cover javax.management.OrQueryExp185queries.add(Query.or(186Query.eq(Query.attr(primitiveIntAttName),187Query.value(intValue)),188Query.eq(Query.attr(primitiveLongAttName),189Query.value(longValue))));190191// case 13:192// True if193// an attribute named primitiveIntAttName of type int has194// the value intValue195// and196// an attribute named primitiveLongAttName of type long has197// the value longValue198// We cover javax.management.AndQueryExp199queries.add(Query.and(200Query.eq(Query.attr(primitiveIntAttName),201Query.value(intValue)),202Query.eq(Query.attr(primitiveLongAttName),203Query.value(longValue))));204205// case 14:206// True if an attribute named primitiveIntAttName of type int has207// the value intValue208// We cover javax.management.InQueryExp209ValueExp[] inArray = {Query.value(intValue)};210queries.add(Query.in(Query.attr(primitiveIntAttName), inArray));211212// case 15:213// True if an attribute named primitiveIntAttName of type int has214// its value in between smallerIntValue and biggerIntValue215// We cover javax.management.BetweenRelQueryExp216queries.add(Query.between(Query.attr(primitiveIntAttName),217Query.value(smallerIntValue),218Query.value(biggerIntValue)));219220// case 16:221// True if an attribute named primitiveIntAttName of type int has222// a value greater than smallerIntValue223// We cover javax.management.BinaryRelQueryExp with224// a relOp equal to GT225queries.add(Query.gt(Query.attr(primitiveIntAttName),226Query.value(smallerIntValue)));227228// case 17:229// True if an attribute named primitiveIntAttName of type int has230// a value greater or equal to smallerIntValue231// We cover javax.management.BinaryRelQueryExp with232// a relOp equal to GE233queries.add(Query.geq(Query.attr(primitiveIntAttName),234Query.value(smallerIntValue)));235236// case 18:237// True if an attribute named primitiveIntAttName of type int has238// a value smaller than biggerIntValue239// We cover javax.management.BinaryRelQueryExp with240// a relOp equal to LT241queries.add(Query.lt(Query.attr(primitiveIntAttName),242Query.value(biggerIntValue)));243244// case 19:245// True if an attribute named primitiveIntAttName of type int has246// a value smaller or equal to biggerIntValue247// We cover javax.management.BinaryRelQueryExp with248// a relOp equal to LE249queries.add(Query.leq(Query.attr(primitiveIntAttName),250Query.value(biggerIntValue)));251252// case 20:253// True if an attribute named primitiveIntAttName of type int has254// a value equal to intValue minus zero255// We cover javax.management.BinaryRelQueryExp with256// a relOp equal to MINUS257queries.add(Query.eq(Query.attr(primitiveIntAttName),258Query.minus(Query.value(intValue), Query.value(0))));259260// case 21:261// True if an attribute named primitiveIntAttName of type int has262// a value equal to intValue plus zero263// We cover javax.management.BinaryRelQueryExp with264// a relOp equal to PLUS265queries.add(Query.eq(Query.attr(primitiveIntAttName),266Query.plus(Query.value(intValue), Query.value(0))));267268// case 22:269// True if an attribute named primitiveIntAttName of type int has270// a value equal to intValue divided by one271// We cover javax.management.BinaryRelQueryExp with272// a relOp equal to DIV273queries.add(Query.eq(Query.attr(primitiveIntAttName),274Query.div(Query.value(intValue), Query.value(1))));275276// case 23:277// True if an attribute named primitiveIntAttName of type int has278// a value equal to intValue multiplicated by one279// We cover javax.management.BinaryRelQueryExp with280// a relOp equal to TIMES281queries.add(Query.eq(Query.attr(primitiveIntAttName),282Query.times(Query.value(intValue), Query.value(1))));283284// case 24:285// That query is a complex one that combines within a big AND286// queries with index 2 to 23 inclusive. But because a List is287// zero based, we must decrement all indexes by 1 when retrieving288// any previously stored query.289QueryExp q2_3 = Query.and(queries.get(2-1), queries.get(3-1));290QueryExp q4_5 = Query.and(queries.get(4-1), queries.get(5-1));291QueryExp q6_7 = Query.and(queries.get(6-1), queries.get(7-1));292QueryExp q8_9 = Query.and(queries.get(8-1), queries.get(9-1));293QueryExp q10_11 = Query.and(queries.get(10-1), queries.get(11-1));294QueryExp q12_13 = Query.and(queries.get(12-1), queries.get(13-1));295QueryExp q14_15 = Query.and(queries.get(14-1), queries.get(15-1));296QueryExp q16_17 = Query.and(queries.get(16-1), queries.get(17-1));297QueryExp q18_19 = Query.and(queries.get(18-1), queries.get(19-1));298QueryExp q20_21 = Query.and(queries.get(20-1), queries.get(21-1));299QueryExp q22_23 = Query.and(queries.get(22-1), queries.get(23-1));300QueryExp q2_5 = Query.and(q2_3, q4_5);301QueryExp q6_9 = Query.and(q6_7, q8_9);302QueryExp q10_13 = Query.and(q10_11, q12_13);303QueryExp q14_17 = Query.and(q14_15, q16_17);304QueryExp q18_21 = Query.and(q18_19, q20_21);305QueryExp q2_9 = Query.and(q2_5, q6_9);306QueryExp q10_17 = Query.and(q10_13, q14_17);307QueryExp q18_23 = Query.and(q18_21, q22_23);308QueryExp q2_17 = Query.and(q2_9, q10_17);309queries.add(Query.and(q2_17, q18_23));310311// case 25:312// Complex query mixing AND and OR.313queries.add(Query.or(q6_9, q18_23));314}315}316317/**318* Returns a QueryExp taken is the ArrayList populated by buildQueries().319* Lowest index is 1.320* Highest index is returned by getSize().321* <br>The queries numbered 1 to 23 allow to cover all the underlying322* Java classes of the JMX API used to build queries.323*/324public QueryExp getQuery(int index) {325return queries.get(index - 1);326}327}328329330