KickJava   Java API By Example, From Geeks To Geeks.

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


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  * JvmOptions.java
21  *
22  * Created on February 24, 2004, 1:21 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 // ludo import org.netbeans.modules.j2ee.sun.ide.j2ee.ui.Util;
45

46 /**
47  *
48  * @author nityad
49  */

50 public class JvmOptions extends ModuleMBean implements Constants{
51     
52     private ObjectName JavaDoc configObjName = null;
53     private boolean isServerEightOne = true;
54     /** Creates a new instance of JvmOptions */
55     public JvmOptions(MBeanServerConnection JavaDoc in_conn) {
56         super(in_conn);
57         this.configObjName = createConfigObjectName();
58         this.runtimeObjName = configObjName;
59     }
60     
61     public JvmOptions(ObjectName JavaDoc objName, MBeanServerConnection JavaDoc in_conn) {
62         super(objName, in_conn);
63         this.configObjName = createConfigObjectName();
64     }
65     
66     public JvmOptions(ObjectName JavaDoc objName, MBeanServerConnection JavaDoc in_conn, boolean in_isServerEightOne) {
67         super(objName, in_conn);
68         this.configObjName = createConfigObjectName();
69         this.isServerEightOne = in_isServerEightOne;
70     }
71     
72     private ObjectName JavaDoc createConfigObjectName(){
73        ObjectName JavaDoc configName = null;
74        try{
75            configName = new ObjectName JavaDoc(this.MAP_JVMOptions);
76        }catch(Exception JavaDoc ex){
77            return null;
78        }
79        return configName;
80     }
81     
82     public ObjectName JavaDoc getConfigObjectName(){
83         return configObjName;
84     }
85     public AttributeList JavaDoc getAttributes(String JavaDoc[] attributes){
86         AttributeList JavaDoc attList = null;
87         try{
88             String JavaDoc addrValue = getAddressValue();
89             Attribute JavaDoc jpdaAttr = null;
90             if(isSharedMemory()){
91                 //Fix for bug# 4989322 - solaris does not support shmem
92
if(isWindows())
93                     jpdaAttr = new Attribute JavaDoc(SHARED_MEM, (Object JavaDoc)addrValue);
94                 else{
95                 //ludo Util.setStatusBar(bundle.getString("Msg_SolarisShmem"));
96
Attribute JavaDoc newAttr = new Attribute JavaDoc(DEBUG_OPTIONS, DEF_DEUG_OPTIONS_SOCKET);
97                     this.setAttribute(newAttr);
98                     Integer JavaDoc value = new Integer JavaDoc(11000);
99                     jpdaAttr = new Attribute JavaDoc(JPDA_PORT, value);
100                 }
101             }else{
102                 Integer JavaDoc value = new Integer JavaDoc(addrValue);
103                 jpdaAttr = new Attribute JavaDoc(JPDA_PORT, (Object JavaDoc)value);
104             }
105             attList = this.conn.getAttributes(this.configObjName, attributes);
106             attList.add(jpdaAttr);
107         }catch(Exception JavaDoc ex){
108             return attList;
109             //Attribute list is empty
110
}
111         return attList;
112     }
113     
114     public MBeanInfo JavaDoc getMBeanInfo() {
115         MBeanInfo JavaDoc updatedInfo = null;
116         try{
117             MBeanInfo JavaDoc newInfo = this.conn.getMBeanInfo(this.configObjName);
118             
119             //Adding custom property jpda-port number
120
MBeanAttributeInfo JavaDoc[] newAttrInfo = newInfo.getAttributes();
121             int size = newAttrInfo.length + 1;
122             MBeanAttributeInfo JavaDoc[] updatedAttrInfo = new MBeanAttributeInfo JavaDoc[size];
123             //get list of attributes from Config bean
124
for ( int i=0; i<newAttrInfo.length; i++ ) {
125                 updatedAttrInfo[i] = newAttrInfo[i];
126             }
127             
128             //Fix for bug# 4989322 - solaris does not support shmem
129
if(isSharedMemory() && isWindows())
130                 updatedAttrInfo[size-1] = new MBeanAttributeInfo JavaDoc(SHARED_MEM, "java.lang.String", "Shared Memory", true, true, false); //NOI18N
131
else
132                 updatedAttrInfo[size-1] = new MBeanAttributeInfo JavaDoc(JPDA_PORT, "int", "JPDA port number", true, true, false); //NOI18N
133

134             updatedInfo = new MBeanInfo JavaDoc(newInfo.getClassName(), newInfo.getDescription(), updatedAttrInfo, newInfo.getConstructors(),
135                   newInfo.getOperations(), newInfo.getNotifications());
136             
137         }catch(Exception JavaDoc ex){
138             //System.out.println("Error in getMBeanInfo of JVMOptions " + ex.getMessage());
139
return updatedInfo;
140         }
141         return updatedInfo;
142     }
143     
144     public void setAttribute(Attribute JavaDoc attribute) throws RemoteException JavaDoc, InstanceNotFoundException JavaDoc, AttributeNotFoundException JavaDoc,
145      InvalidAttributeValueException JavaDoc, MBeanException JavaDoc, ReflectionException JavaDoc, java.io.IOException JavaDoc{
146         if(attribute.getName().equals(this.JPDA_PORT)){
147             if(attribute.getValue() != null){
148                 setAddressValue(attribute.getValue().toString());
149             }
150         }else if(attribute.getName().equals(this.SHARED_MEM)){
151             if(attribute.getValue() != null){
152                     setAddressValue(attribute.getValue().toString());
153             }
154         }else if(attribute.getName().equals(this.DEBUG_OPTIONS)){
155             //Fix for bug# 4989322 - solaris does not support shmem
156
if((attribute.getValue() != null) && (attribute.getValue().toString().indexOf(ISMEM) == -1)){
157                 this.conn.setAttribute(this.configObjName, attribute);
158             }else{
159                 if(isWindows()){
160                     this.conn.setAttribute(this.configObjName, attribute);
161                 }
162               //ludo else
163
//ludo Util.setStatusBar(bundle.getString("Msg_SolarisShmem"));
164
}
165         }else{
166             this.conn.setAttribute(this.configObjName, attribute);
167         }
168     }
169     
170     private String JavaDoc getDebugOptions(){
171         Object JavaDoc debugOptionsAttr = getConfigAttributeValue(DEBUG_OPTIONS);
172         String JavaDoc debugOptionsVal = null;
173         if(debugOptionsAttr != null){
174             debugOptionsVal = debugOptionsAttr.toString().trim();
175         }
176         return debugOptionsVal;
177     }
178     
179     public String JavaDoc getAddressValue(){
180         String JavaDoc debugOptionsVal = getDebugOptions();
181         if(debugOptionsVal != null){
182             debugOptionsVal = debugOptionsVal.substring(debugOptionsVal.indexOf(DEBUG_OPTIONS_ADDRESS)+DEBUG_OPTIONS_ADDRESS.length(), debugOptionsVal.length());
183             int hasMore = debugOptionsVal.indexOf(","); //NOI18N
184
if(hasMore != -1){
185                 debugOptionsVal = debugOptionsVal.substring(0, hasMore);
186             }
187         }
188         return debugOptionsVal;
189     }
190     
191     public void setAddressValue(String JavaDoc portNumber){
192         try{
193             String JavaDoc debugOptionsVal = getDebugOptions();
194             String JavaDoc debugOptionsStart = debugOptionsVal.substring(0, debugOptionsVal.indexOf(DEBUG_OPTIONS_ADDRESS) + DEBUG_OPTIONS_ADDRESS.length());
195             debugOptionsStart = debugOptionsStart + portNumber;
196             String JavaDoc inMiddle = debugOptionsVal.substring(debugOptionsVal.indexOf(DEBUG_OPTIONS_ADDRESS) + DEBUG_OPTIONS_ADDRESS.length(), debugOptionsVal.length());
197             int hasMore = inMiddle.indexOf(","); //NOI18N
198
if(hasMore != -1){
199                 String JavaDoc debugOptionsEnd = inMiddle.substring(hasMore, inMiddle.length());
200                 debugOptionsStart = debugOptionsStart + debugOptionsEnd;
201             }
202             Attribute JavaDoc newAttr = new Attribute JavaDoc(DEBUG_OPTIONS, debugOptionsStart);
203             this.setAttribute(newAttr);
204         }catch(Exception JavaDoc ex){
205             //System.out.println("Cannot set debug options : " + ex.getMessage());
206
}
207     }
208     
209     public String JavaDoc getClassPath(){
210         String JavaDoc serverClassPath = getConfigAttributeValue("server_classpath"); //NOI18N
211
return serverClassPath;
212     }
213     
214     public void setClassPath(String JavaDoc classpath){
215         try{
216             Attribute JavaDoc classPathAttr = new Attribute JavaDoc("server_classpath", classpath); //NOI18N
217
this.setAttribute(classPathAttr);
218         }catch(Exception JavaDoc ex){
219             //System.out.println("Error in setClassPath " + ex.getMessage());
220
}
221     }
222     
223     public String JavaDoc getConfigAttributeValue(String JavaDoc attributeName){
224         return super.getAttribute(this.configObjName, attributeName);
225     }
226     
227     //Fix for bug#5020943 - switch between dt_shmem & dt_socket in JVM Options
228
public boolean isSharedMemory(){
229         boolean isShared = false;
230         String JavaDoc debugOpt = getDebugOptions();
231         if((debugOpt.indexOf(ISMEM) == -1) && (debugOpt.indexOf(ISSOCKET) == -1)){
232             try{
233                 Attribute JavaDoc attr = null;
234                 if(this.isServerEightOne)
235                     attr = new Attribute JavaDoc(DEBUG_OPTIONS, DEF_DEUG_OPTIONS_81);
236                 else
237                     attr = new Attribute JavaDoc(DEBUG_OPTIONS, DEF_DEUG_OPTIONS);
238                 setAttribute(attr);
239             }catch(Exception JavaDoc ex){
240                 //System.out.println("Invalid value for debug options. Resetting to default value ");
241
}
242         }else if(debugOpt.indexOf(ISSOCKET) != -1){
243             String JavaDoc addrValue = getAddressValue();
244             Integer JavaDoc value = null;
245             try{
246                 value = new Integer JavaDoc(addrValue);
247             }catch(Exception JavaDoc ex){
248                 if(this.isServerEightOne){
249                     value = new Integer JavaDoc(9009);
250                     setAddressValue("9009"); //NOI18N
251
}else{
252                     value = new Integer JavaDoc(1044);
253                     setAddressValue("1044"); //NOI18N
254
}
255             }
256         }else if (debugOpt.indexOf(ISMEM) != -1){
257             return true;
258         }
259         return isShared;
260     }
261     
262     //Fix for bug# 4989322 - solaris does not support shmem
263
public boolean isWindows(){
264         Object JavaDoc javaHome = getConfigAttributeValue(JAVA_HOME);
265         if (javaHome==null){//scary... assume not windows.
266
return false;
267         }
268         try{
269             //FIXME -- temporary workaround till plugin can map the env keys returned by appserver
270
//to actual values
271
String JavaDoc local_osName = System.getProperty("os.name"); //NOI18N
272
if(local_osName.toLowerCase().indexOf("windows") != -1) //NOI18N
273
return true;
274             else
275                 return false;
276             /*String javaHomeVal = javaHome.toString().trim();
277             String semiColon = javaHomeVal.substring(1,2);
278             if(semiColon.equals(":"))//NOI18N
279                 return true;
280             else
281                 return false;*/

282         }
283         catch (IndexOutOfBoundsException JavaDoc ee){
284             return false;
285         }
286     }
287     
288     //Fix for bug# 4989322 - solaris does not support shmem
289
/** API to check if the server is on solaris or windows
290      * @param: address value for dt_shmem.
291      * If windows, sets the transport value to dt_shmem
292      * If solaris, sets the transport value to dt_socket
293      * and if address is 1044, changes it to 11000 or leaves it as is.
294      */

295     public void setDefaultTransportForDebug(String JavaDoc addressVal) {
296         Attribute JavaDoc newAttr = null;
297         if(isWindows()){
298            String JavaDoc value = DEF_DEUG_OPTIONS_SHMEM;
299            if(addressVal != null)
300                value = value + addressVal;
301            else
302                value = value + "defaultAddress"; //NOI18N
303
newAttr = new Attribute JavaDoc(DEBUG_OPTIONS, value);
304         }else{
305             if((getAddressValue().equals("1044")) || (getAddressValue().equals("9009"))) {//NOI18N
306
newAttr = new Attribute JavaDoc(DEBUG_OPTIONS, DEF_DEUG_OPTIONS_SOCKET);
307             }
308         }
309         try{
310             if(newAttr != null){
311                 this.setAttribute(newAttr);
312             }
313         }catch(Exception JavaDoc ex){}
314     }
315
316 }
317
Popular Tags