1 18 19 package org.apache.jmeter.util; 20 21 import java.awt.Dimension ; 22 import java.awt.event.ActionListener ; 23 import java.io.BufferedReader ; 24 import java.io.File ; 25 import java.io.FileInputStream ; 26 import java.io.IOException ; 27 import java.io.InputStream ; 28 import java.io.InputStreamReader ; 29 import java.util.Enumeration ; 30 import java.util.Hashtable ; 31 import java.util.Iterator ; 32 import java.util.Locale ; 33 import java.util.MissingResourceException ; 34 import java.util.Properties ; 35 import java.util.Random ; 36 import java.util.ResourceBundle ; 37 import java.util.StringTokenizer ; 38 import java.util.Vector ; 39 40 import javax.swing.ImageIcon ; 41 import javax.swing.JButton ; 42 import javax.swing.JComboBox ; 43 import javax.swing.JOptionPane ; 44 46 import org.apache.jmeter.gui.GuiPackage; 47 import org.apache.jorphan.logging.LoggingManager; 48 import org.apache.jorphan.test.UnitTestManager; 49 import org.apache.jorphan.util.JOrphanUtils; 50 import org.apache.log.Logger; 51 import org.apache.oro.text.PatternCacheLRU; 52 import org.apache.oro.text.regex.Perl5Compiler; 53 import org.apache.oro.text.regex.Perl5Matcher; 54 import org.xml.sax.XMLReader ; 55 56 61 public class JMeterUtils implements UnitTestManager 62 { 63 private static PatternCacheLRU patternCache = 64 new PatternCacheLRU(1000, new Perl5Compiler()); 65 66 transient private static Logger log = LoggingManager.getLoggerForClass(); 67 68 private static final String EXPERT_MODE_PROPERTY="jmeter.expertMode"; 69 70 private static Properties appProperties; 86 private static Vector localeChangeListeners = new Vector (); 87 private static Locale locale; 88 private static ResourceBundle resources; 89 90 private static ThreadLocal localMatcher = new ThreadLocal () 91 { 92 protected Object initialValue() 93 { 94 return new Perl5Matcher(); 95 } 96 }; 97 98 private static Random rand = new Random (); 100 101 104 public static Perl5Matcher getMatcher() 105 { 106 return (Perl5Matcher) localMatcher.get(); 107 } 108 109 117 public static Properties getProperties(String file) 118 { 119 Properties p = new Properties (System.getProperties()); 120 try 121 { 122 File f = new File (file); 123 p.load(new FileInputStream (f)); 124 } 125 catch (Exception e) 126 { 127 try 128 { 129 InputStream is = ClassLoader.getSystemResourceAsStream( 130 "org/apache/jmeter/jmeter.properties"); 131 if (is == null) throw new RuntimeException ("Could not read JMeter properties file"); 132 p.load(is); 133 } 134 catch (IOException ex) 135 { 136 } 139 } 140 appProperties = p; 141 LoggingManager.initializeLogging(appProperties); 142 log = LoggingManager.getLoggerForClass(); 143 String loc = appProperties.getProperty("language"); 144 if (loc != null) 145 { 146 setLocale(new Locale (loc, "")); 147 } 148 else 149 { 150 setLocale(Locale.getDefault()); 151 } 152 return p; 153 } 154 155 public static PatternCacheLRU getPatternCache() 156 { 157 return patternCache; 158 } 159 160 public void initializeProperties(String file) 161 { 162 System.out.println("Initializing Properties: " + file); 163 getProperties(file); 164 String home; 165 int pathend=file.lastIndexOf("/"); 166 if (pathend == -1){ home = "."; 168 } else { 169 home = file.substring(0, pathend); 170 } 171 home = new File (home + "/..").getAbsolutePath(); 172 System.out.println("Setting JMeter home: " + home); 173 setJMeterHome(home); 174 } 175 public static String [] getSearchPaths() 176 { 177 String p= JMeterUtils.getPropDefault("search_paths", null); 178 String [] result= new String [1]; 179 180 if (p!=null) { 181 String [] paths= JOrphanUtils.split(p,";"); result= new String [paths.length+1]; 183 for (int i=1; i<result.length; i++) 184 { 185 result[i]= paths[i-1]; 186 } 187 } 188 result[0]= getJMeterHome() + "/lib/ext"; 189 return result; 190 } 191 192 196 public static int getRandomInt(int r) 197 { 198 return rand.nextInt(r); 199 } 200 201 208 public static void setLocale(Locale loc) 209 { 210 locale = loc; 211 resources = 212 ResourceBundle.getBundle( 213 "org.apache.jmeter.resources.messages", 214 locale); 215 notifyLocaleChangeListeners(); 216 } 217 218 224 public static Locale getLocale() 225 { 226 return locale; 227 } 228 229 232 public static void addLocaleChangeListener(LocaleChangeListener listener) 233 { 234 localeChangeListeners.add(listener); 235 } 236 237 240 public static void removeLocaleChangeListener(LocaleChangeListener listener) 241 { 242 localeChangeListeners.remove(listener); 243 } 244 245 250 private static void notifyLocaleChangeListeners() 251 { 252 LocaleChangeEvent event = 253 new LocaleChangeEvent(JMeterUtils.class, locale); 254 Iterator iterator = ((Vector )localeChangeListeners.clone()).iterator(); 255 256 while (iterator.hasNext()) 257 { 258 LocaleChangeListener listener = 259 (LocaleChangeListener) iterator.next(); 260 listener.localeChanged(event); 261 } 262 } 263 264 273 public static String getResString(String key) 274 { 275 return getResStringDefault(key,RES_KEY_PFX+key+"]"); 276 } 277 public static final String RES_KEY_PFX="[res_key="; 278 279 291 public static String getResString(String key, String defaultValue) 292 { 293 return getResStringDefault(key,defaultValue); 294 } 295 296 300 private static String getResStringDefault(String key, String defaultValue) 301 { 302 if (key == null) 303 { 304 return null; 305 } 306 key = key.replace(' ', '_'); 307 key = key.toLowerCase(); 308 String resString = null; 309 try 310 { 311 resString = resources.getString(key); 312 } 313 catch (MissingResourceException mre) 314 { 315 log.warn("ERROR! Resource string not found: [" + key + "]"); 316 resString = defaultValue; 317 } 318 return resString; 319 } 320 326 public static Properties getJMeterProperties() 327 { 328 return appProperties; 329 } 330 337 public static ImageIcon getImage(String name) 338 { 339 try 340 { 341 return new ImageIcon ( 342 JMeterUtils.class.getClassLoader().getResource( 343 "org/apache/jmeter/images/" + name.trim())); 344 } 345 catch (NullPointerException e) 346 { 347 log.warn("no icon for " + name); 348 return null; 349 } 350 } 351 public static String getResourceFileAsText(String name) 352 { 353 try 354 { 355 String lineEnd = System.getProperty("line.separator"); 356 BufferedReader fileReader = 357 new BufferedReader ( 358 new InputStreamReader ( 359 JMeterUtils.class.getClassLoader().getResourceAsStream( 360 name))); 361 StringBuffer text = new StringBuffer (); 362 String line = "NOTNULL"; 363 while (line != null) 364 { 365 line = fileReader.readLine(); 366 if (line != null) 367 { 368 text.append(line); 369 text.append(lineEnd); 370 } 371 } 372 fileReader.close(); 373 return text.toString(); 374 } 375 catch (NullPointerException e) { 377 return ""; 378 } 379 catch (IOException e) 380 { 381 return ""; 382 } 383 } 384 390 public static Vector getTimers(Properties properties) 391 { 392 return instantiate( 393 getVector(properties, "timer."), 394 "org.apache.jmeter.timers.Timer"); 395 } 396 402 public static Vector getVisualizers(Properties properties) 403 { 404 return instantiate( 405 getVector(properties, "visualizer."), 406 "org.apache.jmeter.visualizers.Visualizer"); 407 } 408 414 public static Vector getControllers(Properties properties) 416 { 417 String name = "controller."; 418 Vector v = new Vector (); 419 Enumeration names = properties.keys(); 420 while (names.hasMoreElements()) 421 { 422 String prop = (String ) names.nextElement(); 423 if (prop.startsWith(name)) 424 { 425 Object o = 426 instantiate( 427 properties.getProperty(prop), 428 "org.apache.jmeter.control.SamplerController"); 429 v.addElement(o); 430 } 431 } 432 return v; 433 } 434 441 public static String [] getTestSamples(Properties properties, String name) 442 { 443 return (String []) getVector(properties, name + ".testsample").toArray( 444 new String [0]); 445 } 446 453 public static XMLReader getXMLParser(Properties properties) 454 { 455 return getXMLParser(); 456 } 457 462 public static XMLReader getXMLParser() 463 { 464 XMLReader reader = null; 465 try 466 { 467 reader = 468 (XMLReader ) instantiate(getPropDefault("xml.parser", 469 "org.apache.xerces.parsers.SAXParser"), 470 "org.xml.sax.XMLReader"); 471 } 473 catch (Exception e) 474 { 475 reader = 476 (XMLReader ) instantiate(getPropDefault("xml.parser", 477 "org.apache.xerces.parsers.SAXParser"), 478 "org.xml.sax.XMLReader"); 479 } 480 return reader; 481 } 482 483 489 public static Hashtable getAlias(Properties properties) 490 { 491 return getHashtable(properties, "alias."); 492 } 493 494 502 public static Vector getVector(Properties properties, String name) 503 { 504 Vector v = new Vector (); 505 Enumeration names = properties.keys(); 506 while (names.hasMoreElements()) 507 { 508 String prop = (String ) names.nextElement(); 509 if (prop.startsWith(name)) 510 { 511 v.addElement(properties.getProperty(prop)); 512 } 513 } 514 return v; 515 } 516 517 525 public static Hashtable getHashtable(Properties properties, String name) 526 { 527 Hashtable t = new Hashtable (); 528 Enumeration names = properties.keys(); 529 while (names.hasMoreElements()) 530 { 531 String prop = (String ) names.nextElement(); 532 if (prop.startsWith(name)) 533 { 534 t.put( 535 prop.substring(name.length()), 536 properties.getProperty(prop)); 537 } 538 } 539 return t; 540 } 541 542 549 public static int getPropDefault(String propName, int defaultVal) 550 { 551 int ans; 552 try 553 { 554 ans = 555 (Integer 556 .valueOf( 557 appProperties 558 .getProperty(propName, Integer.toString(defaultVal)) 559 .trim())) 560 .intValue(); 561 } 562 catch (Exception e) 563 { 564 ans = defaultVal; 565 } 566 return ans; 567 } 568 575 public static boolean getPropDefault(String propName, boolean defaultVal) 576 { 577 boolean ans; 578 try 579 { 580 String strVal = 581 appProperties 582 .getProperty(propName, JOrphanUtils.booleanToString(defaultVal)) 583 .trim(); 584 if (strVal.equalsIgnoreCase("true") 585 || strVal.equalsIgnoreCase("t")) 586 { 587 ans = true; 588 } 589 else if ( 590 strVal.equalsIgnoreCase("false") 591 || strVal.equalsIgnoreCase("f")) 592 { 593 ans = false; 594 } 595 else 596 { 597 ans = ((Integer.valueOf(strVal)).intValue() == 1); 598 } 599 } 600 catch (Exception e) 601 { 602 ans = defaultVal; 603 } 604 return ans; 605 } 606 613 public static long getPropDefault(String propName, long defaultVal) 614 { 615 long ans; 616 try 617 { 618 ans = 619 (Long 620 .valueOf( 621 appProperties 622 .getProperty(propName, Long.toString(defaultVal)) 623 .trim())) 624 .longValue(); 625 } 626 catch (Exception e) 627 { 628 ans = defaultVal; 629 } 630 return ans; 631 } 632 639 public static String getPropDefault(String propName, String defaultVal) 640 { 641 String ans; 642 try 643 { 644 ans = appProperties.getProperty(propName, defaultVal).trim(); 645 } 646 catch (Exception e) 647 { 648 ans = defaultVal; 649 } 650 return ans; 651 } 652 658 public static String getProperty(String propName) 659 { 660 String ans = null; 661 try 662 { 663 ans = appProperties.getProperty(propName); 664 } 665 catch (Exception e) 666 { 667 ans = null; 668 } 669 return ans; 670 } 671 672 679 public static Object setProperty(String propName, String propValue) 680 { 681 return appProperties.setProperty(propName,propValue); 682 } 683 684 688 public static void selJComboBoxItem( 689 Properties properties, 690 JComboBox combo, 691 Vector namVec, 692 String name) 693 { 694 int idx = namVec.indexOf(name); 695 combo.setSelectedIndex(idx); 696 combo.updateUI(); 698 return; 699 } 700 701 708 public static Object instantiate(String className, String impls) 709 { 710 if (className != null) 711 { 712 className=className.trim(); 713 } 714 715 if (impls != null) 716 { 717 impls=impls.trim(); 718 } 719 720 try 721 { 722 Class c = Class.forName(impls); 723 try 724 { 725 Class o = Class.forName(className); 726 Object res = o.newInstance(); 727 if (c.isInstance(res)) 728 { 729 return res; 730 } 731 else 732 { 733 throw new IllegalArgumentException ( 734 className + " is not an instance of " + impls); 735 } 736 } 737 catch (ClassNotFoundException e) 738 { 739 log.error( 740 "Error loading class " 741 + className 742 + ": class is not found"); 743 } 744 catch (IllegalAccessException e) 745 { 746 log.error( 747 "Error loading class " 748 + className 749 + ": does not have access"); 750 } 751 catch (InstantiationException e) 752 { 753 log.error( 754 "Error loading class " 755 + className 756 + ": could not instantiate"); 757 } 758 catch (NoClassDefFoundError e) 759 { 760 log.error( 761 "Error loading class " 762 + className 763 + ": couldn't find class " 764 + e.getMessage()); 765 } 766 } 767 catch (ClassNotFoundException e) 768 { 769 log.error("Error loading class " + impls + ": was not found."); 770 } 771 return null; 772 } 773 780 public static Vector instantiate(Vector v, String className) 781 { 782 Vector i = new Vector (); 783 try 784 { 785 Class c = Class.forName(className); 786 Enumeration elements = v.elements(); 787 while (elements.hasMoreElements()) 788 { 789 String name = (String ) elements.nextElement(); 790 try 791 { 792 Object o = Class.forName(name).newInstance(); 793 if (c.isInstance(o)) 794 { 795 i.addElement(o); 796 } 797 } 798 catch (ClassNotFoundException e) 799 { 800 log.error( 801 "Error loading class " + name + ": class is not found"); 802 } 803 catch (IllegalAccessException e) 804 { 805 log.error( 806 "Error loading class " 807 + name 808 + ": does not have access"); 809 } 810 catch (InstantiationException e) 811 { 812 log.error( 813 "Error loading class " 814 + name 815 + ": could not instantiate"); 816 } 817 catch (NoClassDefFoundError e) 818 { 819 log.error( 820 "Error loading class " 821 + name 822 + ": couldn't find class " 823 + e.getMessage()); 824 } 825 } 826 } 827 catch (ClassNotFoundException e) 828 { 829 log.error( 830 "Error loading class " + className + ": class is not found"); 831 } 832 return i; 833 } 834 841 public static Vector tokenize(String string, String separator) 842 { 843 Vector v = new Vector (); 844 StringTokenizer s = new StringTokenizer (string, separator); 845 while (s.hasMoreTokens()) 846 { 847 v.addElement(s.nextToken()); 848 } 849 return v; 850 } 851 858 public static JButton createButton(String name, ActionListener listener) 859 { 860 JButton button = new JButton (getImage(name + ".on.gif")); 861 button.setDisabledIcon(getImage(name + ".off.gif")); 862 button.setRolloverIcon(getImage(name + ".over.gif")); 863 button.setPressedIcon(getImage(name + ".down.gif")); 864 button.setActionCommand(name); 865 button.addActionListener(listener); 866 button.setRolloverEnabled(true); 867 button.setFocusPainted(false); 868 button.setBorderPainted(false); 869 button.setOpaque(false); 870 button.setPreferredSize(new Dimension (24, 24)); 871 return button; 872 } 873 880 public static JButton createSimpleButton( 881 String name, 882 ActionListener listener) 883 { 884 JButton button = new JButton (getImage(name + ".gif")); 885 button.setActionCommand(name); 886 button.addActionListener(listener); 887 button.setFocusPainted(false); 888 button.setBorderPainted(false); 889 button.setOpaque(false); 890 button.setPreferredSize(new Dimension (25, 25)); 891 return button; 892 } 893 894 904 public static String [] split(String splittee, String splitChar, String def) 905 { 906 if (splittee == null || splitChar == null) 907 { 908 return new String [0]; 909 } 910 int spot; 911 while ((spot = splittee.indexOf(splitChar + splitChar)) != -1) 912 { 913 splittee = 914 splittee.substring(0, spot + splitChar.length()) 915 + def 916 + splittee.substring( 917 spot + 1 * splitChar.length(), 918 splittee.length()); 919 } 920 Vector returns = new Vector (); 921 int start = 0; 922 int length = splittee.length(); 923 spot = 0; 924 while (start < length 925 && (spot = splittee.indexOf(splitChar, start)) > -1) 926 { 927 if (spot > 0) 928 { 929 returns.addElement(splittee.substring(start, spot)); 930 } 931 start = spot + splitChar.length(); 932 } 933 if (start < length) 934 { 935 returns.add(splittee.substring(start)); 936 } 937 String [] values = new String [returns.size()]; 938 returns.copyInto(values); 939 return values; 940 } 941 942 947 public static void reportErrorToUser(String errorMsg) 948 { 949 if (errorMsg == null) 950 { 951 errorMsg = "Unknown error - see log file"; 952 log.warn("Unknown error", new Throwable ("errorMsg == null")); 953 } 954 try 955 { 956 JOptionPane.showMessageDialog( 957 GuiPackage.getInstance().getMainFrame(), 958 errorMsg, 959 "Error", 960 JOptionPane.ERROR_MESSAGE); 961 } 962 catch (RuntimeException e) 963 { 964 if (e.getClass().getName().equals("java.awt.HeadlessException")) { 966 log.warn("reportErrorToUser(\""+errorMsg+"\") caused",e); 969 } 970 else 971 { 972 throw e; 973 } 974 } 975 } 976 983 public static int findInArray(String [] array, String value) 984 { 985 int count = -1; 986 int index = -1; 987 if (array != null && value != null) 988 { 989 while (++count < array.length) 990 { 991 if (array[count] != null && array[count].equals(value)) 992 { 993 index = count; 994 break; 995 } 996 } 997 } 998 return index; 999 } 1000 1001 1010 public static String unsplit(Object [] splittee, Object splitChar) 1011 { 1012 StringBuffer retVal = new StringBuffer (""); 1013 int count = -1; 1014 while (++count < splittee.length) 1015 { 1016 if (splittee[count] != null) 1017 { 1018 retVal.append(splittee[count]); 1019 } 1020 if (count + 1 < splittee.length && splittee[count + 1] != null) 1021 { 1022 retVal.append(splitChar); 1023 } 1024 } 1025 return retVal.toString(); 1026 } 1027 1029 1039 public static String unsplit( 1040 Object [] splittee, 1041 Object splitChar, 1042 String def) 1043 { 1044 StringBuffer retVal = new StringBuffer (""); 1045 int count = -1; 1046 while (++count < splittee.length) 1047 { 1048 if (splittee[count] != null) 1049 { 1050 retVal.append(splittee[count]); 1051 } 1052 else 1053 { 1054 retVal.append(def); 1055 } 1056 if (count + 1 < splittee.length) 1057 { 1058 retVal.append(splitChar); 1059 } 1060 } 1061 return retVal.toString(); 1062 } 1063 public static String getJMeterHome() 1065 { 1066 return jmDir; 1067 } 1068 public static void setJMeterHome(String home) 1069 { 1070 jmDir = home; 1071 } 1072 private static String jmDir; 1073 public static final String JMETER = "jmeter"; 1074 public static final String ENGINE = "jmeter.engine"; 1075 public static final String ELEMENTS = "jmeter.elements"; 1076 public static final String GUI = "jmeter.gui"; 1077 public static final String UTIL = "jmeter.util"; 1078 public static final String CLASSFINDER = "jmeter.util.classfinder"; 1079 public static final String TEST = "jmeter.test"; 1080 public static final String HTTP = "jmeter.protocol.http"; 1081 public static final String JDBC = "jmeter.protocol.jdbc"; 1082 public static final String FTP = "jmeter.protocol.ftp"; 1083 public static final String JAVA = "jmeter.protocol.java"; 1084 public static final String PROPERTIES = "jmeter.elements.properties"; 1085 1089 public static String getJMeterVersion() 1090 { 1091 return JMeterVersion.getVERSION(); 1092 } 1093 1094 1098 public static String getJMeterCopyright() 1099 { 1100 return JMeterVersion.COPYRIGHT; 1101 } 1102 1103 1111 public static String getExtendedFrameTitle(String fname) 1112 { 1113 if (fname == null) 1116 { 1117 return "Apache JMeter"; 1118 } 1119 1120 String temp = fname.replace('\\','/'); 1122 String simpleName = temp.substring(temp.lastIndexOf("/") + 1); 1123 return simpleName + " (" + fname + ") - Apache JMeter"; 1124 } 1125 1126 1132 public static boolean isExpertMode() 1133 { 1134 return JMeterUtils.getPropDefault(EXPERT_MODE_PROPERTY, false); 1135 } 1136} 1137 | Popular Tags |