KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > enterprise > admin > server > core > mbean > config > 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     PROPRIETARY/CONFIDENTIAL. Use of this product is subject
26     to license terms. Copyright (c) 2002 Sun Microsystems, Inc.
27         All rights reserved.
28  
29     $Id: MBeanNamingDescriptor.java,v 1.4 2005/12/25 04:14:34 tcfujii Exp $
30  */

31
32 package com.sun.enterprise.admin.server.core.mbean.config.naming;
33
34 //import com.sun.enterprise.admin.util.Debug;
35
import com.sun.enterprise.admin.common.Name;
36 import com.sun.enterprise.admin.common.MalformedNameException;
37
38 import com.sun.enterprise.admin.common.exception.MBeanConfigException;
39
40 //JMX imports
41
import javax.management.ObjectName JavaDoc;
42 import javax.management.MalformedObjectNameException JavaDoc;
43
44 import java.lang.reflect.Constructor JavaDoc;
45 import java.util.ArrayList JavaDoc;
46 import java.util.Hashtable JavaDoc;
47 import java.util.Enumeration JavaDoc;
48 import java.text.MessageFormat JavaDoc;
49
50 //i18n import
51
import com.sun.enterprise.util.i18n.StringManager;
52
53
54 /**
55  * Provides naming support for ConfigMbeans
56  */

57 public class MBeanNamingDescriptor
58 {
59     String JavaDoc m_type;
60     String JavaDoc m_className;
61     String JavaDoc m_objectPattern;
62     String JavaDoc[] m_dottedPatterns;
63     String JavaDoc m_xpathPattern;
64     int m_mode;
65     
66     //work objects
67
int m_parmListSize = 0;
68     Object JavaDoc[][] m_dottedTokens = null;
69     Object JavaDoc[] m_objectTokens = null;
70
71     // i18n StringManager
72
private static StringManager localStrings =
73         StringManager.getManager( MBeanNamingDescriptor.class );
74     
75     public MBeanNamingDescriptor(Object JavaDoc[] description) throws MBeanConfigException
76     {
77         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]);
78     }
79     
80     public MBeanNamingDescriptor(String JavaDoc type, Integer JavaDoc mode, String JavaDoc dottedPatterns, String JavaDoc objectPattern, String JavaDoc xpathPattern, String JavaDoc className) throws MBeanConfigException
81     {
82         m_type = type;
83         m_className = className;
84         m_dottedPatterns = splitDottedPatternsString(dottedPatterns);
85         m_xpathPattern = xpathPattern;
86         m_objectPattern = objectPattern;
87         m_mode = mode.intValue();
88         
89         try
90         {
91             m_dottedTokens = new Object JavaDoc[m_dottedPatterns.length][];
92             for(int i=0; i<m_dottedPatterns.length; i++)
93                 m_dottedTokens[i] = getDottedNamePatternTokens(m_dottedPatterns[i]);
94             m_objectTokens = getObjectNamePatternTokens(m_objectPattern);
95             m_parmListSize = getMaxTokenIndex(m_objectTokens) + 1;
96             
97             checkConsistency();
98         }
99         catch(Exception JavaDoc e)
100         {
101             String JavaDoc msg = localStrings.getString( "admin.server.core.mbean.config.naming.mbeandescriptor_creation_failure_for_object_pattern", objectPattern, e.getMessage() );
102             throw new MBeanConfigException( msg );
103         }
104     }
105     
106     private void checkConsistency() throws MBeanConfigException
107     {
108     }
109     
110     //DOTTED NAME SERVICES
111
boolean isDottedPatternMatch(Name name)
112     {
113         if(findDottedPatternTokens(name)!=null)
114             return true;
115         return false;
116     }
117
118     private Object JavaDoc[] findDottedPatternTokens(Name name)
119     {
120         for(int i=0; i<m_dottedTokens.length; i++)
121         {
122             if(isDottedPatternMatch(name, m_dottedTokens[i], true))
123                 return m_dottedTokens[i];
124         }
125         return null;
126     }
127     
128     boolean isObjectNamePatternMatch(Hashtable JavaDoc sample)
129     {
130         if(m_objectTokens.length!=(sample.size()*2))
131             return false;
132         for(int i=0; i<m_objectTokens.length; i = i+2)
133         {
134             String JavaDoc sampleVal = (String JavaDoc)sample.get(m_objectTokens[i]);
135             if(sampleVal==null ||
136                ((m_objectTokens[i+1] instanceof String JavaDoc) && !sampleVal.equals((String JavaDoc)m_objectTokens[i+1])) )
137                 return false;
138         }
139         return (true);
140     }
141     
142     
143     String JavaDoc[] extractParmList(String JavaDoc dottedName) throws MalformedNameException
144     {
145         if(m_dottedTokens==null)
146             return null;
147         Name name = new Name(dottedName);
148         Object JavaDoc[] tokens = findDottedPatternTokens(name);
149         if(tokens == null)
150             return null;
151         int nTokens = name.getNumParts();
152         if(name.getNumParts()!=tokens.length)
153             return null;
154         
155         String JavaDoc[] parmList = new String JavaDoc[m_parmListSize];
156         
157         for(int i=0; i<nTokens; i++)
158         {
159             if( tokens[i] instanceof Integer JavaDoc )
160             {
161                 parmList[((Integer JavaDoc)tokens[i]).intValue()] = name.getNamePart(i).toString();
162             }
163         }
164         return parmList;
165     }
166     
167     
168     
169     private Object JavaDoc[] getDottedNamePatternTokens(String JavaDoc dottedPattern) throws MalformedNameException
170     {
171         ArrayList JavaDoc list = new ArrayList JavaDoc();
172         int idx = 0, idx2 = 0;
173         
174         if(dottedPattern!=null)
175         {
176             while(idx<dottedPattern.length() && (idx2=dottedPattern.indexOf('.', idx))>=0)
177             {
178                 if(idx == idx2)
179                     list.add("");
180                 else
181                     list.add(dottedPattern.substring(idx,idx2).trim());
182                 idx = idx2+1;
183             }
184             if(idx<dottedPattern.length())
185                 list.add(dottedPattern.substring(idx).trim());
186             Object JavaDoc[] tokens = list.toArray();
187             replacePlaceholdersToIntegers(tokens);
188             return tokens;
189         }
190         return null;
191     }
192     
193     
194     //ObjectName services
195
private Object JavaDoc[] getObjectNamePatternTokens(String JavaDoc objectPattern) throws MalformedObjectNameException JavaDoc
196     {
197         if(objectPattern!=null)
198         {
199             ObjectName JavaDoc objName = new ObjectName JavaDoc(objectPattern);
200             Hashtable JavaDoc ht = objName.getKeyPropertyList();
201             Enumeration JavaDoc e = ht.keys();
202             Object JavaDoc[] tokens = new Object JavaDoc[ht.size()*2];
203             int i = 0;
204             while(e.hasMoreElements())
205             {
206                 String JavaDoc key = (String JavaDoc)e.nextElement();
207                 tokens[i++] = key;
208                 tokens[i++] = ht.get(key);
209             }
210             replacePlaceholdersToIntegers(tokens);
211             return tokens;
212         }
213         return null;
214     }
215     
216     public int getParmListSize()
217     {
218         return m_parmListSize;
219     }
220     public String JavaDoc[] extractParmList(ObjectName JavaDoc objectName)
221     {
222         if(m_objectTokens==null)
223             return null;
224         Hashtable JavaDoc ht = objectName.getKeyPropertyList();
225         String JavaDoc[] parmList = new String JavaDoc[m_parmListSize];
226         for(int i=0; i<m_objectTokens.length; i=i+2)
227         {
228             if( m_objectTokens[i+1] instanceof Integer JavaDoc )
229             {
230                 parmList[((Integer JavaDoc)m_objectTokens[i+1]).intValue()] = (String JavaDoc)ht.get(m_objectTokens[i]);
231             }
232         }
233         return parmList;
234     }
235     
236     private void replacePlaceholdersToIntegers(Object JavaDoc[] tokens)
237     {
238         for(int i=0; i<tokens.length; i++)
239         {
240             Object JavaDoc idx = getIndexForPlaceholder((String JavaDoc)tokens[i]);
241             if(idx!=null)
242                 tokens[i] = idx;
243         }
244     }
245     
246     private Integer JavaDoc getIndexForPlaceholder(String JavaDoc str)
247     {
248         int len = str.length();
249         if(len<3 || str.charAt(0)!='{' || str.charAt(len-1)!='}')
250             return null;
251         try
252         {
253             return Integer.valueOf(str.substring(1,len-1));
254         }
255         catch(Throwable JavaDoc e)
256         {
257         }
258         return null;
259     }
260     
261     private int getMaxTokenIndex(Object JavaDoc[] tokens)
262     {
263         int res = -1;
264         int current;
265         for(int i=0; i<tokens.length; i++)
266         {
267             if(tokens[i] instanceof Integer JavaDoc &&
268             res < (current=((Integer JavaDoc)tokens[i]).intValue()))
269                 res = current;
270         }
271         return res;
272     }
273     
274     public String JavaDoc getMBeanClassName()
275     {
276         return m_className;
277     }
278     
279     public String JavaDoc getType()
280     {
281         return m_type;
282     }
283     
284     public int getMode()
285     {
286         return m_mode;
287     }
288     
289     public String JavaDoc[] getDottedPatterns()
290     {
291         return m_dottedPatterns;
292     }
293     
294     public Object JavaDoc[][] getDottedTokens()
295     {
296         return m_dottedTokens;
297     }
298
299     public String JavaDoc getXPathPattern()
300     {
301         return m_xpathPattern;
302     }
303     
304     public ObjectName JavaDoc createObjectName(Object JavaDoc[] params) throws MalformedObjectNameException JavaDoc
305     {
306         return new ObjectName JavaDoc(formatPattern(m_objectPattern, params));
307     }
308     public String JavaDoc createDottedNameName(Object JavaDoc[] params)
309     {
310         if(m_dottedPatterns==null || m_dottedPatterns.length<1)
311             return null;
312         return formatPattern(m_dottedPatterns[0], params);
313     }
314     public String JavaDoc createXPath(Object JavaDoc[] params)
315     {
316         return formatPattern(m_xpathPattern, params);
317     }
318     private String JavaDoc formatPattern(String JavaDoc pattern, Object JavaDoc[] params)
319     {
320         return MessageFormat.format(pattern, params);
321     }
322
323     private String JavaDoc[] splitDottedPatternsString(String JavaDoc names)
324     {
325         ArrayList JavaDoc list = new ArrayList JavaDoc();
326         int idx = 0, idx2 = 0;
327         while(idx<names.length() && (idx2=names.indexOf(MBeansDescriptions.PATTERNS_SEPARATOR, idx))>=0)
328         {
329             if(idx2!=idx)
330                 list.add(names.substring(idx, idx2));
331             idx = idx2+1;
332         }
333         if(idx2<0)
334             list.add(names.substring(idx));
335         return (String JavaDoc[])list.toArray(new String JavaDoc[list.size()]);
336     }
337
338     public static boolean isDottedPatternMatch(Name name, Object JavaDoc[] tokens, boolean bExactMatch)
339     {
340         if(tokens==null)
341             return false;
342         int nTokens = name.getNumParts();
343         if(bExactMatch)
344         {
345             if(nTokens!=tokens.length)
346                 return false;
347         }
348         else
349         {
350             if(nTokens>tokens.length)
351                 return false;
352         }
353         for(int i=0; i<nTokens; i++)
354         {
355             if( (tokens[i] instanceof String JavaDoc) &&
356             !name.getNamePart(i).toString().equals((String JavaDoc)tokens[i]))
357                 return false;
358         }
359         return true;
360     }
361     
362 }
363
Popular Tags