KickJava   Java API By Example, From Geeks To Geeks.

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


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  * ModuleMBean.java
21  *
22  * Created on February 19, 2004, 2:55 PM
23  */

24
25 package org.netbeans.modules.j2ee.sun.ide.j2ee.mbmapping;
26
27 import javax.management.Attribute JavaDoc;
28 import javax.management.MBeanInfo JavaDoc;
29 import javax.management.ObjectName JavaDoc;
30 import javax.management.AttributeList JavaDoc;
31 import javax.management.MBeanAttributeInfo JavaDoc;
32 import javax.management.MBeanServerConnection JavaDoc;
33 import javax.management.MBeanException JavaDoc;
34 import javax.management.ReflectionException JavaDoc;
35 import javax.management.InstanceNotFoundException JavaDoc;
36 import javax.management.AttributeNotFoundException JavaDoc;
37 import javax.management.InvalidAttributeValueException JavaDoc;
38
39 import java.rmi.RemoteException JavaDoc;
40
41
42
43
44 /**
45  *
46  * @author nityad
47  */

48 public abstract class ModuleMBean implements Constants{
49     protected ObjectName JavaDoc runtimeObjName = null;
50     protected MBeanServerConnection JavaDoc conn = null;
51     
52     public ObjectName JavaDoc configApplicationsObjName = null;
53     
54     public ModuleMBean(MBeanServerConnection JavaDoc in_conn) {
55         this.conn = in_conn;
56         this.configApplicationsObjName = setApplicationsObjectName();
57     }
58     
59     public ModuleMBean(ObjectName JavaDoc objName) {
60         this.runtimeObjName = objName;
61         this.configApplicationsObjName = setApplicationsObjectName();
62     }
63      
64     public ModuleMBean(ObjectName JavaDoc objName, MBeanServerConnection JavaDoc in_conn) {
65         this.runtimeObjName = objName;
66         this.conn = in_conn;
67         this.configApplicationsObjName = setApplicationsObjectName();
68     }
69      
70     public void setConnection(MBeanServerConnection JavaDoc in_conn){
71          this.conn = in_conn;
72          this.configApplicationsObjName = setApplicationsObjectName();
73     }
74     
75     public abstract MBeanInfo JavaDoc getMBeanInfo();
76         
77     public abstract AttributeList JavaDoc getAttributes(String JavaDoc[] attributes);
78    
79     public abstract void setAttribute(Attribute JavaDoc attribute) throws RemoteException JavaDoc, InstanceNotFoundException JavaDoc, AttributeNotFoundException JavaDoc,
80         InvalidAttributeValueException JavaDoc, MBeanException JavaDoc, ReflectionException JavaDoc, java.io.IOException JavaDoc;
81     
82     public Object JavaDoc start(){
83         Object JavaDoc retVal = null;
84         try{
85             retVal = this.conn.invoke(this.runtimeObjName, "start", null, null); //NOI18N
86
}catch(Exception JavaDoc ex){
87             return null;
88         }
89         return retVal;
90     }
91     
92     public void stop(){
93         try{
94             this.conn.invoke(this.runtimeObjName, "stop", null, null); //NOI18N
95
}catch(Exception JavaDoc ex){ }
96     }
97     
98     public void restart(){
99         try{
100             this.conn.invoke(this.runtimeObjName, "start", null, null); //NOI18N
101
this.conn.invoke(this.runtimeObjName, "stop", null, null); //NOI18N
102
}catch(Exception JavaDoc ex){ }
103     }
104     
105     public ObjectName JavaDoc setApplicationsObjectName(){
106         ObjectName JavaDoc objName = null;
107         try{
108             objName = new ObjectName JavaDoc(MAP_J2EEAPP_STANDALONE);
109         }catch(Exception JavaDoc ex){
110             
111         }
112         return objName;
113     }
114     
115     public String JavaDoc getResourceName(String JavaDoc key){
116         String JavaDoc keyName = null;
117         Object JavaDoc keyNameAttr = getAttribute(this.runtimeObjName, key);
118         if(keyNameAttr != null){
119             keyName = keyNameAttr.toString();
120         }
121         return keyName;
122     }
123     
124     public String JavaDoc getAttribute(ObjectName JavaDoc objName, String JavaDoc attributeName){
125         String JavaDoc attrValue = null;
126         try{
127             Object JavaDoc attValue = this.conn.getAttribute(objName, attributeName);
128             if(attValue != null){
129                 attrValue = attValue.toString();
130             }
131         }catch(Exception JavaDoc ex){
132             //suppress Exception. Callers to any of the getters should handle null condition
133
}
134         return attrValue;
135     }
136     
137     public ObjectName JavaDoc getConfigObjectName(String JavaDoc query, String JavaDoc resourceName){
138         ObjectName JavaDoc configObjectName = null;
139         try{
140             configObjectName = (ObjectName JavaDoc)this.conn.invoke(this.configApplicationsObjName, query, getStringParam(resourceName), getStringSignature());
141         }catch(Exception JavaDoc ex){
142             
143         }
144         return configObjectName;
145     }
146     
147     public Object JavaDoc invokeOperation(String JavaDoc operationName, Object JavaDoc[] params, String JavaDoc[] signature){
148         Object JavaDoc retValue = null;
149         try{
150             retValue = this.conn.invoke(this.runtimeObjName, operationName, params, signature);
151         }catch(Exception JavaDoc ex){
152         }
153         return retValue;
154     }
155     
156     /*********************Util classes ****************************************************/
157     private String JavaDoc[] getStringSignature(){
158         return Utils.getStringSignature();
159     }
160     
161     private Object JavaDoc[] getStringParam(String JavaDoc paramValue){
162         return Utils.getStringParam(paramValue);
163     }
164     
165     public boolean isUserResource(ObjectName JavaDoc currentComp){
166         boolean userRes = Utils.isUserResource(currentComp, this.conn);
167         return userRes;
168     }
169     
170     public MBeanAttributeInfo JavaDoc[] setSystemResourceNonEditable(MBeanAttributeInfo JavaDoc[] currentAttr){
171         MBeanAttributeInfo JavaDoc[] updatedAttr = null;
172         try{
173             updatedAttr = new MBeanAttributeInfo JavaDoc[currentAttr.length];
174             for ( int i=0; i<currentAttr.length; i++ ) {
175                 MBeanAttributeInfo JavaDoc currentAttrInfo = currentAttr[i];
176                 updatedAttr[i] = new MBeanAttributeInfo JavaDoc(currentAttrInfo.getName(), currentAttrInfo.getType(), currentAttrInfo.getDescription(), true, false, false);
177             }
178         }catch(Exception JavaDoc ex){
179             //System.out.println("Error in setSystemResourceNonEditable " + ex.getLocalizedMessage());
180
}
181         return updatedAttr;
182     }
183     
184     public ObjectName JavaDoc getRequiredObjectName(ObjectName JavaDoc origName, ObjectName JavaDoc confName, Attribute JavaDoc attribute){
185         ObjectName JavaDoc oName = Utils.getRequiredObjectName(origName, confName, attribute, this.conn);
186         return oName;
187     }
188         
189     public ObjectName JavaDoc getRequiredObjectName(ObjectName JavaDoc origName, ObjectName JavaDoc confName, String JavaDoc operationName){
190         ObjectName JavaDoc oName = Utils.getRequiredObjectName(origName, confName, operationName, this.conn);
191         return oName;
192     }
193     
194     /*********************Util classes ****************************************************/
195 }
196
Popular Tags