KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > enterprise > cli > commands > DisplayErrorDistributionCommand


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.cli.commands;
25
26 import com.sun.enterprise.cli.framework.*;
27 import java.util.HashMap JavaDoc;
28 import javax.management.MBeanServerConnection JavaDoc;
29 import javax.management.ObjectName JavaDoc;
30 import java.util.Map JavaDoc;
31 import java.util.HashMap JavaDoc;
32 import java.util.Iterator JavaDoc;
33
34
35 /**
36  * This class is the implementation for display-error-distribution command.
37  **/

38
39 public class DisplayErrorDistributionCommand extends S1ASCommand
40 {
41     /**
42      * A method that Executes the command
43      * @throws CommandException
44      */

45     public void runCommand() throws CommandException, CommandValidationException
46     {
47         if (!validateOptions())
48             throw new CommandValidationException("Validation is false");
49         
50         //use http connector
51
MBeanServerConnection JavaDoc mbsc = getMBeanServerConnection(getHost(), getPort(),
52                                                               getUser(), getPassword());
53         final String JavaDoc objectName = getObjectName();
54         final String JavaDoc operationName = getOperationName();
55         final long timeStamp = new Long JavaDoc((String JavaDoc)getOperands().get(0)).longValue();
56         
57         try
58         {
59             // get the modules with SEVERE count
60
Object JavaDoc[] params = new Object JavaDoc[]{timeStamp, "SEVERE"};
61             String JavaDoc[] types = getTypesInfo();
62             Map JavaDoc returnMap = (Map JavaDoc) mbsc.invoke(new ObjectName JavaDoc(objectName),
63                                                 operationName, params, types);
64             Map JavaDoc errorsMap = handleErrorDistribution(returnMap,
65                                                         new HashMap JavaDoc(), true);
66             
67             // get the modules with WARNING count
68
params = new Object JavaDoc[]{timeStamp, "WARNING"};
69             returnMap = (Map JavaDoc) mbsc.invoke(new ObjectName JavaDoc(objectName),
70                                                 operationName, params, types);
71             errorsMap = handleErrorDistribution(returnMap, errorsMap, false);
72
73             //Display the error distribution
74
displayErrorDistribution(errorsMap);
75         }
76         catch(Exception JavaDoc e)
77         {
78             displayExceptionMessage(e);
79         }
80
81         CLILogger.getInstance().printDetailMessage(getLocalizedString(
82                                                    "CommandSuccessful",
83                                                    new Object JavaDoc[] {name}));
84     }
85
86
87     private Map JavaDoc handleErrorDistribution(Map JavaDoc errorMap, Map JavaDoc resultMap, boolean isSevere)
88     {
89         java.util.Set JavaDoc<String JavaDoc> keySet = errorMap.keySet();
90         for (String JavaDoc module : keySet) {
91             Integer JavaDoc count = (Integer JavaDoc) errorMap.get(module);
92             if (count > 0)
93             {
94                 if (isSevere)
95                 {
96                     int[] errorCounts = new int[]{count,0};
97                     resultMap.put(module, errorCounts);
98                 }
99                 else
100                 {
101                     if (resultMap.containsKey(module))
102                     {
103                         int[] errorCounts = (int[])resultMap.get(module);
104                         errorCounts[1] = count;
105                         resultMap.put(module, errorCounts);
106                     }
107                     else
108                     {
109                         int[] errorCounts = new int[]{0, count};
110                         resultMap.put(module, errorCounts);
111                     }
112                 }
113             }
114         }
115         return resultMap;
116     }
117
118     
119     private void displayErrorDistribution(Map JavaDoc errorsMap)
120     {
121         if (errorsMap.size() > 0)
122         {
123             //display header
124
String JavaDoc sTitle = String.format("%1$-9s %2$-14s %3$-40s",
125                                           getLocalizedString("Severity"),
126                                           getLocalizedString("Warning"),
127                                           getLocalizedString("ModuleID"));
128            
129             CLILogger.getInstance().printDetailMessage(sTitle);
130            
131             CLILogger.getInstance().printDetailMessage("---------------------------------------------------------");
132             
133         }
134         else
135         {
136             CLILogger.getInstance().printMessage(getLocalizedString("NoElementsToList"));
137             return;
138         }
139         java.util.Set JavaDoc<String JavaDoc> keySet = errorsMap.keySet();
140         for (String JavaDoc module : keySet)
141         {
142             int[] errorCounts = (int[]) errorsMap.get(module);
143             final String JavaDoc sErrorRecord = String.format(" %1$-8s %2$-8s %3$-40s",
144                                                   errorCounts[0], errorCounts[1], module);
145             CLILogger.getInstance().printMessage(sErrorRecord);
146         }
147     }
148 }
149
Popular Tags