View Javadoc
1   /*
2    * Copyright 2020 SPF4J.
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    *      http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  package org.spf4j.base;
17  
18  import java.util.function.Supplier;
19  import java.util.logging.Level;
20  import java.util.logging.Logger;
21  import javax.annotation.Nullable;
22  import javax.annotation.ParametersAreNonnullByDefault;
23  import org.checkerframework.checker.nullness.qual.NonNull;
24  
25  /**
26   *
27   * @author Zoltan Farkas
28   */
29  @ParametersAreNonnullByDefault
30  public final class Env {
31  
32    private Env() { }
33  
34    public static int getValue(final String envname, final int defaultValue) {
35      String v = System.getenv(envname);
36      if (v == null) {
37        return defaultValue;
38      } else {
39        return Integer.parseInt(v);
40      }
41    }
42  
43    public static float getValue(final String envname, final float defaultValue) {
44      String v = System.getenv(envname);
45      if (v == null) {
46        return defaultValue;
47      } else {
48        return Float.parseFloat(v);
49      }
50    }
51  
52    public static double getValue(final String envname, final double defaultValue) {
53      String v = System.getenv(envname);
54      if (v == null) {
55        return defaultValue;
56      } else {
57        return Float.parseFloat(v);
58      }
59    }
60  
61    public static boolean getValue(final String envname, final boolean defaultValue) {
62      String v = System.getenv(envname);
63      if (v == null) {
64        return defaultValue;
65      } else {
66        return Boolean.parseBoolean(v);
67      }
68    }
69  
70    @NonNull
71    public static String getValue(final String envname, final String defaultValue) {
72      String v = System.getenv(envname);
73      if (v == null) {
74        return defaultValue;
75      } else {
76        return v;
77      }
78    }
79  
80    @NonNull
81    public static String getValue(final String envname, final Supplier<String> defaultValue) {
82      String v = System.getenv(envname);
83      if (v == null) {
84        return defaultValue.get();
85      } else {
86        return v;
87      }
88    }
89  
90    @NonNull
91    public static String getSystemProperty(final String name, final String[] deprecatedAliases,
92            final String defaultValue) {
93      String val = getSystemProperty(name, deprecatedAliases);
94      if (val != null) {
95        return val;
96      }
97      return defaultValue;
98    }
99  
100   @Nullable
101   public static String getSystemProperty(final String name, final String... deprecatedAliases) {
102     String val = System.getProperty(name);
103     if (val != null) {
104       return val;
105     }
106     for (String key : deprecatedAliases) {
107       val = System.getProperty(key);
108       if (val != null) {
109         Logger.getLogger(Env.class.getName()).log(Level.WARNING,
110                 "Use {0} system property instead of {1}", new Object[] {name, key});
111         return val;
112       }
113     }
114     return null;
115   }
116 }