KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > enterprise > tools > admingui > handlers > JVMOptionsHandler


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 package com.sun.enterprise.tools.admingui.handlers;
25
26 import java.util.ArrayList JavaDoc;
27 import java.util.EventObject JavaDoc;
28
29 import com.iplanet.jato.view.View;
30 import com.iplanet.jato.view.ViewBase;
31 import com.iplanet.jato.view.ViewBean;
32 import com.iplanet.jato.view.html.SelectableGroup;
33 import com.iplanet.jato.view.html.OptionList;
34 import com.iplanet.jato.view.ContainerViewBase;
35 import com.iplanet.jato.RequestContext;
36 import com.iplanet.jato.RequestManager;
37 import com.iplanet.jato.model.DefaultModel;
38 import com.iplanet.jato.model.Model;
39 import com.iplanet.jato.model.ModelControlException;
40
41 import javax.management.MBeanException JavaDoc;
42 import javax.management.ObjectName JavaDoc;
43 import javax.management.AttributeList JavaDoc;
44 import javax.management.Attribute JavaDoc;
45
46 import com.sun.enterprise.tools.guiframework.exception.FrameworkException;
47 import com.sun.enterprise.tools.guiframework.model.ModelManager;
48 import com.sun.enterprise.tools.guiframework.view.DescriptorCCPageTitle;
49 import com.sun.enterprise.tools.guiframework.view.DescriptorContainerView;
50 import com.sun.enterprise.tools.guiframework.view.HandlerContext;
51 import com.sun.enterprise.tools.guiframework.view.descriptors.ViewDescriptor;
52 import com.sun.enterprise.tools.guiframework.view.descriptors.CCActionTableDescriptor;
53 import com.sun.enterprise.tools.guiframework.view.event.BeforeCreateEvent;
54
55 import com.sun.web.ui.model.CCActionTableModelInterface;
56
57 import com.sun.enterprise.tools.admingui.util.MBeanUtil;
58
59
60 public class JVMOptionsHandler {
61
62     private CCActionTableModelInterface getModel(HandlerContext handlerCtx, ViewDescriptor desc) {
63         CCActionTableModelInterface model =
64             (CCActionTableModelInterface)handlerCtx.getInputValue("model");
65         model.setRowSelectionType("multiple");
66
67         if (model == null) {
68             throw new FrameworkException("JVMOptionsHandler.getModel: No Model Specified.",
69                 desc, handlerCtx.getView());
70         }
71         return model;
72     }
73
74     private void loadModel(CCActionTableModelInterface model, String JavaDoc [] list) throws ModelControlException {
75         model.beforeFirst();
76         for(int rowNo = 0; rowNo < list.length; rowNo++) {
77             model.appendRow();
78             model.setValue(PROPERTY_VALUE, list[rowNo]);
79             model.setRowSelected(false);
80         }
81     }
82
83     public void populateModel(RequestContext ctx, HandlerContext handlerCtx) {
84     View view = handlerCtx.getView();
85         ViewDescriptor desc = null;
86         if (handlerCtx.getEvent() instanceof BeforeCreateEvent) {
87             desc = ((BeforeCreateEvent)handlerCtx.getEvent()).getViewDescriptor();
88     } else {
89             DescriptorContainerView descView = (DescriptorContainerView)
90                 (((ViewBase)view).getParentViewBean());
91             desc = descView.getViewDescriptor();
92         }
93
94         String JavaDoc objectName = (String JavaDoc)handlerCtx.getInputValue(OBJECT_NAME);
95         String JavaDoc[] jvmOptions = null;
96         try {
97             jvmOptions = (String JavaDoc[])MBeanUtil.getAttribute(new ObjectName JavaDoc(objectName), ATTRIBUTE_NAME);
98         } catch (Exception JavaDoc ex) {
99             // the above method will throw an exception when the object doesn't exist to get the attr.
100
return;
101             //throw new FrameworkException("populatePropsTableModel: Loading error. ", ex,
102
// desc, view);
103
}
104
105         try {
106             CCActionTableModelInterface model = getModel(handlerCtx, desc);
107             ((DefaultModel)model).clear();
108             loadModel(model, jvmOptions);
109         } catch (Exception JavaDoc ex) {
110             throw new FrameworkException("populatePropsTableModel: Loading error. ", ex,
111                 desc, view);
112         }
113     }
114
115     public void addProperty(RequestContext ctx, HandlerContext handlerCtx) {
116     View view = handlerCtx.getView();
117     DescriptorContainerView descView = (DescriptorContainerView)
118         (((ViewBase)view).getParentViewBean());
119     ViewDescriptor vd = descView.getViewDescriptor();
120
121     try {
122         CCActionTableModelInterface model = getModel(handlerCtx, vd);
123         model.appendRow();
124         model.setValue(PROPERTY_VALUE, "");
125         model.beforeFirst();
126         } catch (Exception JavaDoc ex) {
127             throw new FrameworkException("JVMOptionsHandler.addProperty: ", ex,
128                 vd, view);
129     }
130     }
131
132
133     public void updateOptions(RequestContext ctx, HandlerContext handlerCtx) {
134     View view = handlerCtx.getView();
135     DescriptorContainerView descView = (DescriptorContainerView)
136         (((ViewBase)view).getParentViewBean());
137     ViewDescriptor vd = descView.getViewDescriptor();
138
139         String JavaDoc objectName = (String JavaDoc)handlerCtx.getInputValue(OBJECT_NAME);
140         CCActionTableModelInterface model = getModel(handlerCtx, vd);
141     ArrayList JavaDoc options = new ArrayList JavaDoc();
142
143     try {
144             model.beforeFirst();
145             while(model.next()) {
146                 String JavaDoc value = (String JavaDoc) model.getValue(PROPERTY_VALUE);
147                 if (value != null && (! value.trim().equals(""))) {
148                     options.add(value);
149                     model.setRowSelected(false);
150                 }
151             }
152             String JavaDoc[] jvmOptions = (String JavaDoc[])options.toArray(new String JavaDoc[0]);
153             //set the new values
154
Attribute JavaDoc attr = new Attribute JavaDoc(ATTRIBUTE_NAME, jvmOptions);
155             MBeanUtil.setAttribute(objectName, attr);
156
157             ((DefaultModel)model).clear();
158             // The model will be reloaded from the backend on the beginDisplay Event
159
} catch (Exception JavaDoc ex) {
160             throw new FrameworkException("JVMOptionsHandler.updateOptions: ", ex,
161                 vd, view);
162     }
163     }
164
165     public void deleteOptions(RequestContext ctx, HandlerContext handlerCtx) {
166     View view = handlerCtx.getView();
167     DescriptorContainerView descView = (DescriptorContainerView)
168         (((ViewBase)view).getParentViewBean());
169     ViewDescriptor vd = descView.getViewDescriptor();
170
171         String JavaDoc objectName = (String JavaDoc)ctx.getRequest().getAttribute(OBJECT_NAME);
172     //using the same method for both save, and delete based on the option clicked.
173

174         CCActionTableModelInterface model = getModel(handlerCtx, vd);
175     ArrayList JavaDoc options = new ArrayList JavaDoc();
176
177     try {
178             model.beforeFirst();
179             while(model.next()) {
180                 String JavaDoc value = (String JavaDoc) model.getValue(PROPERTY_VALUE);
181                 if (value != null && (! value.trim().equals(""))) {
182                     if (model.isRowSelected() == false) {
183                         options.add(value);
184                         //model.setRowSelected(false);
185
}
186                 }
187             }
188             String JavaDoc[] jvmOptions = (String JavaDoc[])options.toArray(new String JavaDoc[0]);
189             ((DefaultModel)model).clear();
190             loadModel(model, jvmOptions);
191         } catch (Exception JavaDoc ex) {
192             throw new FrameworkException("JVMOptionsHandler.deleteOptions: ", ex,
193                 vd, view);
194     }
195     }
196     
197     public void getSecurityManagerStatus(RequestContext ctx, HandlerContext handlerCtx) {
198     
199         String JavaDoc objectName = (String JavaDoc)handlerCtx.getInputValue("objectName");
200         if (objectName == null){
201              throw new FrameworkException("JVMOptionsHandler.getSecurityManagerStatus requires INPUT objectName");
202         }
203                 
204         Boolean JavaDoc status = isSecurityManagerEnabled(objectName);
205         handlerCtx.setOutputValue("securityManagerEnabled", status.toString());
206     }
207     
208     
209     private Boolean JavaDoc isSecurityManagerEnabled(String JavaDoc objectName){
210         String JavaDoc[] jvmOptions = null;
211         try {
212             jvmOptions = (String JavaDoc[])MBeanUtil.getAttribute(new ObjectName JavaDoc(objectName), ATTRIBUTE_NAME);
213             if (jvmOptions != null){
214                 for(int i=0; i< jvmOptions.length; i++){
215                     if (jvmOptions[i].trim().equals(JVM_OPTION_SECURITY_MANAGER) ||
216                             jvmOptions[i].trim().startsWith(JVM_OPTION_SECURITY_MANAGER_WITH_EQUAL)){
217                         return Boolean.TRUE;
218                     }
219                 }
220             }
221         } catch (Exception JavaDoc ex) {
222             // the above method will throw an exception when the object doesn't exist to get the attr.
223
}
224         return Boolean.FALSE;
225     }
226     
227     
228     public void setSecurityManager(RequestContext ctx, HandlerContext handlerCtx) {
229     
230         String JavaDoc objectName = (String JavaDoc)handlerCtx.getInputValue("objectName");
231         String JavaDoc value = (String JavaDoc)handlerCtx.getInputValue("value");
232         if (objectName == null || value == null){
233              throw new FrameworkException("JVMOptionsHandler.setSecurityManager requires INPUT objectName and value");
234         }
235         Boolean JavaDoc status = isSecurityManagerEnabled(objectName);
236         Boolean JavaDoc userValue = new Boolean JavaDoc(value);
237         if (status.equals(userValue)){
238             //no need to change
239
return;
240         }
241         ArrayList JavaDoc newOptions = new ArrayList JavaDoc();
242         try {
243             String JavaDoc[] origOptions = (String JavaDoc[])MBeanUtil.getAttribute(new ObjectName JavaDoc(objectName), ATTRIBUTE_NAME);
244             if(userValue){
245                 for(int i=0; i<origOptions.length; i++){
246                     newOptions.add(origOptions[i]);
247                 }
248                 newOptions.add(JVM_OPTION_SECURITY_MANAGER);
249             }else{
250                 for(int i=0; i<origOptions.length; i++){
251                     if (! (origOptions[i].trim().equals(JVM_OPTION_SECURITY_MANAGER) ||
252                             origOptions[i].trim().startsWith(JVM_OPTION_SECURITY_MANAGER_WITH_EQUAL))){
253                        newOptions.add(origOptions[i]);
254                     }
255                 }
256             }
257             
258         } catch (Exception JavaDoc ex) {
259             // the above method will throw an exception when the object doesn't exist to get the attr.
260
}
261         String JavaDoc[] jvmOptions = (String JavaDoc[])newOptions.toArray(new String JavaDoc[0]);
262         //set the new values
263
Attribute JavaDoc attr = new Attribute JavaDoc(ATTRIBUTE_NAME, jvmOptions);
264         MBeanUtil.setAttribute(objectName, attr);
265     }
266
267     //viewxml file Attribute Names
268
public static final String JavaDoc OBJECT_NAME = "objectName";
269     //DisplayField Name
270
public static final String JavaDoc PROPERTY_VALUE = "propertyValue";
271     //mbean Attribute Name
272
private static final String JavaDoc ATTRIBUTE_NAME = "jvm-options";
273     
274     private static final String JavaDoc JVM_OPTION_SECURITY_MANAGER = "-Djava.security.manager";
275     private static final String JavaDoc JVM_OPTION_SECURITY_MANAGER_WITH_EQUAL = "-Djava.security.manager=";
276 }
277
Popular Tags