@@ -0,0 +1,2710 @@
+package cz.muni.fi.jboss.migration.actions;%0A%0Aimport cz.muni.fi.jboss.migration.ex.ActionException;%0Aimport cz.muni.fi.jboss.migration.ex.MigrationException;%0Aimport cz.muni.fi.jboss.migration.spi.IMigrator;%0Aimport java.io.File;%0Aimport java.io.IOException;%0Aimport org.apache.commons.io.FileUtils;%0A%0A/**%0A * Creates the given dir.%0A * Rollback deletes it if it did not exist previously.%0A * %0A * @author Ondrej Zizka, ozizka at redhat.com%0A */%0Apublic class CreateDirectoryAction extends AbstractStatefulAction %7B%0A%0A    private File dir;%0A    private boolean existed = false;%0A%0A%0A    public CreateDirectoryAction( File dir, Class%3C? extends IMigrator%3E fromMigrator ) %7B%0A        super( fromMigrator );%0A        this.dir = dir;%0A    %7D%0A    %0A    %0A%0A    @Override%0A    public String toDescription() %7B%0A        return %22Create directory %22 + dir.getPath();%0A    %7D%0A%0A%0A    @Override%0A    public void preValidate() throws MigrationException %7B%0A        %0A        // If it already exists, it must be a directory.%0A        if( dir.exists()  )%7B%0A            if( ! dir.isDirectory() )%0A                throw new ActionException(this, %22Already exists, but is not a directory: %22 + dir.getPath());%0A            existed = true;%0A            return;%0A        %7D%0A        %0A        // Check if the first existing parent is writable.%0A        File parentFile = dir.getParentFile();%0A        do %7B%0A            if( null == parentFile )%0A                return;%0A                //throw new ActionException(this, %22Can't create directory - no existing parent dir: %22 + dir.getPath());%0A            %0A            if( parentFile.exists() )%0A                break;%0A            %0A            parentFile = parentFile.getParentFile();%0A        %7D while( true );%0A        %0A        if( ! parentFile.canWrite() )%0A            throw new ActionException(this, %22Can't write to the directory: %22 + parentFile.getPath());%0A    %7D%0A%0A%0A    @Override%0A    public void backup() throws MigrationException %7B%0A    %7D%0A%0A%0A    @Override%0A    public void perform() throws MigrationException %7B%0A        if( existed )%0A            return;%0A        try %7B%0A            FileUtils.forceMkdir( dir );%0A        %7D catch( IOException ex ) %7B%0A            throw new ActionException(this, %22Can't create directory %22 + dir.getPath() + %22: %22 + ex.getMessage(), ex);%0A        %7D%0A    %7D%0A%0A%0A    @Override%0A    public void postValidate() throws MigrationException %7B%0A    %7D%0A%0A%0A    @Override%0A    public void cleanBackup() %7B%0A    %7D%0A%0A%0A    @Override%0A    public void rollback() throws MigrationException %7B%0A        if( existed ) return;%0A        try %7B%0A            FileUtils.deleteDirectory( dir );%0A        %7D catch( IOException ex ) %7B%0A            throw new ActionException( this, %22Can't rollback dir creation: %22 + ex.getMessage(), ex);%0A        %7D%0A    %7D%0A    %0A%7D// class%0A

Add CreateDirectoryAction