Java tutorial
/** * personium.io * Copyright 2017 FUJITSU LIMITED * * Licensed 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 io.personium.plugin.base; import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.IOException; import java.io.InputStream; import java.util.Map; import java.util.Properties; import org.apache.commons.lang.StringUtils; import org.slf4j.Logger; import org.slf4j.LoggerFactory; /** * ???. ????? personium-unit-config.properties??????? */ public class PluginConfig { /** * personium-unit-config.properties?. */ static final String KEY_CONFIG_FILE = "io.personium.configurationFile"; /** * ???. */ static final String KEY_ROOT = "io.personium.core."; /** * Core version?. */ public static final String CORE_VERSION = KEY_ROOT + "version"; /** * ?. */ public static final String MASTER_TOKEN = KEY_ROOT + "masterToken"; /** * ???????. */ public static final String UNIT_USER_ISSUERS = KEY_ROOT + "unitUser.issuers"; /** * ?. */ public static final String UNIT_SCHEME = KEY_ROOT + "unitScheme"; /** * ?. */ public static final String PLUGIN_PATH = KEY_ROOT + "plugin.path"; /** * Proxy?. */ public static final class Proxy { /** * PROXY ??. */ public static final String HOST_NAME = KEY_ROOT + "proxy.host"; /** * PROXY ??. */ public static final String PORT_NUMBER = KEY_ROOT + "proxy.port"; /** * PROXY ??. */ public static final String USER_NAME = KEY_ROOT + "proxy.user"; /** * PROXY . */ public static final String USER_PSWD = KEY_ROOT + "proxy.pswd"; } /** * OpenID Connect?. */ public static final class OIDC { /** * OpenID Connect?????????ClientID????. */ public static final String OIDC = KEY_ROOT + "oidc."; /** * OpenID Connect TRUSTED_CLIENTIDS. */ public static final String TRUSTED_CLIENTIDS = ".trustedClientIds"; /** * ?ClientID?????????????. * @param provider String * @param clientId ClientID * @return boolean ???True */ public static boolean isProviderClientIdTrusted(String provider, String clientId) { String val = get(OIDC + provider + TRUSTED_CLIENTIDS); //???????true return isAstarisk(val) || isSpaceSeparatedValueIncluded(val, clientId); } } static { // ??? PluginLog.loadConfig(); } /** * singleton. */ private static PluginConfig singleton = new PluginConfig(); /** * ??. */ private final Properties props = new Properties(); /** * ????. */ private final Properties propsOverride = new Properties(); /** * protected?. */ protected PluginConfig() { this.doReload(); } /** * ?. */ private synchronized void doReload() { Logger log = LoggerFactory.getLogger(PluginConfig.class); Properties properties = getUnitConfigDefaultProperties(); Properties propertiesOverride = getPersoniumConfigProperties(); // ???????????? if (!properties.isEmpty()) { this.props.clear(); for (Map.Entry<Object, Object> entry : properties.entrySet()) { if (!(entry.getKey() instanceof String)) { continue; } this.props.setProperty((String) entry.getKey(), (String) entry.getValue()); } } if (!propertiesOverride.isEmpty()) { this.propsOverride.clear(); for (Map.Entry<Object, Object> entry : propertiesOverride.entrySet()) { if (!(entry.getKey() instanceof String)) { continue; } this.propsOverride.setProperty((String) entry.getKey(), (String) entry.getValue()); } } for (Object keyObj : propsOverride.keySet()) { String key = (String) keyObj; String value = this.propsOverride.getProperty(key); if (value == null) { continue; } log.debug("Overriding Config " + key + "=" + value); this.props.setProperty(key, value); } } private static boolean isSpaceSeparatedValueIncluded(String spaceSeparatedValue, String testValue) { if (testValue == null || spaceSeparatedValue == null) { return false; } String[] values = spaceSeparatedValue.split(" "); for (String val : values) { if (testValue.equals(val)) { return true; } } return false; } private static boolean isAstarisk(String val) { if (val == null) { return false; } else if ("*".equals(val)) { return true; } return false; } /** * personium-unit-config-default.properties?. * @return personium-unit-config-default.properties */ protected Properties getUnitConfigDefaultProperties() { Properties properties = new Properties(); InputStream is = PluginConfig.class.getClassLoader() .getResourceAsStream("personium-unit-config-default.properties"); try { properties.load(is); } catch (IOException e) { throw new RuntimeException("failed to load config!", e); } finally { try { is.close(); } catch (IOException e) { throw new RuntimeException("failed to close config stream", e); } } return properties; } /** * personium-unit-config.properties?. * @return personium-unit-config.properties */ protected Properties getPersoniumConfigProperties() { Logger log = LoggerFactory.getLogger(PluginConfig.class); Properties propertiesOverride = new Properties(); String configFilePath = System.getProperty(KEY_CONFIG_FILE); InputStream is = getConfigFileInputStream(configFilePath); try { if (is != null) { propertiesOverride.load(is); } else { log.debug( "[personium-unit-config.properties] file not found on the classpath. using default config."); } } catch (IOException e) { log.debug("IO Exception when loading [personium-unit-config.properties] file."); } finally { try { if (is != null) { is.close(); } } catch (IOException e) { log.debug("IO Exception when closing [personium-unit-config.properties] file."); } } return propertiesOverride; } /** * personium-unit-config.propertiesInputStream????. * @param configFilePath * @return personium-unit-config.properties */ protected InputStream getConfigFileInputStream(String configFilePath) { Logger log = LoggerFactory.getLogger(PluginConfig.class); InputStream configFileInputStream = null; if (configFilePath == null) { configFileInputStream = PluginConfig.class.getClassLoader() .getResourceAsStream("personium-unit-config.properties"); return configFileInputStream; } try { // ???? File configFile = new File(configFilePath); configFileInputStream = new FileInputStream(configFile); log.info("personium-unit-config.properties from system properties."); } catch (FileNotFoundException e) { // ???????????? configFileInputStream = PluginConfig.class.getClassLoader() .getResourceAsStream("personium-unit-config.properties"); log.info("personium-unit-config.properties from class path."); } return configFileInputStream; } /** * ??. * @param key * @return */ private String doGet(final String key) { return props.getProperty(key); } /** * ?. * @param key * @param value */ private void doSet(final String key, final String value) { props.setProperty(key, value); } /** * ???????? * @return */ public static Properties getProperties() { return singleton.props; } /** * Key??????. * @param key * @return */ public static String get(final String key) { return singleton.doGet(key); } /** * Key?????. * @param key * @param value */ public static void set(final String key, final String value) { singleton.doSet(key, value); } /** * Core Version?????. * @return Core Version? */ public static String getCoreVersion() { return get(CORE_VERSION); } /** * ?????. * @return ? */ public static String getMasterToken() { return get(MASTER_TOKEN); } /** * @return ???????. */ public static String getUnitUserIssuers() { return get(UNIT_USER_ISSUERS); } /** * @return ?. */ public static String getUnitScheme() { return get(UNIT_SCHEME); } /** * @return ?. */ public static String getPluginPath() { return get(PLUGIN_PATH); } /** * @return $proxy??. */ public static String getProxyHostName() { return get(Proxy.HOST_NAME); } /** * @return $proxy?. */ public static int getProxyHostNumber() { String port = get(Proxy.PORT_NUMBER); if (StringUtils.isNotEmpty(port)) { return Integer.parseInt(port); } return 0; } /** * @return $proxy??. */ public static String getProxyUserName() { return get(Proxy.USER_NAME); } /** * @return $proxy. */ public static String getProxyPassword() { return get(Proxy.USER_PSWD); } }