Method from org.apache.jmeter.util.JMeterUtils Detail: |
public static void addLocaleChangeListener(LocaleChangeListener listener) {
localeChangeListeners.add(listener);
}
|
public static JButton createButton(String name,
ActionListener listener) {
JButton button = new JButton(getImage(name + ".on.gif")); // $NON-NLS-1$
button.setDisabledIcon(getImage(name + ".off.gif")); // $NON-NLS-1$
button.setRolloverIcon(getImage(name + ".over.gif")); // $NON-NLS-1$
button.setPressedIcon(getImage(name + ".down.gif")); // $NON-NLS-1$
button.setActionCommand(name);
button.addActionListener(listener);
button.setRolloverEnabled(true);
button.setFocusPainted(false);
button.setBorderPainted(false);
button.setOpaque(false);
button.setPreferredSize(new Dimension(24, 24));
return button;
}
Create a button with the netscape style |
public static JButton createSimpleButton(String name,
ActionListener listener) {
JButton button = new JButton(getImage(name + ".gif")); // $NON-NLS-1$
button.setActionCommand(name);
button.addActionListener(listener);
button.setFocusPainted(false);
button.setBorderPainted(false);
button.setOpaque(false);
button.setPreferredSize(new Dimension(25, 25));
return button;
}
Create a button with the netscape style |
public static File findFile(String fileName) {
File f =new File(fileName);
if (!f.exists()){
f=new File(getJMeterBinDir(),fileName);
}
return f;
}
Find a file in the current directory or in the JMeter bin directory. |
public static int findInArray(String[] array,
String value) {
int count = -1;
int index = -1;
if (array != null && value != null) {
while (++count < array.length) {
if (array[count] != null && array[count].equals(value)) {
index = count;
break;
}
}
}
return index;
}
Finds a string in an array of strings and returns the |
public static Hashtable getAlias(Properties properties) {
return getHashtable(properties, "alias."); // $NON-NLS-1$
}
Creates the vector of alias strings. |
public static Vector getControllers(Properties properties) {
String name = "controller."; // $NON-NLS-1$
Vector v = new Vector();
Enumeration names = properties.keys();
while (names.hasMoreElements()) {
String prop = (String) names.nextElement();
if (prop.startsWith(name)) {
Object o = instantiate(properties.getProperty(prop),
"org.apache.jmeter.control.SamplerController"); // $NON-NLS-1$
v.addElement(o);
}
}
return v;
}
Creates a vector of SampleController plugins. |
public static Hashtable getHashtable(Properties properties,
String name) {
Hashtable t = new Hashtable();
Enumeration names = properties.keys();
while (names.hasMoreElements()) {
String prop = (String) names.nextElement();
if (prop.startsWith(name)) {
t.put(prop.substring(name.length()), properties.getProperty(prop));
}
}
return t;
}
Creates a table of strings for all the properties that start with a
common prefix. |
public static ImageIcon getImage(String name) {
try {
return new ImageIcon(JMeterUtils.class.getClassLoader().getResource(
"org/apache/jmeter/images/" + name.trim())); // $NON-NLS-1$
} catch (NullPointerException e) {
log.warn("no icon for " + name);
return null;
} catch (NoClassDefFoundError e) {// Can be returned by headless hosts
log.info("no icon for " + name + " " + e.getMessage());
return null;
} catch (InternalError e) {// Can be returned by headless hosts
log.info("no icon for " + name + " " + e.getMessage());
return null;
}
}
This looks for the requested image in the classpath under
org.apache.jmeter.images. |
public static ImageIcon getImage(String name,
String description) {
ImageIcon icon = getImage(name);
if(icon != null) {
icon.setDescription(description);
}
return icon;
}
This looks for the requested image in the classpath under
org.apache.jmeter.images. , and also sets the description
of the image, which is useful if the icon is going to be placed
on the clipboard. |
public static String getJMeterBinDir() {
return jmBin;
}
Get the JMeter bin directory - does not include the trailing separator. |
public static String getJMeterCopyright() {
return JMeterVersion.COPYRIGHT;
}
Gets the JMeter copyright. |
public static String getJMeterHome() {
return jmDir;
}
Get the JMeter home directory - does not include the trailing separator. |
public static Properties getJMeterProperties() {
return appProperties;
}
This gets the currently defined appProperties. It can only be called
after the #getProperties(String) method is called. |
public static String getJMeterVersion() {
return JMeterVersion.getVERSION();
}
|
public static synchronized String getLocalHostFullName() {
if (localHostFullName == null) {
getLocalHostDetails();
}
return localHostFullName;
}
Returns the cached result from calling
InetAddress.getLocalHost().getCanonicalHostName() |
public static synchronized String getLocalHostIP() {
if (localHostIP == null) {
getLocalHostDetails();
}
return localHostIP;
}
Returns the cached result from calling
InetAddress.getLocalHost().getHostAddress() |
public static synchronized String getLocalHostName() {
if (localHostName == null) {
getLocalHostDetails();
}
return localHostName;
}
Returns the cached result from calling
InetAddress.getLocalHost().getHostName() |
public static Locale getLocale() {
return locale;
}
|
public static String getLocaleString(String locale) {
// All keys in messages.properties are lowercase (historical reasons?)
String resKey = locale.toLowerCase(java.util.Locale.ENGLISH);
try {
return resources.getString(resKey);
} catch (MissingResourceException e) {
}
return locale;
}
Get the locale name as a resource.
Does not log an error if the resource does not exist.
This is needed to support additional locales, as they won't be in existing messages files. |
public static Perl5Matcher getMatcher() {
return (Perl5Matcher) localMatcher.get();
}
Gets Perl5Matcher for this thread. |
public static Pattern getPattern(String expression) {
return getPattern(expression, Perl5Compiler.READ_ONLY_MASK);
}
Get a compiled expression from the pattern cache (READ_ONLY). |
public static Pattern getPattern(String expression,
int options) {
return patternCache.getPattern(expression, options);
}
Get a compiled expression from the pattern cache. |
public static PatternCacheLRU getPatternCache() {
return patternCache;
}
|
public static int getPropDefault(String propName,
int defaultVal) {
int ans;
try {
ans = (Integer.valueOf(appProperties.getProperty(propName, Integer.toString(defaultVal)).trim()))
.intValue();
} catch (Exception e) {
ans = defaultVal;
}
return ans;
}
Get a int value with default if not present. |
public static boolean getPropDefault(String propName,
boolean defaultVal) {
boolean ans;
try {
String strVal = appProperties.getProperty(propName, Boolean.toString(defaultVal)).trim();
if (strVal.equalsIgnoreCase("true") || strVal.equalsIgnoreCase("t")) { // $NON-NLS-1$ // $NON-NLS-2$
ans = true;
} else if (strVal.equalsIgnoreCase("false") || strVal.equalsIgnoreCase("f")) { // $NON-NLS-1$ // $NON-NLS-2$
ans = false;
} else {
ans = ((Integer.valueOf(strVal)).intValue() == 1);
}
} catch (Exception e) {
ans = defaultVal;
}
return ans;
}
Get a boolean value with default if not present. |
public static long getPropDefault(String propName,
long defaultVal) {
long ans;
try {
ans = (Long.valueOf(appProperties.getProperty(propName, Long.toString(defaultVal)).trim())).longValue();
} catch (Exception e) {
ans = defaultVal;
}
return ans;
}
Get a long value with default if not present. |
public static String getPropDefault(String propName,
String defaultVal) {
String ans;
try {
ans = appProperties.getProperty(propName, defaultVal).trim();
} catch (Exception e) {
ans = defaultVal;
}
return ans;
}
Get a String value with default if not present. |
public static Properties getProperties(String file) {
loadJMeterProperties(file);
initLogging();
initLocale();
return appProperties;
}
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 |
public static String getProperty(String propName) {
String ans = null;
try {
ans = appProperties.getProperty(propName);
} catch (Exception e) {
ans = null;
}
return ans;
}
Get the value of a JMeter property. |
public static int getRandomInt(int r) {
return rand.nextInt(r);
}
|
public static String getResString(String key) {
return getResStringDefault(key, RES_KEY_PFX + key + "]"); // $NON-NLS-1$
}
Gets the resource string for this key.
If the resource is not found, a warning is logged |
public static String getResString(String key,
String defaultValue) {
return getResStringDefault(key, defaultValue);
} Deprecated! Only - intended for use in development; use
getResString(String) normally
Gets the resource string for this key.
If the resource is not found, a warning is logged |
public static String getResourceFileAsText(String name) {
BufferedReader fileReader = null;
try {
String lineEnd = System.getProperty("line.separator"); // $NON-NLS-1$
fileReader = new BufferedReader(new InputStreamReader(JMeterUtils.class.getClassLoader()
.getResourceAsStream(name)));
StringBuffer text = new StringBuffer();
String line = "NOTNULL"; // $NON-NLS-1$
while (line != null) {
line = fileReader.readLine();
if (line != null) {
text.append(line);
text.append(lineEnd);
}
}
// Done by finally block: fileReader.close();
return text.toString();
} catch (NullPointerException e) // Cannot find file
{
return ""; // $NON-NLS-1$
} catch (IOException e) {
return ""; // $NON-NLS-1$
} finally {
IOUtils.closeQuietly(fileReader);
}
}
|
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;
}
|
public static String[] getTestSamples(Properties properties,
String name) {
return (String[]) getVector(properties, name + ".testsample").toArray(new String[0]); // $NON-NLS-1$
}
Create a string of class names for a particular SamplerController |
public static Vector getTimers(Properties properties) {
return instantiate(getVector(properties, "timer."), // $NON-NLS-1$
"org.apache.jmeter.timers.Timer"); // $NON-NLS-1$
}
Creates the vector of Timers plugins. |
public static Vector getVector(Properties properties,
String name) {
Vector v = new Vector();
Enumeration names = properties.keys();
while (names.hasMoreElements()) {
String prop = (String) names.nextElement();
if (prop.startsWith(name)) {
v.addElement(properties.getProperty(prop));
}
}
return v;
}
Creates a vector of strings for all the properties that start with a
common prefix. |
public static Vector getVisualizers(Properties properties) {
return instantiate(getVector(properties, "visualizer."), // $NON-NLS-1$
"org.apache.jmeter.visualizers.Visualizer"); // $NON-NLS-1$
}
Creates the vector of visualizer plugins. |
public static XMLReader getXMLParser() {
XMLReader reader = null;
try {
reader = (XMLReader) instantiate(getPropDefault("xml.parser", // $NON-NLS-1$
"org.apache.xerces.parsers.SAXParser"), // $NON-NLS-1$
"org.xml.sax.XMLReader"); // $NON-NLS-1$
// reader = xmlFactory.newSAXParser().getXMLReader();
} catch (Exception e) {
reader = (XMLReader) instantiate(getPropDefault("xml.parser", // $NON-NLS-1$
"org.apache.xerces.parsers.SAXParser"), // $NON-NLS-1$
"org.xml.sax.XMLReader"); // $NON-NLS-1$
}
return reader;
}
Create an instance of an org.xml.sax.Parser based on the default props. |
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());
}
}
Initialise the JMeter Locale |
public static void initLogging() {
LoggingManager.initializeLogging(appProperties);
}
Initialise JMeter logging |
public void initializeProperties(String file) {
System.out.println("Initializing Properties: " + file);
getProperties(file);
}
|
public static Object instantiate(String className,
String impls) {
if (className != null) {
className = className.trim();
}
if (impls != null) {
impls = impls.trim();
}
try {
Class c = Class.forName(impls);
try {
Class o = Class.forName(className);
Object res = o.newInstance();
if (c.isInstance(res)) {
return res;
}
throw new IllegalArgumentException(className + " is not an instance of " + impls);
} catch (ClassNotFoundException e) {
log.error("Error loading class " + className + ": class is not found");
} catch (IllegalAccessException e) {
log.error("Error loading class " + className + ": does not have access");
} catch (InstantiationException e) {
log.error("Error loading class " + className + ": could not instantiate");
} catch (NoClassDefFoundError e) {
log.error("Error loading class " + className + ": couldn't find class " + e.getMessage());
}
} catch (ClassNotFoundException e) {
log.error("Error loading class " + impls + ": was not found.");
}
return null;
}
Instatiate an object and guarantee its class. |
public static Vector instantiate(Vector v,
String className) {
Vector i = new Vector();
try {
Class c = Class.forName(className);
Enumeration elements = v.elements();
while (elements.hasMoreElements()) {
String name = (String) elements.nextElement();
try {
Object o = Class.forName(name).newInstance();
if (c.isInstance(o)) {
i.addElement(o);
}
} catch (ClassNotFoundException e) {
log.error("Error loading class " + name + ": class is not found");
} catch (IllegalAccessException e) {
log.error("Error loading class " + name + ": does not have access");
} catch (InstantiationException e) {
log.error("Error loading class " + name + ": could not instantiate");
} catch (NoClassDefFoundError e) {
log.error("Error loading class " + name + ": couldn't find class " + e.getMessage());
}
}
} catch (ClassNotFoundException e) {
log.error("Error loading class " + className + ": class is not found");
}
return i;
}
Instantiate a vector of classes |
public static boolean isExpertMode() {
return JMeterUtils.getPropDefault(EXPERT_MODE_PROPERTY, false);
}
Determine whether we are in 'expert' mode. Certain features may be hidden
from user's view unless in expert mode. |
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;
}
Load the JMeter properties file; if not found, then
default to "org/apache/jmeter/jmeter.properties" from the classpath
c.f. loadProperties |
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;
}
This method loads a property file that may reside in the user space, or
in the classpath |
public static void removeLocaleChangeListener(LocaleChangeListener listener) {
localeChangeListeners.remove(listener);
}
|
public static void reportErrorToUser(String errorMsg) {
reportErrorToUser(errorMsg, JMeterUtils.getResString("error_title")); // $NON-NLS-1$
}
Report an error through a dialog box.
Title defaults to "error_title" resource string |
public static void reportErrorToUser(String errorMsg,
String titleMsg) {
if (errorMsg == null) {
errorMsg = "Unknown error - see log file";
log.warn("Unknown error", new Throwable("errorMsg == null"));
}
GuiPackage instance = GuiPackage.getInstance();
if (instance == null) {
System.out.println(errorMsg);
return; // Done
}
try {
JOptionPane.showMessageDialog(instance.getMainFrame(),
errorMsg,
titleMsg,
JOptionPane.ERROR_MESSAGE);
} catch (HeadlessException e) {
log.warn("reportErrorToUser(\"" + errorMsg + "\") caused", e);
}
}
Report an error through a dialog box. |
public static void selJComboBoxItem(Properties properties,
JComboBox combo,
Vector namVec,
String name) {
int idx = namVec.indexOf(name);
combo.setSelectedIndex(idx);
// Redisplay.
combo.updateUI();
return;
}
Sets the selection of the JComboBox to the Object 'name' from the list in
namVec. |
public static void setJMeterHome(String home) {
jmDir = home;
jmBin = jmDir + File.separator + "bin"; // $NON-NLS-1$
}
|
public static void setLocale(Locale loc) {
log.info("Setting Locale to " + loc.toString());
/*
* 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;
boolean isDefault = false; // Are we the default language?
if (loc.getLanguage().equals(ENGLISH_LANGUAGE)) {
isDefault = true;
def = Locale.getDefault();
// Don't change locale from en_GB to en
if (!def.getLanguage().equals(ENGLISH_LANGUAGE)) {
Locale.setDefault(Locale.ENGLISH);
} else {
def = null; // no need to reset Locale
}
}
if (loc.toString().equals("ignoreResources")){ // $NON-NLS-1$
log.warn("Resource bundles will be ignored");
ignoreResorces = true;
// Keep existing settings
} else {
ignoreResorces = false;
ResourceBundle resBund = ResourceBundle.getBundle("org.apache.jmeter.resources.messages", loc); // $NON-NLS-1$
resources = resBund;
locale = loc;
final Locale resBundLocale = resBund.getLocale();
if (isDefault || resBundLocale.equals(loc)) {// language change worked
// Check if we at least found the correct language:
} else if (resBundLocale.getLanguage().equals(loc.getLanguage())) {
log.warn("Could not find resources for '"+loc.toString()+"', using '"+resBundLocale.toString()+"'");
} else {
log.error("Could not find resources for '"+loc.toString()+"'");
}
}
notifyLocaleChangeListeners();
/*
* Reset Locale if necessary so other locales are properly handled
*/
if (def != null) {
Locale.setDefault(def);
}
}
Changes the current locale: re-reads resource strings and notifies
listeners. |
public static Object setProperty(String propName,
String propValue) {
return appProperties.setProperty(propName, propValue);
}
|
public static Vector tokenize(String string,
String separator) {
Vector v = new Vector();
StringTokenizer s = new StringTokenizer(string, separator);
while (s.hasMoreTokens()) {
v.addElement(s.nextToken());
}
return v;
}
Tokenize a string into a vector of tokens |
public static String unsplit(Object[] splittee,
Object splitChar) {
StringBuffer retVal = new StringBuffer();
int count = -1;
while (++count < splittee.length) {
if (splittee[count] != null) {
retVal.append(splittee[count]);
}
if (count + 1 < splittee.length && splittee[count + 1] != null) {
retVal.append(splitChar);
}
}
return retVal.toString();
}
Takes an array of strings and a tokenizer character, and returns a string
of all the strings concatenated with the tokenizer string in between each
one. |
public static String unsplit(Object[] splittee,
Object splitChar,
String def) {
StringBuffer retVal = new StringBuffer();
int count = -1;
while (++count < splittee.length) {
if (splittee[count] != null) {
retVal.append(splittee[count]);
} else {
retVal.append(def);
}
if (count + 1 < splittee.length) {
retVal.append(splitChar);
}
}
return retVal.toString();
}
Takes an array of strings and a tokenizer character, and returns a string
of all the strings concatenated with the tokenizer string in between each
one. |