Search in sources :

Example 46 with FlywayException

use of org.flywaydb.core.api.FlywayException in project flyway by flyway.

the class AndroidScanner method scanForClasses.

public Class<?>[] scanForClasses(Location location, Class<?> implementedInterface) throws Exception {
    String pkg = location.getPath().replace("/", ".");
    List<Class> classes = new ArrayList<Class>();
    DexFile dex = new DexFile(context.getApplicationInfo().sourceDir);
    Enumeration<String> entries = dex.entries();
    while (entries.hasMoreElements()) {
        String className = entries.nextElement();
        if (className.startsWith(pkg)) {
            Class<?> clazz = classLoader.loadClass(className);
            if (Modifier.isAbstract(clazz.getModifiers())) {
                LOG.debug("Skipping abstract class: " + className);
                continue;
            }
            if (!implementedInterface.isAssignableFrom(clazz)) {
                continue;
            }
            try {
                ClassUtils.instantiate(className, classLoader);
            } catch (Exception e) {
                throw new FlywayException("Unable to instantiate class: " + className);
            }
            classes.add(clazz);
            LOG.debug("Found class: " + className);
        }
    }
    return classes.toArray(new Class<?>[classes.size()]);
}
Also used : FlywayException(org.flywaydb.core.api.FlywayException) ArrayList(java.util.ArrayList) DexFile(dalvik.system.DexFile) FlywayException(org.flywaydb.core.api.FlywayException)

Example 47 with FlywayException

use of org.flywaydb.core.api.FlywayException in project flyway by flyway.

the class MetaDataTableImpl method createIfNotExists.

/**
     * Creates the metatable if it doesn't exist, upgrades it if it does.
     */
private void createIfNotExists() {
    int retries = 0;
    while (!table.exists()) {
        if (retries == 0) {
            LOG.info("Creating Metadata table: " + table);
        }
        try {
            String resourceName = "org/flywaydb/core/internal/dbsupport/" + dbSupport.getDbName() + "/createMetaDataTable.sql";
            String source = new ClassPathResource(resourceName, getClass().getClassLoader()).loadAsString("UTF-8");
            Map<String, String> placeholders = new HashMap<String, String>();
            placeholders.put("schema", table.getSchema().getName());
            placeholders.put("table", table.getName());
            String sourceNoPlaceholders = new PlaceholderReplacer(placeholders, "${", "}").replacePlaceholders(source);
            final SqlScript sqlScript = new SqlScript(sourceNoPlaceholders, dbSupport);
            sqlScript.execute(jdbcTemplate);
            LOG.debug("Metadata table " + table + " created.");
        } catch (FlywayException e) {
            if (++retries >= 10) {
                throw e;
            }
            try {
                LOG.debug("Metadata table creation failed. Retrying in 1 sec ...");
                Thread.sleep(1000);
            } catch (InterruptedException e1) {
            // Ignore
            }
        }
    }
}
Also used : FlywayException(org.flywaydb.core.api.FlywayException) HashMap(java.util.HashMap) PlaceholderReplacer(org.flywaydb.core.internal.util.PlaceholderReplacer) SqlScript(org.flywaydb.core.internal.dbsupport.SqlScript) ClassPathResource(org.flywaydb.core.internal.util.scanner.classpath.ClassPathResource)

Example 48 with FlywayException

use of org.flywaydb.core.api.FlywayException in project flyway by flyway.

the class PlaceholderReplacer method checkForUnmatchedPlaceholderExpression.

/**
     * Check for unmatched placeholder expressions in the input string and throw
     * a FlywayException if they do not have corresponding values.
     *
     * @param input The input string.
     * @throws FlywayException An exception listing the unmatched expressions.
     */
private void checkForUnmatchedPlaceholderExpression(String input) {
    String regex = Pattern.quote(placeholderPrefix) + "(.+?)" + Pattern.quote(placeholderSuffix);
    Matcher matcher = Pattern.compile(regex).matcher(input);
    Set<String> unmatchedPlaceHolderExpressions = new TreeSet<String>();
    while (matcher.find()) {
        unmatchedPlaceHolderExpressions.add(matcher.group());
    }
    if (!unmatchedPlaceHolderExpressions.isEmpty()) {
        throw new FlywayException("No value provided for placeholder expressions: " + StringUtils.collectionToCommaDelimitedString(unmatchedPlaceHolderExpressions) + ".  Check your configuration!");
    }
}
Also used : FlywayException(org.flywaydb.core.api.FlywayException) Matcher(java.util.regex.Matcher) TreeSet(java.util.TreeSet)

Example 49 with FlywayException

use of org.flywaydb.core.api.FlywayException in project flyway by flyway.

the class ClassPathResource method loadAsString.

public String loadAsString(String encoding) {
    try {
        InputStream inputStream = classLoader.getResourceAsStream(location);
        if (inputStream == null) {
            throw new FlywayException("Unable to obtain inputstream for resource: " + location);
        }
        Reader reader = new InputStreamReader(inputStream, Charset.forName(encoding));
        return FileCopyUtils.copyToString(reader);
    } catch (IOException e) {
        throw new FlywayException("Unable to load resource: " + location + " (encoding: " + encoding + ")", e);
    }
}
Also used : FlywayException(org.flywaydb.core.api.FlywayException) InputStreamReader(java.io.InputStreamReader) InputStream(java.io.InputStream) Reader(java.io.Reader) InputStreamReader(java.io.InputStreamReader) IOException(java.io.IOException)

Example 50 with FlywayException

use of org.flywaydb.core.api.FlywayException in project flyway by flyway.

the class ClassPathScanner method scanForClasses.

@Override
public Class<?>[] scanForClasses(Location location, Class<?> implementedInterface) throws Exception {
    LOG.debug("Scanning for classes at '" + location + "' (Implementing: '" + implementedInterface.getName() + "')");
    List<Class<?>> classes = new ArrayList<Class<?>>();
    Set<String> resourceNames = findResourceNames(location, "", ".class");
    for (String resourceName : resourceNames) {
        String className = toClassName(resourceName);
        Class<?> clazz;
        try {
            clazz = classLoader.loadClass(className);
            if (!implementedInterface.isAssignableFrom(clazz)) {
                continue;
            }
            if (Modifier.isAbstract(clazz.getModifiers()) || clazz.isEnum() || clazz.isAnonymousClass()) {
                LOG.debug("Skipping non-instantiable class: " + className);
                continue;
            }
            ClassUtils.instantiate(className, classLoader);
        } catch (InternalError e) {
            LOG.debug("Skipping invalid class: " + className);
            continue;
        } catch (IncompatibleClassChangeError e) {
            LOG.debug("Skipping incompatibly changed class: " + className);
            continue;
        } catch (NoClassDefFoundError e) {
            LOG.debug("Skipping non-loadable class: " + className);
            continue;
        } catch (Exception e) {
            throw new FlywayException("Unable to instantiate class: " + className, e);
        }
        classes.add(clazz);
        LOG.debug("Found class: " + className);
    }
    return classes.toArray(new Class<?>[classes.size()]);
}
Also used : FlywayException(org.flywaydb.core.api.FlywayException) ArrayList(java.util.ArrayList) FlywayException(org.flywaydb.core.api.FlywayException) URISyntaxException(java.net.URISyntaxException) IOException(java.io.IOException)

Aggregations

FlywayException (org.flywaydb.core.api.FlywayException)82 SQLException (java.sql.SQLException)22 IOException (java.io.IOException)17 ArrayList (java.util.ArrayList)14 PreparedStatement (java.sql.PreparedStatement)8 HashMap (java.util.HashMap)8 URL (java.net.URL)7 ResultSet (java.sql.ResultSet)7 Statement (java.sql.Statement)7 File (java.io.File)6 MigrationVersion (org.flywaydb.core.api.MigrationVersion)6 ResolvedMigrationImpl (org.flywaydb.core.internal.resolver.ResolvedMigrationImpl)6 Test (org.junit.Test)6 InputStreamReader (java.io.InputStreamReader)5 Method (java.lang.reflect.Method)5 BufferedReader (java.io.BufferedReader)4 FileInputStream (java.io.FileInputStream)4 Reader (java.io.Reader)4 StringReader (java.io.StringReader)4 URLClassLoader (java.net.URLClassLoader)4