Source code
Description: source: package: org.apache.commons.lang3, class: RuntimeEnvironment

    001/*
    002 * Licensed to the Apache Software Foundation (ASF) under one or more
    003 * contributor license agreements.  See the NOTICE file distributed with
    004 * this work for additional information regarding copyright ownership.
    005 * The ASF licenses this file to You under the Apache License, Version 2.0
    006 * (the "License"); you may not use this file except in compliance with
    007 * the License.  You may obtain a copy of the License at
    008 *
    009 *      http://www.apache.org/licenses/LICENSE-2.0
    010 *
    011 * Unless required by applicable law or agreed to in writing, software
    012 * distributed under the License is distributed on an "AS IS" BASIS,
    013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    014 * See the License for the specific language governing permissions and
    015 * limitations under the License.
    016 */
    017
    018package org.apache.commons.lang3;
    019
    020import java.io.IOException;
    021import java.nio.file.Files;
    022import java.nio.file.Paths;
    023import java.util.stream.Stream;
    024
    025/**
    026 * Helps query the runtime environment.
    027 *
    028 * @since 3.15.0
    029 */
    030public class RuntimeEnvironment {
    031
    032    /**
    033     * Tests whether the file at the given path string contains a specific line.
    034     *
    035     * @param path The path to a file.
    036     * @param line The line to find.
    037     * @return whether the file at the given path string contains a specific line.
    038     */
    039    private static Boolean containsLine(final String path, final String line) {
    040        try (Stream<String> stream = Files.lines(Paths.get(path))) {
    041            return stream.anyMatch(test -> test.contains(line));
    042        } catch (final IOException e) {
    043            return false;
    044        }
    045    }
    046
    047    /**
    048     * Tests whether we are running in a container like Docker or Podman.
    049     *
    050     * @return whether we are running in a container like Docker or Podman.
    051     */
    052    public static Boolean inContainer() {
    053        return inDocker() || inPodman();
    054    }
    055
    056    /**
    057     * Tests whether we are running in a Docker container.
    058     * <p>
    059     * Package-private for testing.
    060     * </p>
    061     *
    062     * @return whether we are running in a Docker container.
    063     */
    064    // Could be public at a later time.
    065    static Boolean inDocker() {
    066        return containsLine("/proc/1/cgroup", "/docker");
    067    }
    068
    069    /**
    070     * Tests whether we are running in a Podman container.
    071     * <p>
    072     * Package-private for testing.
    073     * </p>
    074     *
    075     * @return whether we are running in a Podman container.
    076     */
    077    // Could be public at a later time.
    078    static Boolean inPodman() {
    079        return containsLine("/proc/1/environ", "container=podman");
    080    }
    081
    082    /**
    083     * Tests whether we are running in a Windows Subsystem for Linux (WSL).
    084     * <p>
    085     * Package-private for testing.
    086     * </p>
    087     *
    088     * @return whether we are running in a Windows Subsystem for Linux (WSL).
    089     */
    090    // Could be public at a later time.
    091    static Boolean inWsl() {
    092        return containsLine("/proc/1/environ", "container=wslcontainer_host_id");
    093    }
    094
    095    /**
    096     * Constructs a new instance.
    097     *
    098     * @deprecated Will be removed in 4.0.0.
    099     */
    100    @Deprecated
    101    public RuntimeEnvironment() {
    102        // empty
    103    }
    104}