patterntesting.runtime.util.Environment Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of patterntesting-rt Show documentation
Show all versions of patterntesting-rt Show documentation
PatternTesting Runtime (patterntesting-rt) is the runtime component for
the PatternTesting framework. It provides the annotations and base classes
for the PatternTesting testing framework (e.g. patterntesting-check,
patterntesting-concurrent or patterntesting-exception) but can be also
used standalone for classpath monitoring or profiling.
It uses AOP and AspectJ to perform this feat.
/*
* $Id: Environment.java,v 1.26 2014/04/21 16:04:52 oboehm Exp $
*
* Copyright (c) 2010 by Oliver Boehm
*
* Licensed 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 orimplied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
* (c)reated 01.02.2010 by oliver ([email protected])
*/
package patterntesting.runtime.util;
import java.io.*;
import java.net.*;
import java.util.Map.Entry;
import java.util.*;
import java.util.jar.*;
import org.slf4j.*;
import patterntesting.runtime.io.FileHelper;
import patterntesting.runtime.monitor.ClasspathMonitor;
/**
* This class provides some utilities for the access to the environment
* (e.g. the system properties).
*
* TODO: Use common.lang.SystemUtils for OS_NAME or other system properties
*
* @author oliver
* @since 1.0 (01.02.2010)
*/
public final class Environment {
private static final Logger log = LoggerFactory.getLogger(Environment.class);
/** operating system (e.g. "Mac OS X"). */
public static final String OS_NAME = System.getProperty("os.name", "unknown");
/** architecture (e.g. "x86_64"). */
public static final String OS_ARCH = System.getProperty("os.arch", "unknown");
/** os version (e.g. "10.6.2"). */
public static final String OS_VERSION = System.getProperty("os.version", "unknown");
/** JDK version (e.g. "1.6.0_17"). */
public static final String JAVA_VERSION = System.getProperty("java.version");
/** Java vendor (e.g. "Apple Inc."). */
public static final String JAVA_VENDOR = System.getProperty("java.vendor");
/** User (e.g. "oliver") */
public static final String USER_NAME = System.getProperty("user.name");
/** System property to disable multithreading. */
public static final String DISABLE_THREADS = "patterntesting.disableThreads";
/** System property to enable integration tests */
public static final String INTEGRATION_TEST = "patterntesting.integrationTest";
/** System property for annotation RunTestsParallel. */
public static final String RUN_TESTS_PARALLEL = "patterntesting.runTestsParallel";
/** System property for annotation SmokeTest. */
public static final String RUN_SMOKE_TESTS = "patterntesting.runSmokeTests";
/** True if property for integration test is set. */
public static final boolean integrationTestEnabled = Environment
.isPropertyEnabled(Environment.INTEGRATION_TEST);
/** True if SmokeTest property is set. */
public static final boolean smokeTestEnabled = Environment
.isPropertyEnabled(Environment.RUN_SMOKE_TESTS);
/** to avoid instantiation of this (static) utility class. */
private Environment() {}
/**
* The name of an environment is derived from the classloader.
* I.e. on a Tomcat server the name is "org.apache.catalina".
*
* @return e.g. "com.google.apphosting" for Google App Enginge
*/
public static String getName() {
String name = getClassLoader().getClass().getName();
String[] packages = name.split("\\.|\\$", 4);
return packages[0] + "." + packages[1] + "." + packages[2];
}
/**
* @{link "http://www.odi.ch/prog/design/newbies.php#23"}
* @return a valid classloader
*/
public static ClassLoader getClassLoader() {
ClassLoader cloader = Thread.currentThread().getContextClassLoader();
if (cloader == null) {
cloader = Environment.class.getClassLoader();
log.warn("no ContextClassLoader found - using " + cloader);
}
return cloader;
}
/**
* Looks if one of the given properties matches a system property.
* If one system property is found and this system property is not
* "false" true is returned.
*
* @param props the properties to be checked
* @return true if one of the given properties exist (and are not "false")
*/
public static boolean matchesOneOf(final String[] props) {
for (int i = 0; i < props.length; i++) {
if (props[i].contains("*") || props[i].contains("?")) {
log.warn("Wildcard in property \"" + props[i] + "\" is not supported!");
}
String prop = System.getProperty(props[i]);
if ((prop != null) && !"false".equalsIgnoreCase(prop)) {
return true;
}
}
return false;
}
/**
* Returns true if the given property is set as System property and the
* value of it is not false.
*
* @param key e.g. "patterntesting.runTestsParallel"
* @return true if property is set
*/
public static boolean isPropertyEnabled(final String key) {
String prop = System.getProperty(key, "false");
return !prop.equalsIgnoreCase("false");
}
/**
* Loads the properties from the classpath and provides them as system
* properties.
*
* @param resource the name of the classpath resource
* @return the loaded properties
* @throws IOException if properties can't be loaded
* @see #loadProperties(InputStream)
*/
public static Properties loadProperties(final String resource)
throws IOException {
ClassLoader cloader = getClassLoader();
InputStream istream = cloader.getResourceAsStream(resource);
if ((istream == null) && resource.startsWith("/")) {
istream = cloader.getResourceAsStream(resource.substring(1));
}
if (istream == null) {
log.debug("using Environment.class to get " + resource + "...");
istream = Environment.class.getResourceAsStream(resource);
}
Properties props = loadProperties(istream);
istream.close();
return props;
}
/**
* Loads the properties from the given InputStream and provides them as
* system properties.
*
* Note: Setting it as system property is not guaranteed to run in a
* cluster or cloud. E.g. on Google's App Engine this seems not to work.
*
* @param istream from here the properties are loaded
* @return the loaded properties
* @throws IOException if properties can't be loaded
*/
public static Properties loadProperties(final InputStream istream)
throws IOException {
Properties props = new Properties();
props.load(istream);
Properties systemProps = System.getProperties();
for (Entry