com.blade.Environment Maven / Gradle / Ivy
/**
* Copyright (c) 2017, biezhi 王爵 ([email protected])
*
* 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 com.blade;
import com.blade.kit.IOKit;
import lombok.AccessLevel;
import lombok.NoArgsConstructor;
import lombok.NonNull;
import lombok.extern.slf4j.Slf4j;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.UnsupportedEncodingException;
import java.net.MalformedURLException;
import java.net.URL;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.HashMap;
import java.util.Map;
import java.util.Optional;
import java.util.Properties;
/**
* Blade environment config
*
* @author biezhi
* 2017/6/1
*/
@Slf4j
@NoArgsConstructor(access = AccessLevel.PRIVATE)
public class Environment {
private Properties props = new Properties();
public static Environment empty() {
return new Environment();
}
/**
* Properties to Environment
*
* @param props
* @return
*/
public static Environment of(@NonNull Properties props) {
Environment environment = new Environment();
environment.props = props;
return environment;
}
/**
* Map to Environment
*
* @param map
* @return
*/
public static Environment of(@NonNull Map map) {
Environment environment = new Environment();
map.forEach((key, value) -> environment.props.setProperty(key, value));
return environment;
}
/**
* load Environment by URL
*
* @param url
* @return
*/
public Environment of(@NonNull URL url) {
try {
return of(url.openStream());
} catch (UnsupportedEncodingException e) {
} catch (IOException e) {
throw new IllegalStateException(e);
}
return null;
}
/**
* load Environment by file
*
* @param file
* @return
*/
public static Environment of(@NonNull File file) {
try {
return of(Files.newInputStream(Paths.get(file.getPath())));
} catch (IOException e) {
throw new IllegalStateException(e);
}
}
/**
* load Environment by location
*
* @param location
* @return
*/
public static Environment of(@NonNull String location) {
if (location.startsWith("classpath:")) {
location = location.substring("classpath:".length());
return loadClasspath(location);
} else if (location.startsWith("file:")) {
location = location.substring("file:".length());
return new Environment().of(new File(location));
} else if (location.startsWith("url:")) {
location = location.substring("url:".length());
try {
return new Environment().of(new URL(location));
} catch (MalformedURLException e) {
log.error("", e);
return null;
}
} else {
return new Environment().loadClasspath(location);
}
}
private static Environment loadClasspath(@NonNull String classpath) {
if (classpath.startsWith("/")) {
classpath = classpath.substring(1);
}
InputStream is = getDefault().getResourceAsStream(classpath);
if (null == is) {
return new Environment();
}
return of(is);
}
private static Environment of(@NonNull InputStream is) {
try {
Environment environment = new Environment();
environment.props.load(is);
return environment;
} catch (IOException e) {
throw new IllegalStateException(e);
} finally {
IOKit.closeQuietly(is);
}
}
/**
* Returns current thread's context class loader
*/
public static ClassLoader getDefault() {
ClassLoader loader = null;
try {
loader = Thread.currentThread().getContextClassLoader();
} catch (Exception e) {
}
if (loader == null) {
loader = Environment.class.getClassLoader();
if (loader == null) {
try {
// getClassLoader() returning null indicates the bootstrap ClassLoader
loader = ClassLoader.getSystemClassLoader();
} catch (Exception e) {
// Cannot access system ClassLoader - oh well, maybe the caller can live with null...
}
}
}
return loader;
}
public Environment set(@NonNull String key, @NonNull Object value) {
props.put(key, value);
return this;
}
public Environment add(@NonNull String key, @NonNull Object value) {
props.put(key, value);
return this;
}
public Environment addAll(@NonNull Map map) {
map.forEach((key, value) -> this.props.setProperty(key, value));
return this;
}
public Environment addAll(@NonNull Properties props) {
props.forEach((key, value) -> this.props.setProperty(key.toString(), value.toString()));
return this;
}
public Optional get(@NonNull String key) {
return Optional.ofNullable(props.getProperty(key));
}
public String get(@NonNull String key, String defaultValue) {
return props.getProperty(key, defaultValue);
}
public Optional