001package org.kualigan.tools.ant.tasks;
002
003import liquibase.CatalogAndSchema;
004import liquibase.Liquibase;
005import liquibase.integration.ant.BaseLiquibaseTask;
006import liquibase.util.StringUtils;
007import org.apache.tools.ant.BuildException;
008
009import java.util.ArrayList;
010import java.util.List;
011
012public class DropAllTask extends BaseLiquibaseTask {
013
014    private String schemas;
015
016    public String getSchemas() {
017        return schemas;
018    }
019
020    public void setSchemas(String schemas) {
021        this.schemas = schemas;
022    }
023    
024    protected void executeWithLiquibaseClassloader() throws BuildException {
025        Liquibase liquibase = null;
026        try {
027            liquibase = createLiquibase();
028            boolean retry = true;
029            while (retry) {
030                try {
031                    if (StringUtils.trimToNull(schemas) != null) {
032                        final List<CatalogAndSchema> schemas = new ArrayList<CatalogAndSchema>() {{
033                                for (final String schema : StringUtils.splitAndTrim(getSchemas(), ",")) {
034                                    add(new CatalogAndSchema(null, schema));
035                                }
036                            }};
037                        
038                        liquibase.dropAll(schemas.toArray(new CatalogAndSchema[schemas.size()]));
039                    } else {
040                        liquibase.dropAll();
041                    }
042                    retry = false;
043                }
044                catch (Exception e2) {
045                    log(e2.getMessage());
046                    if (e2.getMessage().indexOf("ORA-02443") < 0 && e2.getCause() != null && retry) {
047                        retry = (e2.getCause().getMessage().indexOf("ORA-02443") > -1);
048                    }
049                    
050                    if (!retry) {
051                        throw e2;
052                    }
053                    else {
054                        log("Got ORA-2443. Retrying...");
055                    }
056                }
057            }       
058        } catch (Exception e) {
059            throw new BuildException(e);
060        } finally {
061            closeDatabase(liquibase);
062        }
063    }
064}