Code, JavaEE

External JSON Configuration

Here is one way using JSON as configuration to specify custom settings for an application.


// ConfigurationLoader Interface

public interface ConfigurationLoader {

public Object loadJsonConfiguration();
}

// HandlerConfigurationLoader class
public class HandlerConfigurationLoader implements ConfigurationLoader {
 private static final String HandlerConfigJsonFile = "c:\\Temp\\mapping.json";

 @Override
 public Object loadJsonConfiguration() {
 HandlerConfiguration handlerConfig = null;
 JsonReader reader = null;

 try {
 reader = new JsonReader(new InputStreamReader(
 new FileInputStream(
 new File(HandlerConfigJsonFile))));
 } catch (FileNotFoundException e) {
 e.printStackTrace();
 }

 Gson gson = new Gson();
 return handlerConfig = gson.fromJson(reader, HandlerConfiguration.class);
 }
}

// HandlerConfiguration class
public class HandlerConfiguration implements Serializable {
 private String handlerClazz;
 private List<MethodMapping> methods;

 public HandlerConfiguration(String handlerClazz, List<MethodMapping> methods) {
 this.handlerClazz = handlerClazz;
 this.methods = methods;
 }

 public String getHandlerClazz() {
 return handlerClazz;
 }

 public void setHandlerClazz(String handlerClazz) {
 this.handlerClazz = handlerClazz;
 }

 public List<MethodMapping> getMethods() {
 return methods;
 }

 public void setMethods(List<MethodMapping> methods) {
 this.methods = methods;
 }

 @Override
 public String toString() {
 return "HandlerConfiguration{" +
 "handlerClazz='" + handlerClazz + '\'' +
 ", methods=" + methods +
 '}';
 }
}

// MethodMapping class
public class MethodMapping implements Serializable {
 private String methodName;
 private String clazzName;

 public MethodMapping() {
 }

 public MethodMapping(String methodName, String clazzName) {
 this.methodName = methodName;
 this.clazzName = clazzName;
 }

 public String getMethodName() {
 return methodName;
 }

 public void setMethodName(String methodName) {
 this.methodName = methodName;
 }

 public String getClazzName() {
 return clazzName;
 }

 public void setClazzName(String clazzName) {
 this.clazzName = clazzName;
 }

 @Override
 public boolean equals(Object o) {
 if (this == o) return true;
 if (o == null || getClass() != o.getClass()) return false;

 MethodMapping that = (MethodMapping) o;

 if (clazzName != null ? !clazzName.equals(that.clazzName) : that.clazzName != null) return false;
 if (methodName != null ? !methodName.equals(that.methodName) : that.methodName != null) return false;

 return true;
 }

 @Override
 public int hashCode() {
 int result = methodName != null ? methodName.hashCode() : 0;
 result = 31 * result + (clazzName != null ? clazzName.hashCode() : 0);
 return result;
 }

 @Override
 public String toString() {
 return "MethodMapping{" +
 "methodName='" + methodName + '\'' +
 ", clazzName='" + clazzName + '\'' +
 '}';
 }
}

// Unit Test client class
public class HandlerConfigurationTest {

 @Test
 public void testLoadHandlerConfiguration() {
 ConfigurationLoader configurationLoader = new HandlerConfigurationLoader();
 HandlerConfiguration handlerConfiguration =
 (HandlerConfiguration) configurationLoader.loadJsonConfiguration();

 System.out.printf("HandlerConfiguration Loaded: %s%n", handlerConfiguration);
 }

}

{
"handlerClazz" : "model.ModelObject",
"methods" : [
{ "methodName" : "process", "clazzName" : "some.package.name.Handler" }
]
}

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s