001 /*
002 * Copyright 2008-2014 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 griffon.util.GriffonNameUtils.isBlank;
025
026 /**
027 * An enum that represents the current environment
028 *
029 * @author Graeme Rocher (Grails 1.1)
030 */
031 public enum Environment {
032 /**
033 * The development environment
034 */
035 DEVELOPMENT,
036
037 /**
038 * The production environment
039 */
040 PRODUCTION,
041
042 /**
043 * The test environment
044 */
045 TEST,
046
047 /**
048 * A custom environment
049 */
050 CUSTOM;
051
052 /**
053 * Constant used to resolve the environment via System.getProperty(Environment.KEY)
054 */
055 public static final String KEY = "griffon.env";
056
057 /**
058 * Constants that indicates whether this GriffonApplication is running in the default environment
059 */
060 public static final String DEFAULT = "griffon.env.default";
061 private static final String PRODUCTION_ENV_SHORT_NAME = "prod";
062 private static final String DEVELOPMENT_ENVIRONMENT_SHORT_NAME = "dev";
063 private static final String TEST_ENVIRONMENT_SHORT_NAME = "test";
064
065 private static final Map<String, String> ENV_NAME_MAPPINGS = new LinkedHashMap<String, String>() {{
066 put(DEVELOPMENT_ENVIRONMENT_SHORT_NAME, Environment.DEVELOPMENT.getName());
067 put(PRODUCTION_ENV_SHORT_NAME, Environment.PRODUCTION.getName());
068 put(TEST_ENVIRONMENT_SHORT_NAME, Environment.TEST.getName());
069 }
070
071 private static final long serialVersionUID = -8447299990856630300L;
072 };
073
074 /**
075 * Returns the current environment which is typically either DEVELOPMENT, PRODUCTION or TEST.
076 * For custom environments CUSTOM type is returned.
077 *
078 * @return The current environment.
079 */
080 @Nonnull
081 public static Environment getCurrent() {
082 String envName = System.getProperty(Environment.KEY);
083 Metadata metadata = Metadata.getCurrent();
084 if (metadata != null && isBlank(envName)) {
085 envName = metadata.getEnvironment();
086 }
087
088 if (isBlank(envName)) {
089 return DEVELOPMENT;
090 }
091
092 Environment env = getEnvironment(envName);
093 if (env == null) {
094 try {
095 env = Environment.valueOf(envName.toUpperCase());
096 } catch (IllegalArgumentException e) {
097 // ignore
098 }
099 }
100 if (env == null) {
101 env = Environment.CUSTOM;
102 env.setName(envName);
103 }
104 return env;
105 }
106
107 /**
108 * @return Return true if the environment has been set as a System property
109 */
110 public static boolean isSystemSet() {
111 return System.getProperty(KEY) != null;
112 }
113
114 /**
115 * Returns the environment for the given short name
116 *
117 * @param shortName The short name
118 * @return The Environment or null if not known
119 */
120 @Nullable
121 public static Environment getEnvironment(@Nullable String shortName) {
122 final String envName = ENV_NAME_MAPPINGS.get(shortName);
123 if (envName != null) {
124 return Environment.valueOf(envName.toUpperCase());
125 }
126 return null;
127 }
128
129 private String name;
130
131 /**
132 * @return The name of the environment
133 */
134 @Nonnull
135 public String getName() {
136 if (name == null) {
137 return this.toString().toLowerCase(Locale.getDefault());
138 }
139 return name;
140 }
141
142 public void setName(String name) {
143 this.name = name;
144 }
145
146 @Nonnull
147 public static String getEnvironmentShortName() {
148 switch(Environment.getCurrent()) {
149 case DEVELOPMENT: return "dev";
150 case TEST: return "test";
151 case PRODUCTION: return "prod";
152 default: return Environment.getCurrent().getName();
153 }
154 }
155 }
|