1 64 65 package com.jcorporate.expresso.services.controller; 66 67 import com.jcorporate.expresso.core.cache.CacheManager; 68 import com.jcorporate.expresso.core.controller.Block; 69 import com.jcorporate.expresso.core.controller.ControllerException; 70 import com.jcorporate.expresso.core.controller.ControllerRequest; 71 import com.jcorporate.expresso.core.controller.ControllerResponse; 72 import com.jcorporate.expresso.core.controller.DBController; 73 import com.jcorporate.expresso.core.controller.NonHandleableException; 74 import com.jcorporate.expresso.core.controller.Output; 75 import com.jcorporate.expresso.core.controller.ServletControllerRequest; 76 import com.jcorporate.expresso.core.controller.State; 77 import com.jcorporate.expresso.core.controller.Transition; 78 import com.jcorporate.expresso.core.db.DBConnection; 79 import com.jcorporate.expresso.core.db.DBConnectionPool; 80 import com.jcorporate.expresso.core.db.DBException; 81 import com.jcorporate.expresso.core.db.TypeMapper; 82 import com.jcorporate.expresso.core.dbobj.CacheStatEntry; 83 import com.jcorporate.expresso.core.dbobj.DBObject; 84 import com.jcorporate.expresso.core.misc.ConfigManager; 85 import com.jcorporate.expresso.core.misc.ConfigurationException; 86 import com.jcorporate.expresso.core.misc.CurrentLogin; 87 import com.jcorporate.expresso.core.misc.DateTime; 88 import com.jcorporate.expresso.core.misc.StringUtil; 89 import com.jcorporate.expresso.core.security.User; 90 import com.jcorporate.expresso.services.dbobj.DBObjLimit; 91 import com.jcorporate.expresso.services.dbobj.GroupMembers; 92 import com.jcorporate.expresso.services.dbobj.Setup; 93 94 import javax.servlet.ServletException ; 95 import javax.servlet.http.Cookie ; 96 import javax.servlet.http.HttpServletRequest ; 97 import java.text.NumberFormat ; 98 import java.text.SimpleDateFormat ; 99 import java.util.ArrayList ; 100 import java.util.Calendar ; 101 import java.util.Collections ; 102 import java.util.Date ; 103 import java.util.Enumeration ; 104 import java.util.Hashtable ; 105 import java.util.Iterator ; 106 import java.util.Locale ; 107 import java.util.Map ; 108 import java.util.TreeMap ; 109 110 111 118 public class Status 119 extends DBController { 120 121 124 public Status() { 125 super(); 126 127 State show = new State("show", "Show Status"); 128 show.addOptionalParameter("contextName"); 129 addState(show); 130 setInitialState("show"); 131 132 State packages = new State("showPackages", "Show Installed Packages"); 133 addState(packages); 134 this.setSchema(com.jcorporate.expresso.core.ExpressoSchema.class); 135 } 136 137 145 protected void runShowPackagesState(ControllerRequest request, 146 ControllerResponse response) 147 throws ControllerException, NonHandleableException { 148 149 showPackages(request, response); 150 showTransitions(request, response); 151 } 152 153 154 162 protected void runShowState(ControllerRequest request, 163 ControllerResponse response) 164 throws ControllerException, NonHandleableException { 165 String displayContext = request.getParameter("contextName"); 166 if (displayContext == null || displayContext.length() == 0) { 167 displayContext = "default"; 168 } 169 170 173 try { 174 String userName = request.getUser(); 175 Output t = null; 176 177 if (userName == null) { 178 t = new Output("general", 179 response.getString("Status_at") + 180 DateTime.getDateTimeString() + 181 response.getString("_Not_logged_in._Logging_level_is_") + 182 Setup.getValue(request.getDataContext(), "LogLevel")); 183 } else { 184 Locale l = request.getLocale(); 185 String language = l.getLanguage(); 186 String country = l.getCountry(); 187 String dbDescrip = ""; 188 189 try { 190 dbDescrip = StringUtil.notNull(ConfigManager.getContext(request.getDataContext()).getDescription()); 191 } catch (ConfigurationException ce) { 192 throw new ControllerException(ce); 193 } 194 if (dbDescrip.equals("")) { 195 dbDescrip = request.getDataContext(); 196 } 197 198 Object [] args = { 199 userName, dbDescrip, language + "/" + country 200 }; 201 t = new Output("general", 202 response.getString("Status_at") + 203 DateTime.getDateTimeString() + 204 response.getString("Logged_in_as", args)); 205 } 206 207 response.add(t); 208 209 Block oneRow = null; 210 211 212 showContextInformation(request, response, displayContext); 213 214 215 216 217 Block cookieTable = new Block("cookies"); 218 cookieTable.setAttribute("table", "Y"); 219 cookieTable.setDescription(response.getString("Cookies")); 220 cookieTable.setAttribute("header-row", 221 response.getString("Name|Value")); 222 223 230 ServletControllerRequest sr = (ServletControllerRequest) request; 231 HttpServletRequest hreq = (HttpServletRequest ) sr.getServletRequest(); 232 Cookie [] cookies = hreq.getCookies(); 233 String name = null; 234 String value = null; 235 236 if (cookies != null) { 237 for (int i = 0; i < cookies.length; i++) { 238 oneRow = new Block(); 239 oneRow.setAttribute("row", "Y"); 240 name = StringUtil.notNull(cookies[i].getName()); 241 value = StringUtil.notNull(cookies[i].getValue()); 242 oneRow.add(new Output(name)); 243 oneRow.add(new Output(value)); 244 cookieTable.add(oneRow); 245 } 246 247 } 248 249 250 response.add(cookieTable); 251 response.add(new Output("memory1", 252 response.getString("Total_Memory_") + Runtime.getRuntime().totalMemory())); 253 response.add(new Output("memory2", 254 response.getString("Free_Memory_") + Runtime.getRuntime().freeMemory())); 255 CacheManager.displayStatus(); 256 257 259 Hashtable dbMapHash = ConfigManager.getOtherDBLocations(); 260 261 if (dbMapHash == null) { 262 response.add(new Output("No mappings to databases other " 263 + "than the default database could be found. The DBOtherMap is empty.")); 264 } else { 265 Block mapTable = new Block("dbother"); 266 mapTable.setDescription("Mappings to Other Databases"); 267 mapTable.setAttribute("table", "Y"); 268 mapTable.setAttribute("header-row", 269 "Context|DBObject Class Name|Database Mapping"); 270 271 if (dbMapHash.size() == 0) { 272 response.add(new Output("No entries in DBOtherMap")); 273 } else { 274 response.add(mapTable); 275 } 276 277 String objectName = null; 278 String dbMapping = null; 279 280 for (Enumeration emap = dbMapHash.keys(); 281 emap.hasMoreElements();) { 282 objectName = (String ) emap.nextElement(); 283 java.util.StringTokenizer st = new java.util.StringTokenizer (objectName, "|"); 284 String contextName = st.nextToken(); 285 String dbObjName = st.nextToken(); 286 dbMapping = (String ) dbMapHash.get(objectName); 287 288 Block aRow = new Block("new row"); 289 aRow.setAttribute("row", "Y"); 290 aRow.add(new Output(contextName)); 291 aRow.add(new Output(dbObjName)); 292 aRow.add(new Output(dbMapping)); 293 mapTable.add(aRow); 294 } 295 } 297 298 299 Map cacheStats = DBObject.getCacheStatsMap(); 300 301 if (cacheStats == null) { 302 response.add(new Output("Caching statistics not " + "currently available. Edit expresso-config.xml " + 303 "to begin collecting cache statistics")); 304 } else { 305 Block statsTable = new Block("dbstats"); 306 statsTable.setDescription("DB Object Statistics"); 307 statsTable.setAttribute("table", "Y"); 308 statsTable.setAttribute("header-row", 309 "Object (adjust cache)|Hit %|Reads|Cache Hits|Cache Misses|Cache Size|DB/Context"); 310 311 if (cacheStats.size() == 0) { 312 response.add(new Output("No entries in cache stats")); 313 } else { 314 response.add(statsTable); 315 } 316 317 double hits = 0; 318 double misses = 0; 319 double reads = 0; 320 double perc = 0.0; 321 322 NumberFormat numFormat = NumberFormat.getInstance(); 323 numFormat.setMaximumFractionDigits(0); 324 numFormat.setGroupingUsed(true); 325 NumberFormat percentFormat = NumberFormat.getPercentInstance(); 326 percentFormat.setMaximumFractionDigits(1); 327 328 ArrayList list = new ArrayList (cacheStats.values()); 329 Collections.sort(list); for (Iterator ee = list.iterator(); ee.hasNext();) { 331 CacheStatEntry oneEntry = (CacheStatEntry) ee.next(); 332 if (oneEntry.getDataContext().equalsIgnoreCase(displayContext)) { 333 reads = oneEntry.getReadCount(); 334 hits = oneEntry.getCacheHits(); 335 misses = oneEntry.getCacheMisses(); 336 perc = (hits / reads); 337 338 Block aRow = new Block("new row"); 339 aRow.setAttribute("row", "Y"); 340 Transition trans = new Transition(oneEntry.getDBObjName(), DBMaint.class, DBMaint.UPDATE); 342 trans.addParam("key", oneEntry.getDBObjName()); 343 trans.addParam("dbobj", DBObjLimit.class.getName()); 344 aRow.add(trans); 345 346 aRow.add(new Output("" + percentFormat.format(perc))); 347 aRow.add(new Output("" + numFormat.format(reads))); 348 aRow.add(new Output("" + numFormat.format(hits))); 349 aRow.add(new Output("" + numFormat.format(misses))); 350 351 DBObject oneObj = null; 352 try { 353 Class c = Class.forName(oneEntry.getDBObjName()); 354 oneObj = (DBObject) c.newInstance(); 355 } catch (ClassNotFoundException cn) { 356 throw new ServletException ("Database object '" + 357 oneEntry.getDBObjName() + 358 "' not found", cn); 359 } catch (InstantiationException ie) { 360 throw new ServletException ("Database object '" + 361 oneEntry.getDBObjName() + 362 "' cannot be instantiated", 363 ie); 364 } catch (IllegalAccessException iae) { 365 throw new ServletException ("llegal access loading " + 366 "Database object '" + 367 oneEntry.getDBObjName() + 368 "'", iae); 369 } 370 371 oneObj.setDataContext(oneEntry.getDataContext()); 372 aRow.add(new Output("" + oneObj.getCacheSize())); 373 374 aRow.add(new Output(oneEntry.getDataContext())); 375 statsTable.add(aRow); 376 } 377 } 378 } 379 380 Hashtable allSessions = ConfigManager.getCurrentLogins(); 381 response.add(new Output("Currently there are " + allSessions.size() + 382 " users connected")); 383 384 if (userName.equalsIgnoreCase(User.ADMIN_USER)) { 385 Block userTable = new Block("User_Table"); 386 userTable.setAttribute("table", "Y"); 387 userTable.setAttribute("header-row", 388 "User|Session|IP Address|Logged In At|DB/Context"); 389 userTable.setDescription("Logged In Users"); 390 391 CurrentLogin oneLogin = null; 392 String oneSessionId = null; 393 394 for (Enumeration cu = allSessions.keys(); 395 cu.hasMoreElements();) { 396 oneSessionId = (String ) cu.nextElement(); 397 oneLogin = (CurrentLogin) allSessions.get(oneSessionId); 398 399 if (oneLogin != null) { 400 oneRow = new Block(); 401 oneRow.setAttribute("row", "Y"); 402 if (oneLogin.getDBName().equalsIgnoreCase(displayContext)) { 403 oneRow.add(new Output(oneLogin.getUserName())); 404 oneRow.add(new Output(oneSessionId)); 405 oneRow.add(new Output(oneLogin.getIPAddress())); 406 oneRow.add(new Output(new Date (oneLogin.getLogInTime()).toString())); 407 oneRow.add(new Output(oneLogin.getDBName())); 408 userTable.add(oneRow); 409 } 410 } 411 } 412 413 response.add(userTable); 414 showTransitions(request, response); 415 } 416 417 } catch (Exception he) { 418 throw new ControllerException(he); 419 } 420 } 421 422 431 protected void showContextInformation(ControllerRequest request, 432 ControllerResponse response, String contextName) throws ControllerException, 433 DBException { 434 435 Block oneRow = null; 436 Block poolBlock = new Block("pools"); 437 response.add(poolBlock); 438 String userName = request.getUser(); 439 440 String dbDescrip = ""; 441 442 try { 443 dbDescrip = StringUtil.notNull(ConfigManager.getContext(contextName).getDescription()); 444 } catch (ConfigurationException ce) { 445 throw new ControllerException(ce); 446 } 447 if (dbDescrip.equals("")) { 448 dbDescrip = contextName; 449 } 450 451 Block thisPoolBlock = new Block("Block for connection pool " + 452 contextName); 453 thisPoolBlock.setDescription(dbDescrip); 454 poolBlock.add(thisPoolBlock); 455 456 Block oneTable = new Block("connection pool table " + 459 contextName + " database"); 460 oneTable.setDescription("Connections for " + dbDescrip); 461 oneTable.setAttribute("table", "Y"); 462 463 if (userName == null) { 464 userName = (""); 465 } 466 if (userName.equalsIgnoreCase(User.ADMIN_USER)) { 467 oneTable.setAttribute("header-row", 468 response.getString("Connection_Number|Description|Status|Last_Touched|Last_SQL")); 469 } else { 470 oneTable.setAttribute("header-row", 471 response.getString("Connection_Number|Description|Status|Last_Touched")); 472 } 473 474 DBConnectionPool currPool = DBConnectionPool.getInstance(contextName); 475 DBConnection oneConnection = null; 476 int connectionNumber = 0; 477 Calendar c = Calendar.getInstance(); 478 SimpleDateFormat df = new SimpleDateFormat ("E',' MMM d yyyy 'at' hh:mm:ss a"); 479 480 for (Iterator e = currPool.getPoolList().iterator(); 481 e.hasNext();) { 482 connectionNumber++; 483 oneConnection = (DBConnection) e.next(); 484 oneRow = new Block(); 485 oneRow.setAttribute("row", "Y"); 486 oneRow.add(new Output("" + connectionNumber)); 487 oneRow.add(new Output(oneConnection.getDescription())); 488 489 if (oneConnection.isAvailable()) { 490 oneRow.add(new Output("AVAILABLE")); 491 } else { 492 oneRow.add(new Output("IN USE")); 493 } 494 long l = oneConnection.getLastTouched(); 495 c.setTime(new Date (l)); 496 497 df.setCalendar(c); 498 oneRow.add(new Output(df.format(c.getTime()))); 499 500 if (userName.equalsIgnoreCase(User.ADMIN_USER)) { 501 oneRow.add(new Output(oneConnection.getSQL())); 502 } 503 504 oneTable.add(oneRow); 505 } 506 507 if (oneTable.getNumContents() > 0) { 508 thisPoolBlock.add(oneTable); 509 } 510 511 GroupMembers oneGroupMembers = new GroupMembers(); 512 oneGroupMembers.setDataContext(request.getDataContext()); 513 TypeMapper typeMapper = TypeMapper.getInstance(contextName); 514 515 Map expressoToSQLTypes = typeMapper.getExpressoToSQLMap(); 516 517 522 523 Block typeMapTable = new Block("Type_Map_Table" + 524 contextName); 525 typeMapTable.setDescription("Type Mappings for " + dbDescrip); 526 typeMapTable.setAttribute("table", "Y"); 527 typeMapTable.setAttribute("header-row", 528 response.getString("mapHeader")); 529 530 String oneDatabaseTypeName = null; 531 String oneJavaSQLTypeName = null; 532 String oneExpressoTypeName = null; 533 534 for (Iterator en = expressoToSQLTypes.keySet().iterator(); 535 en.hasNext();) { 536 oneExpressoTypeName = (String ) en.next(); 537 538 539 Integer sqlType = typeMapper.getExpressoToJava(oneExpressoTypeName.toLowerCase()); 545 if (sqlType == null) { 546 oneJavaSQLTypeName = "Not Mapped"; 547 oneDatabaseTypeName = "Not Mapped"; 548 } else { 549 oneJavaSQLTypeName = typeMapper.getNameForSQL(sqlType.intValue()); 550 if (oneJavaSQLTypeName == null) { 551 oneJavaSQLTypeName = "No Name (" + sqlType + ")"; 552 } 553 554 Map sqlToDB = typeMapper.getSQLToDBMap(); 555 556 oneDatabaseTypeName = (String ) sqlToDB.get(sqlType); 557 558 if (oneDatabaseTypeName == null) { 559 oneDatabaseTypeName = "Not Mapped"; 560 } 561 562 } 563 564 oneRow = new Block(); 565 oneRow.setAttribute("row", "Y"); 566 oneRow.add(new Output(oneExpressoTypeName)); 567 oneRow.add(new Output(oneJavaSQLTypeName)); 568 oneRow.add(new Output(oneDatabaseTypeName)); 569 typeMapTable.add(oneRow); 570 } 571 572 thisPoolBlock.add(typeMapTable); 573 574 } 575 576 577 584 protected void showTransitions(ControllerRequest request, ControllerResponse response) throws ControllerException { 585 String currentState = StringUtil.notNull(request.getParameter(STATE_PARAM_KEY)); 586 if (currentState.length() == 0) { 587 currentState = "show"; 588 } 589 boolean showState = (currentState.equals("show")); 590 String currentContextName = request.getParameter("contextName"); 591 if (currentContextName == null || currentContextName.length() == 0) { 592 currentContextName = "default"; 593 } 594 595 596 for (Enumeration e = ConfigManager.getAllConfigKeys(); e.hasMoreElements();) { 597 String oneKey = (String ) e.nextElement(); 598 Transition t = new Transition("show" + oneKey, "Show '" + oneKey + "' Context Status", this.getClass(), 599 "show"); 600 t.addParam("contextName", oneKey); 601 if (showState) { 602 if (currentContextName.equals(oneKey)) { 603 t.setAttribute("current", "y"); 604 } 605 } 606 response.add(t); 607 } 608 609 Transition t = new Transition("showPackages", this); 610 t.setLabel("Show Installed Packages"); 611 if (!showState) { 612 t.setAttribute("current", "y"); 613 } 614 response.add(t); 615 } 616 617 624 protected void showPackages(ControllerRequest request, ControllerResponse response) throws ControllerException { 625 Map sortedMap = new TreeMap (); 626 Block packageBlock = new Block("packages"); 627 response.add(packageBlock); 628 packageBlock.add(new Output("Currently installed packages")); 629 Block packageTable = new Block("packageTable"); 630 packageTable.setAttribute("table", "Y"); 631 packageTable.setAttribute("header-row", "Name|Impl. Title|Impl. Vendor|" + 632 "Impl. Version|Spec. Title|Spec. Vendor|Spec. Version|Is Sealed "); 633 packageBlock.add(packageTable); 634 635 Package packages[] = Package.getPackages(); 636 if (packages == null) { 637 return; 638 } 639 640 for (int i = 0; i < packages.length; i++) { 641 sortedMap.put(packages[i].getName(), packages[i]); 642 } 643 644 for (Iterator i = sortedMap.keySet().iterator(); i.hasNext();) { 645 String packageKey = (String ) i.next(); 646 Package onePackage = (Package ) sortedMap.get(packageKey); 647 Block oneRow = new Block(); 648 packageTable.add(oneRow); 649 oneRow.setAttribute("row", "Y"); 650 651 oneRow.add(new Output(StringUtil.notNull(onePackage.getName()))); 652 oneRow.add(new Output(StringUtil.notNull(onePackage.getImplementationTitle()))); 653 oneRow.add(new Output(StringUtil.notNull(onePackage.getImplementationVendor()))); 654 oneRow.add(new Output(StringUtil.notNull(onePackage.getImplementationVersion()))); 655 oneRow.add(new Output(StringUtil.notNull(onePackage.getSpecificationTitle()))); 656 oneRow.add(new Output(StringUtil.notNull(onePackage.getSpecificationVendor()))); 657 oneRow.add(new Output(StringUtil.notNull(onePackage.getSpecificationVersion()))); 658 if (onePackage.isSealed()) { 659 oneRow.add(new Output("YES")); 660 } else { 661 oneRow.add(new Output("no")); 662 } 663 664 } 665 666 } 667 668 669 672 public String getTitle() { 673 return ("System Status"); 674 } 675 } 676 | Popular Tags |