Skip to content

Commit

Permalink
release version 1.0.7: update class loader
Browse files Browse the repository at this point in the history
  • Loading branch information
tvd12 committed Feb 27, 2019
1 parent 2d6c003 commit 0a7726f
Show file tree
Hide file tree
Showing 10 changed files with 113 additions and 381 deletions.
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@

<groupId>com.tvd12</groupId>
<artifactId>properties-file</artifactId>
<version>1.0.6-SNAPSHOT</version>
<version>1.0.7-SNAPSHOT</version>
<packaging>jar</packaging>

<name>properties-file</name>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,6 @@ public class PropertiesMapper {
//mapped class to hold data
private Class<?> clazz;

//class to get resource as stream
private Class<?> context;

//properties object to map
private Properties properties;

Expand All @@ -35,6 +32,9 @@ public class PropertiesMapper {
//properties file reader
private FileReader reader;

//class to get resource as stream
private ClassLoader classLoader;

/**
* set mapped class
*
Expand All @@ -54,7 +54,7 @@ public PropertiesMapper clazz(Class<?> clazz) {
* @return this pointer
*/
public PropertiesMapper context(Class<?> context) {
this.context = context;
this.classLoader = context.getClassLoader();
return this;
}

Expand Down Expand Up @@ -169,11 +169,9 @@ private Object newBeanInstance() {
* @return properties object
*/
private Properties getProperties() {
if(context == null)
context = clazz;
try {
if(properties == null)
properties = reader.read(context, propertiesFile);
properties = reader.read(classLoader, propertiesFile);
} catch (PropertiesFileException e) {
throw new IllegalStateException(e);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,12 +29,17 @@ public abstract class AbstractClassFetcher {
* @param builder builder
*/
protected AbstractClassFetcher(AbstractBuilder builder) {
this.reader = builder.reader;
this.init(builder);
this.load();
}

/**
* Initialize properties
*
* @param builder the builder
*/
protected void init(AbstractBuilder builder) {
this.reader = builder.reader;
}

/**
Expand Down
56 changes: 43 additions & 13 deletions src/main/java/com/tvd12/properties/file/reader/BaseFileReader.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,25 +27,39 @@ public class BaseFileReader implements FileReader {
@Override
public List<Properties> read(Class<?> context, Collection<String> propertiesFiles)
throws PropertiesFileException {
return read(context, propertiesFiles.toArray(new String[propertiesFiles.size()]));
return read(context.getClassLoader(), propertiesFiles);
}

/*
* (non-Javadoc)
* @see com.tvd12.properties.file.reader.FileReader#read(java.lang.ClassLoader, java.util.Collection)
*/
@Override
public List<Properties> read(ClassLoader classLoader, Collection<String> propertiesFiles)
throws PropertiesFileException {
return read(classLoader, propertiesFiles.toArray(new String[propertiesFiles.size()]));
}

/*
* @see com.tvd12.properties.file.reader.FileReader#read(java.lang.Class, java.lang.String)
*/
@Override
public Properties read(Class<?> context, String propertiesFile)
throws PropertiesFileException {
InputStream inputStream = getResourceAsStream(context, propertiesFile);
return read(context.getClassLoader(), propertiesFile);
}

@Override
public Properties read(ClassLoader classLoader, String propertiesFile) throws PropertiesFileException {
InputStream inputStream = getResourceAsStream(classLoader, propertiesFile);
if(inputStream == null)
inputStream = getResourceAsStream(context, "/" + propertiesFile);
inputStream = getResourceAsStream(classLoader, "/" + propertiesFile);
if(inputStream == null)
inputStream = getInputStreamByAbsolutePath(propertiesFile);
if(inputStream == null)
throw new PropertiesFileException("Can not read properties file in path "
+ propertiesFile);
return loadInputStream(inputStream);

}

/*
Expand All @@ -54,9 +68,14 @@ public Properties read(Class<?> context, String propertiesFile)
@Override
public List<Properties> read(Class<?> context, String... propertiesFiles)
throws PropertiesFileException {
List<Properties> result = new ArrayList<>();
return read(context.getClassLoader(), propertiesFiles);
}

@Override
public List<Properties> read(ClassLoader classLoader, String... propertiesFiles) throws PropertiesFileException {
List<Properties> result = new ArrayList<>();
for(String file : propertiesFiles)
result.add(read(context, file));
result.add(read(classLoader, file));
return result;
}

Expand All @@ -65,15 +84,15 @@ public List<Properties> read(Class<?> context, String... propertiesFiles)
*/
@Override
public Properties read(String propertiesFile) throws PropertiesFileException {
return read(getClass(), propertiesFile);
return read(getDefaultClassLoader(), propertiesFile);
}

/*
* @see com.tvd12.properties.file.reader.FileReader#read(java.lang.String)
*/
@Override
public List<Properties> read(String... propertiesFiles) throws PropertiesFileException {
return read(getClass(), propertiesFiles);
return read(getDefaultClassLoader(), propertiesFiles);
}

/* (non-Javadoc)
Expand Down Expand Up @@ -180,16 +199,27 @@ private InputStream getInputStreamByAbsolutePath(File file) {
/**
* Get a input stream from a file in project
*
* @param classLoader the class loader
* @param propertiesFile properties file in class path
* @return an inputstream object
*/
private InputStream getResourceAsStream(Class<?> context,
private InputStream getResourceAsStream(ClassLoader classLoader,
String propertiesFile) {
InputStream ip = context
.getClassLoader()
.getResourceAsStream(propertiesFile);
ClassLoader acceptClassLoader = classLoader;
if(acceptClassLoader == null)
acceptClassLoader = getDefaultClassLoader();
InputStream ip = acceptClassLoader.getResourceAsStream(propertiesFile);
if(ip == null)
ip = context.getResourceAsStream(propertiesFile);
ip = ClassLoader.getSystemResourceAsStream(propertiesFile);
return ip;
}

/**
* Get default class loader
*
* @return the current thread's class loader
*/
protected ClassLoader getDefaultClassLoader() {
return Thread.currentThread().getContextClassLoader();
}
}
21 changes: 16 additions & 5 deletions src/main/java/com/tvd12/properties/file/reader/ClassFetcher.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
public class ClassFetcher extends AbstractClassFetcher {

//which class to get resource as stream
protected Class<?> context;
protected ClassLoader classLoader;

//array of properties file path to read
protected List<String> files;
Expand All @@ -33,7 +33,7 @@ protected ClassFetcher(AbstractBuilder builder) {
protected void init(AbstractBuilder builder) {
super.init(builder);
Builder bd = (Builder)builder;
this.context = bd.context;
this.classLoader = bd.classLoader;
this.files = bd.files;
}

Expand All @@ -42,7 +42,7 @@ protected void init(AbstractBuilder builder) {
*/
@Override
protected List<Properties> loadPropertiesList() {
return reader.read(context, files);
return reader.read(classLoader, files);
}

/**
Expand All @@ -54,7 +54,7 @@ protected List<Properties> loadPropertiesList() {
public static class Builder extends AbstractBuilder {

// class to get resource as stream
private Class<?> context;
private ClassLoader classLoader;

// list of properties file paths to read
private List<String> files = new ArrayList<>();
Expand All @@ -66,7 +66,18 @@ public static class Builder extends AbstractBuilder {
* @return this pointer
*/
public Builder context(Class<?> context) {
this.context = context;
this.classLoader = context.getClassLoader();
return this;
}

/**
* set class loader
*
* @param classLoader loader class loader to get resource as stream
* @return this pointer
*/
public Builder classLoader(ClassLoader classLoader) {
this.classLoader = classLoader;
return this;
}

Expand Down
30 changes: 30 additions & 0 deletions src/main/java/com/tvd12/properties/file/reader/FileReader.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,16 @@ public interface FileReader {
*/
Properties read(Class<?> context, String propertiesFile) throws PropertiesFileException;

/**
* Read properties file in a path
*
* @param propertiesFile properties file path
* @param classLoader the class loader
* @return properties object
* @throws PropertiesFileException when properties file not exists or can't read properties
*/
Properties read(ClassLoader classLoader, String propertiesFile) throws PropertiesFileException;

/**
* Read properties files in multiple paths
*
Expand All @@ -30,6 +40,16 @@ public interface FileReader {
*/
List<Properties> read(Class<?> context, String... propertiesFiles) throws PropertiesFileException;

/**
* Read properties files in multiple paths
*
* @param propertiesFiles list of properties files
* @param classLoader the class loader
* @return list of properties object
* @throws PropertiesFileException when properties file not exists or can't read properties
*/
List<Properties> read(ClassLoader classLoader, String... propertiesFiles) throws PropertiesFileException;

/**
* Read properties files in multiple paths
*
Expand All @@ -40,6 +60,16 @@ public interface FileReader {
*/
List<Properties> read(Class<?> context, Collection<String> propertiesFiles) throws PropertiesFileException;

/**
* Read properties files in multiple paths
*
* @param propertiesFiles list of properties files
* @param classLoader the class loader
* @return list of properties object
* @throws PropertiesFileException when properties file not exists or can't read properties
*/
List<Properties> read(ClassLoader classLoader, Collection<String> propertiesFiles) throws PropertiesFileException;

/**
* Read properties file in a path
*
Expand Down
Loading

0 comments on commit 0a7726f

Please sign in to comment.