KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > enterprise > admin > util > jmx > AttributeListUtils


1 /*
2  * The contents of this file are subject to the terms
3  * of the Common Development and Distribution License
4  * (the License). You may not use this file except in
5  * compliance with the License.
6  *
7  * You can obtain a copy of the license at
8  * https://glassfish.dev.java.net/public/CDDLv1.0.html or
9  * glassfish/bootstrap/legal/CDDLv1.0.txt.
10  * See the License for the specific language governing
11  * permissions and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL
14  * Header Notice in each file and include the License file
15  * at glassfish/bootstrap/legal/CDDLv1.0.txt.
16  * If applicable, add the following below the CDDL Header,
17  * with the fields enclosed by brackets [] replaced by
18  * you own identifying information:
19  * "Portions Copyrighted [year] [name of copyright owner]"
20  *
21  * Copyright 2006 Sun Microsystems, Inc. All rights reserved.
22  */

23
24 /*
25  * Copyright 2004-2005 Sun Microsystems, Inc. All rights reserved.
26  * Use is subject to license terms.
27  */

28
29 /*
30  * JmxNamedAttributeList.java
31  * $Id: AttributeListUtils.java,v 1.2 2005/12/25 03:53:18 tcfujii Exp $
32  * $Revision: 1.2 $
33  * $Date: 2005/12/25 03:53:18 $
34  * Indentation Information:
35  * 0. Please (try to) preserve these settings.
36  * 1. Tabs are preferred over spaces.
37  * 2. In vi/vim -
38  * :set tabstop=4 :set shiftwidth=4 :set softtabstop=4
39  * 3. In S1 Studio -
40  * 1. Tools->Options->Editor Settings->Java Editor->Tab Size = 4
41  * 2. Tools->Options->Indentation Engines->Java Indentation Engine->Expand Tabs to Spaces = False.
42  * 3. Tools->Options->Indentation Engines->Java Indentation Engine->Number of Spaces per Tab = 4.
43  */

44
45 package com.sun.enterprise.admin.util.jmx;
46
47 import java.util.Iterator JavaDoc;
48 import java.util.Map JavaDoc;
49 import java.util.HashMap JavaDoc;
50 import java.util.ArrayList JavaDoc;
51 import javax.management.AttributeList JavaDoc;
52 import javax.management.Attribute JavaDoc;
53 import java.util.StringTokenizer JavaDoc;
54
55 /** A class that has some useful utility methods to deal with
56  * <ul>
57  * <li> Jmx Attributes </li>
58  * <li> Jmx AttributeLists. </li>
59  * </ul>
60  * It is expected to enhance this class with more utility methods.
61  * @author Kedar.Mhaswade@Sun.Com
62  * @since Sun Java System Application Server 8
63  */

64 public class AttributeListUtils {
65     
66     private AttributeListUtils() {
67         //disallow
68
}
69     
70     /** Checks whether this list contains a JMX {@link Attribute} with given name. Note that
71      * this method will return true if there is at least one attribute with given name.
72      * @param an instance of {@link AttributeList}
73      * @param name a String representing the name of the attribute. The name may not be null.
74      * @return true if there is at least one attribute in the list with given name,
75      * false otherwise
76      * @throws IllegalArgumentException if the attribute list or name is null
77      */

78     public static final boolean containsNamedAttribute(final AttributeList JavaDoc al,
79     final String JavaDoc name) {
80         if (al == null || name == null)
81             throw new IllegalArgumentException JavaDoc ("null arg");
82         boolean contains = false;
83         final Iterator JavaDoc it = al.iterator();
84         while (it.hasNext()) {
85             final Attribute JavaDoc at = (Attribute JavaDoc) it.next();
86             if (name.equals(at.getName())) { //attribute name may not be null - guaranteed
87
contains = true;
88                 break;
89             }
90         }
91         return ( contains );
92     }
93     
94     /** Checks whether an attribute with the name same as that of the given
95      * attribute exists in this list. The given name may not be null.
96      * @param an instance of {@link AttributeList}
97      * @param a an Attribute with a name and a value
98      * @return true if there exists at least one attribute with same name, false
99      * otherwise
100      * @throws IllegalArgumentException if the attribute list or name is null
101      */

102     public static final boolean containsNamedAttribute(final AttributeList JavaDoc al,
103     final Attribute JavaDoc a) {
104         if (al == null || a == null)
105             throw new IllegalArgumentException JavaDoc ("null arg");
106         return ( containsNamedAttribute(al, a.getName()) );
107     }
108     
109     /** Returns the given list as a map of attributes, keyed on the names of the attribute
110      * in the list. The passed argument may not be null. The mappings are between
111      * the names of attributes and attributes themselves.
112      * @param the list of attributes that need to be mapped
113      * @return an instance of {@link Map}
114      * @throws IllegalArgumentException if the argument is null
115      */

116     public static final Map JavaDoc asNameMap(final AttributeList JavaDoc al) {
117         if (al == null) {
118             throw new IllegalArgumentException JavaDoc ("null arg");
119         }
120         final Map JavaDoc m = new HashMap JavaDoc();
121         final Iterator JavaDoc it = al.iterator();
122         while (it.hasNext()) {
123             final Attribute JavaDoc a = (Attribute JavaDoc) it.next();
124             m.put(a.getName(), a);
125         }
126         return ( m );
127     }
128     
129     /** JMX 1.2 specification had a weird limitation that a Dynamic MBean
130      * may not have an attribute whose name is <b> not a valid Java
131      * identifier <b>. This method is a utility method to convert the any
132      * arbitrary name into a String that can be a valid JMX 1.2 attribute.
133      * Every character in the string passed that is neither a Character.isJavaIdentifierStart
134      * nor a Character.isJavaIdentifierPart is replace with a valid character
135      * '_'.
136      * @param a String that represents any non null name
137      * @return a String that represents a name valid for a JMX 1.2 MBean.
138      * @throws IllegalArgumentException if the parameter is null or is of zero length
139      */

140     public static final String JavaDoc toJmx12Attribute(final String JavaDoc name) {
141         if (name == null || name.length() == 0)
142             throw new IllegalArgumentException JavaDoc ("invalid arg");
143         final char rc = '_';
144         assert (Character.isJavaIdentifierStart(rc) && Character.isJavaIdentifierPart(rc));
145         final char[] chars = new char[name.length()];
146         name.getChars(0, name.length(), chars, 0);
147         if (! Character.isJavaIdentifierStart(chars[0])) {
148             chars[0] = rc;
149         }
150         for (int i = 1 ; i < name.length() ; i++) { //note the index
151
if (! Character.isJavaIdentifierPart(chars[i])) {
152                 chars[i] = rc;
153             }
154         }
155         return ( new String JavaDoc(chars) );
156     }
157     
158     /** Returns a String representation of an attribute list such that:
159      * <ul>
160      * <li> Each attribute is a name and value separated by a ','. toString() on the value is called. </li>
161      * <li> Each pair is separated by a new line character '\n'. </li>
162      * </ul>
163      * @param the list of attributes - may be null, in which case an empty String is returned.
164      * @return a String representing the parameter passed
165      */

166     public static final String JavaDoc toString(final AttributeList JavaDoc al) {
167         final StringBuffer JavaDoc sb = new StringBuffer JavaDoc();
168         final char SEP = ',';
169         final char NL = '\n';
170         if (al != null) {
171             final Iterator JavaDoc it = al.iterator();
172             while (it.hasNext()) {
173                 final Attribute JavaDoc a = (Attribute JavaDoc) it.next();
174                 sb.append(a.getName()).append(SEP).append(a.getValue().toString()).append(NL);
175             }
176         }
177         return (sb.toString());
178     }
179     
180     public static String JavaDoc dash2CamelCase(String JavaDoc dashed) {
181         /* This algorithm is obviously not accurate, as I have not written
182          * a generic parser/lexical analyzer, nor have I written a BNF. All it
183          * does is it converts Strings like abc-def-ghi to AbcDefGhi. A hyphen, if followed
184          * by an alphabet, will be removed and the alphabet is capitalized. No more
185          * complications. The passed String is converted into lower case by default.
186          */

187         if (dashed == null)
188             throw new IllegalArgumentException JavaDoc ("Null Arg");
189         dashed = dashed.toLowerCase();
190         final ArrayList JavaDoc list = new ArrayList JavaDoc();
191         final StringTokenizer JavaDoc tz = new StringTokenizer JavaDoc(dashed, "-");
192         while (tz.hasMoreTokens()) {
193             list.add(tz.nextToken());
194         }
195         final String JavaDoc[] tmp = new String JavaDoc[list.size()];
196         final String JavaDoc[] strings = getCamelCaseArray((String JavaDoc[])list.toArray(tmp));
197         return ( strings2String(strings) );
198     }
199     
200     private static String JavaDoc[] getCamelCaseArray(final String JavaDoc[] from) {
201         final String JavaDoc[] humps = new String JavaDoc[from.length];
202         for (int i = 0 ; i < from.length ; i++) {
203             final StringBuffer JavaDoc sb = new StringBuffer JavaDoc(from[i]);
204             sb.setCharAt(0, Character.toUpperCase(sb.charAt(0)));
205             humps[i] = sb.toString();
206         }
207         return ( humps );
208     }
209     private static String JavaDoc strings2String(final String JavaDoc[] a) {
210         final StringBuffer JavaDoc sb = new StringBuffer JavaDoc();
211         for (int i = 0 ; i < a.length ; i++) {
212             sb.append(a[i]);
213         }
214         return ( sb.toString() );
215     }
216 }
Popular Tags