/** * Private constructor. * * @param api the API for the builder */ private GoogleClientSecretsForApiBuilder(Api api) { this(new ConfigurationHelper(), api); }
@Override public GoogleClientSecretsForApiBuilder fromFile(String path) throws ConfigurationLoadException { return from(configHelper.fromFile(path), path); }
@Override public Configuration get() { try { return configurationHelper.createCombinedConfiguration(propertyFilePaths, propertyUrls); } catch (ConfigurationLoadException e) { throw new IllegalArgumentException("Configuration could be loaded.", e); } } }
@Nullable List<ConfigurationInfo<URL>> urls) throws ConfigurationLoadException { CombinedConfiguration combinedConfiguration = setupConfiguration(new CombinedConfiguration(new OverrideCombiner())); addConfiguration(combinedConfiguration, fromSystem()); if (path != null && path.getLocation() != null) { try { addConfiguration(combinedConfiguration, fromFile(path.getLocation())); } catch (ConfigurationLoadException e) { if (!path.isOptional) { if (url != null && url.getLocation() != null) { try { addConfiguration(combinedConfiguration, fromFile(url.getLocation())); } catch (ConfigurationLoadException e) { if (!url.isOptional) {
/** * Creates a list of configuration infos from the single location and if they are * optional. * * @param <T> the type of location. Only {@code String} and {@code URL} are * supported. */ public static <T> List<ConfigurationInfo<T>> newList(boolean isOptional, T location) { if (location == null) { throw new IllegalArgumentException("location cannot be null"); } return newList(Collections.<T>singletonList(location), isOptional); }
/** * Loads configuration from system defined arguments, i.e. -Dapi.x.y.z=abc. */ public Configuration fromSystem() { MapConfiguration mapConfig = setupConfiguration(new MapConfiguration((Properties) System.getProperties().clone())); // Disables trimming so system properties that include whitespace (such as line.separator) will // be preserved. mapConfig.setTrimmingDisabled(true); return mapConfig; }
@Test public void testFromSystem() throws Exception { test3Properties.forEach(System::setProperty); assertContains(test3Properties, configurationHelper.fromSystem()); }
@Nullable List<ConfigurationInfo<URL>> urls) throws ConfigurationLoadException { CombinedConfiguration combinedConfiguration = setupConfiguration(new CombinedConfiguration(new OverrideCombiner())); addConfiguration(combinedConfiguration, fromSystem()); if (path != null && path.getLocation() != null) { try { addConfiguration(combinedConfiguration, fromFile(path.getLocation())); } catch (ConfigurationLoadException e) { if (!path.isOptional) { if (url != null && url.getLocation() != null) { try { addConfiguration(combinedConfiguration, fromFile(url.getLocation())); } catch (ConfigurationLoadException e) { if (!url.isOptional) {
/** * Creates a list of configuration infos from the single location and if they are * optional. * * @param <T> the type of location. Only {@code String} and {@code URL} are * supported. */ public static <T> List<ConfigurationInfo<T>> newList(boolean isOptional, T location) { if (location == null) { throw new IllegalArgumentException("location cannot be null"); } return newList(Collections.<T>singletonList(location), isOptional); }
/** * Loads configuration from system defined arguments, i.e. -Dapi.x.y.z=abc. */ public Configuration fromSystem() { MapConfiguration mapConfig = setupConfiguration(new MapConfiguration((Properties) System.getProperties().clone())); // Disables trimming so system properties that include whitespace (such as line.separator) will // be preserved. mapConfig.setTrimmingDisabled(true); return mapConfig; }
@Test public void testFromSystem_containsListValues() throws Exception { AbstractConfiguration.setDefaultListDelimiter('|'); Map<String, String> properties = Maps.newHashMap(); properties.put("testProperty", "b,bee"); properties.forEach(System::setProperty); Splitter splitter = Splitter.on(','); Configuration systemConfiguration = configurationHelper.fromSystem(); for (Entry<String, String> entry : properties.entrySet()) { String[] actualValues = systemConfiguration.getStringArray(entry.getKey()); String[] expectedValues; if ("line.separator".equals(entry.getKey())) { expectedValues = new String[] {SystemUtils.LINE_SEPARATOR}; } else { expectedValues = splitter.splitToList(entry.getValue()).toArray(new String[0]); } assertArrayEquals(String.format("Values for key %s do not match", entry.getKey()), expectedValues, actualValues); } }
@Override public GoogleClientSecretsForApiBuilder fromFile(String path) throws ConfigurationLoadException { return from(configHelper.fromFile(path), path); }
/** * Constructs an empty builder. To construct a builder initialized to the settings of an * existing {@link AdManagerSession}, use {@link AdManagerSession#newBuilder()} instead. */ public Builder() { this.configHelper = new ConfigurationHelper(); }
@Override public Configuration get() { try { return configurationHelper.createCombinedConfiguration(propertyFilePaths, propertyUrls); } catch (ConfigurationLoadException e) { throw new IllegalArgumentException("Configuration could be loaded.", e); } } }
/** * Gets a combined configuration list with the default added last so that * {@code filePaths} will override {@code defaultFilePath}. */ private List<ConfigurationHelper.ConfigurationInfo<String>> getFilePathConfigurationListWithDefault(@Nullable List<String> filePaths, String defaultFilePath) { List<ConfigurationHelper.ConfigurationInfo<String>> defaultConfigList = ConfigurationHelper.newList(true, defaultFilePath); List<ConfigurationHelper.ConfigurationInfo<String>> filePathConfigList = ConfigurationHelper.newList(filePaths, true); if (defaultConfigList != null) { if (filePathConfigList == null) { filePathConfigList = defaultConfigList; } else { filePathConfigList.addAll(defaultConfigList); } } return filePathConfigList; } }
/** * Loads configuration from a specified path. If not absolute, will look in * the user home directory, the current classpath and the system classpath. * Absolute classpath references will not work. * * @param path the path to try first as a resource, then as a file * @throws ConfigurationLoadException if the configuration could not be * loaded. * @returns properties loaded from the specified path or null. */ public Configuration fromFile(String path) throws ConfigurationLoadException { PropertiesConfiguration propertiesConfiguration = setupConfiguration(new PropertiesConfiguration()); propertiesConfiguration.setFileName(path); try { propertiesConfiguration.load(); } catch (ConfigurationException e) { if (Throwables.getRootCause(e) instanceof AccessControlException){ AdsServiceLoggers.ADS_API_LIB_LOG.debug("Properties could not be loaded.", e); } else { throw new ConfigurationLoadException( "Encountered a problem reading the provided configuration file \"" + path + "\"!", e); } } return propertiesConfiguration; }
@Override public Builder fromFile(URL path) throws ConfigurationLoadException { return from(configHelper.fromFile(path)); }
/** * Constructs an empty builder. To construct a builder initialized to the settings of * an existing {@link AdWordsSession}, use {@link AdWordsSession#newBuilder()} instead. */ public Builder() { this.configHelper = new ConfigurationHelper(); }
@Test @SuppressWarnings("unchecked") public void testCreateCombinedConfiguration_requiredUrlNotFound() throws Exception { test1Properties.forEach(System::setProperty); System.setProperty("testProperty", "testValue"); thrown.expect(ConfigurationLoadException.class); configurationHelper.createCombinedConfiguration(null, Lists.<ConfigurationInfo<URL>>newArrayList(new ConfigurationInfo<URL>( new URL("file:///does/not/exist" + System.currentTimeMillis()), false))); }
/** * Gets a combined configuration list with the default added last so that * {@code filePaths} will override {@code defaultFilePath}. */ private List<ConfigurationHelper.ConfigurationInfo<String>> getFilePathConfigurationListWithDefault(@Nullable List<String> filePaths, String defaultFilePath) { List<ConfigurationHelper.ConfigurationInfo<String>> defaultConfigList = ConfigurationHelper.newList(true, defaultFilePath); List<ConfigurationHelper.ConfigurationInfo<String>> filePathConfigList = ConfigurationHelper.newList(filePaths, true); if (defaultConfigList != null) { if (filePathConfigList == null) { filePathConfigList = defaultConfigList; } else { filePathConfigList.addAll(defaultConfigList); } } return filePathConfigList; } }