1 23 24 package com.sun.enterprise.tools.admingui.util; 25 26 import com.iplanet.jato.*; 27 import com.iplanet.jato.view.*; 28 import com.iplanet.jato.view.event.*; 29 import com.iplanet.jato.view.html.*; 30 import com.iplanet.jato.model.*; 31 import com.sun.web.ui.common.CCI18N; 32 33 import com.sun.enterprise.tools.admingui.AdminGUIConstants; 34 import com.sun.enterprise.tools.admingui.tree.IndexTreeNode; 35 import com.sun.enterprise.tools.admingui.tree.IndexTreeModelImpl; 36 import com.sun.enterprise.tools.admingui.tree.MonitorTreeModelImpl; 37 import com.sun.enterprise.tools.admingui.tree.IndexTreeModel; 38 import com.sun.enterprise.tools.guiframework.exception.FrameworkException; 39 40 import java.io.File ; 41 import java.io.IOException ; 42 import java.io.FileNotFoundException ; 43 import java.io.StringWriter ; 44 import java.io.FileReader ; 45 import java.io.EOFException ; 46 47 import java.util.Locale ; 48 import java.util.List ; 49 import java.util.Vector ; 50 import java.util.HashMap ; 51 import java.util.StringTokenizer ; 52 import java.util.logging.Level ; 53 import java.util.logging.Logger ; 54 import java.util.ResourceBundle ; 55 import java.util.MissingResourceException ; 56 import java.util.Date ; 57 58 import java.net.URLEncoder ; 59 import java.text.MessageFormat ; 60 import java.text.DateFormat ; 61 62 import javax.servlet.ServletContext ; 63 import javax.servlet.http.HttpServletRequest ; 64 65 import com.sun.enterprise.util.SystemPropertyConstants; 67 68 public class Util { 69 70 public static String getMessage(String key) { 71 CCI18N i18n = null; 72 RequestContext ctx = RequestManager.getRequestContext(); 73 i18n = (CCI18N)ctx.getRequest().getAttribute("__i18nObject"); 74 if (i18n == null) { 75 i18n = new CCI18N(ctx, "com.sun.enterprise.tools.admingui.resources.Resources"); 76 ctx.getRequest().setAttribute("__i18nObject", i18n); 77 } 78 return i18n.getMessage(key); 79 } 80 81 public static String getMessage(String key, Object args[]) { 82 CCI18N i18n = null; 83 RequestContext ctx = RequestManager.getRequestContext(); 84 i18n = (CCI18N)ctx.getRequest().getAttribute("__i18nObject"); 85 if (i18n == null) { 86 i18n = new CCI18N(ctx, "com.sun.enterprise.tools.admingui.resources.Resources"); 87 ctx.getRequest().setAttribute("__i18nObject", i18n); 88 89 } 90 return i18n.getMessage(key, args); 91 } 92 93 public static void setCurrentTreeModelType(RequestContext rc, String modelType) { 94 rc.getRequest().getSession().setAttribute(TREE_MODEL_TYPE, modelType); 95 } 96 97 public static String getCurrentTreeModelType(RequestContext rc) { 98 String modelType = (String ) rc.getRequest().getSession().getAttribute(TREE_MODEL_TYPE); 99 if (modelType == null) 100 modelType = DEFAULT_TREE_MODEL_TYPE; 101 return modelType; 102 } 103 104 public static IndexTreeModel getCurrentTreeModel() { 105 RequestContext rc = RequestManager.getRequestContext(); 106 String modelType = getCurrentTreeModelType(rc); 107 108 if (modelType.equals("index")) { 109 return IndexTreeModelImpl.getIndexTreeModel(rc); 110 } else if (modelType.equals("monitor")) { 111 return MonitorTreeModelImpl.getIndexTreeModel(rc); 112 } 113 throw new RuntimeException ("Invalid model type specified."); 114 } 115 116 public static void setCurrentViewURL(String viewName) { 118 RequestContext rc = RequestManager.getRequestContext(); 119 rc.getRequest().getSession().setAttribute( 120 getCurrentTreeModelType(rc), viewName); 121 } 122 123 public static String getCurrentViewURL(RequestContext rc) { 124 if (rc == null) 125 rc = RequestManager.getRequestContext(); 126 String view = (String ) rc.getRequest().getSession().getAttribute( 127 getCurrentTreeModelType(rc)); 128 return (view == null)?(""):(view); 129 } 130 131 public static final IndexTreeNode getSelectedNode() { 132 RequestContext rc = RequestManager.getRequestContext(); 133 IndexTreeNode selectedNode = (IndexTreeNode) 134 rc.getRequest().getSession().getAttribute( 135 getCurrentTreeModelType(rc)+SELECTEDNODE); 136 137 if (selectedNode == null) { 139 IndexTreeModel model = getCurrentTreeModel(); 140 selectedNode = ((IndexTreeNode)model.getCurrentNode()); 141 if (selectedNode == null) { 142 selectedNode = model.getRoot(); 144 } 145 } 146 return selectedNode; 147 } 148 149 public static void setSelectedNode(IndexTreeNode selectedNode) { 150 RequestContext rc = RequestManager.getRequestContext(); 151 rc.getRequest().getSession().setAttribute( 152 getCurrentTreeModelType(rc)+SELECTEDNODE, selectedNode); 153 } 154 155 public static String URLencode(RequestContext ctx, String str) { 156 String encoding = ctx.getResponse().getCharacterEncoding() != null ? 157 ctx.getResponse().getCharacterEncoding() : CCI18N.ISO_ENCODING; 158 try { 159 str = URLEncoder.encode(str, encoding); 160 } catch (java.io.UnsupportedEncodingException ex) { 161 } 162 return str; 163 } 164 165 169 170 public static String concatDelimiter(String [] str, String delimiter){ 171 StringBuffer retStr = new StringBuffer (); 172 173 if(str != null) { 174 for (int i=0; i < str.length; i++) { 175 String element = str[i]; 176 177 if (element == null || element.length() == 0) { 178 throw new IllegalArgumentException (); 179 } 180 retStr.append(element); 181 182 if (i < str.length - 1) { 183 retStr.append(delimiter); 184 } 185 } 186 } 187 188 return retStr.toString(); 189 190 } 191 192 196 197 public static String [] stringToArray(String str, String delimiter) { 198 String [] retString = new String [0]; 199 200 if (str != null) { 201 if(delimiter == null) { 202 delimiter = " "; 203 } 204 StringTokenizer tokens = new StringTokenizer (str, delimiter); 205 retString = new String [tokens.countTokens()]; 206 int i = 0; 207 while(tokens.hasMoreTokens()) { 208 retString[i++] = tokens.nextToken(); 209 } 210 } 211 return retString; 212 } 213 214 217 218 public static HashMap stringArrayToHashMap(String [] str) { 219 HashMap h = new HashMap (); 220 221 for (int i=0; str != null && i < str.length; i++) { 222 h.put(str[i], str[i]); 223 } 224 return h; 225 } 226 227 public static String getDefaultDisplayURL(String url) { 228 if (isLoggableFINER()) { 229 logFINER("URL = "+AdminGUIConstants.DEFAULT_DISPLAY_URL_DIR+url); 230 } 231 return AdminGUIConstants.DEFAULT_DISPLAY_URL_DIR+url; 232 } 233 234 public static String getLocalizedURL(RequestContext rc, String fileName) { 235 return getLocalizedURL(rc, fileName, null); 236 } 237 238 public static String getLocalizedURL(RequestContext rc, String fileName, Locale locale) { 239 if(locale == null) { 240 locale = getLocale(); 241 } 242 if (locale.equals(Locale.ENGLISH) || locale.getLanguage().equals("en")) 243 return fileName; 244 245 int lastdot = fileName.lastIndexOf("."); 246 String newName = fileName.substring(0,lastdot)+"_"+locale.getLanguage() + 247 fileName.substring(lastdot,fileName.length()); 248 249 try { 250 verifyClassExists(rc.getServletContext(), newName); 251 } catch (ClassNotFoundException ex) { 252 return fileName; 254 } 255 return newName; 256 } 257 258 public static String getLocalizedString(String key) { 259 return key; 260 } 261 262 private static void verifyClassExists(ServletContext sc, String fileName) throws ClassNotFoundException { 263 String className = null; 264 if (fileName.endsWith(".jsp")) { 265 className = fileName.substring(0,fileName.lastIndexOf(".jsp"))+"_jsp"; 266 className = className.replaceAll("/", "._"); 267 className = "_jasper"+className; 268 269 try { 270 Class clazz = Class.forName(className); 271 return; 272 } catch (ClassNotFoundException ex) { 273 } catch (Exception ex) { 274 } 275 } 276 if (sc != null) { 277 String path = sc.getRealPath(fileName); 279 File f = new File (path); 280 if (f.exists() == false) { 281 String msg = ""; 282 if (className != null) { 283 msg = "Class \""+className+"\" not found, and"; 284 } 285 msg += "File \""+path+"\" not found!"; 286 throw new ClassNotFoundException (msg); 287 } 288 } else { 289 throw new FrameworkException("sc null"); 290 } 291 } 292 293 294 344 public static boolean isLoggableSEVERE() { 346 return sLogger.isLoggable(Level.SEVERE); 347 } 348 349 public static void logSEVERE(String msg) { 350 sLogger.log(Level.SEVERE, msg); 351 } 352 353 public static void logSEVERE(Throwable ex) { 354 logSEVERE("", ex); 355 } 356 357 public static void logSEVERE(String msg, Throwable ex) { 358 sLogger.log(Level.SEVERE, msg, ex); 359 } 360 361 public static boolean isLoggableWARNING() { 363 return sLogger.isLoggable(Level.WARNING); 364 } 365 366 public static void logWARNING(String msg) { 367 sLogger.log(Level.WARNING, msg); 368 } 369 370 public static void logWARNING(Throwable ex) { 371 logWARNING("", ex); 372 } 373 374 public static void logWARNING(String msg, Throwable ex) { 375 sLogger.log(Level.WARNING, msg, ex); 376 } 377 378 public static boolean isLoggableINFO() { 380 return sLogger.isLoggable(Level.INFO); 381 } 382 383 public static void logINFO(String msg) { 384 sLogger.log(Level.INFO, msg); 385 } 386 387 public static void logINFO(Throwable ex) { 388 logINFO("", ex); 389 } 390 391 public static void logINFO(String msg, Throwable ex) { 392 sLogger.log(Level.INFO, msg, ex); 393 } 394 395 public static boolean isLoggableCONFIG() { 397 return sLogger.isLoggable(Level.CONFIG); 398 } 399 400 public static void logCONFIG(String msg) { 401 sLogger.log(Level.CONFIG, msg); 402 } 403 404 public static void logCONFIG(Throwable ex) { 405 logCONFIG("", ex); 406 } 407 408 public static void logCONFIG(String msg, Throwable ex) { 409 sLogger.log(Level.CONFIG, msg, ex); 410 } 411 412 public static boolean isLoggableFINE() { 414 return sLogger.isLoggable(Level.FINE); 415 } 416 417 public static void logFINE(String msg) { 418 sLogger.log(Level.FINE, msg); 419 } 420 421 public static void logFINE(Throwable ex) { 422 logFINE("", ex); 423 } 424 425 public static void logFINE(String msg, Throwable ex) { 426 sLogger.log(Level.FINE, msg, ex); 427 } 428 429 public static boolean isLoggableFINER() { 431 return sLogger.isLoggable(Level.FINER); 432 } 433 434 public static void logFINER(String msg) { 435 sLogger.log(Level.FINER, msg); 436 } 437 438 public static void logFINER(Throwable ex) { 439 logFINER("", ex); 440 } 441 442 public static void logFINER(String msg, Throwable ex) { 443 sLogger.log(Level.FINER, msg, ex); 444 } 445 446 public static boolean isLoggableFINEST() { 448 return sLogger.isLoggable(Level.FINEST); 449 } 450 451 public static void logFINEST(String msg) { 452 sLogger.log(Level.FINEST, msg); 453 } 454 455 public static void logFINEST(Throwable ex) { 456 logFINEST("", ex); 457 } 458 459 public static void logFINEST(String msg, Throwable ex) { 460 sLogger.log(Level.FINEST, msg, ex); 461 } 462 463 public static String getLocalizedHTML(RequestContext rc, String fileName) { 464 return getLocalizedHTML(rc, fileName, null); 465 } 466 public static String getLocalizedHTML(RequestContext rc, String fileName, Locale locale) { 467 if (locale == null) { 468 locale = getLocale(); 469 } 470 if (locale.equals(Locale.ENGLISH) || 471 locale.getLanguage() == null || 472 locale.getLanguage().equalsIgnoreCase("null") || 473 locale.getLanguage().equals("en")) { 474 return fileName; 475 } 476 477 int lastdot = fileName.lastIndexOf(".html"); 478 String newName = fileName.substring(0,lastdot)+"_"+locale.getLanguage()+".html"; 479 480 String path = rc.getServletContext().getRealPath(newName); 481 File f = new File (path); 482 483 if (!(f.exists())) { 484 if (isLoggableWARNING()) { 485 logWARNING("AdminGUI: "+path+" File Not Found"); 486 } 487 return fileName; 488 } 489 return newName; 490 } 491 492 public static String getMessage(String baseName, String key, Object [] args) { 495 return getMessage(baseName, key, args, null); 496 } 497 498 499 public static String getMessage(String baseName, String key, Object [] args, Locale locale) { 500 if (key == null) { 501 return null; 502 } 503 if(locale == null) { 504 locale = getLocale(); 505 } 506 ResourceBundle bundle = getResourceBundle(baseName, locale); 507 508 if(args == null) { 509 return getMessageFromBundle(key, bundle); 510 } 511 String pattern = getMessageFromBundle(key, bundle); 512 MessageFormat mf = new MessageFormat (pattern); 513 514 Object [] mfArgs = new Object [args.length]; 515 for(int i = 0; i < args.length; i++) { 516 mfArgs[i] = getMessageFromBundle(args[i].toString(), bundle); 517 } 518 519 key = mf.format(mfArgs); 520 return key; 521 } 522 523 527 private static Locale getLocale() { 528 RequestContext ctx = RequestManager.getRequestContext(); 529 Locale locale = null; 530 if(ctx != null) { 531 HttpServletRequest request = ctx.getRequest(); 532 try { 538 locale = request.getLocale(); 539 } catch (Exception ex) { 540 locale = Locale.getDefault(); 541 } 542 } 543 return (locale == null ? Locale.getDefault() : locale); 544 } 545 546 public static String fileReader(String fileName) throws FileNotFoundException , IOException { 547 StringWriter sr = new StringWriter (); 548 try { 549 FileReader fr = new FileReader (fileName); 550 551 char[] buf = new char[1024]; 552 int len = 0; 553 while (len != -1) { 554 try { 555 len = fr.read(buf, 0, buf.length); 556 } catch (EOFException eof) { 557 break; 558 } 559 if (len != -1) { 560 sr.write(buf, 0, len); 561 } 562 } 563 564 fr.close(); 565 sr.close(); 566 567 } catch (FileNotFoundException fnfe) { 568 throw fnfe; 570 } 574 catch (IOException ioe) { 575 throw ioe; 576 } 579 return sr.toString(); 580 } 581 582 public static boolean isEmpty(String str) { 583 if (str == null) 584 return true; 585 if (str.trim().length() == 0) 586 return true; 587 return false; 588 } 589 590 private static String getMessageFromBundle(String key, ResourceBundle bundle) { 591 try { 592 key = bundle.getString(key); 593 } 594 catch (MissingResourceException ex) { 595 } 596 return key; 597 } 598 private static ResourceBundle getResourceBundle(String baseName, Locale locale) { 599 ResourceBundle bundle = null; 600 try { 601 bundle = ResourceBundle.getBundle(baseName, locale); 602 } 603 catch(MissingResourceException ex) { 604 try { 605 bundle = ResourceBundle.getBundle(baseName); 606 } 607 catch(MissingResourceException ex1) { 608 bundle = ResourceBundle.getBundle("com.sun.enterprise.tools.admingui.resources.Resources"); 610 } 611 } 612 return bundle; 613 } 614 615 public static String addHtmlProp(String tag, String addText) { 617 int i = tag.indexOf(' '); 618 if (i<0) 619 return tag; 620 return tag.substring(0,i) + " " + addText + tag.substring(i,tag.length()); 621 } 622 623 624 public static String removeHtmlProp(String tag, String prop) { 625 int i = tag.indexOf(prop); 626 if (i<0) return tag; 627 int j = tag.indexOf('\"', i+2+prop.length()); 628 if (j<0) return tag; 629 630 631 632 637 644 645 return tag.substring(0,i) + tag.substring(j+1, tag.length()); 646 647 648 } 649 650 public static String extractHtmlProp(String tag, String prop) { 651 int i = tag.indexOf(prop); 652 if (i<0) return ""; 653 int j = tag.indexOf('\"', i+2+prop.length()); 654 if (j<0) return ""; 655 return tag.substring(i+2+prop.length(), j); 656 } 657 658 673 public static List parseStringList(String line) 674 { 675 return parseStringList(line, null); 676 } 677 678 696 public static List parseStringList(String line, String sep) 697 { 698 if (line == null) 699 return null; 700 701 StringTokenizer st; 702 if (sep == null) 703 st = new StringTokenizer (line); 704 else 705 st = new StringTokenizer (line, sep); 706 707 String token; 708 709 List tokens = new Vector (); 710 while (st.hasMoreTokens()) 711 { 712 token = st.nextToken().trim(); 713 if (token.length() > 0) 714 tokens.add(token); 715 } 716 717 return tokens; 718 } 719 720 public static String getFormattedDate(long time, int dateStyle) { 721 DateFormat df = DateFormat.getDateTimeInstance(dateStyle, DateFormat.MEDIUM, getLocale()); 722 String formattedDate = df.format(new Date (time)); 723 return formattedDate; 724 } 725 726 public static String getDomainRoot() { 727 String domainRoot = System.getProperty(SystemPropertyConstants.INSTANCE_ROOT_PROPERTY); 728 return domainRoot; 729 } 730 731 private static final String SELECTEDNODE = "selectedNode"; 732 private static final String TREE_MODEL_TYPE = "treeModelType"; 733 public static final String DEFAULT_TREE_MODEL_TYPE = "index"; 734 public static final Logger sLogger = Logger.getLogger(AdminGUIConstants.LOGGER_NAME); 735 } 736 | Popular Tags |