001: /**
002: * The XMOJO Project 5
003: * Copyright © 2003 XMOJO.org. All rights reserved.
004:
005: * NO WARRANTY
006:
007: * BECAUSE THE LIBRARY IS LICENSED FREE OF CHARGE, THERE IS NO WARRANTY FOR
008: * THE LIBRARY, TO THE EXTENT PERMITTED BY APPLICABLE LAW. EXCEPT WHEN
009: * OTHERWISE STATED IN WRITING THE COPYRIGHT HOLDERS AND/OR OTHER PARTIES
010: * PROVIDE THE LIBRARY "AS IS" WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESSED
011: * OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
012: * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE ENTIRE RISK AS
013: * TO THE QUALITY AND PERFORMANCE OF THE LIBRARY IS WITH YOU. SHOULD THE
014: * LIBRARY PROVE DEFECTIVE, YOU ASSUME THE COST OF ALL NECESSARY SERVICING,
015: * REPAIR OR CORRECTION.
016:
017: * IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING WILL
018: * ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MAY MODIFY AND/OR REDISTRIBUTE
019: * THE LIBRARY AS PERMITTED ABOVE, BE LIABLE TO YOU FOR DAMAGES, INCLUDING ANY
020: * GENERAL, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING OUT OF THE
021: * USE OR INABILITY TO USE THE LIBRARY (INCLUDING BUT NOT LIMITED TO LOSS OF
022: * DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY YOU OR THIRD
023: * PARTIES OR A FAILURE OF THE LIBRARY TO OPERATE WITH ANY OTHER SOFTWARE),
024: * EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE POSSIBILITY OF
025: * SUCH DAMAGES.
026: **/package com.adventnet.jmx.utils;
027:
028: import java.util.Enumeration;
029: import java.util.Hashtable;
030:
031: import javax.management.ObjectName;
032:
033: /**
034: * The internal utility class used by the jmx extension package classes.
035: * This contains the pattern matching static methods useful for ObjectName
036: * pattern matching.
037: */
038: public class JmxUtilities {
039: public JmxUtilities() {
040: }
041:
042: public static boolean followsObjectNamePattern(String string,
043: String pattern) throws Exception {
044: ObjectName patON = new ObjectName("tempDomain:" + pattern);
045: if (patON.isPropertyPattern())
046: return true;
047:
048: return false;
049: }
050:
051: public static boolean objectNameSpecificChecks(String string,
052: String pattern) throws Exception {
053: ObjectName stringON = new ObjectName("tempDomain:" + string);
054: ObjectName patON = new ObjectName("tempDomain:" + pattern);
055:
056: Hashtable patProps = patON.getKeyPropertyList();
057: Hashtable stringProps = stringON.getKeyPropertyList();
058:
059: for (Enumeration e = patProps.keys(); e.hasMoreElements();) {
060: String key = (String) e.nextElement();
061: String value = (String) patProps.get(key);
062:
063: if (stringProps.get(key) != null
064: && stringProps.get(key).equals(value))
065: ;
066: else
067: return false;
068:
069: }
070: return true;
071:
072: }
073:
074: public static boolean checkPattern(String info, String pat,
075: boolean wc) {
076: boolean returnFlag;
077:
078: if (!wc) {
079: return info.equals(pat);
080: }
081:
082: try {
083: if (followsObjectNamePattern(info, pat))
084: return objectNameSpecificChecks(info, pat);
085: } catch (Exception e) {
086: }
087:
088: returnFlag = wildcardMatch(pat, info);
089:
090: return returnFlag;
091: }
092:
093: public static boolean wildCPattern(String p_string, String p_pattern) {
094: char c;
095: char p;
096: boolean retval;
097:
098: for (;;) {
099: if (p_pattern.length() == 0) /* End of pattern. If end of string return true */
100: {
101: if (p_string.length() == 0) {
102: return true;
103: } else {
104: return false;
105: }
106: }
107:
108: if (p_string.length() == 0) {
109: /* Critical condition */
110: return false;
111: }
112:
113: p = p_pattern.charAt(0);
114: p_pattern = p_pattern.substring(1);
115:
116: if (p == '$') {
117: if (p_pattern.length() == 0) {
118: /* Critical condition */
119: return false;
120: }
121:
122: p = p_pattern.charAt(0);
123: p_pattern = p_pattern.substring(1);
124: //System.out.println("P before switch is " + p);
125:
126: switch (p) {
127: case '*':
128: while (p_string.length() != 0) {
129: p_string = p_string.substring(1);
130: //System.out.println("PString is " + p_string);
131:
132: /* Match zero or more char */
133: retval = wildCPattern(p_string, p_pattern);
134:
135: if (retval == true) {
136: //System.out.println("Returning as retVal true\n");
137: return true;
138: }
139: }
140:
141: retval = wildCPattern(p_string, p_pattern);
142:
143: return retval;
144:
145: case '?':
146: /* match any one char */
147: if (p_string.length() == 0) {
148: /* not end of string */
149: //System.out.println("Returning as CRITICAL condition III\n");
150: return false;
151: }
152: p_string = p_string.substring(1);
153:
154: break;
155: case '$': /* check for $ character */
156: p_string = p_string.substring(1);
157: c = p_string.charAt(0);
158: //System.out.println("C is " + c);
159: if (c != '$') {
160: return false;
161: }
162: break;
163: default: /* Wild Character Not Found */
164: //System.out.println("Returning as NO_WILD CARD character found\n");
165: return false;
166: } /* End of switch */
167: } else {
168: c = p_string.charAt(0);
169: p_string = p_string.substring(1);
170: if (c != p) /* check for exact char */
171: {
172: //System.out.println("Returning as not match condition %c "+c + "\n");
173: return false; /* not a match */
174: }
175: }
176:
177: } /* End of For */
178:
179: }
180:
181: public static boolean wildcardMatch(String pattern, String string) {
182: int stringLength = string.length();
183: int stringIndex = 0;
184: for (int patternIndex = 0; patternIndex < pattern.length(); ++patternIndex) {
185: char c = pattern.charAt(patternIndex);
186: if (c == '*') {
187: // Recurse with the pattern without this '*' and the actual string, until
188: // match is found or we inspected the whole string
189: while (stringIndex < stringLength) {
190: if (wildcardMatch(pattern
191: .substring(patternIndex + 1), string
192: .substring(stringIndex))) {
193: return true;
194: }
195: // No match found, try a shorter string, since we are matching '*'
196: ++stringIndex;
197: }
198: } else if (c == '?') {
199: // Increment the string index, since '?' match a single char in the string
200: ++stringIndex;
201: if (stringIndex > stringLength) {
202: return false;
203: }
204: } else {
205: // A normal character in the pattern, must match the one in the string
206: if (stringIndex >= stringLength
207: || c != string.charAt(stringIndex)) {
208: return false;
209: }
210: ++stringIndex;
211: }
212: }
213:
214: // I've inspected the whole pattern, but not the whole string
215: return stringIndex == stringLength;
216: }
217: }
|