KickJava   Java API By Example, From Geeks To Geeks.

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


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: MBeansNaming.java,v 1.4 2006/03/12 01:26:57 jluehe Exp $
26  * @author: alexkrav
27  *
28  * $Log: MBeansNaming.java,v $
29  * Revision 1.4 2006/03/12 01:26:57 jluehe
30  * Renamed AS's org.apache.commons.* to com.sun.org.apache.commons.*, to avoid collisions with org.apache.commons.* packages bundled by webapps.
31  *
32  * Tests run: QL, Servlet TCK
33  *
34  * Revision 1.3 2005/12/25 03:47:39 tcfujii
35  * Updated copyright text and year.
36  *
37  * Revision 1.2 2005/06/27 21:19:45 tcfujii
38  * Issue number: CDDL header updates.
39  *
40  * Revision 1.1.1.1 2005/05/27 22:52:02 dpatil
41  * GlassFish first drop
42  *
43  * Revision 1.6 2004/11/14 07:04:23 tcfujii
44  * Updated copyright text and/or year.
45  *
46  * Revision 1.5 2004/02/20 03:56:16 qouyang
47  *
48  *
49  * First pass at code merge.
50  *
51  * Details for the merge will be published at:
52  * http://javaweb.sfbay.sun.com/~qouyang/workspace/PE8FCSMerge/02202004/
53  *
54  * Revision 1.4.4.1 2004/02/02 07:25:21 tcfujii
55  * Copyright updates notices; reviewer: Tony Ng
56  *
57  * Revision 1.4 2003/06/25 20:03:41 kravtch
58  * 1. java file headers modified
59  * 2. properties handling api is added
60  * 3. fixed bug for xpathes containing special symbols;
61  * 4. new testcases added for jdbc-resource
62  * 5. introspector modified by not including base classes operations;
63  *
64  *
65 */

66
67
68 package com.sun.enterprise.admin.meta.naming;
69
70
71 //import com.sun.enterprise.admin.meta.MBeanRegistry;
72

73 //Config imports
74
import com.sun.enterprise.config.ConfigException;
75 import com.sun.enterprise.config.ConfigContext;
76 import com.sun.enterprise.config.ConfigFactory;
77 import com.sun.enterprise.config.ConfigBean;
78 import com.sun.enterprise.config.ConfigBeansFactory;
79
80 import com.sun.org.apache.commons.modeler.ManagedBean;
81 import com.sun.org.apache.commons.modeler.FieldInfo;
82
83 //JMX imports
84
import javax.management.ObjectName JavaDoc;
85
86 import java.util.Hashtable JavaDoc;
87 import java.util.HashSet JavaDoc;
88 import java.util.HashMap JavaDoc;
89 import java.util.Iterator JavaDoc;
90 import java.util.List JavaDoc;
91 import java.util.ArrayList JavaDoc;
92 import java.util.logging.Level JavaDoc;
93 import java.util.logging.Logger JavaDoc;
94
95 /**
96  * Provides naming support for Mbeans
97  *
98  */

99
100 public class MBeansNaming
101 {
102     //PUBLIC CONSTANTS
103
public static final int MBEAN_CATEGORY_SYSTEM = 0x01;
104     public static final int MBEAN_CATEGORY_CONFIG = 0x02;
105     public static final int MBEAN_CATEGORY_RUNTIME = 0x04;
106
107     //PRIVATE CONTSTANTS
108
final static String JavaDoc CONFIG_MBEANS_BASE_CLASS_PREFIX = "com.iplanet.ias.admin.server.core.mbean.config.";
109     final static char PATTERNS_SEPARATOR = '|';
110     final public static int MODE_CONFIG = 0x0001;
111     final public static int MODE_MONITOR = 0x0002;
112     private final static String JavaDoc MSG_FINDNAMINDESCRIPTOR_FAILED = "mbean.config.findnamingdescriptor_failed";
113     private final static String JavaDoc MSG_MALFORMED_DOTTED_NAME = "mbean.config.malformed_dotted_name";
114     private final static String JavaDoc MSG_EXCEPTION_DURING_LIST_NAMES = "mbean.config.list_names_failed";
115     public static final Logger JavaDoc sLogger = Logger.getLogger("test"); //AdminConstants.kLoggerName);
116
/* alexkrav
117     protected MBeanNamingDescriptor[] m_mbeanDescr = null; //initMBeanNaming();
118     protected String m_defaultDomainName = "domain_name"; //default value for {0} placeholder - set in initNaming()
119     protected String m_defaultInstanceName = "instance_name"; //default value for {1} placeholder - set in initNaming()
120
121     //**************************************************************************
122     protected MBeanNamingDescriptor findNamingDescriptorByType(/*int category,/ String type)
123     {
124         try
125         {
126             for(int i=0; i<m_mbeanDescr.length; i++)
127             {
128                 if(/*category==m_mbeanDescr[i].m_category &&/ type.equals(m_mbeanDescr[i].m_type))
129                     return m_mbeanDescr[i];
130             }
131         }
132         catch (Exception e)
133         {
134             sLogger.log(Level.FINE, MSG_FINDNAMINDESCRIPTOR_FAILED, e);
135         }
136         return null;
137     }
138     //**************************************************************************
139     public MBeanNamingDescriptor findNamingDescriptor(String dottedName)
140     {
141         try
142         {
143             Name name = new Name(dottedName);
144             for(int i=0; i<m_mbeanDescr.length; i++)
145             {
146                 if(m_mbeanDescr[i].isDottedPatternMatch(name))
147                     return m_mbeanDescr[i];
148             }
149         }
150         catch (Exception e)
151         {
152             sLogger.log(Level.FINE, MSG_FINDNAMINDESCRIPTOR_FAILED, e);
153         }
154         return null;
155     }
156     //**************************************************************************
157     public MBeanNamingDescriptor findNamingDescriptor(ObjectName objectName)
158     {
159         try
160         {
161            Hashtable ht = objectName.getKeyPropertyList();
162            ht.put(":",objectName.getDomain()); //add domain name pseudo pair
163            for(int i=0; i<m_mbeanDescr.length; i++)
164            {
165                if(m_mbeanDescr[i].isObjectNamePatternMatch(ht))
166                    return m_mbeanDescr[i];
167            }
168         }
169         catch (Exception e)
170         {
171             sLogger.log(Level.FINE, MSG_FINDNAMINDESCRIPTOR_FAILED, e);
172         }
173         return null;
174     }
175 */

176 /*
177 //**************************************************************************
178     public String[] findNameContinuation(String instanceName, String dottedName)
179     {
180         HashSet hs = new HashSet();
181         int wildDescrIndex = -1;
182         Name name = null;
183         int nNameTokens = 0;
184
185         //if there is no such instance - next statement will throw runtime exception
186         InstanceEnvironment instanceEnvironment= new InstanceEnvironment(instanceName);
187         
188         //First: add "static" continuations
189         try
190         {
191             name = new Name(dottedName);
192             nNameTokens = name.getNumParts();
193         }
194         catch (Exception e)
195         {
196             sLogger.log(Level.FINE, MSG_MALFORMED_DOTTED_NAME, e);
197             return new String[0];
198         }
199
200         for(int i=0; i<m_mbeanDescr.length; i++) //enumerate all descriptors
201         {
202             Object[][] tokens = m_mbeanDescr[i].getDottedTokens();
203             if(tokens!=null)
204             {
205                 for(int j=0; j<tokens.length; j++) //enum different dotted patterns presentations
206                 {
207                     if(MBeanNamingDescriptor.isDottedPatternMatch(name, tokens[j], false) && tokens[j].length>nNameTokens)
208                     {
209                         //dotted pattern beginning matches to sample
210                         if(!(tokens[j][nNameTokens] instanceof String)) //wildcard?
211                         {
212                             if(tokens[j].length==nNameTokens+1) //only if wildcard at the end; otherwise - ignore
213                                 wildDescrIndex = i;
214                         }
215                         else
216                         {
217                             hs.add(dottedName+"."+tokens[j][nNameTokens]);
218                         }
219                     }
220                 }
221             }
222         }
223         //Now try to add childrens names
224         String xpath = null;
225         if(wildDescrIndex>=0)
226         {
227             try
228             {
229                 MBeanNamingInfo info = new MBeanNamingInfo(dottedName + ".fake");
230                 xpath = info.getXPath();
231             }
232             catch (Exception e)
233             {
234                sLogger.log(Level.FINE, MSG_EXCEPTION_DURING_LIST_NAMES, e);
235             }
236         }
237         if(xpath!=null)
238         {
239             String attributeName = null;
240             String elementName= null;
241             // seek for elemname[@attrname=]
242             xpath = xpath.trim();
243             if(xpath.length()>0 && xpath.endsWith("]"))
244             {
245                 int i = xpath.lastIndexOf('@') + 1;
246                 int j = xpath.indexOf('=',i) ;
247                 if(i>0 && j>i)
248                 {
249                     attributeName = xpath.substring(i,j).trim();
250                     j = xpath.lastIndexOf('[');
251                     if(j>0 && j<i)
252                     {
253                         xpath = xpath.substring(0,j);
254                         j = xpath.lastIndexOf('/');
255                         if(j>0 && j<xpath.length()-2)
256                         {
257                             elementName = xpath.substring(j+1).trim();
258                             xpath = xpath.substring(0,j);
259                         }
260                     }
261                 }
262                 
263             }
264             
265             if(attributeName!=null && elementName!=null) //is parsed successfully
266             {
267                 //here we are to call ConfiBeans methods
268                 ConfigContext configContext;
269                 try
270                 {
271                     String fileUrl = instanceEnvironment.getBackupConfigFilePath();
272                     configContext = ConfigFactory.createConfigContext(fileUrl);
273                     ConfigBean bean = ConfigBeansFactory.getConfigBeanByXPath(configContext, xpath);
274                     ConfigBean[] childs = bean.getChildBeansByName(elementName);
275                     for(int i=0; i<childs.length; i++)
276                     {
277                         String next = childs[i].getAttributeValue(attributeName);
278                         if(next!=null)
279                             hs.add(dottedName+"."+next);
280                     }
281                     
282                 }
283                 catch (ConfigException ce)
284                 {
285                    sLogger.log(Level.FINE, MSG_EXCEPTION_DURING_LIST_NAMES, ce);
286                 }
287                 catch (NullPointerException npe) //ConfigBean returns this exception by many reasons
288                 {
289                    sLogger.log(Level.FINE, MSG_EXCEPTION_DURING_LIST_NAMES, npe);
290                 }
291             }
292         }
293         return (String[])hs.toArray(new String[hs.size()]);
294     }
295 */

296     
297 /* private HashMap convertListFieldsToHashMap(List fields)
298     {
299         Iterator iter = fields.iterator();
300         HashMap fldsMap = new HashMap();
301         while(iter.hasNext())
302         {
303             FieldInfo field = (FieldInfo)iter.next();
304             fldsMap.put(field.getName(), field.getValue());
305         }
306         return fldsMap;
307     }
308 */

309 /* synchronized public MBeanNamingDescriptor[] initMBeanNaming() // throws MBeanNamingException
310     {
311         print("####################################### initMBeanNaming() ##############################################");
312         ArrayList arr = new ArrayList();
313         try
314         {
315             String[] names = MBeanRegistry.getMBeanResgistryEntryNames();
316             for(int i=0; i<names.length; i++)
317             {
318                 //FIXME just for prototype
319                 ManagedBean mb = MBeanRegistry.findMBeanRegistryEntry(names[i]).managedBean;
320                 HashMap fields = convertListFieldsToHashMap(mb.getFields());
321                     arr.add( new MBeanNamingDescriptor( //MBeansNaming.MBEAN_CATEGORY_RUNTIME,
322                             names[i], new Integer(MODE_CONFIG),
323                             (String)fields.get("CLIName"),
324                             (String)fields.get("ObjectName"),
325                             (String)fields.get("persistLocation"),
326                             (String)mb.getClassName())); // fields.get("Class")));
327             }
328         }
329        catch (MBeanNamingException e)
330        {
331        }
332       
333        m_mbeanDescr = (MBeanNamingDescriptor[])arr.toArray(new MBeanNamingDescriptor[arr.size()]);
334        return m_mbeanDescr; //descrs;
335     }
336 */

337     
338 /*
339     static private MBeanNamingInfo getMBeanNamingDescriptor(String type)
340     {
341         int idx = getTypeIndex(type);
342         if(idx<0)
343            return null;
344         return m_mbeanDescr[idx];
345     }
346     
347     static private int getTypeIndex(String type)
348     {
349        Integer i = (Integer)m_typeIndex.get(type);
350        if(i==null)
351            return -1;
352        return i.intValue();
353     }
354 */

355         
356     
357 }
358
Popular Tags