KickJava   Java API By Example, From Geeks To Geeks.

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


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 /*
25  * TableHandlers.java
26  *
27  * Created on March 16, 2004, 3:13 PM
28  */

29
30 package com.sun.enterprise.tools.admingui.handlers;
31
32 import java.util.ArrayList JavaDoc;
33 import java.util.Iterator JavaDoc;
34 import java.util.List JavaDoc;
35
36 import com.iplanet.jato.RequestContext;
37 import com.iplanet.jato.RequestContextImpl;
38 import com.iplanet.jato.RequestManager;
39 import com.iplanet.jato.ViewBeanManager;
40 import com.iplanet.jato.model.DefaultModel;
41 import com.iplanet.jato.model.Model;
42 import com.iplanet.jato.util.RootCauseException;
43 import com.iplanet.jato.view.ContainerView;
44 import com.iplanet.jato.view.ContainerViewBase;
45 import com.iplanet.jato.view.View;
46 import com.iplanet.jato.view.ViewBase;
47 import com.iplanet.jato.view.ViewBean;
48 import com.iplanet.jato.view.DisplayField;
49
50 import javax.management.MBeanException JavaDoc;
51 import javax.management.ObjectName JavaDoc;
52 import javax.management.AttributeList JavaDoc;
53 import javax.management.Attribute JavaDoc;
54
55 import javax.servlet.ServletRequest JavaDoc;
56
57 import com.sun.enterprise.tools.guiframework.exception.FrameworkException;
58 import com.sun.enterprise.tools.guiframework.model.ModelManager;
59 import com.sun.enterprise.tools.guiframework.view.DescriptorContainerView;
60 import com.sun.enterprise.tools.guiframework.view.HandlerContext;
61 import com.sun.enterprise.tools.guiframework.view.ViewDescriptorManager;
62 import com.sun.enterprise.tools.guiframework.view.descriptors.ViewDescriptor;
63 import com.sun.enterprise.tools.guiframework.view.descriptors.CCActionTableDescriptor;
64 import com.sun.enterprise.tools.guiframework.view.event.BeforeCreateEvent;
65 import com.sun.enterprise.tools.guiframework.view.event.ErrorEvent;
66 import com.sun.web.ui.model.CCActionTableModelInterface;
67 import com.sun.web.ui.view.table.CCActionTable;
68 import com.sun.web.ui.view.html.CCButton;
69 import com.sun.web.ui.taglib.pagetitle.CCPageTitleTag;
70
71 import com.sun.enterprise.tools.admingui.util.MBeanUtil;
72 import com.sun.enterprise.tools.admingui.util.Util;
73 import com.sun.enterprise.tools.admingui.ConfigProperties;
74
75 import com.sun.enterprise.transaction.monitor.JTSMonitorMBean;
76
77 import com.sun.enterprise.admin.servermgmt.RuntimeStatus;
78 import com.sun.enterprise.admin.servermgmt.RuntimeStatusList;
79
80
81 /**
82  *
83  * @author ACER USER
84  */

85 public class TableHandlers {
86     
87   
88     
89     public void executeMethod(RequestContext ctx, HandlerContext handlerCtx) {
90         // Executes the given method on the selected rows of the table.
91
//
92
View view = handlerCtx.getView(); // this return the CCButton
93
DescriptorContainerView descView =
94             (DescriptorContainerView)(((ViewBase)view).getParentViewBean());
95     ViewDescriptor vd = descView.getViewDescriptor();
96     String JavaDoc childName = (String JavaDoc)handlerCtx.getInputValue("tableChildName");
97     if(childName == null) {
98         throw new FrameworkException("executeMethod: childName not specified", vd, view);
99     }
100         
101     ViewDescriptor tableDescriptor = vd.getChildDescriptor(childName);
102     if (tableDescriptor == null) {
103         throw new FrameworkException("executeMethod: tableDescriptor is null", vd, view);
104     }
105     if(!(tableDescriptor instanceof CCActionTableDescriptor)) {
106         throw new FrameworkException(
107                 "executeMethod: tableDescriptor is of wrong type", tableDescriptor, view);
108     }
109         CCActionTableModelInterface model =
110             ((CCActionTableDescriptor)tableDescriptor).getModel();
111     String JavaDoc method = (String JavaDoc)handlerCtx.getInputValue("method");
112         if (method == null) {
113         throw new FrameworkException("executeMethod: method name is null", vd, view);
114         }
115         
116     model.setRowSelectionType("multiple");
117         String JavaDoc objectName = null;
118     try {
119         model.beforeFirst();
120         while(model.next()) {
121         if (model.isRowSelected()) {
122                     objectName = (String JavaDoc)model.getValue("objectName");
123                     if (objectName == null)
124                         continue; // should be an error.
125
MBeanUtil.invoke(objectName, method, null, null);
126             model.setRowSelected(false);
127                 }
128             }
129     } catch (Exception JavaDoc ex) {
130         throw new FrameworkException("Error while invoking '" + method +
131                 "' on " + objectName, ex, tableDescriptor, view);
132         }
133     ContainerViewBase containerView =
134             (ContainerViewBase)(tableDescriptor.getView(ctx).getParent());
135     containerView.removeChild(tableDescriptor.getName());
136     ((DefaultModel)model).clear();
137     }
138     
139     public void deleteHandler(RequestContext ctx, HandlerContext handlerCtx) {
140     View view = handlerCtx.getView();
141     DescriptorContainerView descView = (DescriptorContainerView)(((ViewBase)view).getParentViewBean());
142     ViewDescriptor vd = descView.getViewDescriptor();
143     String JavaDoc childName = (String JavaDoc)handlerCtx.getInputValue("tableChildName");
144     if(childName == null) {
145         throw new FrameworkException("deleteHandler: childName not specified", vd, view);
146     }
147         
148     ViewDescriptor tableDescriptor = vd.getChildDescriptor(childName);
149     if (tableDescriptor == null) {
150         throw new FrameworkException("deleteHandler: tableDescriptor is null", vd, view);
151     }
152     if(!(tableDescriptor instanceof CCActionTableDescriptor)) {
153         throw new FrameworkException("deleteHandler: tableDescriptor is of wrong type", tableDescriptor, view);
154     }
155         CCActionTableModelInterface model = ((CCActionTableDescriptor)tableDescriptor).getModel();
156         
157     String JavaDoc deleteKey = (String JavaDoc)handlerCtx.getInputValue("deleteKey");
158     String JavaDoc deleteMethodName = (String JavaDoc)handlerCtx.getInputValue("deleteMethodName");
159     String JavaDoc objectName = (String JavaDoc)handlerCtx.getInputValue("rowObjectName");
160     String JavaDoc cascade = (String JavaDoc)handlerCtx.getInputValue("deleteWithCascade");
161     String JavaDoc deleteTarget = (String JavaDoc)handlerCtx.getInputValue("deleteTarget");
162     Boolean JavaDoc isResource = (Boolean JavaDoc)handlerCtx.getInputValue("isResource");
163         Boolean JavaDoc isLifecycleModule = (Boolean JavaDoc)handlerCtx.getInputValue("isLifecycleModule");
164     
165     if (objectName == null) {
166         throw new FrameworkException("No ObjectName specified", tableDescriptor, view);
167     }
168     if (deleteKey == null) {
169         throw new FrameworkException("No delete key specified", tableDescriptor, view);
170     }
171     if (deleteMethodName == null) {
172                 throw new FrameworkException("deleteMethodName must be specified", tableDescriptor, view);
173     }
174     model.setRowSelectionType("multiple");
175     try {
176         model.beforeFirst();
177         // from the model, get the child that has the needed value...
178
while(model.next()) {
179         if (model.isRowSelected()) {
180                     if (deleteMethodName != null) {
181                         deleteTableRow(model.getValue(deleteKey), objectName, deleteMethodName,
182                             cascade, deleteTarget, isResource, isLifecycleModule);
183                     }
184             model.setRowSelected(false);
185                 }
186             }
187     } catch (Exception JavaDoc ex) {
188         throw new FrameworkException("Error while deleting from: '"+
189         tableDescriptor.getName()+"'", ex, tableDescriptor, view);
190         }
191     ContainerViewBase containerView =
192             (ContainerViewBase)(tableDescriptor.getView(ctx).getParent());
193     containerView.removeChild(tableDescriptor.getName());
194     ((DefaultModel)model).clear();
195     }
196
197     //Need to write a new method bcz the signature is different.
198
public void deleteMBean(RequestContext ctx, HandlerContext handlerCtx) {
199     View view = handlerCtx.getView();
200     DescriptorContainerView descView = (DescriptorContainerView)(((ViewBase)view).getParentViewBean());
201     ViewDescriptor vd = descView.getViewDescriptor();
202     String JavaDoc childName = (String JavaDoc)handlerCtx.getInputValue("tableChildName");
203     if(childName == null) {
204         throw new FrameworkException("deleteHandler: childName not specified", vd, view);
205     }
206         
207     ViewDescriptor tableDescriptor = vd.getChildDescriptor(childName);
208     if (tableDescriptor == null) {
209         throw new FrameworkException("deleteHandler: tableDescriptor is null", vd, view);
210     }
211     if(!(tableDescriptor instanceof CCActionTableDescriptor)) {
212         throw new FrameworkException("deleteHandler: tableDescriptor is of wrong type", tableDescriptor, view);
213     }
214         CCActionTableModelInterface model = ((CCActionTableDescriptor)tableDescriptor).getModel();
215         
216     String JavaDoc deleteKey = (String JavaDoc)handlerCtx.getInputValue("deleteKey");
217     String JavaDoc deleteMethodName = (String JavaDoc)handlerCtx.getInputValue("deleteMethodName");
218     String JavaDoc objectName = (String JavaDoc)handlerCtx.getInputValue("rowObjectName");
219
220     if (objectName == null) {
221         throw new FrameworkException("No ObjectName specified", tableDescriptor, view);
222     }
223     if (deleteKey == null) {
224         throw new FrameworkException("No delete key specified", tableDescriptor, view);
225     }
226     if (deleteMethodName == null) {
227                 throw new FrameworkException("deleteMethodName must be specified", tableDescriptor, view);
228     }
229     model.setRowSelectionType("multiple");
230     try {
231         model.beforeFirst();
232         // from the model, get the child that has the needed value...
233
while(model.next()) {
234         if (model.isRowSelected()) {
235             String JavaDoc mbeanName = (String JavaDoc)model.getValue(deleteKey);
236             //get the references first, to delete all the associated targets.
237
String JavaDoc[] targets = null;
238             boolean isTargetSupported = ConfigProperties.getInstance().getTargetSupported().booleanValue();
239             if(isTargetSupported) {
240                 targets = getReferences(objectName, "listReferencees", mbeanName);
241             }
242             if(targets == null || targets.length == 0) {
243                 targets = new String JavaDoc[]{ConfigProperties.getInstance().getDefaultTarget()};
244             }
245             for(int i =0; i < targets.length; i++) {
246                 MBeanUtil.invoke(objectName, deleteMethodName, new Object JavaDoc[]{targets[i], mbeanName}, new String JavaDoc[]{"java.lang.String", "java.lang.String"});
247             }
248             
249             model.setRowSelected(false);
250                 }
251             }
252     } catch (Exception JavaDoc ex) {
253         throw new FrameworkException("Error while deleting from: '"+
254         tableDescriptor.getName()+"'", ex, tableDescriptor, view);
255         }
256     ContainerViewBase containerView =
257             (ContainerViewBase)(tableDescriptor.getView(ctx).getParent());
258     containerView.removeChild(tableDescriptor.getName());
259     ((DefaultModel)model).clear();
260     }
261
262     private String JavaDoc[] getReferences(String JavaDoc objectName, String JavaDoc methodName, String JavaDoc key) {
263         ObjectName JavaDoc[] refs = (ObjectName JavaDoc[])MBeanUtil.invoke(objectName, methodName, new Object JavaDoc[]{key}, new String JavaDoc[]{"java.lang.String"});
264         String JavaDoc[] targets = null;
265         if(refs != null) {
266             targets = new String JavaDoc[refs.length];
267         }
268         for (int i=0; refs != null && i < refs.length; i++) {
269             targets[i] = refs[i].getKeyProperty("name");
270         }
271         return targets;
272     }
273
274     private void deleteTableRow(Object JavaDoc key, String JavaDoc objectName, String JavaDoc methodName,
275             String JavaDoc cascade, String JavaDoc target, Boolean JavaDoc isResource, Boolean JavaDoc isLifecycleModule) throws Exception JavaDoc {
276         Object JavaDoc[] params = null;
277         String JavaDoc[] types = null;
278         
279         String JavaDoc defaultTarget = target;
280         if (isResource != null && isResource.booleanValue() == true) {
281             deleteResourceReferences(key.toString());
282             defaultTarget = ConfigProperties.getInstance().getDefaultTarget();
283         }
284         if (isLifecycleModule != null && isLifecycleModule.booleanValue() == true) {
285             deleteLifecycleReferences(key.toString());
286             defaultTarget = ConfigProperties.getInstance().getDefaultTarget();
287         }
288
289         if (cascade != null && cascade.equals("true")) {
290             if (defaultTarget == null) {
291                 params = new Object JavaDoc[]{key, new Boolean JavaDoc(true)};
292                 types = new String JavaDoc[]{"java.lang.String", "java.lang.Boolean"};
293             }
294             else {
295                 params = new Object JavaDoc[]{key, new Boolean JavaDoc(true), defaultTarget};
296                 types = new String JavaDoc[]{"java.lang.String", "java.lang.Boolean", "java.lang.String"};
297             }
298         }
299         else { // cascade == false
300
if (defaultTarget == null) {
301                 params = new Object JavaDoc[]{key};
302                 types = new String JavaDoc[]{"java.lang.String"};
303             }
304             else {
305                 params = new Object JavaDoc[]{key, defaultTarget};
306                 types = new String JavaDoc[]{"java.lang.String", "java.lang.String"};
307             }
308         }
309         //System.out.println("................. calling delete............");
310
//System.out.println(" "+objectName+" "+methodName);
311
//for (int i=0; i<params.length; i++)
312
// System.out.println(" "+params[i]);
313
MBeanUtil.invoke(objectName, methodName, params, types);
314     }
315     
316     private void deleteResourceReferences(String JavaDoc resourceName)
317         throws Exception JavaDoc {
318         if (ConfigProperties.getInstance().getTargetSupported().booleanValue() == false) {
319                 return;
320         }
321         Object JavaDoc[] param = new Object JavaDoc[]{resourceName};
322         String JavaDoc[] type = new String JavaDoc[]{"java.lang.String"};
323         ObjectName JavaDoc[] references =
324             (ObjectName JavaDoc[])MBeanUtil.invoke("com.sun.appserv:type=resources,category=config",
325                 "listReferencees",
326                 param, type);
327         for(int i = 0; i < references.length; i++) {
328             String JavaDoc targetName = (String JavaDoc)MBeanUtil.getAttribute(
329                 references[i], "name");
330             MBeanUtil.invoke(
331                 "com.sun.appserv:type=resources,category=config",
332                 "deleteResourceReference",
333                 new Object JavaDoc[]{targetName, resourceName},
334                 new String JavaDoc[]{"java.lang.String", "java.lang.String"});
335         }
336     }
337     
338     private void deleteLifecycleReferences(String JavaDoc lifecycleName)
339         throws Exception JavaDoc {
340         if (ConfigProperties.getInstance().getTargetSupported().booleanValue() == false) {
341                 return;
342         }
343         Object JavaDoc[] param = new Object JavaDoc[]{lifecycleName};
344         String JavaDoc[] type = new String JavaDoc[]{"java.lang.String"};
345         ObjectName JavaDoc[] references =
346             (ObjectName JavaDoc[])MBeanUtil.invoke("com.sun.appserv:type=applications,category=config",
347                 "listReferencees",
348                 param, type);
349         for(int i = 0; i < references.length; i++) {
350             String JavaDoc targetName = (String JavaDoc)MBeanUtil.getAttribute(
351                 references[i], "name");
352             MBeanUtil.invoke(
353                 "com.sun.appserv:type=applications,category=config",
354                 "removeLifecycleModuleReference",
355                 new Object JavaDoc[]{lifecycleName, targetName},
356                 new String JavaDoc[]{"java.lang.String", "java.lang.String"});
357         }
358     }
359     
360     
361     
362     private Object JavaDoc getName(Object JavaDoc obj) {
363         // monitor objects names are suitable for showing in the GUI, need to
364
// call "getName()" to get to show in the GUI.
365
if (obj instanceof String JavaDoc) {
366             String JavaDoc name = (String JavaDoc) obj;
367             if (name.indexOf("category=monitor") >= 0)
368                 return MBeanUtil.invoke(name, "getName", null, null);
369         }
370         return obj;
371     }
372     
373     private CCActionTableDescriptor getTableDescriptor(HandlerContext handlerCtx) {
374     View view = handlerCtx.getView();
375         // Find the CCActionTableDescriptor
376
ViewDescriptor desc = null;
377         View tableView = (View)handlerCtx.getInputValue("tableView");
378         if (tableView != null) {
379             desc = ((DescriptorContainerView)tableView).getViewDescriptor();
380         }
381         else if (handlerCtx.getEvent() instanceof BeforeCreateEvent) {
382             desc = ((BeforeCreateEvent)handlerCtx.getEvent()).getViewDescriptor();
383         } else if (view instanceof DescriptorContainerView) {
384             desc = ((DescriptorContainerView)view).getViewDescriptor();
385         }
386         if ((desc == null) || !(desc instanceof CCActionTableDescriptor)) {
387             throw new FrameworkException("Unable to determine "+
388                 "CCActionTableDescriptor. This handler should be invoked "+
389                 "from a CCActionTable.", desc, view);
390         }
391         return (CCActionTableDescriptor)desc;
392     }
393     
394     public void resetXMLinModel(RequestContext ctx, HandlerContext handlerCtx) {
395         CCActionTableDescriptor ccDesc = getTableDescriptor(handlerCtx);
396         ccDesc.resetXML();
397     }
398     
399     public void loadTableModel(RequestContext ctx, HandlerContext handlerCtx) {
400         // Get the CCActionTableModel
401
CCActionTableDescriptor ccDesc = getTableDescriptor(handlerCtx);
402         CCActionTableModelInterface model = ccDesc.getModel();
403         ((DefaultModel)model).clear();
404         
405         ArrayList JavaDoc displayNamesList = (ArrayList JavaDoc)handlerCtx.getInputValue("displayNames");
406         Object JavaDoc keysObj = handlerCtx.getInputValue("keys"); //object names
407
Object JavaDoc[] keys = null;
408         if (keysObj instanceof Object JavaDoc[]) {
409             keys = (Object JavaDoc[])keysObj;
410         } else if (keysObj instanceof List JavaDoc) {
411             List JavaDoc keysList = (List JavaDoc)keysObj;
412             keys = new Object JavaDoc[keysList.size()];
413             for (int i=0; i < keys.length; i++) {
414                keys[i] = keysList.get(i);
415             }
416         }
417
418         if (keys == null) {
419             return; //nothing to load..
420
}
421
422         ArrayList JavaDoc modelNamesList = (ArrayList JavaDoc)handlerCtx.getInputValue("attributeNames");
423         String JavaDoc[] modelNames = null;
424         if (modelNamesList != null)
425             modelNames = (String JavaDoc[])modelNamesList.toArray(new String JavaDoc[modelNamesList.size()]);
426         if (keys instanceof String JavaDoc[] || modelNames == null) {
427             // When there is no model names, use the ObjectName (keys) as the
428
// value for the display name.
429

430             for (int rowNo = 0; rowNo < keys.length; rowNo++) {
431                 model.appendRow();
432                 model.setValue((String JavaDoc)displayNamesList.get(0), getName(keys[rowNo].toString()));
433                 model.setValue("objectName", keys[rowNo].toString());
434             }
435         }
436         else if (keys instanceof ObjectName JavaDoc[]) {
437             ObjectName JavaDoc[] objNames = (ObjectName JavaDoc[])keys;
438             if (isMixed(modelNames)) {
439                 loadTable(objNames, modelNames, displayNamesList, model);
440             } else {
441                 loadTableByGroup(objNames, modelNames, displayNamesList, model);
442             }
443         }
444     }
445
446     /*
447     public void setTableColumn(RequestContext ctx, HandlerContext handlerCtx) throws Exception {
448         CCActionTableDescriptor tableDescriptorDesc = getTableDescriptor(handlerCtx);
449         CCActionTableModelInterface model = tableDescriptorDesc.getModel();
450         Object value = (handlerCtx.getInputValue("value"));
451         String columnName = (String)handlerCtx.getInputValue("columnName");
452         
453         try {
454             if (value instanceof Object[]) {
455                 Object[] valuesArray = (Object[])value;
456                 model.beforeFirst();
457                 for (int i = 0; i < valuesArray.length; i++) {
458                     if (!model.next())
459                         break;
460                     model.setValue(columnName, valuesArray[i]);
461                 }
462             }
463             else {
464                 model.beforeFirst();
465                 while(model.next())
466                     model.setValue(columnName, value);
467             }
468         } catch (Exception ex) {
469                 throw new FrameworkException("Error setting column : '" + columnName + "'", ex);
470         }
471     }
472     
473 */

474     private boolean isMixed(String JavaDoc[] names) {
475         // return true if the name list contains both attribute and method names
476
for (int i=0; i<names.length; i++) {
477             if (names[i] != null && names[i].indexOf('@') == 0)
478                 return true;
479         }
480         return false;
481     }
482     
483     private void loadTableByGroup(ObjectName JavaDoc[] objNames, String JavaDoc[] modelNames,
484             ArrayList JavaDoc displayNamesList, CCActionTableModelInterface model) {
485         for (int rowNo = 0; rowNo < objNames.length; rowNo++) {
486             // getAttributes for the given objectName...
487
AttributeList JavaDoc attrList =
488                 MBeanUtil.getAttributes(objNames[rowNo].toString(), modelNames);
489             model.appendRow();
490
491             for (int i = 0, colNo = 0; i < attrList.size(); i++) {
492                 Attribute JavaDoc attr = (Attribute JavaDoc)attrList.get(i);
493
494                 while (colNo < modelNames.length &&
495                        ! attr.getName().equals(modelNames[colNo])) {
496                     colNo++;
497                 }
498                 if (colNo < modelNames.length) {
499                     Object JavaDoc attrValue = attr.getValue();
500                     if (attrValue != null) {
501                         attrValue = attrValue.toString();
502                     }
503                     model.setValue((String JavaDoc)displayNamesList.get(colNo),
504                                     getName(attr.getValue()));
505                 }
506             }
507             model.setValue("objectName", objNames[rowNo].toString());
508             model.setValue("objectType", objNames[rowNo].getKeyProperty("type"));
509         }
510     }
511
512     
513     private void loadTable(ObjectName JavaDoc[] objNames, String JavaDoc[] modelNames,
514             ArrayList JavaDoc displayNamesList, CCActionTableModelInterface model) {
515         // loads the table with attribute values either from individual calls
516
// to get the attr value or by a method call.
517
for (int rowNo = 0; rowNo < objNames.length; rowNo++) {
518             // getAttributes for the given objectName...
519
model.appendRow();
520             
521             for (int colNo = 0; colNo < modelNames.length; colNo++) {
522                 model.setValue((String JavaDoc)displayNamesList.get(colNo),
523                     getValue(objNames[rowNo], modelNames[colNo]));
524                 model.setValue("objectName", objNames[rowNo].toString());
525                 model.setValue("objectType", objNames[rowNo].getKeyProperty("type"));
526             }
527         }
528     }
529
530     private String JavaDoc getValue(ObjectName JavaDoc objName, String JavaDoc attrName) {
531         Object JavaDoc value = null;
532         if (attrName.indexOf('@') == 0 && attrName.length() > 3) {
533             String JavaDoc method = attrName.substring(2, attrName.length()-1);
534             try {
535                 value = MBeanUtil.invoke(objName, method, null, null);
536             } catch (Exception JavaDoc ex) {
537                 if (method.equals("getRuntimeStatus"))
538                     // special case hack
539
return TargetHandlers.getStatusHtml(value);
540                 else
541                     value = ex.getMessage();
542             }
543         } else {
544             value = MBeanUtil.getAttribute(objName, attrName);
545         }
546         String JavaDoc result = "";
547         if (value != null) {
548             if (value instanceof RuntimeStatus || value instanceof RuntimeStatusList) {
549                 result = TargetHandlers.getStatusHtml(value);
550             } else {
551                 result = value.toString();
552             }
553         }
554         return result;
555     }
556     
557     /**
558      *
559      * <P>ENSURE_MODEL_ROWS_ARRAY is a request attribute that contains the
560      * array to iterate over, ENSURE_MODEL_ROWS_MODEL should contain the model
561      * to ensure has the right number of rows.</P>
562      */

563     public void ensureModelRows(RequestContext ctx, HandlerContext handlerCtx) {
564         ViewDescriptor desc = null;
565         if (handlerCtx.getEvent() instanceof BeforeCreateEvent)
566             desc = ((BeforeCreateEvent)handlerCtx.getEvent()).getViewDescriptor();
567         else {
568         throw new FrameworkException("ensureModelRows can only be called in BeforeCreate event handler!", desc, handlerCtx.getView());
569         }
570         String JavaDoc dummyArrayName = "";
571         while (desc != null) {
572             if (dummyArrayName.equals("") == false)
573                 dummyArrayName = "." + dummyArrayName;
574             dummyArrayName = desc.getName() + dummyArrayName;
575             desc = desc.getParent();
576         }
577         dummyArrayName += "." + "rowCounter";
578
579         ServletRequest JavaDoc req = ctx.getRequest();
580     Object JavaDoc arr[] = (Object JavaDoc [])req.getParameterValues(dummyArrayName);
581
582         // Get the ServletRequest
583
//Object arr[] = (Object [])req.getAttribute(ENSURE_MODEL_ROWS_ARRAY);
584
if (arr == null) {
585         // Nothing to do
586
return;
587     }
588
589     // NOTE: CCActionTableModelInterface defines appendRow() and
590
// getNumRows()... might want to use this model instead.
591
// However, DefaultModel is more generic, so I am using this for
592
// now.
593
DefaultModel model = (DefaultModel)handlerCtx.getInputValue(MODEL);
594     if (model == null) {
595         throw new FrameworkException("Model cannot be null!", desc, handlerCtx.getView());
596     }
597
598     // Iterate over the array, adding rows to the Model
599
int rowsToAdd = arr.length;
600     rowsToAdd -= model.getNumRows();
601     for (int count=0; count<rowsToAdd; count++) {
602         model.appendRow();
603     }
604     }
605     
606     
607     //FIXME : more work to do here....
608
public void handleTableMenuAction(RequestContext ctx, HandlerContext handlerCtx) {
609         String JavaDoc action = (String JavaDoc)handlerCtx.getInputValue("action");
610
611         ViewDescriptor desc = null;
612         View tableView = (View)handlerCtx.getInputValue("tableView");
613         if (tableView != null) {
614             desc = ((DescriptorContainerView)tableView).getViewDescriptor();
615         }
616         CCActionTableDescriptor ccDesc = (CCActionTableDescriptor)desc;
617         
618         CCActionTableModelInterface model = ccDesc.getModel();
619     try {
620         model.beforeFirst();
621         // from the model, get the child that has the needed value...
622
while(model.next()) {
623                 if (Util.isLoggableFINER())
624                     Util.logFINER("isRowSelected = " + model.isRowSelected());
625             }
626         }catch (Exception JavaDoc ex){
627             ex.printStackTrace();
628         }
629     }
630
631     public void getTableFilterValue(RequestContext ctx, HandlerContext handlerCtx) {
632         String JavaDoc filterValue = (String JavaDoc)handlerCtx.getInputValue("displayFieldValue");
633         if (filterValue != null &&
634             filterValue.equals(CCActionTable.FILTER_ALL_ITEMS_OPTION)) {
635                 filterValue = null;
636         }
637         handlerCtx.setOutputValue("filterValue", filterValue);
638     }
639     
640     public void loadTransactionIds(RequestContext ctx, HandlerContext handlerCtx) {
641     View view = handlerCtx.getView();
642         // Find the CCActionTableDescriptor
643
ViewDescriptor desc = null;
644         if (handlerCtx.getEvent() instanceof BeforeCreateEvent) {
645             desc = ((BeforeCreateEvent)handlerCtx.getEvent()).getViewDescriptor();
646         } else if (view instanceof DescriptorContainerView) {
647             desc = ((DescriptorContainerView)view).getViewDescriptor();
648         }
649         if ((desc == null) || !(desc instanceof CCActionTableDescriptor)) {
650             throw new FrameworkException("Unable to determine "+
651                 "CCActionTableDescriptor. This handler should be invoked "+
652                 "from a CCActionTable.", desc, view);
653         }
654         // Get the CCActionTableModel
655
CCActionTableDescriptor ccDesc = (CCActionTableDescriptor)desc;
656         CCActionTableModelInterface model = ccDesc.getModel();
657
658         ArrayList JavaDoc displayName = (ArrayList JavaDoc)handlerCtx.getInputValue("displayNames");
659         List JavaDoc maps = (List JavaDoc)handlerCtx.getInputValue("maps");
660
661         if (maps == null) {
662             return; //nothing to load..
663
}
664
665         for (Iterator JavaDoc iter = maps.iterator(); iter.hasNext();) {
666             java.util.Map JavaDoc m = (java.util.Map JavaDoc)iter.next();
667             model.appendRow();
668             model.setValue((String JavaDoc)displayName.get(0), (String JavaDoc)m.get(JTSMonitorMBean.TRANSACTION_ID));
669             model.setValue((String JavaDoc)displayName.get(1), (String JavaDoc)m.get(JTSMonitorMBean.STATE));
670             model.setValue((String JavaDoc)displayName.get(2), (String JavaDoc)m.get(JTSMonitorMBean.ELAPSED_TIME));
671             model.setValue((String JavaDoc)displayName.get(3), (String JavaDoc)m.get(JTSMonitorMBean.COMPONENT_NAME));
672             model.setValue((String JavaDoc)displayName.get(4), (String JavaDoc)m.get(JTSMonitorMBean.RESOURCE_NAMES));
673         }
674     }
675     
676     public void setTransactionPageTitle(RequestContext ctx, HandlerContext handlerCtx) {
677         Object JavaDoc obj = handlerCtx.getEvent().getSource();
678         //System.out.println("OBJECT ="+obj.getClass().getName());
679
CCPageTitleTag tag = (CCPageTitleTag)handlerCtx.getEvent().getSource();
680         AttributeList JavaDoc state = (AttributeList JavaDoc)handlerCtx.getInputValue("state");
681         String JavaDoc status = Util.getMessage("common.unknown");
682         if (state != null && state.get(0) != null) {
683             String JavaDoc value = (String JavaDoc)((Attribute JavaDoc)state.get(0)).getValue();
684             status = value.equals("False")?Util.getMessage("transactionId.UnFreeze"):Util.getMessage("transactionId.Freeze");
685         }
686         String JavaDoc bundle = "com.sun.enterprise.tools.admingui.resources.Resources";
687         String JavaDoc pageTitle = tag.getPageTitleText();
688         String JavaDoc localizedString = Util.getMessage(bundle, pageTitle, new Object JavaDoc[]{status});
689         tag.setPageTitleText(localizedString);
690     }
691     
692     public void setTransactionDisplayFieldValues(RequestContext ctx, HandlerContext handlerCtx) {
693         View childView = handlerCtx.getView();
694     DescriptorContainerView view = (DescriptorContainerView)(((ViewBase)childView).getParentViewBean());
695         AttributeList JavaDoc state = (AttributeList JavaDoc)handlerCtx.getInputValue("state");
696         String JavaDoc value = (String JavaDoc)((Attribute JavaDoc)state.get(0)).getValue();
697         String JavaDoc status = value.equals("False")?Util.getMessage("transactionId.Freeze"):Util.getMessage("transactionId.UnFreeze");
698         DisplayField stateField = (DisplayField)view.getDisplayField("Status");
699     stateField.setValue(value);
700         CCButton button = (CCButton)childView;
701         button.setDisplayLabel(status);
702     }
703     
704     public void setTransactionState(RequestContext ctx, HandlerContext handlerCtx) {
705         String JavaDoc objectName = (String JavaDoc)handlerCtx.getInputValue("objectName");
706         View childView = handlerCtx.getView();
707     DescriptorContainerView view = (DescriptorContainerView)(((ViewBase)childView).getParentViewBean());
708         String JavaDoc state = (String JavaDoc)view.getDisplayFieldValue("Status");
709         String JavaDoc methodName = state.equals("False")?"freeze":"unfreeze";
710         MBeanUtil.invoke(objectName, methodName, null, null);
711     }
712
713     private void deleteCluster(String JavaDoc clusterName) {
714         removeLbRefs(clusterName); // first see if there are load balancer refs and remove.
715
boolean running = true;
716         // see if the cluster is running...
717
try {
718             RuntimeStatusList status = (RuntimeStatusList)MBeanUtil.invoke(
719                 "com.sun.appserv:type=clusters,category=config",
720                 "getRuntimeStatus",
721                 new Object JavaDoc[]{clusterName},
722                 new String JavaDoc[]{"java.lang.String"});
723                 running = status.anyRunning();
724         } catch (Exception JavaDoc ex) {
725             // log the error and keep going.
726
if (Util.isLoggableFINE())
727                 Util.logFINE(ex);
728         }
729         if (running) {
730             // try to stop the cluster.
731
try {
732                 MBeanUtil.invoke(
733                     "com.sun.appserv:type=clusters,category=config",
734                     "stopCluster",
735                     new Object JavaDoc[]{clusterName},
736                     new String JavaDoc[]{"java.lang.String"});
737             } catch (Exception JavaDoc ex) {
738                 // if can't stop a running cluster, bail out.
739
throw new FrameworkException(ex);
740             }
741         }
742     try {
743             // get the list of instances of the cluster
744
ObjectName JavaDoc[] objectNames = (ObjectName JavaDoc[])MBeanUtil.invoke(
745                 "com.sun.appserv:type=servers,category=config",
746                 "listServerInstances",
747                 new Object JavaDoc[]{clusterName},
748                 new String JavaDoc[]{"java.lang.String"});
749             // delete the instances one by one.
750
for (int i = 0; i < objectNames.length; i++) {
751                 //System.out.print(">>> objectNames[i]: " + objectNames[i]);
752
String JavaDoc instanceName = objectNames[i].getKeyProperty("name");
753                 
754                 String JavaDoc[] modules = (String JavaDoc[])MBeanUtil.invoke(
755                     "com.sun.appserv:type=servers,category=config",
756                     "deleteServerInstance",
757                     new Object JavaDoc[]{instanceName},
758                     new String JavaDoc[]{"java.lang.String"});
759             }
760             // delete the cluster itself.
761
MBeanUtil.invoke(
762                 "com.sun.appserv:type=clusters,category=config",
763                 "deleteCluster",
764                 new Object JavaDoc[]{clusterName},
765                 new String JavaDoc[]{"java.lang.String"});
766     } catch (Exception JavaDoc ex) {
767             if (Util.isLoggableFINE()) {
768                 Util.logFINE(ex);
769             }
770             throw new FrameworkException(ex);
771         }
772     }
773     
774    private void removeLbRefs(String JavaDoc clusterName) {
775        Object JavaDoc params[] = {clusterName};
776        String JavaDoc[] types= new String JavaDoc[]{"java.lang.String"};
777        String JavaDoc[] lbs = null;
778        // first see if there are lb refs...
779
try {
780            lbs = (String JavaDoc[])MBeanUtil.invoke("com.sun.appserv:type=olbadmin,category=config", "getLBConfigsForCluster", params, types);
781            
782        } catch (Exception JavaDoc ex) {
783            // log the error and keep going.
784
if (Util.isLoggableFINE())
785                Util.logFINE(ex);
786        }
787        // remove lb refs from cluster
788
if (lbs != null) {
789            try {
790                for (int i = 0; i < lbs.length; i++) {
791                   MBeanUtil.invoke("com.sun.appserv:type=lb-config,name="+lbs[i]+",category=config", "removeClusterRefByRef", params, types);
792                }
793            } catch (Exception JavaDoc ex) {
794               throw new FrameworkException(ex);
795            }
796        }
797     }
798    
799     public void deleteClusterAndInstances(RequestContext ctx, HandlerContext handlerCtx) {
800         // Executes the given method on the selected rows of the table.
801
//
802
View view = handlerCtx.getView(); // this return the CCButton
803
DescriptorContainerView descView =
804             (DescriptorContainerView)(((ViewBase)view).getParentViewBean());
805     ViewDescriptor vd = descView.getViewDescriptor();
806     String JavaDoc childName = (String JavaDoc)handlerCtx.getInputValue("tableChildName");
807     if(childName == null) {
808         throw new FrameworkException(
809                 "deleteClusterAndInstances: childName not specified", vd, view);
810     }
811         
812     ViewDescriptor tableDescriptor = vd.getChildDescriptor(childName);
813     if (tableDescriptor == null) {
814         throw new FrameworkException(
815                 "deleteClusterAndInstances: tableDescriptor is null", vd, view);
816     }
817     if(!(tableDescriptor instanceof CCActionTableDescriptor)) {
818         throw new FrameworkException(
819                 "deleteClusterAndInstances: tableDescriptor is of wrong type", tableDescriptor, view);
820     }
821         CCActionTableModelInterface model =
822             ((CCActionTableDescriptor)tableDescriptor).getModel();
823         
824     model.setRowSelectionType("multiple");
825         String JavaDoc objectName = null;
826     try {
827         model.beforeFirst();
828         while(model.next()) {
829         if (model.isRowSelected()) {
830                     objectName = (String JavaDoc)model.getValue("objectName");
831                     if (objectName == null)
832                         continue; // should be an error.
833
deleteCluster(new ObjectName JavaDoc(objectName).getKeyProperty("name"));
834             model.setRowSelected(false);
835                 }
836             }
837     } catch (Exception JavaDoc ex) {
838             if (Util.isLoggableFINE())
839                 Util.logFINE(ex);
840             throw new FrameworkException(ex);
841         }
842     ContainerViewBase containerView =
843             (ContainerViewBase)(tableDescriptor.getView(ctx).getParent());
844     containerView.removeChild(tableDescriptor.getName());
845     ((DefaultModel)model).clear();
846     }
847     
848     public static final String JavaDoc MODEL = "model";
849 }
850
Popular Tags