All Downloads are FREE. Search and download functionalities are using the official Maven repository.

patterntesting.runtime.util.Environment Maven / Gradle / Ivy

Go to download

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.

There is a newer version: 2.4.0
Show newest version
/*
 * $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 (Entryentry : props.entrySet()) { systemProps.setProperty((String) entry.getKey(), (String) entry .getValue()); } if (log.isDebugEnabled()) { log.debug(props.size() + " properties loaded from " + istream); } return props; } /** * In some JEE environment like Google's App Engine (GAE) it is not allowed * to use multi threading. But you can also disable multi threading by * setting the system property {@link #DISABLE_THREADS}. * * @return normally true */ public static boolean areThreadsAllowed() { if (isGoogleAppEngine() || isPropertyEnabled(DISABLE_THREADS)) { return false; } return true; } /** * If we are in a Google App Engine (GAE) environment we will return true * here. * * @return true if application runs on GAE */ public static boolean isGoogleAppEngine() { return System.getProperty("com.google.appengine.runtime.version") != null; } /** * If we are inside a Weblogic Server (WLS) we will return true here. * We look only for the system property "weblogic.Name" here. * Another possibility (not realized) would be to look at the classloader. * * @return true inside Weblogic Server */ public static boolean isWeblogicServer() { return System.getProperty("weblogic.Name") != null; } /** * Normally you'll find the local Maven repository at ~/.m2/repository. * But on cloudbees it is the local .repository directory. So we try to * find out here which one we should use. * * @return the root dir of the local Maven repository * @throws IOException Signals that an I/O exception has occurred. */ public static File getLocalMavenRepositoryDir() throws IOException { File[] repoDirs = { new File(FileHelper.getHomeDir(), ".m2/repository"), new File(".repository").getAbsoluteFile(), new File("../.repository").getAbsoluteFile() }; for (int i = 0; i < repoDirs.length; i++) { if (repoDirs[i].exists() && repoDirs[i].isDirectory()) { return repoDirs[i]; } } throw new IOException("local maven repository not found in " + Converter.toString(repoDirs)); } /** * It is only tested for Jamon 2.4 and 2.7 so we look for it * * @return true if Jamon 2.4 or 2.7 (or greater) was found */ public static boolean isJamonAvailable() { String resource = "/com/jamonapi/MonitorFactory.class"; try { URL classURL = Environment.class.getResource(resource); URI jarURI = ClasspathHelper.getParent(classURL.toURI(), resource); JarFile jarfile = ClasspathMonitor.whichResourceJar(jarURI); Manifest manifest = jarfile.getManifest(); Attributes attributes = manifest.getMainAttributes(); String version = attributes.getValue("version"); if (version.equalsIgnoreCase("JAMon 2.4") || (version.compareTo("JAMon 2.7") >= 0)) { log.info("{} available for profiling.", version); return true; } else { log.info(version + " not supported (only JAMon 2.4 and 2.7 or higher), using simple profiling."); } } catch (RuntimeException e) { // if JAMon is not in classpath we come with a NPE to here log.debug("JAMon not available, using simple profiling"); } catch (IOException ioe) { log.info("Manifest for JAMon not found, using simple profiling", ioe); } catch (URISyntaxException use) { log.info(resource + " not available as URI, using simple profiling", use); } return false; } }




© 2015 - 2024 Weber Informatics LLC | Privacy Policy