Home | History | Annotate | Download | only in internal
      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 
     71   public static IncludeStackTraceOption getIncludeStackTraceOption() {
     72     return INCLUDE_STACK_TRACES;
     73   }
     74 
     75   public static CustomClassLoadingOption getCustomClassLoadingOption() {
     76     return CUSTOM_CLASS_LOADING;
     77   }
     78 
     79   public static NullableProvidesOption getNullableProvidesOption() {
     80     return NULLABLE_PROVIDES;
     81   }
     82 
     83   private static IncludeStackTraceOption parseIncludeStackTraceOption() {
     84     return getSystemOption("guice_include_stack_traces",
     85         IncludeStackTraceOption.ONLY_FOR_DECLARING_SOURCE);
     86   }
     87 
     88   private static CustomClassLoadingOption parseCustomClassLoadingOption() {
     89     return getSystemOption("guice_custom_class_loading",
     90         CustomClassLoadingOption.BRIDGE, CustomClassLoadingOption.OFF);
     91   }
     92 
     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    */
    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    */
    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 = AccessController.doPrivileged(new PrivilegedAction<String>() {
    125         public String run() {
    126           return System.getProperty(name);
    127         }
    128       });
    129       return (value != null && value.length() > 0) ? Enum.valueOf(enumType, value) : defaultValue;
    130     } catch (SecurityException e) {
    131       return secureValue;
    132     } catch (IllegalArgumentException e) {
    133       logger.warning(value + " is not a valid flag value for " + name + ". "
    134           + " Values must be one of " + Arrays.asList(enumType.getEnumConstants()));
    135       return defaultValue;
    136     }
    137   }
    138 }
    139