01 /*
02 * SPDX-License-Identifier: Apache-2.0
03 *
04 * Copyright 2008-2017 the original author or authors.
05 *
06 * Licensed under the Apache License, Version 2.0 (the "License");
07 * you may not use this file except in compliance with the License.
08 * You may obtain a copy of the License at
09 *
10 * http://www.apache.org/licenses/LICENSE-2.0
11 *
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
17 */
18 package griffon.core.env;
19
20 import javax.annotation.Nonnull;
21 import javax.annotation.Nullable;
22 import java.util.LinkedHashMap;
23 import java.util.Locale;
24 import java.util.Map;
25
26
27 /**
28 * An enum that represents the current running mode.
29 *
30 * @author Andres Almiray
31 * @since 2.0.0
32 */
33 public enum RunMode {
34 STANDALONE, WEBSTART, APPLET, CUSTOM;
35 /**
36 * Constant used to resolve the runMode via System.getProperty(RunMode.KEY)
37 */
38 public static final String KEY = "griffon.runmode";
39
40 private static final String STANDALONE_RUNMODE_SHORT_NAME = "standalone";
41 private static final String WEBSTART_RUNMODE_SHORT_NAME = "webstart";
42 private static final String APPLET_RUNMODE_SHORT_NAME = "applet";
43
44 private static final Map<String, String> MODE_NAME_MAPPINGS = new LinkedHashMap<>();
45
46 static {
47 MODE_NAME_MAPPINGS.put(STANDALONE_RUNMODE_SHORT_NAME, RunMode.STANDALONE.getName());
48 MODE_NAME_MAPPINGS.put(WEBSTART_RUNMODE_SHORT_NAME, RunMode.WEBSTART.getName());
49 MODE_NAME_MAPPINGS.put(APPLET_RUNMODE_SHORT_NAME, RunMode.APPLET.getName());
50 }
51
52 private String name;
53
54 /**
55 * @return Return true if the running mode has been set as a System property
56 */
57 public static boolean isSystemSet() {
58 return System.getProperty(KEY) != null;
59 }
60
61 /**
62 * Returns the running mode for the given short name
63 *
64 * @param shortName The short name
65 * @return The RunMode or null if not known
66 */
67 @Nullable
68 public static RunMode resolveRunMode(@Nullable String shortName) {
69 final String modeName = MODE_NAME_MAPPINGS.get(shortName);
70 if (modeName != null) {
71 return RunMode.valueOf(modeName.toUpperCase());
72 }
73 return null;
74 }
75
76 private static boolean isBlank(String value) {
77 return value == null || value.trim().length() == 0;
78 }
79
80 /**
81 * @return The name of the running mode
82 */
83 @Nonnull
84 public String getName() {
85 if (this != CUSTOM || isBlank(name)) {
86 return this.toString().toLowerCase(Locale.getDefault());
87 }
88 return name;
89 }
90
91 public void setName(@Nullable String name) {
92 this.name = name;
93 }
94 }
|