KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > j2ee > sun > ide > j2ee > mbmapping > Utils


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19 /*
20  * Utils.java
21  *
22  * Created on February 24, 2004, 2:20 PM
23  */

24
25 package org.netbeans.modules.j2ee.sun.ide.j2ee.mbmapping;
26
27 import java.util.Set JavaDoc;
28 import java.util.Arrays JavaDoc;
29 import java.util.HashSet JavaDoc;
30
31 import javax.management.Attribute JavaDoc;
32 import javax.management.MBeanInfo JavaDoc;
33 import javax.management.ObjectName JavaDoc;
34 import javax.management.MBeanAttributeInfo JavaDoc;
35 import javax.management.MBeanOperationInfo JavaDoc;
36 import javax.management.MBeanServerConnection JavaDoc;
37
38 /**
39  *
40  * @author nityad
41  */

42 public class Utils {
43
44     /** Creates a new instance of Utils */
45     public Utils() {
46     }
47     
48     public static boolean isUserResource(ObjectName JavaDoc currentComp, MBeanServerConnection JavaDoc in_conn){
49         boolean userResource = false;
50         try{
51             Object JavaDoc resType = getAttribute(currentComp, "object_type", in_conn);
52             if(resType != null){
53                 if(resType.toString().equals("user")){
54                     userResource = true;
55                 }
56             }else{
57                 userResource = true;
58             }
59         }catch(Exception JavaDoc ex){
60             System.out.println("Error during isUserResource " + ex.getLocalizedMessage());
61         }
62         return userResource;
63     }
64     
65     public static String JavaDoc getAttribute(ObjectName JavaDoc objName, String JavaDoc attributeName, MBeanServerConnection JavaDoc in_conn){
66         String JavaDoc attrValue = null;
67         try{
68             Object JavaDoc attValue = in_conn.getAttribute(objName, attributeName);
69             if(attValue != null){
70                 attrValue = attValue.toString();
71             }
72         }catch(Exception JavaDoc ex){
73             return null;
74             //suppress Exception. Callers to any of the getters should handle null condition
75
}
76         return attrValue;
77     }
78     
79     /* Fix bug#5032958 - If deployed module's directory is renamed or deleted, the module is not available through
80      * the runtime mbeans(above). They can be accessed only via the config mbeans
81      * To identify type of modules:
82      * ObjectName of runtime mbeans has key j2eeType
83      * ObjectName of config mbeans has key type
84      *
85      * In this scenario the incoming parameter for Runtime Object Name will be null. This needs to be handled.
86      */

87     
88     public static ObjectName JavaDoc getRequiredObjectName(ObjectName JavaDoc origName, ObjectName JavaDoc confName, Attribute JavaDoc attribute, MBeanServerConnection JavaDoc in_conn){
89         ObjectName JavaDoc oName = null;
90         try{
91             String JavaDoc name = attribute.getName();
92             if(origName != null){
93                 MBeanInfo JavaDoc bnInfo = in_conn.getMBeanInfo(origName);
94                 MBeanAttributeInfo JavaDoc[] attInfo = bnInfo.getAttributes();
95                 String JavaDoc[] attNames = new String JavaDoc[attInfo.length];
96                 for(int i=0; i<attInfo.length; i++){
97                     attNames[i] = attInfo[i].getName();
98                 }
99                 Set JavaDoc appList = new HashSet JavaDoc(Arrays.asList(attNames));
100                 if(appList.contains(name)){
101                     oName = origName;
102                     return oName;
103                 }
104             }
105             MBeanInfo JavaDoc beanInfo = in_conn.getMBeanInfo(confName);
106             MBeanAttributeInfo JavaDoc[] attrInfo = beanInfo.getAttributes();
107             String JavaDoc[] attrNames = new String JavaDoc[attrInfo.length];
108             for(int i=0; i<attrInfo.length; i++){
109                 attrNames[i] = attrInfo[i].getName();
110             }
111             Set JavaDoc newList = new HashSet JavaDoc(Arrays.asList(attrNames));
112             if(newList.contains(name)){
113                 oName = confName;
114             }
115             
116         }catch(Exception JavaDoc ex){
117             //System.out.println("Error in getRequiredObjectName " + ex.getLocalizedMessage());
118
return oName;
119         }
120         return oName;
121     }
122     
123     /* Fix bug#5032958 - If deployed module's directory is renamed or deleted, the module is not available through
124      * the runtime mbeans(above). They can be accessed only via the config mbeans
125      * To identify type of modules:
126      * ObjectName of runtime mbeans has key j2eeType
127      * ObjectName of config mbeans has key type
128      *
129      * In this scenario the incoming parameter for Runtime Object Name will be null. This needs to be handled.
130      */

131     public static ObjectName JavaDoc getRequiredObjectName(ObjectName JavaDoc origName, ObjectName JavaDoc confName, String JavaDoc operationName, MBeanServerConnection JavaDoc in_conn){
132         ObjectName JavaDoc oName = null;
133         try{
134             if(origName != null){
135                 MBeanInfo JavaDoc bnInfo = in_conn.getMBeanInfo(origName);
136                 MBeanOperationInfo JavaDoc[] operInfo = bnInfo.getOperations();
137                 String JavaDoc[] operNames = new String JavaDoc[operInfo.length];
138                 for(int i=0; i<operInfo.length; i++){
139                     operNames[i] = operInfo[i].getName();
140                 }
141                 Set JavaDoc operList = new HashSet JavaDoc(Arrays.asList(operNames));
142                 if(operList.contains(operationName)){
143                     oName = origName;
144                     return oName;
145                 }
146             }
147             MBeanInfo JavaDoc beanInfo = in_conn.getMBeanInfo(confName);
148             MBeanOperationInfo JavaDoc[] operationInfo = beanInfo.getOperations();
149             String JavaDoc[] operationNames = new String JavaDoc[operationInfo.length];
150             for(int i=0; i<operationInfo.length; i++){
151                 operationNames[i] = operationInfo[i].getName();
152             }
153             Set JavaDoc newOperList = new HashSet JavaDoc(Arrays.asList(operationNames));
154             if(newOperList.contains(operationName)){
155                 oName = confName;
156             }
157         }catch(Exception JavaDoc ex){
158             //System.out.println("Error in getRequiredObjectName " + ex.getLocalizedMessage());
159
return oName;
160         }
161         return oName;
162     }
163     
164     public static String JavaDoc[] getStringSignature(){
165         String JavaDoc[] signature = new String JavaDoc[]{"java.lang.String"}; //NOI18N
166
return signature;
167     }
168     
169     public static Object JavaDoc[] getStringParam(String JavaDoc paramValue){
170         String JavaDoc props = paramValue;
171         Object JavaDoc[] params = new Object JavaDoc[]{props};
172         return params;
173     }
174 }
175
Popular Tags