KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > enterprise > admin > meta > naming > MBeanNamingDescriptor


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  * $Id: MBeanNamingDescriptor.java,v 1.3 2005/12/25 03:47:38 tcfujii Exp $
26  * @author: alexkrav
27  *
28  * $Log: MBeanNamingDescriptor.java,v $
29  * Revision 1.3 2005/12/25 03:47:38 tcfujii
30  * Updated copyright text and year.
31  *
32  * Revision 1.2 2005/06/27 21:19:44 tcfujii
33  * Issue number: CDDL header updates.
34  *
35  * Revision 1.1.1.1 2005/05/27 22:52:02 dpatil
36  * GlassFish first drop
37  *
38  * Revision 1.10 2005/02/02 19:15:56 kedar
39  * BugId: 6219838. Additional Files for JDK 1.5 upgrade -- Note that from now onwards, we would need JDK 1.5 to run the installer
40  *
41  * Revision 1.9 2004/11/14 07:04:22 tcfujii
42  * Updated copyright text and/or year.
43  *
44  * Revision 1.8 2004/02/20 03:56:16 qouyang
45  *
46  *
47  * First pass at code merge.
48  *
49  * Details for the merge will be published at:
50  * http://javaweb.sfbay.sun.com/~qouyang/workspace/PE8FCSMerge/02202004/
51  *
52  * Revision 1.7.4.1 2004/02/02 07:25:20 tcfujii
53  * Copyright updates notices; reviewer: Tony Ng
54  *
55  * Revision 1.7 2003/08/15 23:08:29 kravtch
56  * DottedName Support (generation and call to manager)
57  * notifyRegisterMBean/UnregisterMBean are implemented;
58  * dotted name related opeartions are added to NaminDescriptor and NamingInfo
59  * removeChild support is added;
60  *
61  * Revision 1.6 2003/06/25 20:03:41 kravtch
62  * 1. java file headers modified
63  * 2. properties handling api is added
64  * 3. fixed bug for xpathes containing special symbols;
65  * 4. new testcases added for jdbc-resource
66  * 5. introspector modified by not including base classes operations;
67  *
68  *
69 */

70
71 package com.sun.enterprise.admin.meta.naming;
72
73 //import com.iplanet.ias.admin.util.Debug;
74

75
76
77 //JMX imports
78
import javax.management.ObjectName JavaDoc;
79 import javax.management.MalformedObjectNameException JavaDoc;
80
81 import java.lang.reflect.Constructor JavaDoc;
82 import java.util.ArrayList JavaDoc;
83 import java.util.Hashtable JavaDoc;
84 import java.util.Enumeration JavaDoc;
85 import java.text.MessageFormat JavaDoc;
86
87 //i18n import
88
//import com.iplanet.ias.util.i18n.StringManager;
89

90
91 /**
92  * Provides naming support for Mbeans
93  */

94 public class MBeanNamingDescriptor
95 {
96     //int m_category;
97
String JavaDoc m_type;
98     String JavaDoc m_className;
99     String JavaDoc m_objectPattern;
100     String JavaDoc[] m_dottedPatterns;
101     String JavaDoc m_xpathPattern;
102     int m_mode;
103     
104     //work objects
105
int m_parmListSize = 0;
106     Object JavaDoc[][] m_dottedTokens = null;
107     Object JavaDoc[] m_objectTokens = null;
108     Object JavaDoc[] m_xpathTokens = null;
109
110 // // i18n StringManager
111
// private static StringManager localStrings =
112
// StringManager.getManager( MBeanNamingDescriptor.class );
113

114     public MBeanNamingDescriptor(Object JavaDoc[] description) throws MBeanNamingException
115     {
116         this((String JavaDoc)description[0], (Integer JavaDoc)description[1], (String JavaDoc)description[2], (String JavaDoc)description[3], (String JavaDoc)description[4], (String JavaDoc)description[5]);
117     }
118     
119     public MBeanNamingDescriptor(String JavaDoc type, Integer JavaDoc mode, String JavaDoc dottedPatterns,
120                String JavaDoc objectPattern, String JavaDoc xpathPattern, String JavaDoc className) throws MBeanNamingException
121     {
122         m_type = type;
123         m_className = className;
124         m_dottedPatterns = splitDottedPatternsString(dottedPatterns);
125         m_xpathPattern = xpathPattern;
126         m_objectPattern = objectPattern;
127         m_mode = mode.intValue();
128         
129         try
130         {
131             if(m_dottedPatterns!=null)
132             {
133                 m_dottedTokens = new Object JavaDoc[m_dottedPatterns.length][];
134                 for(int i=0; i<m_dottedPatterns.length; i++)
135                     m_dottedTokens[i] = getDottedNamePatternTokens(m_dottedPatterns[i]);
136             }
137             m_objectTokens = getObjectNamePatternTokens(m_objectPattern);
138             m_xpathTokens = getXPathTokens(m_xpathPattern);
139             m_parmListSize = getMaxTokenIndex(m_objectTokens) + 1;
140             
141             checkConsistency();
142         }
143         catch(Exception JavaDoc e)
144         {
145             String JavaDoc msg = /*localStrings.getString*/( "admin.server.core.mbean.config.naming.mbeandescriptor_creation_failure_for_object_pattern"+ objectPattern+ e.getMessage() );
146             throw new MBeanNamingException( msg );
147         }
148     }
149     
150     private void checkConsistency() throws MBeanNamingException
151     {
152     }
153     
154
155     //DOTTED NAME SERVICES
156
boolean isDottedPatternMatch(Name name)
157     {
158         if(findDottedPatternTokens(name)!=null)
159             return true;
160         return false;
161     }
162
163     private Object JavaDoc[] findDottedPatternTokens(Name name)
164     {
165         if(m_dottedTokens!=null)
166         {
167             for(int i=0; i<m_dottedTokens.length; i++)
168             {
169                 if(isDottedPatternMatch(name, m_dottedTokens[i], true))
170                     return m_dottedTokens[i];
171             }
172         }
173         return null;
174     }
175     
176     //**************************************************************************
177
public boolean isObjectNamePatternMatch(ObjectName JavaDoc objectName)
178     {
179        Hashtable JavaDoc ht = objectName.getKeyPropertyList();
180        ht.put(":",objectName.getDomain()); //add domain name pseudo pair
181
return isObjectNamePatternMatch(ht);
182     }
183
184     //**************************************************************************
185
public boolean isObjectNamePatternMatch(Hashtable JavaDoc sample)
186     {
187         if(m_objectTokens.length!=(sample.size()*2))
188             return false;
189         for(int i=0; i<m_objectTokens.length; i = i+2)
190         {
191             String JavaDoc sampleVal = (String JavaDoc)sample.get(m_objectTokens[i]);
192             if(sampleVal==null ||
193                ((m_objectTokens[i+1] instanceof String JavaDoc) && !sampleVal.equals((String JavaDoc)m_objectTokens[i+1])) )
194                 return false;
195         }
196         return (true);
197     }
198     
199     
200     String JavaDoc[] extractParmList(String JavaDoc dottedName) throws MalformedObjectNameException JavaDoc
201     {
202         if(m_dottedTokens==null)
203             return null;
204         Name name = new Name(dottedName);
205         Object JavaDoc[] tokens = findDottedPatternTokens(name);
206         if(tokens == null)
207             return null;
208         int nTokens = name.getNumParts();
209         if(name.getNumParts()!=tokens.length)
210             return null;
211         
212         String JavaDoc[] parmList = new String JavaDoc[m_parmListSize];
213         
214         for(int i=0; i<nTokens; i++)
215         {
216             if( tokens[i] instanceof Integer JavaDoc )
217             {
218                 parmList[((Integer JavaDoc)tokens[i]).intValue()] = name.getNamePart(i).toString();
219             }
220         }
221         return parmList;
222     }
223     
224     
225     
226     private Object JavaDoc[] getDottedNamePatternTokens(String JavaDoc dottedPattern) throws MalformedObjectNameException JavaDoc
227     {
228         ArrayList JavaDoc list = new ArrayList JavaDoc();
229         int idx = 0, idx2 = 0;
230         
231         if(dottedPattern!=null)
232         {
233             while(idx<dottedPattern.length() && (idx2=dottedPattern.indexOf('.', idx))>=0)
234             {
235                 if(idx == idx2)
236                     list.add("");
237                 else
238                     list.add(dottedPattern.substring(idx,idx2).trim());
239                 idx = idx2+1;
240             }
241             if(idx<dottedPattern.length())
242                 list.add(dottedPattern.substring(idx).trim());
243             Object JavaDoc[] tokens = list.toArray();
244             replacePlaceholdersToIntegers(tokens);
245             return tokens;
246         }
247         return null;
248     }
249         
250     //XPath services
251
public static Object JavaDoc[] getXPathTokens(String JavaDoc xpathPattern)
252     {
253         ArrayList JavaDoc list = new ArrayList JavaDoc();
254         int idx = 0, idx2 = 0;
255         
256         if(xpathPattern!=null)
257         {
258             while(idx<xpathPattern.length() && (idx2=xpathPattern.indexOf("'", idx))>=0)
259             {
260                 if(idx != idx2)
261                     list.add(xpathPattern.substring(idx,idx2));
262                 idx = idx2+1;
263             }
264             if(idx<xpathPattern.length())
265                 list.add(xpathPattern.substring(idx).trim());
266             Object JavaDoc[] tokens = list.toArray();
267             replacePlaceholdersToIntegers(tokens);
268             return tokens;
269         }
270         return null;
271     }
272
273     //ObjectName services
274
private Object JavaDoc[] getObjectNamePatternTokens(String JavaDoc objectPattern) throws MalformedObjectNameException JavaDoc
275     {
276         if(objectPattern!=null)
277         {
278             ObjectName JavaDoc objName = new ObjectName JavaDoc(objectPattern);
279             Hashtable JavaDoc ht = objName.getKeyPropertyList();
280             ht.put(":",objName.getDomain()); //add domain name pseudo pair
281
Enumeration JavaDoc ee = ht.keys();
282             Object JavaDoc[] tokens = new Object JavaDoc[ht.size()*2];
283             int i = 0;
284             while(ee.hasMoreElements())
285             {
286                 String JavaDoc key = ((String JavaDoc)ee.nextElement());
287                 tokens[i++] = key; //key.trim(); jmx allows to have spaces in keys and values
288
tokens[i++] = ht.get(key);
289             }
290             replacePlaceholdersToIntegers(tokens);
291             return tokens;
292         }
293         return null;
294     }
295     
296     public int getParmListSize()
297     {
298         return m_parmListSize;
299     }
300     public String JavaDoc[] extractParmList(ObjectName JavaDoc objectName)
301     {
302         if(m_objectTokens==null)
303             return null;
304         Hashtable JavaDoc ht = objectName.getKeyPropertyList();
305         ht.put(":",objectName.getDomain()); //add domain name pseudo pair
306
String JavaDoc[] parmList = new String JavaDoc[m_parmListSize];
307         for(int i=0; i<m_objectTokens.length; i=i+2)
308         {
309             if( m_objectTokens[i+1] instanceof Integer JavaDoc )
310             {
311                 parmList[((Integer JavaDoc)m_objectTokens[i+1]).intValue()] = (String JavaDoc)ht.get(m_objectTokens[i]);
312             }
313         }
314         return parmList;
315     }
316     
317     static private void replacePlaceholdersToIntegers(Object JavaDoc[] tokens)
318     {
319         for(int i=0; i<tokens.length; i++)
320         {
321             Object JavaDoc idx = getIndexForPlaceholder((String JavaDoc)tokens[i]);
322             if(idx!=null)
323                 tokens[i] = idx;
324         }
325     }
326     
327     static private Integer JavaDoc getIndexForPlaceholder(String JavaDoc str)
328     {
329         int len = str.length();
330         if(len<3 || str.charAt(0)!='{' || str.charAt(len-1)!='}')
331             return null;
332         try
333         {
334             return Integer.valueOf(str.substring(1,len-1));
335         }
336         catch(Throwable JavaDoc e)
337         {
338         }
339         return null;
340     }
341     
342     private int getMaxTokenIndex(Object JavaDoc[] tokens)
343     {
344         int res = -1;
345         int current;
346         for(int i=0; i<tokens.length; i++)
347         {
348             if(tokens[i] instanceof Integer JavaDoc &&
349             res < (current=((Integer JavaDoc)tokens[i]).intValue()))
350                 res = current;
351         }
352         return res;
353     }
354     
355     public String JavaDoc getMBeanClassName()
356     {
357         return m_className;
358     }
359     
360     public String JavaDoc getType()
361     {
362         return m_type;
363     }
364     
365     public int getMode()
366     {
367         return m_mode;
368     }
369     
370     public String JavaDoc[] getDottedPatterns()
371     {
372         return m_dottedPatterns;
373     }
374     
375     public Object JavaDoc[][] getDottedTokens()
376     {
377         return m_dottedTokens;
378     }
379
380     public String JavaDoc getXPathPattern()
381     {
382         return m_xpathPattern;
383     }
384     
385     public ObjectName JavaDoc createObjectName(Object JavaDoc[] params) throws MalformedObjectNameException JavaDoc
386     {
387         return new ObjectName JavaDoc(formatPattern(m_objectPattern, params));
388     }
389     public String JavaDoc[] createDottedNames(Object JavaDoc[] params)
390     {
391         if(m_dottedPatterns==null || m_dottedPatterns.length<1)
392             return null;
393         String JavaDoc[] names = new String JavaDoc[m_dottedPatterns.length];
394         for(int i=0; i<m_dottedPatterns.length;i++)
395            names[i] = formatPattern(m_dottedPatterns[i], params);
396         return names;
397     }
398     public String JavaDoc createXPath(Object JavaDoc[] params)
399     {
400         return formatPattern(m_xpathPattern, params);
401     }
402     private String JavaDoc formatPattern(String JavaDoc pattern, Object JavaDoc[] params)
403     {
404         if(pattern==null)
405             return null;
406         return MessageFormat.format(pattern, params);
407     }
408
409     private String JavaDoc[] splitDottedPatternsString(String JavaDoc names)
410     {
411         if(names==null)
412             return null;
413         ArrayList JavaDoc list = new ArrayList JavaDoc();
414         int idx = 0, idx2 = 0;
415         while(idx<names.length() && (idx2=names.indexOf(MBeansNaming.PATTERNS_SEPARATOR, idx))>=0)
416         {
417             if(idx2!=idx)
418                 list.add(names.substring(idx, idx2));
419             idx = idx2+1;
420         }
421         if(idx2<0)
422             list.add(names.substring(idx));
423         return (String JavaDoc[])list.toArray(new String JavaDoc[list.size()]);
424     }
425
426     public boolean isXpathTokensMatch(Object JavaDoc[] tokens)
427     {
428         if(m_xpathTokens==null || tokens==null)
429             return false;
430         if(m_xpathTokens.length!= tokens.length)
431             return false;
432         for(int i=0; i<tokens.length; i++)
433         {
434             if( (m_xpathTokens[i] instanceof String JavaDoc) &&
435             !m_xpathTokens[i].equals(tokens[i]))
436                 return false;
437         }
438         return true;
439     }
440     public String JavaDoc[] extractParmListFromXPath(String JavaDoc xpath)
441     {
442         Object JavaDoc[] tokens = MBeanNamingDescriptor.getXPathTokens(xpath);
443         if(m_xpathTokens==null || tokens==null || m_parmListSize<=0)
444             return null;
445         if(m_xpathTokens.length!= tokens.length)
446             return null;
447         String JavaDoc[] parmList = new String JavaDoc[m_parmListSize];
448         for(int i=0; i<tokens.length; i++)
449         {
450             if(m_xpathTokens[i] instanceof Integer JavaDoc)
451             {
452                 parmList[((Integer JavaDoc)m_xpathTokens[i]).intValue()] = (String JavaDoc)tokens[i];
453             }
454         }
455         return parmList;
456     }
457     
458     public static boolean isDottedPatternMatch(Name name, Object JavaDoc[] tokens, boolean bExactMatch)
459     {
460         if(tokens==null)
461             return false;
462         int nTokens = name.getNumParts();
463         if(bExactMatch)
464         {
465             if(nTokens!=tokens.length)
466                 return false;
467         }
468         else
469         {
470             if(nTokens>tokens.length)
471                 return false;
472         }
473         for(int i=0; i<nTokens; i++)
474         {
475             if( (tokens[i] instanceof String JavaDoc) &&
476             !name.getNamePart(i).toString().equals((String JavaDoc)tokens[i]))
477                 return false;
478         }
479         return true;
480     }
481
482     public static String JavaDoc XPATH_TO_MASK(String JavaDoc xpath)
483     {
484         if(xpath==null || xpath.length()==0)
485             return xpath;
486         
487         char[] chrs = xpath.toCharArray();
488         char[] newchrs = new char[chrs.length*2];
489         int j = 0;
490         for(int i=0; i<chrs.length; i++)
491         {
492             newchrs[j++] = chrs[i];
493             if(chrs[i]=='\'')
494                 newchrs[j++] = '\'';
495         }
496         return String.valueOf(newchrs, 0, j);
497     }
498     
499 }
500
Popular Tags