This project has retired. For details please refer to its Attic page.
Source code
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 org.apache.reef.tang.formats.ConfigurationModule;
022import org.apache.reef.tang.formats.OptionalParameter;
023import org.apache.reef.tang.formats.Param;
024
025import java.io.File;
026import java.nio.file.InvalidPathException;
027import java.nio.file.Path;
028import java.util.HashSet;
029import java.util.Set;
030import java.util.logging.Level;
031import java.util.logging.Logger;
032
033public final class EnvironmentUtils {
034
035  private static final Logger LOG = Logger.getLogger(EnvironmentUtils.class.getName());
036
037  /**
038   * Get a set of all classpath entries EXCEPT of those under
039   * $JAVA_HOME, $YARN_HOME, and $HADOOP_HOME.
040   *
041   * @return A set of classpath entries as strings.
042   */
043  public static Set<String> getAllClasspathJars() {
044    return getAllClasspathJars(
045        "JAVA_HOME",
046        "YARN_HOME",
047        "HADOOP_HOME",
048        "HADOOP_YARN_HOME",
049        "HADOOP_HDFS_HOME",
050        "HADOOP_COMMON_HOME",
051        "HADOOP_MAPRED_HOME",
052        "YARN_CONF_DIR",
053        "HADOOP_CONF_DIR");
054  }
055
056  /**
057   * Get a set of all classpath entries EXCEPT of those under excludeEnv directories.
058   * Every excludeEnv entry is an environment variable name.
059   *
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   * @param config
102   * @param param
103   * @param values
104   * @param <P>
105   * @return
106   * @deprecated in 0.2 this really should be in Tang.
107   */
108  @Deprecated
109  public static <P extends Param> ConfigurationModule addAll(
110      ConfigurationModule config, final P param, final Iterable<String> values) {
111    for (final String val : values) {
112      config = config.set(param, val);
113    }
114    return config;
115  }
116
117  /**
118   * @param config
119   * @param param
120   * @return
121   * @deprecated Using this method is inherently non-deterministic as it depends on environment variables on your local
122   * machine.
123   */
124  @Deprecated
125  public static ConfigurationModule addClasspath(
126      final ConfigurationModule config, final OptionalParameter<String> param) {
127    return addAll(config, param, getAllClasspathJars());
128  }
129
130  /**
131   * Check whether assert() statements are evaluated.
132   *
133   * @return true, if assertions are enabled. False otherwise.
134   */
135  public static boolean areAssertionsEnabled() {
136    try {
137      assert false;
138      // If we got here, the assert above can't have thrown an exception. hence, asserts must be off.
139      return false;
140    } catch (final AssertionError assertionError) {
141      // The assert above threw an exception. Asserts must be enabled.
142      return true;
143    }
144  }
145
146  /**
147   * @param clazz
148   * @return the location (JAR or .class file) where the given class is located.
149   */
150  public static String getClassLocation(final Class<?> clazz) {
151    return clazz.getProtectionDomain().getCodeSource().getLocation().getFile();
152  }
153}