Skip to content

Commit

Permalink
Removing non-RuntimeException throws from Configuration. Fixes Azure#3
Browse files Browse the repository at this point in the history
ConfigurationException class catches all non-runtime issues, enabling
users to avoid dealing with explicit exception throws statements when
configuration is used
  • Loading branch information
lodejard committed Nov 19, 2011
1 parent d8c76f9 commit 49dc9f0
Show file tree
Hide file tree
Showing 6 changed files with 51 additions and 25 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,14 @@

public interface Builder {

public abstract <T> T build(String profile, Class<T> service, Map<String, Object> properties) throws Exception;
public abstract <T> T build(String profile, Class<T> service, Map<String, Object> properties);

public interface Factory<T> {
T create(String profile, Builder builder, Map<String, Object> properties) throws Exception;
T create(String profile, Builder builder, Map<String, Object> properties);
}

public interface Alteration<T> {
T alter(T instance, Builder builder, Map<String, Object> properties) throws Exception;
T alter(T instance, Builder builder, Map<String, Object> properties);
}

public interface Registry {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,15 +32,7 @@ public Configuration(Builder builder) {
}

private void init() {
// DefaultClientConfig clientConfig = new DefaultClientConfig();
// clientConfig.getFeatures().put(JSONConfiguration.FEATURE_POJO_MAPPING, true);
try {
setProperty("ClientConfig", builder.build("", ClientConfig.class, properties));
}
catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
setProperty("ClientConfig", builder.build("", ClientConfig.class, properties));
}

public static Configuration getInstance() {
Expand All @@ -63,7 +55,8 @@ public static void setInstance(Configuration instance) {
public static Configuration load() throws IOException {
Configuration config = new Configuration();

InputStream stream = Configuration.class.getClassLoader().getResourceAsStream("META-INF/com.microsoft.windowsazure.properties");
InputStream stream = Configuration.class.getClassLoader().getResourceAsStream(
"META-INF/com.microsoft.windowsazure.properties");
if (stream != null) {
Properties properties = new Properties();
properties.load(stream);
Expand All @@ -75,11 +68,11 @@ public static Configuration load() throws IOException {
return config;
}

public <T> T create(Class<T> service) throws Exception {
public <T> T create(Class<T> service) {
return builder.build("", service, properties);
}

public <T> T create(String profile, Class<T> service) throws Exception {
public <T> T create(String profile, Class<T> service) {
return builder.build(profile, service, properties);
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package com.microsoft.windowsazure.common;

public class ConfigurationException extends RuntimeException {

private static final long serialVersionUID = -5570476914992165380L;

public ConfigurationException() {
}

public ConfigurationException(String message) {
super(message);
}

public ConfigurationException(Throwable cause) {
super(cause);
}

public ConfigurationException(String message, Throwable cause) {
super(message, cause);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import java.lang.annotation.Annotation;
import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;
import java.util.ArrayList;
Expand All @@ -13,7 +14,6 @@
import javax.inject.Inject;
import javax.inject.Named;


public class DefaultBuilder implements Builder, Builder.Registry {
Map<Class<?>, Factory<?>> factories;
Map<Class<?>, List<Alteration<?>>> alterations;
Expand Down Expand Up @@ -50,7 +50,8 @@ Constructor<?> findInjectConstructor(Class<?> implementation) {
for (Constructor<?> ctor : implementation.getConstructors()) {
if (ctor.getAnnotation(Inject.class) != null) {
if (withInject != null) {
throw new RuntimeException("Class must not have multple @Inject annotations: " + implementation.getName());
throw new RuntimeException("Class must not have multple @Inject annotations: "
+ implementation.getName());
}
withInject = ctor;
}
Expand All @@ -63,7 +64,8 @@ Constructor<?> findInjectConstructor(Class<?> implementation) {
return withInject;
}
if (count != 1) {
throw new RuntimeException("Class without @Inject annotation must have one constructor: " + implementation.getName());
throw new RuntimeException("Class without @Inject annotation must have one constructor: "
+ implementation.getName());
}
return withoutInject;
}
Expand All @@ -75,7 +77,7 @@ public <T, TImpl> Builder.Registry add(Class<T> service, final Class<TImpl> impl

addFactory(service, new Builder.Factory<T>() {
@SuppressWarnings("unchecked")
public T create(String profile, Builder builder, Map<String, Object> properties) throws Exception {
public T create(String profile, Builder builder, Map<String, Object> properties) {
Object[] initargs = new Object[parameterTypes.length];
for (int i = 0; i != parameterTypes.length; ++i) {

Expand Down Expand Up @@ -106,7 +108,18 @@ public T create(String profile, Builder builder, Map<String, Object> properties)
}
}

return (T) ctor.newInstance(initargs);
try {
return (T) ctor.newInstance(initargs);
}
catch (InstantiationException e) {
throw new ConfigurationException(e);
}
catch (IllegalAccessException e) {
throw new ConfigurationException(e);
}
catch (InvocationTargetException e) {
throw new ConfigurationException(e);
}
}
});
return this;
Expand Down Expand Up @@ -143,7 +156,7 @@ public <T> Registry add(Factory<T> factory) {
}

@SuppressWarnings("unchecked")
public <T> T build(String profile, Class<T> service, Map<String, Object> properties) throws Exception {
public <T> T build(String profile, Class<T> service, Map<String, Object> properties) {
Factory<T> factory = (Factory<T>) factories.get(service);
if (factory == null) {
throw new RuntimeException("Service or property not registered: " + profile + " " + service.getName());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,7 @@ public void register(Builder.Registry registry) {
// alter jersey client config for serviceBus
registry.alter(ClientConfig.class, new Builder.Alteration<ClientConfig>() {

public ClientConfig alter(ClientConfig instance, Builder builder,
Map<String, Object> properties) throws Exception {
public ClientConfig alter(ClientConfig instance, Builder builder, Map<String, Object> properties) {

// enable this feature for unattributed json object serialization
instance.getProperties().put(JSONConfiguration.FEATURE_POJO_MAPPING, true);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,7 @@

public class AlterClassWithProperties implements Builder.Alteration<ClassWithProperties> {

public ClassWithProperties alter(ClassWithProperties instance,
Builder builder, Map<String, Object> properties) throws Exception {
public ClassWithProperties alter(ClassWithProperties instance, Builder builder, Map<String, Object> properties) {
instance.setFoo(instance.getFoo() + " - changed");
return instance;
}
Expand Down

0 comments on commit 49dc9f0

Please sign in to comment.