001 /*
002 * SPDX-License-Identifier: Apache-2.0
003 *
004 * Copyright 2008-2017 the original author or authors.
005 *
006 * Licensed under the Apache License, Version 2.0 (the "License");
007 * you may not use this file except in compliance with the License.
008 * 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, software
013 * distributed under the License is distributed on an "AS IS" BASIS,
014 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
015 * See the License for the specific language governing permissions and
016 * limitations under the License.
017 */
018 package griffon.core.env;
019
020 import javax.annotation.Nonnull;
021 import javax.annotation.Nullable;
022 import java.util.LinkedHashMap;
023 import java.util.Locale;
024 import java.util.Map;
025
026 import static java.util.Objects.requireNonNull;
027
028 /**
029 * An enum that represents the current environment
030 *
031 * @author Andres Almiray
032 * @since 2.0.0
033 */
034 public enum Environment {
035 /**
036 * The development environment
037 */
038 DEVELOPMENT,
039
040 /**
041 * The production environment
042 */
043 PRODUCTION,
044
045 /**
046 * The test environment
047 */
048 TEST,
049
050 /**
051 * A custom environment
052 */
053 CUSTOM;
054
055 /**
056 * Constant used to resolve the environment via System.getProperty(Environment.KEY)
057 */
058 public static final String KEY = "griffon.env";
059
060 private static final String PRODUCTION_ENV_SHORT_NAME = "prod";
061 private static final String DEVELOPMENT_ENVIRONMENT_SHORT_NAME = "dev";
062 private static final String TEST_ENVIRONMENT_SHORT_NAME = "test";
063
064 private static final Map<String, String> ENV_NAME_MAPPINGS = new LinkedHashMap<>();
065
066 static {
067 ENV_NAME_MAPPINGS.put(DEVELOPMENT_ENVIRONMENT_SHORT_NAME, Environment.DEVELOPMENT.getName());
068 ENV_NAME_MAPPINGS.put(PRODUCTION_ENV_SHORT_NAME, Environment.PRODUCTION.getName());
069 ENV_NAME_MAPPINGS.put(TEST_ENVIRONMENT_SHORT_NAME, Environment.TEST.getName());
070 }
071
072 private String name;
073
074 /**
075 * @return Return true if the environment has been set as a System property
076 */
077 public static boolean isSystemSet() {
078 return System.getProperty(KEY) != null;
079 }
080
081 /**
082 * Returns the environment for the given short name
083 *
084 * @param shortName The short name
085 * @return The Environment or null if not known
086 */
087 @Nullable
088 public static Environment resolveEnvironment(@Nullable String shortName) {
089 final String envName = ENV_NAME_MAPPINGS.get(shortName);
090 if (envName != null) {
091 return Environment.valueOf(envName.toUpperCase());
092 }
093 return null;
094 }
095
096 @Nonnull
097 public static String getEnvironmentShortName(@Nonnull Environment env) {
098 requireNonNull(env, "Argument 'env' must not be null");
099 switch (env) {
100 case DEVELOPMENT:
101 return "dev";
102 case TEST:
103 return "test";
104 case PRODUCTION:
105 return "prod";
106 default:
107 return env.getName();
108 }
109 }
110
111 /**
112 * @return The name of the environment
113 */
114 @Nonnull
115 public String getName() {
116 if (this != CUSTOM || name == null) {
117 return this.toString().toLowerCase(Locale.getDefault());
118 }
119 return name;
120 }
121
122 public void setName(@Nullable String name) {
123 this.name = name;
124 }
125 }
|