View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    *
9    *      http://www.apache.org/licenses/LICENSE-2.0
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
16   */
17  
18  package org.apache.commons.lang3;
19  
20  import java.io.IOException;
21  import java.nio.file.Files;
22  import java.nio.file.Paths;
23  import java.util.stream.Stream;
24  
25  /**
26   * Helps query the runtime environment.
27   *
28   * @since 3.15.0
29   */
30  public class RuntimeEnvironment {
31  
32      /**
33       * Tests whether the file at the given path string contains a specific line.
34       *
35       * @param path The path to a file.
36       * @param line The line to find.
37       * @return whether the file at the given path string contains a specific line.
38       */
39      private static Boolean containsLine(final String path, final String line) {
40          try (Stream<String> stream = Files.lines(Paths.get(path))) {
41              return stream.anyMatch(test -> test.contains(line));
42          } catch (final IOException e) {
43              return false;
44          }
45      }
46  
47      /**
48       * Tests whether we are running in a container like Docker or Podman.
49       *
50       * @return whether we are running in a container like Docker or Podman.
51       */
52      public static Boolean inContainer() {
53          return inDocker() || inPodman();
54      }
55  
56      /**
57       * Tests whether we are running in a Docker container.
58       * <p>
59       * Package-private for testing.
60       * </p>
61       *
62       * @return whether we are running in a Docker container.
63       */
64      // Could be public at a later time.
65      static Boolean inDocker() {
66          return containsLine("/proc/1/cgroup", "/docker");
67      }
68  
69      /**
70       * Tests whether we are running in a Podman container.
71       * <p>
72       * Package-private for testing.
73       * </p>
74       *
75       * @return whether we are running in a Podman container.
76       */
77      // Could be public at a later time.
78      static Boolean inPodman() {
79          return containsLine("/proc/1/environ", "container=podman");
80      }
81  
82      /**
83       * Tests whether we are running in a Windows Subsystem for Linux (WSL).
84       * <p>
85       * Package-private for testing.
86       * </p>
87       *
88       * @return whether we are running in a Windows Subsystem for Linux (WSL).
89       */
90      // Could be public at a later time.
91      static Boolean inWsl() {
92          return containsLine("/proc/1/environ", "container=wslcontainer_host_id");
93      }
94  
95      /**
96       * Constructs a new instance.
97       *
98       * @deprecated Will be removed in 4.0.0.
99       */
100     @Deprecated
101     public RuntimeEnvironment() {
102         // empty
103     }
104 }