1 23 24 package com.sun.enterprise.tools.admingui.handlers; 25 26 import java.util.ArrayList ; 27 import java.util.EventObject ; 28 import java.util.Iterator ; 29 import java.util.Calendar ; 30 import java.util.Date ; 31 import java.util.Set ; 32 import java.util.Map ; 33 import java.util.HashMap ; 34 import java.util.Random ; 35 import java.text.DateFormat ; 36 import java.util.logging.Level ; 37 import java.lang.Integer ; 38 39 import com.iplanet.jato.view.View; 40 import com.iplanet.jato.view.ViewBase; 41 import com.iplanet.jato.view.ViewBean; 42 import com.iplanet.jato.RequestContext; 43 import com.iplanet.jato.RequestManager; 44 import com.iplanet.jato.model.DefaultModel; 45 import com.iplanet.jato.model.Model; 46 import com.iplanet.jato.model.ModelControlException; 47 48 import javax.management.MBeanException ; 49 import javax.management.ObjectName ; 50 import javax.management.AttributeList ; 51 import javax.management.Attribute ; 52 53 import com.sun.enterprise.tools.admingui.util.Util; 54 55 import com.sun.enterprise.tools.guiframework.exception.FrameworkException; 56 import com.sun.enterprise.tools.guiframework.model.ModelManager; 57 import com.sun.enterprise.tools.guiframework.view.DescriptorCCPageTitle; 58 import com.sun.enterprise.tools.guiframework.view.DescriptorContainerView; 59 import com.sun.enterprise.tools.guiframework.view.HandlerContext; 60 import com.sun.enterprise.tools.guiframework.view.descriptors.ViewDescriptor; 61 import com.sun.enterprise.tools.guiframework.view.descriptors.CCActionTableDescriptor; 62 import com.sun.enterprise.tools.guiframework.view.event.BeforeCreateEvent; 63 64 import com.sun.web.ui.model.CCActionTableModelInterface; 65 import com.sun.enterprise.tools.admingui.util.MBeanUtil; 66 import com.sun.enterprise.tools.admingui.util.AMXUtil; 67 68 import com.sun.appserv.management.monitor.ServerRootMonitor; 70 import com.sun.appserv.management.ext.logging.LogAnalyzer; 71 72 73 public class LogAnalyzerHandler { 74 75 private static final String DEMO_INSTANCE_NAME="@demo@"; 76 private CCActionTableModelInterface getModel(HandlerContext handlerCtx, ViewDescriptor desc) { 77 78 CCActionTableModelInterface model = 79 (CCActionTableModelInterface)handlerCtx.getInputValue("model"); 80 if (model == null) { 81 throw new FrameworkException("LogAnalyzerHandler.getModel: No Model Specified.", 82 desc, handlerCtx.getView()); 83 } 84 85 return model; 86 } 87 88 89 private void populateTable(RequestContext ctx, CCActionTableModelInterface model, Map [] attrsMap, Date selectedTimeStamp) 90 throws ModelControlException { 91 model.beforeFirst(); 93 DateFormat dateFormat = DateFormat.getDateTimeInstance(DateFormat.MEDIUM, DateFormat.MEDIUM, ctx.getRequest().getLocale()); 94 95 for(int rowNo=0; rowNo < attrsMap.length; rowNo++){ 96 model.appendRow(); 97 Map attrs = attrsMap[rowNo]; 98 99 Long ms = (Long ) attrs.get(LogAnalyzer.TIMESTAMP_KEY); 100 model.setValue("timeStamp", ms); 101 String formattedTime = dateFormat.format(new Date (ms.longValue())); 102 model.setValue("timeStampFormatted", formattedTime); 103 104 Integer severe = (Integer ) attrs.get(LogAnalyzer.SEVERE_COUNT_KEY); 105 Object obj = severe; 106 if (severe.intValue() < 0){ 107 obj = Util.getMessage("common.NA"); 108 } 109 model.setValue("severeCount", obj); 110 111 Integer warning = (Integer ) attrs.get(LogAnalyzer.WARNING_COUNT_KEY); 112 obj = warning; 113 if (warning.intValue() < 0){ 114 obj = Util.getMessage("common.NA"); 115 } 116 model.setValue("warningCount", obj); 117 118 } 119 } 120 121 private void printout(CCActionTableModelInterface model) 122 { 123 Object timeS = model.getValue("timeStamp"); 124 String timeSClassName= timeS.getClass().getName(); 125 String tformat = (String ) model.getValue("timeStampFormatted"); 126 Integer sc = (Integer ) model.getValue("severeCount"); 127 Integer wc = (Integer ) model.getValue("warningCount"); 128 } 130 131 public void loadLogAnalyzerTable(RequestContext ctx, HandlerContext handlerCtx) { 132 View view = handlerCtx.getView(); 134 ViewDescriptor desc = null; 135 if (handlerCtx.getEvent() instanceof BeforeCreateEvent) { 136 desc = ((BeforeCreateEvent)handlerCtx.getEvent()).getViewDescriptor(); 137 } else { 138 DescriptorContainerView descView = (DescriptorContainerView) 139 (((ViewBase)view).getParentViewBean()); 140 desc = descView.getViewDescriptor(); 141 } 142 143 Map [] attrsMap = (Map [])handlerCtx.getInputValue("statisticsMap"); 144 if (attrsMap == null || attrsMap.length == 0){ 145 return; 146 } 147 Date selectedTimeStamp = (Date ) handlerCtx.getInputValue("selectedTimeStamp"); 148 try { 149 CCActionTableModelInterface model = getModel(handlerCtx, desc); 150 ((DefaultModel)model).clear(); 151 model.setRowSelectionType("single"); 152 populateTable(ctx, model, attrsMap, selectedTimeStamp); 153 } catch (Exception ex) { 154 throw new FrameworkException("loadAnalyzerTable: Loading error. ", ex, desc, view); 155 } 156 } 157 158 static Map [] map68 = null; 159 static Map [] map10 = null; 160 161 public void getStatisticsMap(RequestContext ctx, HandlerContext handlerCtx) { 162 String objectName = (String )handlerCtx.getInputValue("objectName"); 164 try{ 165 ObjectName currentObjName = new ObjectName (objectName); 166 String serverName = currentObjName.getKeyProperty("server"); 167 int count = (DEMO_INSTANCE_NAME.equals(serverName)) ? 68 : 5; 168 if (count == 68){ 169 if(map68 == null) 170 map68 = getMap(count); 171 handlerCtx.setOutputValue("output", map68); 172 } 173 else 174 if (count == 5){ 175 if(map10 == null) 176 map10 = getMap(count); 177 handlerCtx.setOutputValue("output", map10); 178 } 179 }catch (Exception ex){ 180 ; 181 } 182 } 183 184 private Map [] getMap(int count){ 185 HashMap [] aMap = new HashMap [count]; 186 Calendar rightNow = Calendar.getInstance(); 187 rightNow.add(Calendar.HOUR, -count); 188 Random random = new Random (); 189 for(int i=0; i<count; i++){ 190 rightNow.add(Calendar.HOUR, 1); 191 long tt = rightNow.getTimeInMillis(); 192 Long tLong = new Long (tt); 193 String tkey = LogAnalyzer.TIMESTAMP_KEY; 194 String wKey = LogAnalyzer.WARNING_COUNT_KEY; 195 String sKey = LogAnalyzer.SEVERE_COUNT_KEY; 196 197 HashMap hm = new HashMap (); 198 hm.put(tkey, tLong); 199 200 int xx = Math.abs(random.nextInt()%30); 201 Integer xInt = new Integer (xx); 202 hm.put(wKey, xInt); 203 204 int yy = Math.abs(random.nextInt()%30); 205 Integer yInt = new Integer (yy); 206 hm.put(sKey, yInt); 207 aMap[i] = hm; 208 } 209 return aMap; 210 } 211 212 213 public void revertArrayMap(RequestContext ctx, HandlerContext handlerCtx) { 214 Map [] inMap = (Map [])handlerCtx.getInputValue("inMap"); 215 HashMap [] outMap = new HashMap [0]; 216 217 if(inMap != null && inMap.length > 0){ 218 outMap = new HashMap [inMap.length]; 219 for(int i=0, oix=inMap.length-1; i<inMap.length; i++, oix--){ 220 outMap[oix] = (HashMap ) inMap[i]; 221 } 222 } 223 handlerCtx.setOutputValue("outMap", outMap); 224 225 } 226 227 public void convertToTimeStampArray(RequestContext ctx, HandlerContext handlerCtx) { 228 229 Map [] attrsMap = (Map [])handlerCtx.getInputValue("inputMap"); 230 View view = handlerCtx.getView(); 231 DescriptorContainerView descView = (DescriptorContainerView)(((ViewBase)view).getParentViewBean()); 232 ViewDescriptor desc = descView.getViewDescriptor(); 233 if (attrsMap == null) { 234 throw new FrameworkException("LogAnalyzerHandler.convertToTimeStampArray: No attrsMap specified.", 235 desc, handlerCtx.getView()); 236 } 237 ArrayList timeStampArray = new ArrayList (); 238 for (int rowNo = 0; rowNo < attrsMap.length; rowNo++) { 239 Map attrs = attrsMap[rowNo]; 240 Long ms = (Long ) attrs.get(LogAnalyzer.TIMESTAMP_KEY); 241 timeStampArray.add(ms.toString()); 242 } 243 handlerCtx.setOutputValue("output", timeStampArray); 244 } 245 246 247 public void getSelectedTimeStamp(RequestContext ctx, HandlerContext handlerCtx) { 248 View view = handlerCtx.getView(); 250 DescriptorContainerView descView = (DescriptorContainerView)(((ViewBase)view).getParentViewBean()); 251 ViewDescriptor desc = descView.getViewDescriptor(); 252 CCActionTableModelInterface model = 253 (CCActionTableModelInterface)handlerCtx.getInputValue("model"); 254 if (model == null) { 255 throw new FrameworkException("LogAnalyzerHandler.getSelectedTimeStamp: No Model Specified.", 256 desc, handlerCtx.getView()); 257 } 258 259 model.setRowSelectionType("single"); 260 try{ 261 model.beforeFirst(); 262 Object timeStamp = null; 263 Object fromKey = null; 264 while(model.next()) { 265 if (model.isRowSelected()) { 266 int index = model.getRowIndex(); 267 timeStamp = model.getValue("timeStamp"); 268 String classname = timeStamp.getClass().getName(); 269 } 271 } 272 if (timeStamp == null){ 273 } 275 handlerCtx.setOutputValue("selected", timeStamp); 276 277 }catch(Exception ex) { 278 throw new FrameworkException("Unable to get Selected TimeStamp:", null, handlerCtx.getView()); 279 } 280 } 281 282 283 public void setupChart(RequestContext ctx, HandlerContext handlerCtx) { 284 285 View view = handlerCtx.getView(); 286 DescriptorContainerView descView = (DescriptorContainerView)(((ViewBase)view).getParentViewBean()); 287 ViewDescriptor desc = descView.getViewDescriptor(); 288 289 Map [] data = (Map [])handlerCtx.getInputValue("statisticsMap"); 290 ArrayList timeStampArray = (ArrayList )handlerCtx.getInputValue("timeStampArray"); 291 if (data == null ) { 292 throw new FrameworkException("LogAnalyzerHandler.setupChart: No statisticsMap specified", desc, handlerCtx.getView()); 293 } 294 if (timeStampArray == null ) { 295 throw new FrameworkException("LogAnalyzerHandler.setupChart: No timeStampArray specified", desc, handlerCtx.getView()); 296 } 297 298 299 300 String selectedTime = (String )handlerCtx.getInputValue("selectedTime"); 301 boolean showPrevious10 = Boolean.parseBoolean((String )handlerCtx.getInputValue("showPrevious10")); 302 boolean showNext10 = Boolean.parseBoolean((String )handlerCtx.getInputValue("showNext10")); 303 boolean useStartEnd = Boolean.parseBoolean((String )handlerCtx.getInputValue("useStartEnd")); 304 boolean showAll = Boolean.parseBoolean((String )handlerCtx.getInputValue("showAll")); 305 String startT = (String )handlerCtx.getInputValue("start"); 306 String endT = (String )handlerCtx.getInputValue("end"); 307 int selected = 0; 308 309 boolean showPartial = false; 310 311 if ( (selectedTime != null) || showPrevious10 || showNext10 || useStartEnd) 312 showPartial=true; 313 314 if (!showPartial || showAll){ 315 handlerCtx.setOutputValue("disableNext10", new Boolean ("true")); 316 handlerCtx.setOutputValue("disablePrevious10", new Boolean ("true")); 317 handlerCtx.setOutputValue("disableShowAll", "true"); 318 handlerCtx.setOutputValue("output", data); 319 if (timeStampArray.size() < 11) 320 handlerCtx.setOutputValue("disableZoomIn", "true"); 321 else 322 handlerCtx.setOutputValue("disableZoomIn", "false"); 323 handlerCtx.setOutputValue("startTime", (String )timeStampArray.get(0)); 324 handlerCtx.setOutputValue("endTime", (String )timeStampArray.get(timeStampArray.size()-1)); 325 return; 326 } 327 328 int start =0; 329 int end = timeStampArray.size()-1; 330 331 if (useStartEnd && !showPrevious10 && !showNext10){ 332 for(int i=0; i< timeStampArray.size(); i++){ 333 if ( ((String )timeStampArray.get(i)).equals( startT)){ 334 start = i; 335 break; 336 } 337 } 338 for(int i=0; i< timeStampArray.size(); i++){ 339 if ( ((String )timeStampArray.get(i)).equals( endT)){ 340 end = i; 341 break; 342 } 343 } 344 345 }else{ 346 if(showPrevious10){ 347 selectedTime = (String )handlerCtx.getInputValue("start"); 348 if (selectedTime == null) { 349 throw new FrameworkException("LogAnalyzerHandler.setupChart: No start specified with showPrevious", desc, handlerCtx.getView()); 350 } 351 }else 352 if (showNext10){ 353 selectedTime = (String )handlerCtx.getInputValue("end"); 354 if (selectedTime == null) { 355 throw new FrameworkException("LogAnalyzerHandler.setupChart: No end specified with showNext10", desc, handlerCtx.getView()); 356 } 357 } 358 359 for(int i=0; i< timeStampArray.size(); i++){ 360 if ( ((String )timeStampArray.get(i)).equals( selectedTime)){ 361 selected = i; 362 break; 363 } 364 } 365 366 if (showPrevious10){ 367 if ( (selected-10) <= 0){ 368 start = 0; 369 end = 9; 370 }else{ 371 start = selected - 10; 372 end = start + 9; 373 } 374 }else 375 if (showNext10){ 376 start = selected + 1; 377 if ( (start+9 ) >= end){ 378 }else{ 379 end = start+9; 380 } 381 } 382 else 383 { 384 if ( (selected-5)>0 && ((selected+4)< timeStampArray.size()-1)){ 386 start = selected-5; 387 end = selected+4; 388 }else 389 if ( (selected-5)<= 0){ 390 end = start +9; 391 }else{ 392 start = end-9; 393 } 394 } 395 } 396 397 int s = start; 398 Map [] chartMap = new Map [end-start+1]; 399 for(int i=0; i<chartMap.length; i++){ 400 chartMap[i] = data[s++]; 401 } 402 403 404 handlerCtx.setOutputValue("disablePrevious10", new Boolean ((start!=0) ? "false" : "true")); 405 handlerCtx.setOutputValue("disableNext10", new Boolean ( (end < timeStampArray.size()-1) ? "false" : "true")); 406 handlerCtx.setOutputValue("output", chartMap); 407 handlerCtx.setOutputValue("startTime", (String )timeStampArray.get(start)); 408 handlerCtx.setOutputValue("endTime", (String )timeStampArray.get(end)); 409 handlerCtx.setOutputValue("useStartEnd", "true"); 410 411 handlerCtx.setOutputValue("disableZoomIn", (timeStampArray.size()<11) ? "true" : "false"); 412 if(chartMap.length < timeStampArray.size()) 413 handlerCtx.setOutputValue("disableShowAll", "false"); 414 else 415 handlerCtx.setOutputValue("disableShowAll", "true"); 416 } 417 418 public void getFormatedTime(RequestContext ctx, HandlerContext handlerCtx) { 419 String ms = (String )handlerCtx.getInputValue("ms"); 420 Date useThis = null; 421 if (ms == null || "".equals(ms)){ 422 useThis = new Date (System.currentTimeMillis()); 423 }else{ 424 useThis = new Date ( Long.parseLong(ms)); 425 } 426 DateFormat dateFormat = DateFormat.getDateTimeInstance(DateFormat.MEDIUM, DateFormat.MEDIUM, ctx.getRequest().getLocale()); 427 String currentTime = dateFormat.format(useThis); 428 handlerCtx.setOutputValue("value", currentTime); 429 } 430 431 public void setupDistribution(RequestContext ctx, HandlerContext handlerCtx) { 432 433 View view = handlerCtx.getView(); 434 DescriptorContainerView descView = (DescriptorContainerView)(((ViewBase)view).getParentViewBean()); 435 ViewDescriptor desc = descView.getViewDescriptor(); 436 437 Long selectedTime = new Long ((String )handlerCtx.getInputValue("selectedTime")); 438 String level = "SEVERE"; 439 440 if (selectedTime == null ) { 441 throw new FrameworkException("LogAnalyzerHandler.setupDistribution: No selectedTime specified", desc, handlerCtx.getView()); 442 } 443 String serverName=(String )handlerCtx.getInputValue("instanceName") ; 444 445 Map warningMap = null; 446 Map severeMap = null; 447 if (serverName.equals(DEMO_INSTANCE_NAME)){ 448 warningMap = getDistributionSampleData(""+Level.WARNING); 449 severeMap = getDistributionSampleData(""+Level.SEVERE); 450 }else { 451 Map amp = (Map ) AMXUtil.getMonitoringRoot().getServerRootMonitorMap(); 452 ServerRootMonitor srm = (ServerRootMonitor) amp.get(serverName); 453 LogAnalyzer la = (LogAnalyzer) srm.getLogging(); 454 warningMap = la.getErrorDistribution(selectedTime.longValue(), ""+Level.WARNING); 455 severeMap = la.getErrorDistribution(selectedTime.longValue(), ""+Level.SEVERE); 456 } 457 458 459 HashMap combinedMap = new HashMap (); 460 Iterator iter = severeMap.keySet().iterator(); 462 while (iter.hasNext()){ 463 Object key = iter.next(); 464 Integer value = (Integer ) severeMap.get(key); 465 Integer counts[]={value, null}; 466 combinedMap.put(key, counts); 467 } 468 469 iter = warningMap.keySet().iterator(); 470 while (iter.hasNext()){ 471 Object key = iter.next(); 472 Integer value = (Integer ) warningMap.get(key); 473 Integer [] counts = (Integer []) combinedMap.get(key); 474 if (counts == null){ 475 Integer [] vv = {null, value}; 476 combinedMap.put(key, vv); 477 }else{ 478 counts[1]=value; 479 } 480 } 481 handlerCtx.setOutputValue("combinedMap", combinedMap); 482 } 483 484 private Map getDistributionSampleData(String level){ 485 Map dMap = new HashMap (); 486 Random random = new Random (); 487 if (level.equals("SEVERE")){ 488 dMap.put("javax.enterprise.system.container.web", new Integer (Math.abs(random.nextInt()%100))); 489 dMap.put("javax.enterprise.resource.corba.S1AS-ORB.rpc.transport", new Integer (Math.abs(random.nextInt()%100))); 490 dMap.put("javax.enterprise.system.core.transaction", new Integer (Math.abs(random.nextInt()%100))); 491 dMap.put("javax.enterprise.system.tools.admin", new Integer (Math.abs(random.nextInt()%100))); 492 dMap.put("javax.enterprise.system.container.ejb", new Integer (Math.abs(random.nextInt()%100))); 493 dMap.put("org.apache.catalina.startup.ContextConfig", new Integer (Math.abs(random.nextInt()%100))); 494 dMap.put("javax.enterprise.system.core", new Integer (Math.abs(random.nextInt()%100))); 495 496 }else 497 { 498 dMap.put("org.apache.catalina.core.StandardEngine", new Integer (Math.abs(random.nextInt()%100))); 499 dMap.put("javax.enterprise.system.container.web", new Integer (Math.abs(random.nextInt()%100))); 500 dMap.put("javax.enterprise.resource.corba.S1AS-ORB.rpc.transport", new Integer (Math.abs(random.nextInt()%100))); 501 dMap.put("javax.enterprise.system.core.transaction", new Integer (Math.abs(random.nextInt()%100))); 502 dMap.put("javax.enterprise.system.tools.admin", new Integer (Math.abs(random.nextInt()%100))); 503 } 504 return dMap; 505 } 506 507 public void loadDistributionTable(RequestContext ctx, HandlerContext handlerCtx) { 508 View view = handlerCtx.getView(); 509 ViewDescriptor desc = null; 510 if (handlerCtx.getEvent() instanceof BeforeCreateEvent) { 511 desc = ((BeforeCreateEvent)handlerCtx.getEvent()).getViewDescriptor(); 512 } else { 513 DescriptorContainerView descView = (DescriptorContainerView) 514 (((ViewBase)view).getParentViewBean()); 515 desc = descView.getViewDescriptor(); 516 } 517 518 Map combinedMap = (Map )handlerCtx.getInputValue("combinedMap"); 519 try { 520 CCActionTableModelInterface model = getModel(handlerCtx, desc); 521 ((DefaultModel)model).clear(); 522 model.beforeFirst(); 523 Iterator iters = combinedMap.keySet().iterator(); 524 while (iters.hasNext()){ 525 model.appendRow(); 526 Object key = iters.next(); 527 Integer [] value = (Integer []) combinedMap.get(key); 528 model.setValue("moduleId", key); 529 if (value[0] != null) 530 model.setValue("severeCount", value[0]); 531 if (value[1] != null) 532 model.setValue("warningCount", value[1]); 533 } 534 } catch (Exception ex) { 535 throw new FrameworkException("loadAnalyzerTable: Loading error. ", ex, desc, view); 536 } 537 } 538 539 } 540 | Popular Tags |