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