Java tutorial
/* * Copyright 2015-2016 the original author or authors. * * 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 org.nanoframework.commons.loader; import java.io.File; import java.io.FileInputStream; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.util.Map; import java.util.Properties; import org.apache.commons.lang3.StringUtils; import org.nanoframework.commons.io.ClassPathResource; import org.nanoframework.commons.io.Resource; import org.nanoframework.commons.support.logging.Logger; import org.nanoframework.commons.support.logging.LoggerFactory; import org.nanoframework.commons.util.Charsets; import org.nanoframework.commons.util.ResourceUtils; import com.google.common.collect.Maps; /** * ??. * @author yanghe * @since 1.0 */ public class PropertiesLoader { /** ?/ */ public static final Map<String, Properties> PROPERTIES = Maps.newHashMap(); /** ?. */ public static final String CONTEXT = "context"; private static Logger LOGGER = LoggerFactory.getLogger(PropertiesLoader.class); /** * ?. * @param path * @return Properties */ public static final Properties load(final String path) { try { InputStream input = null; try { final Resource resource = new ClassPathResource(path); input = resource.getInputStream(); } catch (final IOException e) { // ignore } final Properties properties; if (input != null) { properties = PropertiesLoader.load(input); } else { properties = PropertiesLoader.load(ResourceUtils.getFile(path)); } return properties; } catch (IOException e) { throw new LoaderException(": " + e.getMessage(), e); } } /** * ?. * @param input ? * @return ?Properties * @throws LoaderException Loader * @throws IOException IO */ private static final Properties load(final InputStream input) { if (input == null) { throw new LoaderException("?"); } try { final Properties prop = new Properties(); prop.load(new InputStreamReader(input, Charsets.UTF_8)); return prop; } catch (final IOException e) { throw new LoaderException(": " + e.getMessage()); } } /** * . * @param file * @return ?Properties * @throws LoaderException Loader * @throws IOException IO */ private static final Properties load(final File file) throws LoaderException, IOException { if (file == null) { throw new LoaderException(""); } final Properties prop = new Properties(); prop.load(new InputStreamReader(new FileInputStream(file), Charsets.UTF_8)); return prop; } /** * . * @param contextPath * @param stream context? * @param loadContext ?context * @throws LoaderException * @throws IOException IO */ @Deprecated public static final void load(final String contextPath, final InputStream stream, final boolean loadContext) throws LoaderException, IOException { final Properties prop = load(stream); prop.forEach((key, value) -> System.setProperty((String) key, (String) value)); PROPERTIES.put(contextPath, prop); if (loadContext) { final String context = prop.getProperty(CONTEXT); if (StringUtils.isNotEmpty(context)) { final String[] ctxs = context.split(";"); if (ctxs.length > 0) { for (String ctx : ctxs) { final Properties properties = load(ctx); if (properties != null) { PROPERTIES.put(ctx, properties); } else { LOGGER.error(ctx + ": !"); } } } } } } /** * . * @param contextPath * @param loadContext ?context * @throws LoaderException * @throws IOException IO */ public static final void load(final String contextPath, final boolean loadContext) throws LoaderException, IOException { final Properties prop = load(contextPath); prop.forEach((key, value) -> System.setProperty((String) key, (String) value)); PROPERTIES.put(contextPath, prop); if (loadContext) { final String context = prop.getProperty(CONTEXT); if (StringUtils.isNotEmpty(context)) { final String[] ctxs = context.split(";"); if (ctxs.length > 0) { for (String ctx : ctxs) { if (StringUtils.isNotBlank(ctx)) { final Properties properties = load(ctx); if (properties != null) { PROPERTIES.put(ctx, properties); } else { LOGGER.error(ctx + ": !"); } } } } } } } }