com.jn.langx.configuration.resource.ResourceConfigurationLoader Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of langx-java Show documentation
Show all versions of langx-java Show documentation
Java lang extensions for java6+, a supplement to , replacement of a Guava, commons-lang.
Core utilities, Collection utilities, IO utilities, Cache, Configuration library ...
package com.jn.langx.configuration.resource;
import com.jn.langx.annotation.NonNull;
import com.jn.langx.configuration.Configuration;
import com.jn.langx.configuration.ConfigurationLoader;
import com.jn.langx.configuration.InputStreamConfigurationParser;
import com.jn.langx.io.resource.Location;
import com.jn.langx.io.resource.Resource;
import com.jn.langx.io.resource.ResourceLocationProvider;
import com.jn.langx.io.resource.Resources;
import com.jn.langx.util.Preconditions;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.io.IOException;
import java.io.InputStream;
public class ResourceConfigurationLoader implements ConfigurationLoader {
private static final Logger logger = LoggerFactory.getLogger(ResourceConfigurationLoader.class);
private InputStreamConfigurationParser parser;
private ResourceLocationProvider resourceLocationProvider;
public InputStreamConfigurationParser getParser() {
return parser;
}
public void setParser(InputStreamConfigurationParser parser) {
this.parser = parser;
}
public ResourceLocationProvider getResourceLocationProvider() {
return resourceLocationProvider;
}
public void setResourceLocationProvider(ResourceLocationProvider resourceLocationProvider) {
this.resourceLocationProvider = resourceLocationProvider;
}
@Override
public T load(@NonNull String configurationId) {
Preconditions.checkNotEmpty(configurationId, "the configuration id is null or empty");
Location location = resourceLocationProvider.get(configurationId);
Preconditions.checkNotNull(location, "Can't find the location for configuration : {}", configurationId);
T configuration = null;
Resource resource = Resources.loadResource(location);
if (resource != null && resource.exists()) {
InputStream inputStream = null;
try {
inputStream = resource.getInputStream();
configuration = parser.parse(inputStream);
if (configuration != null) {
configuration.setId(configurationId);
}
} catch (IOException ex) {
logger.error(ex.getMessage(), ex);
}
} else {
logger.error("Location {} is not exists", location);
}
return null;
}
}