KickJava   Java API By Example, From Geeks To Geeks.

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


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  * ServerInfo.java
21  *
22  * Created on February 25, 2004, 4:50 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.AttributeList JavaDoc;
35 import javax.management.MBeanAttributeInfo JavaDoc;
36 import javax.management.MBeanServerConnection JavaDoc;
37 import javax.management.MBeanException JavaDoc;
38 import javax.management.ReflectionException JavaDoc;
39 import javax.management.InstanceNotFoundException JavaDoc;
40 import javax.management.AttributeNotFoundException JavaDoc;
41 import javax.management.InvalidAttributeValueException JavaDoc;
42
43 import java.rmi.RemoteException JavaDoc;
44
45
46
47 /**
48  *
49  * @author nityad
50  */

51 public class ServerInfo extends ModuleMBean implements Constants{
52     
53     String JavaDoc PORT = "Port"; //NOI18N
54
String JavaDoc DOMAIN = "Domain"; //NOI18N
55

56     String JavaDoc port = null;
57     String JavaDoc username = null;
58     String JavaDoc password = null;
59     String JavaDoc domain = null;
60     
61     /** Creates a new instance of ServerInfo */
62     public ServerInfo( MBeanServerConnection JavaDoc in_conn) {
63         super(in_conn);
64         this.runtimeObjName = createRuntimeObjectName();
65     }
66     
67     private ObjectName JavaDoc createRuntimeObjectName(){
68         ObjectName JavaDoc runtimeName = null;
69         try{
70             runtimeName = new ObjectName JavaDoc(OBJ_J2EE);
71         }catch(Exception JavaDoc ex){
72             return null;
73         }
74         return runtimeName;
75     }
76     
77     public AttributeList JavaDoc getAttributes(String JavaDoc[] attributes) {
78         AttributeList JavaDoc attList = null;
79         try{
80             attList = this.conn.getAttributes(this.runtimeObjName, attributes);
81             AttributeList JavaDoc jsrList = createAddAttributes();
82             attList.addAll(jsrList);
83         }catch(Exception JavaDoc ex){
84             //System.out.println("Error in getAttributes of ServerInfo " + ex.getMessage());
85
return attList;
86         }
87         return attList;
88     }
89     
90     public MBeanInfo JavaDoc getMBeanInfo() {
91         MBeanInfo JavaDoc updatedInfo = null;
92         try{
93             MBeanInfo JavaDoc currentInfo = this.conn.getMBeanInfo(this.runtimeObjName);
94             MBeanAttributeInfo JavaDoc[] currentAttrInfo = currentInfo.getAttributes();
95             int additionalSize = ADDITIONAL_SERVER_INFO.length;
96             int size = JSR_SERVER_INFO.length + additionalSize;
97             MBeanAttributeInfo JavaDoc[] updatedAttrInfo = new MBeanAttributeInfo JavaDoc[size];
98             
99             Set JavaDoc reqdAttrs = new HashSet JavaDoc(Arrays.asList(JSR_SERVER_INFO));
100             int j=0;
101             for ( int i=0; i<currentAttrInfo.length; i++ ) {
102                 if(reqdAttrs.contains(currentAttrInfo[i].getName())){
103                     updatedAttrInfo[j] = currentAttrInfo[i];
104                     j++;
105                 }
106             }
107             //Additional Attributes : {"port", "username", "password", "domain" }
108
/*MBeanAttributeInfo(String name, String type, String description, boolean isReadable,
109                           boolean isWritable, boolean isIs) throws IllegalArgumentException*/

110             updatedAttrInfo[j] = new MBeanAttributeInfo JavaDoc(this.PORT, "int", "Server's port number", true, false, false); //NOI18N
111
updatedAttrInfo[j+1] = new MBeanAttributeInfo JavaDoc(this.DOMAIN, "char", "Server's domain", true, false, false); //NOI18N
112

113             updatedInfo = new MBeanInfo JavaDoc(currentInfo.getClassName(), currentInfo.getDescription(), updatedAttrInfo, currentInfo.getConstructors(),
114                 null, currentInfo.getNotifications());
115         }catch(Exception JavaDoc ex){
116             //System.out.println("Error in getMBeanInfo of ServerInfo " + ex.getMessage());
117
}
118         return updatedInfo;
119     }
120     
121     public void setAttribute(Attribute JavaDoc attribute) throws RemoteException JavaDoc, InstanceNotFoundException JavaDoc, AttributeNotFoundException JavaDoc,
122     InvalidAttributeValueException JavaDoc, MBeanException JavaDoc, ReflectionException JavaDoc, java.io.IOException JavaDoc {
123         String JavaDoc attrName = attribute.getName();
124         Set JavaDoc appList = new HashSet JavaDoc(Arrays.asList(JSR_SERVER_INFO));
125         if(appList.contains(attrName)){
126             this.conn.setAttribute(this.runtimeObjName, attribute);
127         }
128     }
129     
130     private AttributeList JavaDoc createAddAttributes(){
131         AttributeList JavaDoc atList = new AttributeList JavaDoc();
132         atList.add(new Attribute JavaDoc(this.PORT, this.getPort()));
133         atList.add(new Attribute JavaDoc(this.DOMAIN, this.getDomain()));
134         return atList;
135     }
136     
137     
138     public void setAttributes(String JavaDoc in_port, String JavaDoc in_userName, String JavaDoc in_password, String JavaDoc in_domain){
139         this.port = in_port;
140         this.username = in_userName;
141         this.password = in_password;
142         this.domain = in_domain;
143     }
144     
145     public String JavaDoc getPort(){
146         return this.port;
147     }
148     
149     public String JavaDoc getUserName(){
150         return this.username;
151     }
152     
153     public String JavaDoc getPassword(){
154         return this.password;
155     }
156     
157     public String JavaDoc getHiddenPassword(){
158         String JavaDoc modPassword = ""; //NOI18N
159
for (int i = 0; i < password.length(); i++) {
160             char c = '*' ;
161             modPassword += c;
162         }
163         return modPassword;
164     }
165     
166     public String JavaDoc getDomain(){
167         return this.domain;
168     }
169
170     //Fix for bug#5017963 - add api to identify if server requires restart
171
public boolean isRestartRequired(){
172         boolean restartReq = false;
173         String JavaDoc val = (String JavaDoc)getRuntimeAttributeValue("restartRequired"); //NOI18N
174
if(val != null){
175             restartReq = Boolean.valueOf(val).booleanValue();
176         }
177         return restartReq;
178     }
179     
180     public String JavaDoc getRuntimeAttributeValue(String JavaDoc attributeName){
181         return super.getAttribute(this.runtimeObjName, attributeName);
182     }
183 }
184
Popular Tags