Skip to content
Permalink
Browse files
ensure to skip [config] if not available
  • Loading branch information
rmannibucau committed Jan 2, 2018
1 parent 7bff61c commit 72ab688fb50401ac3911ec8c718f3c1861fd854c
Showing 4 changed files with 150 additions and 6 deletions.
@@ -0,0 +1,71 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
package org.apache.safeguard.impl.config;

import java.time.temporal.ChronoUnit;

import javax.annotation.Priority;

import org.apache.safeguard.api.config.ConfigFacade;

@Priority(1)
public class ConfigFacadeFacade extends ConfigFacade {
private final ConfigFacade delegate = loadDelegate();

private ConfigFacade loadDelegate() {
final ClassLoader loader = Thread.currentThread().getContextClassLoader();
try {
final Class<?> loadClass = loader.loadClass("org.eclipse.microprofile.config.ConfigProvider");
loadClass.getMethod("getConfig").invoke(null);
return new MicroProfileConfigFacade();
} catch (final Throwable notHere) {
return new DefaultConfigFacade();
}
}

@Override
public boolean getBoolean(final String name, final boolean defaultValue) {
return delegate.getBoolean(name, defaultValue);
}

@Override
public long getLong(final String name, final long defaultValue) {
return delegate.getLong(name, defaultValue);
}

@Override
public int getInt(final String name, final int defaultValue) {
return delegate.getInt(name, defaultValue);
}

@Override
public double getDouble(final String name, final double defaultValue) {
return delegate.getDouble(name, defaultValue);
}

@Override
public ChronoUnit getChronoUnit(final String name, final ChronoUnit defaultValue) {
return delegate.getChronoUnit(name, defaultValue);
}

@Override
public Class<? extends Throwable>[] getThrowableClasses(final String name, final Class<? extends Throwable>[] defaultValue) {
return delegate.getThrowableClasses(name, defaultValue);
}
}
@@ -0,0 +1,75 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/

package org.apache.safeguard.impl.config;

import java.time.temporal.ChronoUnit;
import java.util.Optional;
import java.util.stream.Stream;

import javax.annotation.Priority;

import org.apache.safeguard.api.config.ConfigFacade;
import org.eclipse.microprofile.config.Config;
import org.eclipse.microprofile.config.ConfigProvider;

class DefaultConfigFacade extends ConfigFacade {
@Override
public boolean getBoolean(String name, boolean defaultValue) {
return getOptionalValue(name).map(Boolean::parseBoolean).orElse(defaultValue);
}

@Override
public long getLong(String name, long defaultValue) {
return getOptionalValue(name).map(Long::parseLong).orElse(defaultValue);
}

@Override
public int getInt(String name, int defaultValue) {
return getOptionalValue(name).map(Integer::parseInt).orElse(defaultValue);
}

@Override
public double getDouble(String name, double defaultValue) {
return getOptionalValue(name).map(Double::parseDouble).orElse(defaultValue);
}

@Override
public ChronoUnit getChronoUnit(String name, ChronoUnit defaultValue) {
return getOptionalValue(name).map(ChronoUnit::valueOf).orElse(defaultValue);
}

@Override
public Class<? extends Throwable>[] getThrowableClasses(String name, Class<? extends Throwable>[] defaultValue) {
return getOptionalValue(name).map(value -> {
final ClassLoader loader = Thread.currentThread().getContextClassLoader();
return Stream.of(name.split(",")).map(clazz -> {
try {
return loader.loadClass(clazz.trim());
} catch (final ClassNotFoundException e) {
throw new IllegalArgumentException(e);
}
}).toArray(Class[]::new);
}).orElse(defaultValue);
}

private Optional<String> getOptionalValue(final String name) {
return Optional.ofNullable(Optional.ofNullable(System.getenv(name)).orElseGet(() -> System.getProperty(name)));
}
}
@@ -19,15 +19,13 @@

package org.apache.safeguard.impl.config;

import java.time.temporal.ChronoUnit;

import org.apache.safeguard.api.config.ConfigFacade;
import org.eclipse.microprofile.config.Config;
import org.eclipse.microprofile.config.ConfigProvider;

import javax.annotation.Priority;
import java.time.temporal.ChronoUnit;

@Priority(1)
public class MicroProfileConfigFacade extends ConfigFacade {
class MicroProfileConfigFacade extends ConfigFacade {
private final Config config;

public MicroProfileConfigFacade() {
@@ -17,4 +17,4 @@
# under the License.
#

org.apache.safeguard.impl.config.MicroProfileConfigFacade
org.apache.safeguard.impl.config.ConfigFacadeFacade

0 comments on commit 72ab688

Please sign in to comment.