RunMode.java
001 /*
002  * Copyright 2010-2012 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 
017 package griffon.util;
018 
019 import java.util.HashMap;
020 import java.util.Locale;
021 
022 
023 /**
024  * An enum that represents the current running mode.
025  *
026  @author Andres Almiray
027  @since 0.3.1
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 HashMap<String, String> modeNameMappings = new HashMap<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     /**
051      * Returns the current RunMode which is typically either STANDALONE, WEBSTART or APPLET.
052      * For custom running modes CUSTOM type is returned.
053      *
054      @return The current runMode.
055      */
056     public static RunMode getCurrent() {
057         String modeName = System.getProperty(RunMode.KEY);
058 
059         if (isBlank(modeName)) {
060             return STANDALONE;
061         else {
062             RunMode mode = getRunMode(modeName);
063             if (mode == null) {
064                 try {
065                     mode = RunMode.valueOf(modeName.toUpperCase());
066                 catch (IllegalArgumentException e) {
067                     // ignore
068                 }
069             }
070             if (mode == null) {
071                 mode = RunMode.CUSTOM;
072                 mode.setName(modeName);
073             }
074             return mode;
075         }
076     }
077 
078     /**
079      @see #getCurrent()
080      */
081     public static RunMode getCurrentRunMode() {
082         return getCurrent();
083     }
084 
085     /**
086      @return Return true if the running mode has been set as a System property
087      */
088     public static boolean isSystemSet() {
089         return System.getProperty(KEY!= null;
090     }
091 
092     /**
093      * Returns the running mode for the given short name
094      *
095      @param shortName The short name
096      @return The RunMode or null if not known
097      */
098     public static RunMode getRunMode(String shortName) {
099         final String modeName = modeNameMappings.get(shortName);
100         if (modeName != null) {
101             return RunMode.valueOf(modeName.toUpperCase());
102         }
103         return null;
104     }
105 
106     private static boolean isBlank(String value) {
107         return value == null || value.trim().length() == 0;
108     }
109 
110     private String name;
111 
112     /**
113      @return The name of the running mode
114      */
115     public String getName() {
116         if (name == null) {
117             return this.toString().toLowerCase(Locale.getDefault());
118         }
119         return name;
120     }
121 
122     public void setName(String name) {
123         this.name = name;
124     }
125 }