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 java.util.LinkedHashMap;
019 import java.util.Locale;
020 import java.util.Map;
021
022
023 /**
024 * An enum that represents the current running mode.
025 *
026 * @author Andres Almiray
027 * @since 2.0.0
028 */
029 public enum RunMode {
030 STANDALONE, WEBSTART, APPLET, CUSTOM;
031 /**
032 * Constant used to resolve the runMode via System.getProperty(RunMode.KEY)
033 */
034 public static final String KEY = "griffon.runmode";
035
036 /**
037 * Constants that indicates whether this GriffonApplication is running in the default running mode
038 */
039 public static final String DEFAULT = "griffon.runmode.default";
040 private static final String STANDALONE_RUNMODE_SHORT_NAME = "standalone";
041 private static final String WEBSTART_RUNMODE_SHORT_NAME = "webstart";
042 private static final String APPLET_RUNMODE_SHORT_NAME = "applet";
043
044 private static final Map<String, String> MODE_NAME_MAPPINGS = new LinkedHashMap<String, String>() {{
045 put(STANDALONE_RUNMODE_SHORT_NAME, RunMode.STANDALONE.getName());
046 put(WEBSTART_RUNMODE_SHORT_NAME, RunMode.WEBSTART.getName());
047 put(APPLET_RUNMODE_SHORT_NAME, RunMode.APPLET.getName());
048 }
049
050 private static final long serialVersionUID = -1551704296210703535L;
051 };
052
053 /**
054 * Returns the current RunMode which is typically either STANDALONE, WEBSTART or APPLET.
055 * For custom running modes CUSTOM type is returned.
056 *
057 * @return The current runMode.
058 */
059 public static RunMode getCurrent() {
060 String modeName = System.getProperty(RunMode.KEY);
061
062 if (isBlank(modeName)) {
063 return STANDALONE;
064 } else {
065 RunMode mode = getRunMode(modeName);
066 if (mode == null) {
067 try {
068 mode = RunMode.valueOf(modeName.toUpperCase());
069 } catch (IllegalArgumentException e) {
070 // ignore
071 }
072 }
073 if (mode == null) {
074 mode = RunMode.CUSTOM;
075 mode.setName(modeName);
076 }
077 return mode;
078 }
079 }
080
081 /**
082 * @return Return true if the running mode has been set as a System property
083 */
084 public static boolean isSystemSet() {
085 return System.getProperty(KEY) != null;
086 }
087
088 /**
089 * Returns the running mode for the given short name
090 *
091 * @param shortName The short name
092 * @return The RunMode or null if not known
093 */
094 public static RunMode getRunMode(String shortName) {
095 final String modeName = MODE_NAME_MAPPINGS.get(shortName);
096 if (modeName != null) {
097 return RunMode.valueOf(modeName.toUpperCase());
098 }
099 return null;
100 }
101
102 private static boolean isBlank(String value) {
103 return value == null || value.trim().length() == 0;
104 }
105
106 private String name;
107
108 /**
109 * @return The name of the running mode
110 */
111 public String getName() {
112 if (name == null) {
113 return this.toString().toLowerCase(Locale.getDefault());
114 }
115 return name;
116 }
117
118 public void setName(String name) {
119 this.name = name;
120 }
121 }
|