jmeterutils.java
来自「测试工具」· Java 代码 · 共 1,073 行 · 第 1/3 页
JAVA
1,073 行
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/
package org.apache.jmeter.util;
import java.awt.Dimension;
import java.awt.HeadlessException;
import java.awt.event.ActionListener;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.URL;
import java.util.Enumeration;
import java.util.Hashtable;
import java.util.Iterator;
import java.util.Locale;
import java.util.MissingResourceException;
import java.util.Properties;
import java.util.Random;
import java.util.ResourceBundle;
import java.util.StringTokenizer;
import java.util.Vector;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JOptionPane;
// import javax.xml.parsers.SAXParserFactory;
import org.apache.jmeter.gui.GuiPackage;
import org.apache.jorphan.logging.LoggingManager;
import org.apache.jorphan.test.UnitTestManager;
import org.apache.jorphan.util.JOrphanUtils;
import org.apache.log.Logger;
import org.apache.oro.text.PatternCacheLRU;
import org.apache.oro.text.regex.Pattern;
import org.apache.oro.text.regex.Perl5Compiler;
import org.apache.oro.text.regex.Perl5Matcher;
import org.xml.sax.XMLReader;
/**
* This class contains the static utility methods used by JMeter.
*
*/
public class JMeterUtils implements UnitTestManager {
private static Logger log = LoggingManager.getLoggerForClass();
private static PatternCacheLRU patternCache = new PatternCacheLRU(
getPropDefault("oro.patterncache.size",1000), // $NON-NLS-1$
new Perl5Compiler());
private static final String EXPERT_MODE_PROPERTY = "jmeter.expertMode"; // $NON-NLS-1$
private static Properties appProperties;
private static Vector localeChangeListeners = new Vector();
private static Locale locale;
private static ResourceBundle resources;
private static ThreadLocal localMatcher = new ThreadLocal() {
protected Object initialValue() {
return new Perl5Matcher();
}
};
// Provide Random numbers to whomever wants one
private static Random rand = new Random();
/**
* Gets Perl5Matcher for this thread.
*/
public static Perl5Matcher getMatcher() {
return (Perl5Matcher) localMatcher.get();
}
/**
* This method is used by the init method to load the property file that may
* even reside in the user space, or in the classpath under
* org.apache.jmeter.jmeter.properties.
*
* The method also initialises logging and sets up the default Locale
*
* TODO - perhaps remove?
* [still used
*
* @param file
* the file to load
* @return the Properties from the file
*/
public static Properties getProperties(String file) {
loadJMeterProperties(file);
initLogging();
initLocale();
return appProperties;
}
/**
* Initialise JMeter logging
*/
public static void initLogging() {
LoggingManager.initializeLogging(appProperties);
log = LoggingManager.getLoggerForClass();
}
/**
* Initialise the JMeter Locale
*/
public static void initLocale() {
String loc = appProperties.getProperty("language"); // $NON-NLS-1$
if (loc != null) {
String []parts = JOrphanUtils.split(loc,"_");// $NON-NLS-1$
if (parts.length==2) {
setLocale(new Locale(parts[0], parts[1]));
} else {
setLocale(new Locale(loc, "")); // $NON-NLS-1$
}
} else {
setLocale(Locale.getDefault());
}
}
/**
* Load the JMeter properties file; if not found, then
* default to "org/apache/jmeter/jmeter.properties" from the classpath
*
* c.f. loadProperties
*
*/
public static void loadJMeterProperties(String file) {
Properties p = new Properties(System.getProperties());
InputStream is = null;
try {
File f = new File(file);
is = new FileInputStream(f);
p.load(is);
} catch (IOException e) {
try {
is =
ClassLoader.getSystemResourceAsStream("org/apache/jmeter/jmeter.properties"); // $NON-NLS-1$
if (is == null)
throw new RuntimeException("Could not read JMeter properties file");
p.load(is);
} catch (IOException ex) {
// JMeter.fail("Could not read internal resource. " +
// "Archive is broken.");
}
} finally {
JOrphanUtils.closeQuietly(is);
}
appProperties = p;
}
/**
* This method loads a property file that may reside in the user space, or
* in the classpath
*
* @param file
* the file to load
* @return the Properties from the file
*/
public static Properties loadProperties(String file) {
Properties p = new Properties();
InputStream is = null;
try {
File f = new File(file);
is = new FileInputStream(f);
p.load(is);
} catch (IOException e) {
try {
final URL resource = JMeterUtils.class.getClassLoader().getResource(file);
if (resource == null) {
log.warn("Cannot find " + file);
return null;
}
is = resource.openStream();
if (is == null) {
log.warn("Cannot open " + file);
return null;
}
p.load(is);
} catch (IOException ex) {
log.warn("Error reading " + file + " " + ex.toString());
return null;
}
} finally {
JOrphanUtils.closeQuietly(is);
}
return p;
}
public static PatternCacheLRU getPatternCache() {
return patternCache;
}
/**
* Get a compiled expression from the pattern cache (READ_ONLY).
*
* @param expression
* @return compiled pattern
*
* @throws org.apache.oro.text.regex.MalformedPatternException (Runtime)
* This should be caught for expressions that may vary (e.g. user input)
*
*/
public static Pattern getPattern(String expression){
return getPattern(expression, Perl5Compiler.READ_ONLY_MASK);
}
/**
* Get a compiled expression from the pattern cache.
*
* @param expression RE
* @param options e.g. READ_ONLY_MASK
* @return compiled pattern
*
* @throws org.apache.oro.text.regex.MalformedPatternException (Runtime)
* This should be caught for expressions that may vary (e.g. user input)
*
*/
public static Pattern getPattern(String expression, int options){
return patternCache.getPattern(expression, options);
}
public void initializeProperties(String file) {
System.out.println("Initializing Properties: " + file);
getProperties(file);
}
public static String[] getSearchPaths() {
String p = JMeterUtils.getPropDefault("search_paths", null); // $NON-NLS-1$
String[] result = new String[1];
if (p != null) {
String[] paths = p.split(";"); // $NON-NLS-1$
result = new String[paths.length + 1];
for (int i = 1; i < result.length; i++) {
result[i] = paths[i - 1];
}
}
result[0] = getJMeterHome() + "/lib/ext"; // $NON-NLS-1$
return result;
}
/**
* Provide random numbers
*
* @param r -
* the upper bound (exclusive)
*/
public static int getRandomInt(int r) {
return rand.nextInt(r);
}
/**
* Changes the current locale: re-reads resource strings and notifies
* listeners.
*
* @param loc -
* new locale
*/
public static void setLocale(Locale loc) {
log.info("Setting Locale to " + loc.toString());
locale = loc;
/*
* See bug 29920. getBundle() defaults to the property file for the
* default Locale before it defaults to the base property file, so we
* need to change the default Locale to ensure the base property file is
* found.
*/
Locale def = null;
if (loc.getLanguage().equals(Locale.ENGLISH.getLanguage())) {
def = Locale.getDefault();
// Don't change locale from en_GB to en
if (!def.getLanguage().equals(Locale.ENGLISH.getLanguage())) {
Locale.setDefault(Locale.ENGLISH);
} else {
def = null; // no need to reset Locale
}
}
resources = ResourceBundle.getBundle("org.apache.jmeter.resources.messages", locale); // $NON-NLS-1$
notifyLocaleChangeListeners();
/*
* Reset Locale if necessary so other locales are properly handled
*/
if (def != null) {
Locale.setDefault(def);
}
}
/**
* Gets the current locale.
*
* @return current locale
*/
public static Locale getLocale() {
return locale;
}
public static void addLocaleChangeListener(LocaleChangeListener listener) {
localeChangeListeners.add(listener);
}
public static void removeLocaleChangeListener(LocaleChangeListener listener) {
localeChangeListeners.remove(listener);
}
/**
* Notify all listeners interested in locale changes.
*
*/
private static void notifyLocaleChangeListeners() {
LocaleChangeEvent event = new LocaleChangeEvent(JMeterUtils.class, locale);
Iterator iterator = ((Vector) localeChangeListeners.clone()).iterator();
while (iterator.hasNext()) {
LocaleChangeListener listener = (LocaleChangeListener) iterator.next();
listener.localeChanged(event);
}
}
/**
* Gets the resource string for this key.
*
* If the resource is not found, a warning is logged
*
* @param key
* the key in the resource file
* @return the resource string if the key is found; otherwise, return
* "[res_key="+key+"]"
*/
public static String getResString(String key) {
return getResStringDefault(key, RES_KEY_PFX + key + "]"); // $NON-NLS-1$
}
public static final String RES_KEY_PFX = "[res_key="; // $NON-NLS-1$
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?