Created
November 3, 2016 13:50
-
-
Save jangalinski/715981f3121207ff077d27491b7e033f to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package de.holisticon.bpm.camunda.license; | |
import org.flywaydb.core.api.MigrationVersion; | |
import org.flywaydb.core.api.migration.MigrationInfoProvider; | |
import org.flywaydb.core.api.migration.jdbc.JdbcMigration; | |
import javax.sql.DataSource; | |
import java.io.BufferedReader; | |
import java.io.IOException; | |
import java.io.InputStream; | |
import java.io.InputStreamReader; | |
import java.net.URI; | |
import java.net.URISyntaxException; | |
import java.sql.Connection; | |
import java.sql.PreparedStatement; | |
import java.sql.ResultSet; | |
import java.sql.SQLException; | |
import java.text.SimpleDateFormat; | |
import java.util.Date; | |
public class InsertLicenseKeyMigration implements JdbcMigration, MigrationInfoProvider { | |
public static final String LICENSE_FILE = "camunda-license.txt"; | |
private static final String INSERT_SQL = "INSERT INTO ACT_GE_PROPERTY VALUES ('camunda-license-key', ?, 1)"; | |
public static class Util { | |
public static boolean isPackageAvailable(String packagePath) { | |
return Util.class.getClassLoader().getResource(packagePath.replaceAll("\\.", "/")) != null; | |
} | |
public static String queryLicenseKey(DataSource dataSource) { | |
try (Connection connection = dataSource.getConnection()) { | |
final ResultSet resultSet = connection.createStatement() | |
.executeQuery("SELECT VALUE_ FROM ACT_GE_PROPERTY where NAME_ = 'camunda-license-key'"); | |
return resultSet.next() ? resultSet.getString(1) : ""; | |
} catch (SQLException e) { | |
throw new IllegalStateException(e); | |
} | |
} | |
public static String resourceToString(final String resourceName) { | |
try { | |
final InputStream in = Util.class.getClassLoader().getResourceAsStream(resourceName); | |
try (BufferedReader br = new BufferedReader(new InputStreamReader(in))) { | |
StringBuilder sb = new StringBuilder(); | |
String read; | |
while ((read = br.readLine()) != null) { | |
sb.append(read); | |
} | |
return sb.toString(); | |
} | |
} catch (final IOException e) { | |
throw new IllegalArgumentException(e); | |
} | |
} | |
public static String licenseKey() { | |
return resourceToString(LICENSE_FILE).split("---------------")[2].replaceAll("\\n", ""); | |
} | |
} | |
@Override | |
public void migrate(Connection connection) throws Exception { | |
try (PreparedStatement statement = connection.prepareStatement(INSERT_SQL)) { | |
statement.setString(1, Util.licenseKey()); | |
statement.execute(); | |
} | |
} | |
@Override | |
public MigrationVersion getVersion() { | |
return MigrationVersion.fromVersion(new SimpleDateFormat("yyyyMMddHHmmss").format(new Date()) + hashCode()); | |
} | |
@Override | |
public String getDescription() { | |
return "Insert camunda license key."; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment