KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > enterprise > admin > server > core > mbean > config > naming > ConfigMBeansNaming


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: ConfigMBeansNaming.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
37 import com.sun.enterprise.admin.common.exception.MBeanConfigException;
38 import com.sun.enterprise.admin.common.constant.AdminConstants;
39
40
41 import com.sun.enterprise.instance.InstanceEnvironment;
42
43 //Config imports
44
import com.sun.enterprise.config.ConfigException;
45 import com.sun.enterprise.config.ConfigContext;
46 import com.sun.enterprise.config.ConfigFactory;
47 import com.sun.enterprise.config.ConfigBean;
48 import com.sun.enterprise.config.ConfigBeansFactory;
49
50 //JMX imports
51
import javax.management.ObjectName JavaDoc;
52
53 import java.util.Hashtable JavaDoc;
54 import java.util.HashSet JavaDoc;
55 import java.util.logging.Level JavaDoc;
56 import java.util.logging.Logger JavaDoc;
57
58 /**
59     Provides naming support for ConfigMbeans
60 */

61 public class ConfigMBeansNaming extends MBeansDescriptions
62 {
63     static MBeanNamingDescriptor[] m_mbeanDescr = initConfigMBeanNaming();
64     public static final Logger JavaDoc sLogger = Logger.getLogger(AdminConstants.kLoggerName);
65     private final static String JavaDoc MSG_FINDNAMINDESCRIPTOR_FAILED = "mbean.config.findnamingdescriptor_failed";
66     private final static String JavaDoc MSG_MALFORMED_DOTTED_NAME = "mbean.config.malformed_dotted_name";
67     private final static String JavaDoc MSG_EXCEPTION_DURING_LIST_NAMES = "mbean.config.list_names_failed";
68     //**************************************************************************
69
static MBeanNamingDescriptor findNamingDescriptorByType(String JavaDoc type)
70     {
71         try
72         {
73             for(int i=0; i<m_mbeanDescr.length; i++)
74             {
75                 if(type.equals(m_mbeanDescr[i].getType()))
76                     return m_mbeanDescr[i];
77             }
78         }
79         catch (Exception JavaDoc e)
80         {
81             sLogger.log(Level.FINE, MSG_FINDNAMINDESCRIPTOR_FAILED, e);
82         }
83         return null;
84     }
85     //**************************************************************************
86
static MBeanNamingDescriptor findNamingDescriptor(String JavaDoc dottedName)
87     {
88         try
89         {
90             Name name = new Name(dottedName);
91             for(int i=0; i<m_mbeanDescr.length; i++)
92             {
93                 if(m_mbeanDescr[i].isDottedPatternMatch(name))
94                     return m_mbeanDescr[i];
95             }
96         }
97         catch (Exception JavaDoc e)
98         {
99             sLogger.log(Level.FINE, MSG_FINDNAMINDESCRIPTOR_FAILED, e);
100         }
101         return null;
102     }
103     //**************************************************************************
104
static MBeanNamingDescriptor findNamingDescriptor(ObjectName JavaDoc objectName)
105     {
106         try
107         {
108            Hashtable JavaDoc ht = objectName.getKeyPropertyList();
109            for(int i=0; i<m_mbeanDescr.length; i++)
110            {
111                if(m_mbeanDescr[i].isObjectNamePatternMatch(ht))
112                    return m_mbeanDescr[i];
113            }
114         }
115         catch (Exception JavaDoc e)
116         {
117             sLogger.log(Level.FINE, MSG_FINDNAMINDESCRIPTOR_FAILED, e);
118         }
119         return null;
120     }
121
122     //**************************************************************************
123
public static String JavaDoc[] findNameContinuation(String JavaDoc instanceName, String JavaDoc dottedName)
124     {
125         HashSet JavaDoc hs = new HashSet JavaDoc();
126         int wildDescrIndex = -1;
127         Name name = null;
128         int nNameTokens = 0;
129
130         //if there is no such instance - next statement will throw runtime exception
131
InstanceEnvironment instanceEnvironment= new InstanceEnvironment(instanceName);
132         
133         //First: add "static" continuations
134
try
135         {
136             name = new Name(dottedName);
137             nNameTokens = name.getNumParts();
138         }
139         catch (Exception JavaDoc e)
140         {
141             sLogger.log(Level.FINE, MSG_MALFORMED_DOTTED_NAME, e);
142             return new String JavaDoc[0];
143         }
144
145         for(int i=0; i<m_mbeanDescr.length; i++) //enumerate all descriptors
146
{
147             Object JavaDoc[][] tokens = m_mbeanDescr[i].getDottedTokens();
148             if(tokens!=null)
149             {
150                 for(int j=0; j<tokens.length; j++) //enum different dotted patterns presentations
151
{
152                     if(MBeanNamingDescriptor.isDottedPatternMatch(name, tokens[j], false) && tokens[j].length>nNameTokens)
153                     {
154                         //dotted pattern beginning matches to sample
155
if(!(tokens[j][nNameTokens] instanceof String JavaDoc)) //wildcard?
156
{
157                             if(tokens[j].length==nNameTokens+1) //only if wildcard at the end; otherwise - ignore
158
wildDescrIndex = i;
159                         }
160                         else
161                         {
162                             hs.add(dottedName+"."+tokens[j][nNameTokens]);
163                         }
164                     }
165                 }
166             }
167         }
168         //Now try to add childrens names
169
String JavaDoc xpath = null;
170         if(wildDescrIndex>=0)
171         {
172             try
173             {
174                 ConfigMBeanNamingInfo info = new ConfigMBeanNamingInfo(dottedName + ".fake");
175                 xpath = info.getXPath();
176             }
177             catch (Exception JavaDoc e)
178             {
179                sLogger.log(Level.FINE, MSG_EXCEPTION_DURING_LIST_NAMES, e);
180             }
181         }
182         if(xpath!=null)
183         {
184             String JavaDoc attributeName = null;
185             String JavaDoc elementName= null;
186             // seek for elemname[@attrname=]
187
xpath = xpath.trim();
188             if(xpath.length()>0 && xpath.endsWith("]"))
189             {
190                 int i = xpath.lastIndexOf('@') + 1;
191                 int j = xpath.indexOf('=',i) ;
192                 if(i>0 && j>i)
193                 {
194                     attributeName = xpath.substring(i,j).trim();
195                     j = xpath.lastIndexOf('[');
196                     if(j>0 && j<i)
197                     {
198                         xpath = xpath.substring(0,j);
199                         j = xpath.lastIndexOf('/');
200                         if(j>0 && j<xpath.length()-2)
201                         {
202                             elementName = xpath.substring(j+1).trim();
203                             xpath = xpath.substring(0,j);
204                         }
205                     }
206                 }
207                 
208             }
209             
210             if(attributeName!=null && elementName!=null) //is parsed successfully
211
{
212                 //here we are to call ConfiBeans methods
213
ConfigContext configContext;
214                 try
215                 {
216                     String JavaDoc fileUrl = instanceEnvironment.getConfigFilePath();
217                     configContext = ConfigFactory.createConfigContext(fileUrl);
218                     ConfigBean bean = ConfigBeansFactory.getConfigBeanByXPath(configContext, xpath);
219                     ConfigBean[] childs = bean.getChildBeansByName(elementName);
220                     for(int i=0; i<childs.length; i++)
221                     {
222                         String JavaDoc next = childs[i].getAttributeValue(attributeName);
223                         if(next!=null)
224                             hs.add(dottedName+"."+next);
225                     }
226                     
227                 }
228                 catch (ConfigException ce)
229                 {
230                    sLogger.log(Level.FINE, MSG_EXCEPTION_DURING_LIST_NAMES, ce);
231                 }
232                 catch (NullPointerException JavaDoc npe) //ConfigBean returns this exception by many reasons
233
{
234                    sLogger.log(Level.FINE, MSG_EXCEPTION_DURING_LIST_NAMES, npe);
235                 }
236             }
237         }
238         return (String JavaDoc[])hs.toArray(new String JavaDoc[hs.size()]);
239     }
240     
241     synchronized static private MBeanNamingDescriptor[] initConfigMBeanNaming() // throws MBeanConfigException
242
{
243        MBeanNamingDescriptor[] descrs = null;
244        try
245        {
246                descrs = new MBeanNamingDescriptor[mbean_descriptions.length];
247                for(int i=0; i<mbean_descriptions.length; i++)
248                {
249                    descrs[i] = new MBeanNamingDescriptor(mbean_descriptions[i]);
250                }
251        }
252        catch (MBeanConfigException e)
253        {
254        }
255        return descrs;
256     }
257     
258 /*
259     static private ConfigMBeanNamingInfo getMBeanNamingDescriptor(String type)
260     {
261         int idx = getTypeIndex(type);
262         if(idx<0)
263            return null;
264         return m_mbeanDescr[idx];
265     }
266     
267     static private int getTypeIndex(String type)
268     {
269        Integer i = (Integer)m_typeIndex.get(type);
270        if(i==null)
271            return -1;
272        return i.intValue();
273     }
274 */

275         
276     public static void main(String JavaDoc args[])
277     {
278        testDottedName("myInstance.http-listener.myListener");
279        testDottedName("myInstance.http-listener");
280        testDottedName("myInstance.http-server");
281        testDottedName("myInstance.http-server.mymy");
282        testDottedName("myInstance.http-listeners.myListener");
283        testDottedName("myInstance.http-serve");
284        testDottedName("myInstance.http-server.http-listener.myListener");
285
286         try
287         {
288            testObjectName(new ObjectName JavaDoc("ias:type=http-listener,instance-name=myServer,name=myListener"));
289            testObjectName(new ObjectName JavaDoc("ias:type=http-listener,instance-name=myServer,name=myListener,chtoto=to"));
290            testObjectName(new ObjectName JavaDoc("ias:type=http-service,instance-name=myServer,name=jhgv"));
291            testObjectName(new ObjectName JavaDoc("ias:type=http-service,server-instances=myServer"));
292            testObjectName(new ObjectName JavaDoc("ias:type=http-service,instance-name=myServer"));
293            testObjectName(new ObjectName JavaDoc("ias:name=myListener,instance-name=myServer,type=http-listener"));
294         }
295         catch (Throwable JavaDoc e)
296         {
297             print(">>>>>>EXCEPTION: " + e);
298             e.printStackTrace();
299         }
300     }
301
302     private static void print(String JavaDoc str)
303     {
304         System.out.println(str);
305     }
306     
307     private static void testDottedName(String JavaDoc dottedName)
308     {
309         try
310         {
311             print("\n\n\n>>>>>>test for dotted name: " + dottedName);
312             ConfigMBeanNamingInfo mbi = new ConfigMBeanNamingInfo(dottedName);
313             print(" ConfigMBeanNamingInfo =" + mbi);
314             print(" ObjectName =" + mbi.getObjectName());
315             print(" XPath =" + mbi.getXPath());
316         }
317         catch (Throwable JavaDoc e)
318         {
319             print(">>>>>>EXCEPTION: " + e);
320             //e.printStackTrace();
321
}
322             
323     }
324     private static void testObjectName(ObjectName JavaDoc name)
325     {
326         try
327         {
328             print("\n\n\n>>>>>>test for object name: " + name);
329             ConfigMBeanNamingInfo mbi = new ConfigMBeanNamingInfo(name);
330             print(" ConfigMBeanNamingInfo =" + mbi);
331             print(" ObjectName =" + mbi.getObjectName());
332             print(" XPath =" + mbi.getXPath());
333         }
334         catch (Throwable JavaDoc e)
335         {
336             print(">>>>>>EXCEPTION: " + e);
337             e.printStackTrace();
338         }
339             
340     }
341     
342 }
343
Popular Tags