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