diff --git a/microsoft-azure-api/src/main/java/com/microsoft/windowsazure/common/Builder.java b/microsoft-azure-api/src/main/java/com/microsoft/windowsazure/common/Builder.java index 6047f230e230a..4fe8de2011e5c 100644 --- a/microsoft-azure-api/src/main/java/com/microsoft/windowsazure/common/Builder.java +++ b/microsoft-azure-api/src/main/java/com/microsoft/windowsazure/common/Builder.java @@ -4,14 +4,14 @@ public interface Builder { - public abstract T build(String profile, Class service, Map properties) throws Exception; + public abstract T build(String profile, Class service, Map properties); public interface Factory { - T create(String profile, Builder builder, Map properties) throws Exception; + T create(String profile, Builder builder, Map properties); } public interface Alteration { - T alter(T instance, Builder builder, Map properties) throws Exception; + T alter(T instance, Builder builder, Map properties); } public interface Registry { diff --git a/microsoft-azure-api/src/main/java/com/microsoft/windowsazure/common/Configuration.java b/microsoft-azure-api/src/main/java/com/microsoft/windowsazure/common/Configuration.java index b0770ccf96b8c..323a0c6f44d8a 100644 --- a/microsoft-azure-api/src/main/java/com/microsoft/windowsazure/common/Configuration.java +++ b/microsoft-azure-api/src/main/java/com/microsoft/windowsazure/common/Configuration.java @@ -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() { @@ -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); @@ -75,11 +68,11 @@ public static Configuration load() throws IOException { return config; } - public T create(Class service) throws Exception { + public T create(Class service) { return builder.build("", service, properties); } - public T create(String profile, Class service) throws Exception { + public T create(String profile, Class service) { return builder.build(profile, service, properties); } diff --git a/microsoft-azure-api/src/main/java/com/microsoft/windowsazure/common/ConfigurationException.java b/microsoft-azure-api/src/main/java/com/microsoft/windowsazure/common/ConfigurationException.java new file mode 100644 index 0000000000000..b21d4b5147ad6 --- /dev/null +++ b/microsoft-azure-api/src/main/java/com/microsoft/windowsazure/common/ConfigurationException.java @@ -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); + } + +} diff --git a/microsoft-azure-api/src/main/java/com/microsoft/windowsazure/common/DefaultBuilder.java b/microsoft-azure-api/src/main/java/com/microsoft/windowsazure/common/DefaultBuilder.java index 74eef8182403f..5d65f4f8170a3 100644 --- a/microsoft-azure-api/src/main/java/com/microsoft/windowsazure/common/DefaultBuilder.java +++ b/microsoft-azure-api/src/main/java/com/microsoft/windowsazure/common/DefaultBuilder.java @@ -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; @@ -13,7 +14,6 @@ import javax.inject.Inject; import javax.inject.Named; - public class DefaultBuilder implements Builder, Builder.Registry { Map, Factory> factories; Map, List>> alterations; @@ -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; } @@ -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; } @@ -75,7 +77,7 @@ public Builder.Registry add(Class service, final Class impl addFactory(service, new Builder.Factory() { @SuppressWarnings("unchecked") - public T create(String profile, Builder builder, Map properties) throws Exception { + public T create(String profile, Builder builder, Map properties) { Object[] initargs = new Object[parameterTypes.length]; for (int i = 0; i != parameterTypes.length; ++i) { @@ -106,7 +108,18 @@ public T create(String profile, Builder builder, Map 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; @@ -143,7 +156,7 @@ public Registry add(Factory factory) { } @SuppressWarnings("unchecked") - public T build(String profile, Class service, Map properties) throws Exception { + public T build(String profile, Class service, Map properties) { Factory factory = (Factory) factories.get(service); if (factory == null) { throw new RuntimeException("Service or property not registered: " + profile + " " + service.getName()); diff --git a/microsoft-azure-api/src/main/java/com/microsoft/windowsazure/services/serviceBus/Exports.java b/microsoft-azure-api/src/main/java/com/microsoft/windowsazure/services/serviceBus/Exports.java index 679a7d324b7a1..c94c8f68af704 100644 --- a/microsoft-azure-api/src/main/java/com/microsoft/windowsazure/services/serviceBus/Exports.java +++ b/microsoft-azure-api/src/main/java/com/microsoft/windowsazure/services/serviceBus/Exports.java @@ -23,8 +23,7 @@ public void register(Builder.Registry registry) { // alter jersey client config for serviceBus registry.alter(ClientConfig.class, new Builder.Alteration() { - public ClientConfig alter(ClientConfig instance, Builder builder, - Map properties) throws Exception { + public ClientConfig alter(ClientConfig instance, Builder builder, Map properties) { // enable this feature for unattributed json object serialization instance.getProperties().put(JSONConfiguration.FEATURE_POJO_MAPPING, true); diff --git a/microsoft-azure-api/src/test/java/com/microsoft/windowsazure/configuration/builder/AlterClassWithProperties.java b/microsoft-azure-api/src/test/java/com/microsoft/windowsazure/configuration/builder/AlterClassWithProperties.java index 8c847bfbc5c31..5a85851c8308d 100644 --- a/microsoft-azure-api/src/test/java/com/microsoft/windowsazure/configuration/builder/AlterClassWithProperties.java +++ b/microsoft-azure-api/src/test/java/com/microsoft/windowsazure/configuration/builder/AlterClassWithProperties.java @@ -6,8 +6,7 @@ public class AlterClassWithProperties implements Builder.Alteration { - public ClassWithProperties alter(ClassWithProperties instance, - Builder builder, Map properties) throws Exception { + public ClassWithProperties alter(ClassWithProperties instance, Builder builder, Map properties) { instance.setFoo(instance.getFoo() + " - changed"); return instance; }