| 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 &
|