You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

RuntimeMode.java 2.3KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. /*
  2. * Copyright (C) 2012-present the original author or authors.
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  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 org.pf4j;
  17. import java.util.HashMap;
  18. import java.util.Map;
  19. import java.util.NoSuchElementException;
  20. /**
  21. * The runtime mode of the PF4J application.
  22. * <p>
  23. * The runtime mode is used to determine the behavior of the application.
  24. * For example, in development mode, the application may display detailed error messages,
  25. * while in deployment mode, the application may display a generic error message.
  26. *
  27. * @author Decebal Suiu
  28. */
  29. public enum RuntimeMode {
  30. DEVELOPMENT("development", "dev"), // development
  31. DEPLOYMENT("deployment", "prod"); // deployment
  32. private final String name;
  33. private final String[] aliases;
  34. private static final Map<String, RuntimeMode> map = new HashMap<>();
  35. static {
  36. for (RuntimeMode mode : RuntimeMode.values()) {
  37. map.put(mode.name, mode);
  38. for (String alias : mode.aliases) {
  39. map.put(alias, mode);
  40. }
  41. }
  42. }
  43. RuntimeMode(final String name, final String... aliases) {
  44. this.name = name;
  45. this.aliases = aliases;
  46. }
  47. @Override
  48. public String toString() {
  49. return name;
  50. }
  51. /**
  52. * Returns the runtime mode with the specified name.
  53. *
  54. * @param name the name of the runtime mode
  55. * @return the runtime mode with the specified name
  56. * @throws NoSuchElementException if the runtime mode with the specified name is not found
  57. */
  58. public static RuntimeMode byName(String name) {
  59. if (map.containsKey(name)) {
  60. return map.get(name);
  61. }
  62. throw new NoSuchElementException("Cannot found PF4J runtime mode with name '" + name + "'." +
  63. "Must be one value from '" + map.keySet() + ".");
  64. }
  65. }