• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2013 Google Inc.
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 
17 package com.google.inject.internal;
18 
19 
20 import java.security.AccessController;
21 import java.security.PrivilegedAction;
22 import java.util.Arrays;
23 import java.util.logging.Logger;
24 /**
25  * Contains flags for Guice.
26  */
27 public class InternalFlags {
28   private static final Logger logger = Logger.getLogger(InternalFlags.class.getName());
29 
30   private static final IncludeStackTraceOption INCLUDE_STACK_TRACES
31       = parseIncludeStackTraceOption();
32 
33   private static final CustomClassLoadingOption CUSTOM_CLASS_LOADING
34       = parseCustomClassLoadingOption();
35 
36   private static final NullableProvidesOption NULLABLE_PROVIDES
37       = parseNullableProvidesOption(NullableProvidesOption.ERROR);
38 
39 
40   /**
41    * The options for Guice stack trace collection.
42    */
43   public enum IncludeStackTraceOption {
44     /** No stack trace collection */
45     OFF,
46     /**  Minimum stack trace collection (Default) */
47     ONLY_FOR_DECLARING_SOURCE,
48     /** Full stack trace for everything */
49     COMPLETE
50   }
51 
52   /**
53    * The options for Guice custom class loading.
54    */
55   public enum CustomClassLoadingOption {
56     /** No custom class loading */
57     OFF,
58     /** Automatically bridge between class loaders (Default) */
59     BRIDGE
60   }
61 
62   public enum NullableProvidesOption {
63     /** Ignore null parameters to @Provides methods. */
64     IGNORE,
65     /** Warn if null parameters are passed to non-@Nullable parameters of provides methods. */
66     WARN,
67     /** Error if null parameters are passed to non-@Nullable parameters of provides parameters */
68     ERROR
69   }
70 
getIncludeStackTraceOption()71   public static IncludeStackTraceOption getIncludeStackTraceOption() {
72     return INCLUDE_STACK_TRACES;
73   }
74 
getCustomClassLoadingOption()75   public static CustomClassLoadingOption getCustomClassLoadingOption() {
76     return CUSTOM_CLASS_LOADING;
77   }
78 
getNullableProvidesOption()79   public static NullableProvidesOption getNullableProvidesOption() {
80     return NULLABLE_PROVIDES;
81   }
82 
parseIncludeStackTraceOption()83   private static IncludeStackTraceOption parseIncludeStackTraceOption() {
84     return getSystemOption("guice_include_stack_traces",
85         IncludeStackTraceOption.ONLY_FOR_DECLARING_SOURCE);
86   }
87 
parseCustomClassLoadingOption()88   private static CustomClassLoadingOption parseCustomClassLoadingOption() {
89     return getSystemOption("guice_custom_class_loading",
90         CustomClassLoadingOption.BRIDGE, CustomClassLoadingOption.OFF);
91   }
92 
parseNullableProvidesOption( NullableProvidesOption defaultValue)93   private static NullableProvidesOption parseNullableProvidesOption(
94       NullableProvidesOption defaultValue) {
95     return getSystemOption("guice_check_nullable_provides_params", defaultValue);
96   }
97 
98   /**
99    * Gets the system option indicated by the specified key; runs as a privileged action.
100    *
101    * @param name of the system option
102    * @param defaultValue if the option is not set
103    *
104    * @return value of the option, defaultValue if not set
105    */
getSystemOption(final String name, T defaultValue)106   private static <T extends Enum<T>> T getSystemOption(final String name, T defaultValue) {
107     return getSystemOption(name, defaultValue, defaultValue);
108   }
109 
110   /**
111    * Gets the system option indicated by the specified key; runs as a privileged action.
112    *
113    * @param name of the system option
114    * @param defaultValue if the option is not set
115    * @param secureValue if the security manager disallows access to the option
116    *
117    * @return value of the option, defaultValue if not set, secureValue if no access
118    */
getSystemOption(final String name, T defaultValue, T secureValue)119   private static <T extends Enum<T>> T getSystemOption(final String name, T defaultValue,
120       T secureValue) {
121     Class<T> enumType = defaultValue.getDeclaringClass();
122     String value = null;
123     try {
124       value =
125           AccessController.doPrivileged(
126               new PrivilegedAction<String>() {
127                 @Override
128                 public String run() {
129                   return System.getProperty(name);
130                 }
131               });
132       return (value != null && value.length() > 0) ? Enum.valueOf(enumType, value) : defaultValue;
133     } catch (SecurityException e) {
134       return secureValue;
135     } catch (IllegalArgumentException e) {
136       logger.warning(value + " is not a valid flag value for " + name + ". "
137           + " Values must be one of " + Arrays.asList(enumType.getEnumConstants()));
138       return defaultValue;
139     }
140   }
141 }
142