Merge branch 'master' of ../infrautils
[odlguice.git] / inject-guice / src / main / java / org / opendaylight / infrautils / inject / guice / AbstractCheckedModule.java
diff --git a/inject-guice/src/main/java/org/opendaylight/infrautils/inject/guice/AbstractCheckedModule.java b/inject-guice/src/main/java/org/opendaylight/infrautils/inject/guice/AbstractCheckedModule.java
new file mode 100644 (file)
index 0000000..792a2fb
--- /dev/null
@@ -0,0 +1,40 @@
+/*
+ * Copyright (c) 2017 Red Hat, Inc. and others. All rights reserved.
+ *
+ * This program and the accompanying materials are made available under the
+ * terms of the Eclipse Public License v1.0 which accompanies this distribution,
+ * and is available at http://www.eclipse.org/legal/epl-v10.html
+ */
+package org.opendaylight.infrautils.inject.guice;
+
+import com.google.inject.AbstractModule;
+import com.google.inject.Binder;
+import org.opendaylight.infrautils.inject.ModuleSetupRuntimeException;
+
+/**
+ * Convenience Guice module support class with configure method that allows
+ * throwing checked exceptions, which are caught and re-thrown as unchecked
+ * {@link ModuleSetupRuntimeException}.
+ *
+ * @author Michael Vorburger.ch
+ */
+public abstract class AbstractCheckedModule extends AbstractModule {
+
+    /**
+     * Configures a {@link Binder} via the exposed methods.
+     *
+     * @throws ModuleSetupRuntimeException if binding failed
+     */
+    @Override
+    @SuppressWarnings("checkstyle:IllegalCatch")
+    protected final void configure() throws ModuleSetupRuntimeException {
+        try {
+            checkedConfigure();
+        } catch (Exception e) {
+            throw new ModuleSetupRuntimeException(e);
+        }
+    }
+
+    protected abstract void checkedConfigure() throws Exception;
+
+}