Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

This enables us to provide configurations in the form of URLs #6

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion polyguice-akka/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
<parent>
<groupId>com.flipkart.polyguice</groupId>
<artifactId>polyguice-all</artifactId>
<version>1.0.2-SNAPSHOT</version>
<version>1.0.3-SNAPSHOT</version>
</parent>

<artifactId>polyguice-akka</artifactId>
Expand Down
2 changes: 1 addition & 1 deletion polyguice-config/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
<parent>
<groupId>com.flipkart.polyguice</groupId>
<artifactId>polyguice-all</artifactId>
<version>1.0.2-SNAPSHOT</version>
<version>1.0.3-SNAPSHOT</version>
</parent>

<artifactId>polyguice-config</artifactId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,15 @@
package com.flipkart.polyguice.config;

import java.io.File;
import java.lang.reflect.InvocationTargetException;
import java.math.BigDecimal;
import java.math.BigInteger;
import java.net.URL;
import java.util.Locale;
import java.util.Properties;

import org.apache.commons.configuration.CompositeConfiguration;
import org.apache.commons.configuration.Configuration;
import org.apache.commons.configuration.PropertiesConfiguration;
import org.apache.commons.configuration.XMLConfiguration;
import org.slf4j.Logger;
Expand All @@ -45,36 +48,25 @@ public ApacheCommonsConfigProvider() {
rootConfig = new CompositeConfiguration();
}

public ApacheCommonsConfigProvider location(String loc) {
public ApacheCommonsConfigProvider location(String filePath) {
Class<? extends Configuration> clazz = selectConfigImpl(filePath);
try {
if(loc.toLowerCase(Locale.getDefault()).endsWith(".properties")) {
PropertiesConfiguration config = new PropertiesConfiguration(new File(loc));
rootConfig.addConfiguration(config);
LOGGER.debug("properties configuration from {}", loc);
}
else if(loc.toLowerCase(Locale.getDefault()).endsWith(".xml")) {
XMLConfiguration config = new XMLConfiguration(new File(loc));
rootConfig.addConfiguration(config);
LOGGER.debug("xml configuration from {}", loc);
}
else if(loc.toLowerCase(Locale.getDefault()).endsWith(".json")) {
JsonConfiguration config = new JsonConfiguration(new File(loc));
rootConfig.addConfiguration(config);
LOGGER.debug("json configuration from {}", loc);
}
else if(loc.toLowerCase(Locale.getDefault()).endsWith(".yml")) {
YamlConfiguration config = new YamlConfiguration(loc);
rootConfig.addConfiguration(config);
LOGGER.debug("yaml configuration from {}", loc);
}
else if(loc.toLowerCase(Locale.getDefault()).endsWith(".yaml")) {
YamlConfiguration config = new YamlConfiguration(loc);
rootConfig.addConfiguration(config);
LOGGER.debug("yaml configuration from {}", loc);
if (clazz != null) {
this.rootConfig.addConfiguration(clazz.getConstructor(File.class).newInstance(new File(filePath)));
}
} catch (InstantiationException | IllegalAccessException | NoSuchMethodException | InvocationTargetException e) {
throw new RuntimeException(e);
}
catch(Exception exep) {
LOGGER.error("unable to load configuration from " + loc.toString(), exep);
return this;
}
public ApacheCommonsConfigProvider location(URL url) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Instead of having overloaded methods, one taking a URL and the other a string, why not have the original method's implementation extended to accept URL-like capabilities. So instead of taking a decision by looking at the path suffix (e.g. .properties, .yaml, etc.). also look at the prefix to determine load path. For example a path that looks like file:/somepath/filename.yaml will be loaded from the filepath, while a path of the form classpath:/somepath/filename.yaml will be loaded using the classpath. Default behavior, without a prefix will always assume a filepath.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Shouldn't it be the other way round? Take only URLs - the standard way of identifying resources. By identifying the "scheme" part of a given string, we essentially end up re-writing the logic that is contained within URL.

In fact, I kept the String path location to maintain backward compatibility.

Also, if we give something like file:/.jar!/foo.yaml ; the approach you mentioned fails. Although the scheme says "file", it is not a file in the file system but a file contained within a jar. We can possibly work around this by providing classpath:/foo.yaml. However, the former URL is the one that is provided when we do a classLoader.getResource(). Identifying a resource using the classLoader seems more straight forward and less error prone than constructing the string "classpath:/foo.yaml" and then passing it to the config provider.

Class<? extends Configuration> clazz = selectConfigImpl(url.getPath());
try {
if (clazz != null) {
this.rootConfig.addConfiguration(clazz.getConstructor(URL.class).newInstance(url));
}
} catch (InstantiationException | IllegalAccessException | NoSuchMethodException | InvocationTargetException e) {
throw new RuntimeException(e);
}
return this;
}
Expand Down Expand Up @@ -137,4 +129,35 @@ else if(type.equals(TimeInterval.class)) {
}
return null;
}
// PRIVATE METHODS

private Class<? extends Configuration> selectConfigImpl(String loc) {
try {
if(loc.toLowerCase(Locale.getDefault()).endsWith(".properties")) {
LOGGER.debug("properties configuration from {}", loc);
return PropertiesConfiguration.class;
}
else if(loc.toLowerCase(Locale.getDefault()).endsWith(".xml")) {
LOGGER.debug("xml configuration from {}", loc);
return XMLConfiguration.class;
}
else if(loc.toLowerCase(Locale.getDefault()).endsWith(".json")) {
LOGGER.debug("json configuration from {}", loc);
return JsonConfiguration.class;
}
else if(loc.toLowerCase(Locale.getDefault()).endsWith(".yml")) {
LOGGER.debug("yaml configuration from {}", loc);
return YamlConfiguration.class;
}
else if(loc.toLowerCase(Locale.getDefault()).endsWith(".yaml")) {
LOGGER.debug("yaml configuration from {}", loc);
return YamlConfiguration.class;
}
}
catch(Exception exep) {
LOGGER.error("unable to load configuration from " + loc.toString(), exep);
}
return null; // TODO - should ideally throw an exception, but going with the semantics of failing queitly for now
}

}
2 changes: 1 addition & 1 deletion polyguice-core/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
<parent>
<groupId>com.flipkart.polyguice</groupId>
<artifactId>polyguice-all</artifactId>
<version>1.0.2-SNAPSHOT</version>
<version>1.0.3-SNAPSHOT</version>
</parent>

<artifactId>polyguice-core</artifactId>
Expand Down
2 changes: 1 addition & 1 deletion polyguice-dropwiz/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
<parent>
<groupId>com.flipkart.polyguice</groupId>
<artifactId>polyguice-all</artifactId>
<version>1.0.2-SNAPSHOT</version>
<version>1.0.3-SNAPSHOT</version>
</parent>

<artifactId>polyguice-dropwiz</artifactId>
Expand Down
2 changes: 1 addition & 1 deletion polyguice-web/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
<parent>
<groupId>com.flipkart.polyguice</groupId>
<artifactId>polyguice-all</artifactId>
<version>1.0.2-SNAPSHOT</version>
<version>1.0.3-SNAPSHOT</version>
</parent>

<artifactId>polyguice-web</artifactId>
Expand Down
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
<artifactId>polyguice-all</artifactId>
<name>Polyguice</name>
<packaging>pom</packaging>
<version>1.0.2-SNAPSHOT</version>
<version>1.0.3-SNAPSHOT</version>

<scm>
<url>https://github.com/flipkart-incubator/polyguice.git</url>
Expand Down
2 changes: 1 addition & 1 deletion release.sh
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@
set -e
set -x
mvn clean install
mvn deploy -DskipTests -DaltDeploymentRepository=clojars::default::https://clojars.org/repo
mvn deploy -DperformRelease=true -DskipTests -DaltDeploymentRepository=clojars::default::https://clojars.org/repo