C register(final C resultSet) {
+ register(new Closeable() {
+ @Override
+ public void close() throws IOException {
+ try {
+ resultSet.close();
+ } catch (SQLException e) {
+ throw new IOException("Failed to close", e);
+ }
+ }
+ });
+ return resultSet;
+ }
+
+ /**
+ * Stores the given throwable and rethrows it. It will be rethrown as is if it is an
+ * {@code IOException}, {@code RuntimeException} or {@code Error}. Otherwise, it will be rethrown
+ * wrapped in a {@code RuntimeException}. Note: Be sure to declare all of the checked
+ * exception types your try block can throw when calling an overload of this method so as to avoid
+ * losing the original exception type.
+ *
+ * This method always throws, and as such should be called as
+ * {@code throw closer.rethrow(e);} to ensure the compiler knows that it will throw.
+ *
+ * @return this method does not return; it always throws
+ * @throws IOException when the given throwable is an IOException
+ */
+ public RuntimeException rethrow(Throwable e) throws IOException {
+ thrown = e;
+ Throwables.propagateIfPossible(e, IOException.class);
+ throw Throwables.propagate(e);
+ }
+
+ /**
+ * Stores the given throwable and rethrows it. It will be rethrown as is if it is an
+ * {@code IOException}, {@code RuntimeException}, {@code Error} or a checked exception of the
+ * given type. Otherwise, it will be rethrown wrapped in a {@code RuntimeException}. Note:
+ * Be sure to declare all of the checked exception types your try block can throw when calling an
+ * overload of this method so as to avoid losing the original exception type.
+ *
+ *
This method always throws, and as such should be called as
+ * {@code throw closer.rethrow(e, ...);} to ensure the compiler knows that it will throw.
+ *
+ * @return this method does not return; it always throws
+ * @throws IOException when the given throwable is an IOException
+ * @throws X when the given throwable is of the declared type X
+ */
+ public RuntimeException rethrow(Throwable e,
+ Class declaredType) throws IOException, X {
+ thrown = e;
+ Throwables.propagateIfPossible(e, IOException.class);
+ Throwables.propagateIfPossible(e, declaredType);
+ throw Throwables.propagate(e);
+ }
+
+ /**
+ * Stores the given throwable and rethrows it. It will be rethrown as is if it is an
+ * {@code IOException}, {@code RuntimeException}, {@code Error} or a checked exception of either
+ * of the given types. Otherwise, it will be rethrown wrapped in a {@code RuntimeException}.
+ * Note: Be sure to declare all of the checked exception types your try block can throw
+ * when calling an overload of this method so as to avoid losing the original exception type.
+ *
+ * This method always throws, and as such should be called as
+ * {@code throw closer.rethrow(e, ...);} to ensure the compiler knows that it will throw.
+ *
+ * @return this method does not return; it always throws
+ * @throws IOException when the given throwable is an IOException
+ * @throws X1 when the given throwable is of the declared type X1
+ * @throws X2 when the given throwable is of the declared type X2
+ */
+ public RuntimeException rethrow(
+ Throwable e, Class declaredType1, Class declaredType2) throws IOException, X1, X2 {
+ thrown = e;
+ Throwables.propagateIfPossible(e, IOException.class);
+ Throwables.propagateIfPossible(e, declaredType1, declaredType2);
+ throw Throwables.propagate(e);
+ }
+
+ /**
+ * Closes all {@code Closeable} instances that have been added to this {@code Closer}. If an
+ * exception was thrown in the try block and passed to one of the {@code exceptionThrown} methods,
+ * any exceptions thrown when attempting to close a closeable will be suppressed. Otherwise, the
+ * first exception to be thrown from an attempt to close a closeable will be thrown and any
+ * additional exceptions that are thrown after that will be suppressed.
+ */
+ @Override
+ public void close() throws IOException {
+ Throwable throwable = thrown;
+
+ // close closeables in LIFO order
+ while (!stack.isEmpty()) {
+ Closeable closeable = stack.pop();
+ try {
+ closeable.close();
+ } catch (Throwable e) {
+ if (throwable == null) {
+ throwable = e;
+ } else {
+ suppressor.suppress(closeable, throwable, e);
+ }
+ }
+ }
+
+ if (thrown == null && throwable != null) {
+ Throwables.propagateIfPossible(throwable, IOException.class);
+ throw new AssertionError(throwable); // not possible
+ }
+ }
+
+ /**
+ * Close quietly.
+ */
+ public void closeQuietly() {
+ try {
+ close();
+ } catch (IOException ignored) {
+ }
+ }
+
+ /**
+ * Suppression strategy interface.
+ */
+ @VisibleForTesting interface Suppressor {
+ /**
+ * Suppresses the given exception ({@code suppressed}) which was thrown when attempting to close
+ * the given closeable. {@code thrown} is the exception that is actually being thrown from the
+ * method. Implementations of this method should not throw under any circumstances.
+ */
+ void suppress(Closeable closeable, Throwable thrown, Throwable suppressed);
+ }
+
+ /**
+ * Suppresses exceptions by logging them.
+ */
+ @VisibleForTesting static final class LoggingSuppressor implements Suppressor {
+
+ static final LoggingSuppressor INSTANCE = new LoggingSuppressor();
+
+ @Override
+ public void suppress(Closeable closeable, Throwable thrown, Throwable suppressed) {
+ // log to the same place as Closeables
+ logger.log(Level.WARNING, "Suppressing exception thrown when closing " + closeable, suppressed);
+ }
+ }
+
+ /**
+ * Suppresses exceptions by adding them to the exception that will be thrown using JDK7's
+ * addSuppressed(Throwable) mechanism.
+ */
+ @VisibleForTesting static final class SuppressingSuppressor implements Suppressor {
+
+ static final SuppressingSuppressor INSTANCE = new SuppressingSuppressor();
+
+ static boolean isAvailable() {
+ return addSuppressed != null;
+ }
+
+ static final Method addSuppressed = getAddSuppressed();
+
+ private static Method getAddSuppressed() {
+ try {
+ return Throwable.class.getMethod("addSuppressed", Throwable.class);
+ } catch (Throwable e) {
+ return null;
+ }
+ }
+
+ @Override
+ public void suppress(Closeable closeable, Throwable thrown, Throwable suppressed) {
+ // ensure no exceptions from addSuppressed
+ if (thrown == suppressed) {
+ return;
+ }
+ try {
+ addSuppressed.invoke(thrown, suppressed);
+ } catch (Throwable e) {
+ // if, somehow, IllegalAccessException or another exception is thrown, fall back to logging
+ LoggingSuppressor.INSTANCE.suppress(closeable, thrown, suppressed);
+ }
+ }
+ }
+}
diff --git a/contrib/region_storage.sql b/src/main/resources/migrations/region/mysql/V1__Initial.sql
similarity index 79%
rename from contrib/region_storage.sql
rename to src/main/resources/migrations/region/mysql/V1__Initial.sql
index 34d9e80d..985b173f 100644
--- a/contrib/region_storage.sql
+++ b/src/main/resources/migrations/region/mysql/V1__Initial.sql
@@ -5,7 +5,7 @@ SET @OLD_SQL_MODE=@@SQL_MODE, SQL_MODE='TRADITIONAL';
-- -----------------------------------------------------
-- Table `group`
-- -----------------------------------------------------
-CREATE TABLE IF NOT EXISTS `group` (
+CREATE TABLE IF NOT EXISTS `${tablePrefix}group` (
`id` INT(10) UNSIGNED NOT NULL AUTO_INCREMENT ,
`name` VARCHAR(64) CHARACTER SET 'utf8' COLLATE 'utf8_bin' NOT NULL ,
PRIMARY KEY (`id`) ,
@@ -18,7 +18,7 @@ COLLATE = utf8_bin;
-- -----------------------------------------------------
-- Table `world`
-- -----------------------------------------------------
-CREATE TABLE IF NOT EXISTS `world` (
+CREATE TABLE IF NOT EXISTS `${tablePrefix}world` (
`id` INT(10) UNSIGNED NOT NULL AUTO_INCREMENT ,
`name` VARCHAR(128) CHARACTER SET 'utf8' COLLATE 'utf8_bin' NOT NULL ,
PRIMARY KEY (`id`) ,
@@ -31,7 +31,7 @@ COLLATE = utf8_bin;
-- -----------------------------------------------------
-- Table `region`
-- -----------------------------------------------------
-CREATE TABLE IF NOT EXISTS `region` (
+CREATE TABLE IF NOT EXISTS `${tablePrefix}region` (
`id` VARCHAR(128) CHARACTER SET 'utf8' COLLATE 'utf8_bin' NOT NULL ,
`world_id` INT(10) UNSIGNED NOT NULL ,
`type` ENUM('cuboid','poly2d','global') CHARACTER SET 'utf8' COLLATE 'utf8_bin' NOT NULL ,
@@ -40,14 +40,14 @@ CREATE TABLE IF NOT EXISTS `region` (
PRIMARY KEY (`id`, `world_id`) ,
INDEX `fk_region_world` (`world_id` ASC) ,
INDEX `parent` (`parent` ASC) ,
- CONSTRAINT `fk_region_world1`
+ CONSTRAINT `fk_${tablePrefix}region_world1`
FOREIGN KEY (`world_id` )
- REFERENCES `world` (`id` )
+ REFERENCES `${tablePrefix}world` (`id` )
ON DELETE CASCADE
ON UPDATE CASCADE,
- CONSTRAINT `parent`
+ CONSTRAINT `${tablePrefix}parent`
FOREIGN KEY (`parent` )
- REFERENCES `region` (`id` )
+ REFERENCES `${tablePrefix}region` (`id` )
ON DELETE SET NULL
ON UPDATE CASCADE)
ENGINE = InnoDB
@@ -58,7 +58,7 @@ COLLATE = utf8_bin;
-- -----------------------------------------------------
-- Table `region_cuboid`
-- -----------------------------------------------------
-CREATE TABLE IF NOT EXISTS `region_cuboid` (
+CREATE TABLE IF NOT EXISTS `${tablePrefix}region_cuboid` (
`region_id` VARCHAR(128) CHARACTER SET 'utf8' COLLATE 'utf8_bin' NOT NULL ,
`world_id` INT(10) UNSIGNED NOT NULL ,
`min_x` BIGINT(20) NOT NULL ,
@@ -69,9 +69,9 @@ CREATE TABLE IF NOT EXISTS `region_cuboid` (
`max_z` BIGINT(20) NOT NULL ,
PRIMARY KEY (`region_id`, `world_id`) ,
INDEX `fk_region_cuboid_region` (`region_id` ASC) ,
- CONSTRAINT `fk_region_cuboid_region`
+ CONSTRAINT `fk_${tablePrefix}region_cuboid_region`
FOREIGN KEY (`region_id` , `world_id` )
- REFERENCES `region` (`id` , `world_id` )
+ REFERENCES `${tablePrefix}region` (`id` , `world_id` )
ON DELETE CASCADE
ON UPDATE CASCADE)
ENGINE = InnoDB
@@ -82,7 +82,7 @@ COLLATE = utf8_bin;
-- -----------------------------------------------------
-- Table `region_flag`
-- -----------------------------------------------------
-CREATE TABLE IF NOT EXISTS `region_flag` (
+CREATE TABLE IF NOT EXISTS `${tablePrefix}region_flag` (
`id` INT(10) UNSIGNED NOT NULL AUTO_INCREMENT ,
`region_id` VARCHAR(128) CHARACTER SET 'utf8' COLLATE 'utf8_bin' NOT NULL ,
`world_id` INT(10) UNSIGNED NOT NULL ,
@@ -90,9 +90,9 @@ CREATE TABLE IF NOT EXISTS `region_flag` (
`value` VARCHAR(256) CHARACTER SET 'utf8' COLLATE 'utf8_bin' NOT NULL ,
PRIMARY KEY (`id`) ,
INDEX `fk_flags_region` (`region_id` ASC, `world_id` ASC) ,
- CONSTRAINT `fk_flags_region`
+ CONSTRAINT `fk_${tablePrefix}flags_region`
FOREIGN KEY (`region_id` , `world_id` )
- REFERENCES `region` (`id` , `world_id` )
+ REFERENCES `${tablePrefix}region` (`id` , `world_id` )
ON DELETE CASCADE
ON UPDATE CASCADE)
ENGINE = InnoDB
@@ -103,7 +103,7 @@ COLLATE = utf8_bin;
-- -----------------------------------------------------
-- Table `region_groups`
-- -----------------------------------------------------
-CREATE TABLE IF NOT EXISTS `region_groups` (
+CREATE TABLE IF NOT EXISTS `${tablePrefix}region_groups` (
`region_id` VARCHAR(128) CHARACTER SET 'utf8' COLLATE 'utf8_bin' NOT NULL ,
`world_id` INT(10) UNSIGNED NOT NULL ,
`group_id` INT(10) UNSIGNED NOT NULL ,
@@ -111,14 +111,14 @@ CREATE TABLE IF NOT EXISTS `region_groups` (
PRIMARY KEY (`region_id`, `world_id`, `group_id`) ,
INDEX `fk_region_groups_region` (`region_id` ASC) ,
INDEX `fk_region_groups_group` (`group_id` ASC) ,
- CONSTRAINT `fk_region_groups_group`
+ CONSTRAINT `fk_${tablePrefix}region_groups_group`
FOREIGN KEY (`group_id` )
- REFERENCES `group` (`id` )
+ REFERENCES `${tablePrefix}group` (`id` )
ON DELETE CASCADE
ON UPDATE CASCADE,
- CONSTRAINT `fk_region_groups_region`
+ CONSTRAINT `fk_${tablePrefix}region_groups_region`
FOREIGN KEY (`region_id` , `world_id` )
- REFERENCES `region` (`id` , `world_id` )
+ REFERENCES `${tablePrefix}region` (`id` , `world_id` )
ON DELETE CASCADE
ON UPDATE CASCADE)
ENGINE = InnoDB
@@ -129,7 +129,7 @@ COLLATE = utf8_bin;
-- -----------------------------------------------------
-- Table `user`
-- -----------------------------------------------------
-CREATE TABLE IF NOT EXISTS `user` (
+CREATE TABLE IF NOT EXISTS `${tablePrefix}user` (
`id` INT(10) UNSIGNED NOT NULL AUTO_INCREMENT ,
`name` VARCHAR(64) CHARACTER SET 'utf8' COLLATE 'utf8_bin' NOT NULL ,
PRIMARY KEY (`id`) ,
@@ -142,7 +142,7 @@ COLLATE = utf8_bin;
-- -----------------------------------------------------
-- Table `region_players`
-- -----------------------------------------------------
-CREATE TABLE IF NOT EXISTS `region_players` (
+CREATE TABLE IF NOT EXISTS `${tablePrefix}region_players` (
`region_id` VARCHAR(128) CHARACTER SET 'utf8' COLLATE 'utf8_bin' NOT NULL ,
`world_id` INT(10) UNSIGNED NOT NULL ,
`user_id` INT(10) UNSIGNED NOT NULL ,
@@ -150,14 +150,14 @@ CREATE TABLE IF NOT EXISTS `region_players` (
PRIMARY KEY (`region_id`, `world_id`, `user_id`) ,
INDEX `fk_region_players_region` (`region_id` ASC) ,
INDEX `fk_region_users_user` (`user_id` ASC) ,
- CONSTRAINT `fk_region_users_region`
+ CONSTRAINT `fk_${tablePrefix}region_users_region`
FOREIGN KEY (`region_id` , `world_id` )
- REFERENCES `region` (`id` , `world_id` )
+ REFERENCES `${tablePrefix}region` (`id` , `world_id` )
ON DELETE CASCADE
ON UPDATE CASCADE,
- CONSTRAINT `fk_region_users_user`
+ CONSTRAINT `fk_${tablePrefix}region_users_user`
FOREIGN KEY (`user_id` )
- REFERENCES `user` (`id` )
+ REFERENCES `${tablePrefix}user` (`id` )
ON DELETE CASCADE
ON UPDATE CASCADE)
ENGINE = InnoDB
@@ -168,16 +168,16 @@ COLLATE = utf8_bin;
-- -----------------------------------------------------
-- Table `region_poly2d`
-- -----------------------------------------------------
-CREATE TABLE IF NOT EXISTS `region_poly2d` (
+CREATE TABLE IF NOT EXISTS `${tablePrefix}region_poly2d` (
`region_id` VARCHAR(128) CHARACTER SET 'utf8' COLLATE 'utf8_bin' NOT NULL ,
`world_id` INT(10) UNSIGNED NOT NULL ,
`min_y` INT(11) NOT NULL ,
`max_y` INT(11) NOT NULL ,
PRIMARY KEY (`region_id`, `world_id`) ,
INDEX `fk_region_poly2d_region` (`region_id` ASC) ,
- CONSTRAINT `fk_region_poly2d_region`
+ CONSTRAINT `fk_${tablePrefix}region_poly2d_region`
FOREIGN KEY (`region_id` , `world_id` )
- REFERENCES `region` (`id` , `world_id` )
+ REFERENCES `${tablePrefix}region` (`id` , `world_id` )
ON DELETE CASCADE
ON UPDATE CASCADE)
ENGINE = InnoDB
@@ -188,7 +188,7 @@ COLLATE = utf8_bin;
-- -----------------------------------------------------
-- Table `region_poly2d_point`
-- -----------------------------------------------------
-CREATE TABLE IF NOT EXISTS `region_poly2d_point` (
+CREATE TABLE IF NOT EXISTS `${tablePrefix}region_poly2d_point` (
`id` BIGINT(20) UNSIGNED NOT NULL AUTO_INCREMENT ,
`region_id` VARCHAR(128) CHARACTER SET 'utf8' COLLATE 'utf8_bin' NOT NULL ,
`world_id` INT(10) UNSIGNED NOT NULL ,
@@ -196,9 +196,9 @@ CREATE TABLE IF NOT EXISTS `region_poly2d_point` (
`z` BIGINT(20) NOT NULL ,
PRIMARY KEY (`id`) ,
INDEX `fk_region_poly2d_point_region_poly2d` (`region_id` ASC, `world_id` ASC) ,
- CONSTRAINT `fk_region_poly2d_point_region_poly2d`
+ CONSTRAINT `fk_${tablePrefix}region_poly2d_point_region_poly2d`
FOREIGN KEY (`region_id` , `world_id` )
- REFERENCES `region_poly2d` (`region_id` , `world_id` )
+ REFERENCES `${tablePrefix}region_poly2d` (`region_id` , `world_id` )
ON DELETE CASCADE
ON UPDATE CASCADE)
ENGINE = InnoDB