001/* 002 * Licensed to the Apache Software Foundation (ASF) under one 003 * or more contributor license agreements. See the NOTICE file 004 * distributed with this work for additional information 005 * regarding copyright ownership. The ASF licenses this file 006 * to you under the Apache License, Version 2.0 (the 007 * "License"); you may not use this file except in compliance 008 * with the License. You may obtain a copy of the License at 009 * 010 * http://www.apache.org/licenses/LICENSE-2.0 011 * 012 * Unless required by applicable law or agreed to in writing, 013 * software distributed under the License is distributed on an 014 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 015 * KIND, either express or implied. See the License for the 016 * specific language governing permissions and limitations 017 * under the License. 018 */ 019package org.apache.reef.util; 020 021import java.io.File; 022import java.nio.file.InvalidPathException; 023import java.nio.file.Path; 024import java.util.HashSet; 025import java.util.Set; 026import java.util.logging.Level; 027import java.util.logging.Logger; 028 029/** 030 * Utils which detect environment settings. 031 */ 032public final class EnvironmentUtils { 033 034 private static final Logger LOG = Logger.getLogger(EnvironmentUtils.class.getName()); 035 036 /** 037 * Get a set of all classpath entries EXCEPT of those under 038 * $JAVA_HOME, $YARN_HOME, and $HADOOP_HOME. 039 * 040 * @return A set of classpath entries as strings. 041 */ 042 public static Set<String> getAllClasspathJars() { 043 return getAllClasspathJars( 044 "JAVA_HOME", 045 "YARN_HOME", 046 "HADOOP_HOME", 047 "HADOOP_YARN_HOME", 048 "HADOOP_HDFS_HOME", 049 "HADOOP_COMMON_HOME", 050 "HADOOP_MAPRED_HOME", 051 "YARN_CONF_DIR", 052 "HADOOP_CONF_DIR"); 053 } 054 055 /** 056 * Get a set of all classpath entries EXCEPT of those under excludeEnv directories. 057 * Every excludeEnv entry is an environment variable name. 058 * 059 * @param excludeEnv A set of environments to be excluded. 060 * @return A set of classpath entries as strings. 061 */ 062 public static Set<String> getAllClasspathJars(final String... excludeEnv) { 063 064 final Set<String> jars = new HashSet<>(); 065 final Set<Path> excludePaths = new HashSet<>(); 066 067 for (final String env : excludeEnv) { 068 final String path = System.getenv(env); 069 if (null != path) { 070 final File file = new File(path); 071 if (file.exists()) { 072 excludePaths.add(file.toPath()); 073 } 074 } 075 } 076 077 for (final String path : System.getProperty("java.class.path").split(File.pathSeparator)) { 078 try { 079 final File file = new File(path); 080 if (file.exists()) { 081 final Path absolutePath = file.toPath(); 082 boolean toBeAdded = true; 083 for (final Path prefix : excludePaths) { 084 if (absolutePath.startsWith(prefix)) { 085 toBeAdded = false; 086 } 087 } 088 if (toBeAdded) { 089 jars.add(absolutePath.toString()); 090 } 091 } 092 } catch (final InvalidPathException ex) { 093 LOG.log(Level.FINE, "Skip path: {0}: {1}", new Object[]{path, ex}); 094 } 095 } 096 097 return jars; 098 } 099 100 /** 101 * Check whether assert() statements are evaluated. 102 * 103 * @return true, if assertions are enabled. False otherwise. 104 */ 105 public static boolean areAssertionsEnabled() { 106 try { 107 assert false; 108 // If we got here, the assert above can't have thrown an exception. hence, asserts must be off. 109 return false; 110 } catch (final AssertionError assertionError) { 111 // The assert above threw an exception. Asserts must be enabled. 112 return true; 113 } 114 } 115 116 /** 117 * @param clazz class 118 * @return the location (JAR or .class file) where the given class is located. 119 */ 120 public static String getClassLocation(final Class<?> clazz) { 121 return clazz.getProtectionDomain().getCodeSource().getLocation().getFile(); 122 } 123 124 /** 125 * Empty private constructor to prohibit instantiation of utility class. 126 */ 127 private EnvironmentUtils() { 128 } 129}