Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  /*
   * Firebird Open Source J2EE Connector - JDBC Driver
   *
   * Distributable under LGPL license.
   * You may obtain a copy of the License at http://www.gnu.org/copyleft/lgpl.html
   *
   * This program is distributed in the hope that it will be useful,
   * but WITHOUT ANY WARRANTY; without even the implied warranty of
   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  * LGPL License for more details.
  *
  * This file was created by members of the firebird development team.
  * All individual contributions remain the Copyright (C) of those
  * individuals.  Contributors to this file are either listed here or
  * can be obtained from a CVS history command.
  *
  * All rights reserved.
  */
 package org.firebirdsql.management;
 
 
Implements the backup and restore functionality of Firebird Services API.

Author(s):
Roman Rokytskyy
 
 public class FBBackupManager extends FBServiceManager implements BackupManager {

    
Structure that holds path to the database and corresponding size of the file (in case of backup - that is size of the file in megabytes, in case of restore - size of the database file in pages).
 
     private static class PathSizeStruct {
         private int size;
         private String path;
         
         private PathSizeStruct(String pathint size) {
             this. = path;
             this. = size;
         }
         
         public String getPath() {
             return ;
         }
 
         public int getSize() {
             return ;
         }
         
         public boolean equals(Object obj) {
             if (obj == thisreturn true;
             if (!(obj instanceof PathSizeStruct)) return false;
             
             PathSizeStruct that = (PathSizeStruct)obj;
             
             return this..equals(that.path);
         }
 
         public int hashCode() {
             return .hashCode();
         }
 
         public String toString() {
             return  + " " + ;
         }
     }
 
     private boolean noLimitBackup = false;
     private ArrayList backupPaths = new ArrayList();
     
     private boolean noLimitRestore = false;
     private ArrayList restorePaths = new ArrayList();
     
     private boolean verbose;
 
     private int restoreBufferCount;
 
     private int restorePageSize;
 
     private boolean restoreReadOnly;
 
     private boolean restoreReplace;
 
 
     private static final int RESTORE_REPLACE = .;
 
     private static final int RESTORE_CREATE = .;
    
    
Create a new instance of FBBackupManager based on the default GDSType.
    public FBBackupManager()
    {
    	super();
    	init();
    }

    
Create a new instance of FBBackupManager based on a given GDSType.

Parameters:
gdsType type must be PURE_JAVA, EMBEDDED, or NATIVE
    public FBBackupManager(String gdsType)
    {
    	super(gdsType);
    	init();
    }

    
Create a new instance of FBBackupManager based on a given GDSType.

Parameters:
gdsType type must be PURE_JAVA, EMBEDDED, or NATIVE
    public FBBackupManager(GDSType gdsType) {
    	super(gdsType);
    	init();
    }
    
    
Initialize necessary parameters.
    private void init() {
         = false;
         = -1;
         = -1;
         = false;
         = false;
    }

    
    public void setBackupPath(String backupPath) {
        addBackupPath(backupPath, -1);
         = true;
    }
    public void addBackupPath(String path) {
        addBackupPath(path, -1);
    }
    
    public void addBackupPath(String pathint size) {
        
        if ()
            throw new IllegalArgumentException(
                "You cannot use setBackupPath(String) and " +
                "addBackupPath(String, int) methods simultaneously.");
        
        .add(new PathSizeStruct(pathsize));
    }
    
    public void clearBackupPaths() {
        .clear();
         = false;
    }
    
    /* (non-Javadoc)
     * @see org.firebirdsql.management.ServiceManager#setDatabase(java.lang.String)
     */
    public void setDatabase(String database) {
        super.setDatabase(database);
        addRestorePath(database, -1);
         = true;
    }
    public void addRestorePath(String pathint size) {
        if ()
            throw new IllegalArgumentException(
                "You cannot use setDatabase(String) and " +
                "addRestorePath(String, int) methods simultaneously.");
        
        .add(new PathSizeStruct(pathsize));
    }
    
    public void clearRestorePaths() {
        .clear();
         = false;
    }
    
    
    public void backupDatabase() throws SQLException {
        backupDatabase(0);
    }

    
    public void backupMetadata() throws SQLException {
    }

    
    public void backupDatabase(int options ) throws SQLException {
        executeServicesOperation(getBackupSRB(options));
    }

    
Creates and returns the "backup" service request buffer for the Service Manager.

Parameters:
options The isc_spb_bkp_* parameters options to be used
Returns:
the "backup" service request buffer for the Service Manager.
    private ServiceRequestBuffer getBackupSRB(int optionsthrows SQLException {
                .);
        backupSPB.addArgument(.getDatabase());
        
        for (Iterator iter = .iterator(); iter.hasNext();) {
            PathSizeStruct pathSize = (PathSizeStructiter.next();
            
            backupSPB.addArgument(.pathSize.getPath());
            
            if (iter.hasNext() && pathSize.getSize() == -1)
                throw new FBSQLException("No size specified for a backup file " + 
                    pathSize.getPath());
            
            if (iter.hasNext())
                backupSPB.addArgument(.pathSize.size);
        }
        
        if ()
            backupSPB.addArgument(.);
        backupSPB.addArgument(.options);
        return backupSPB;
    }

    
    public void restoreDatabase() throws SQLException {
        restoreDatabase(0);
    }

    
    public void restoreDatabase(int optionsthrows SQLException {
        executeServicesOperation(getRestoreSRB(options));
    }

    
Set whether the operations of this BackupManager will result in verbose logging to the configured logger.

Parameters:
verbose If true, operations will be logged verbosely, otherwise they will not be logged verbosely
    public void setVerbose(boolean verbose) {
        this. = verbose;
    }
      
    
Set the default number of pages to be buffered (cached) by default in a restored database.

Parameters:
bufferCount The page-buffer size to be used, a positive value
    public void setRestorePageBufferCount(int bufferCount) {
        if (bufferCount < 0){
            throw new IllegalArgumentException("Buffer count must be positive");
        }
        this. = bufferCount;
    }

    
Set the page size that will be used for a restored database. The value for pageSize must be one of: 1024, 2048, 4096, 8192 or 16384. The default value depends on the Firebird version.

Parameters:
pageSize The page size to be used in a restored database, one of 1024, 2048, 4196, 8192 or 16384
    public void setRestorePageSize(int pageSize) {
        if (pageSize != 1024 && pageSize != 2048 
                && pageSize != 4096 && pageSize != 8192 && pageSize != 16384){
            throw new IllegalArgumentException(
                    "Page size must be one of 1024, 2048, 4096, 8192 or 16384");
        }
        this. = pageSize;
    }

    
Set the restore operation to create a new database, as opposed to overwriting an existing database. This is true by default.

Parameters:
replace If true, the restore operation will attempt to create a new database, otherwise the restore operation will overwrite an existing database
    public void setRestoreReplace(boolean replace) {
        this. = replace;
    }


    
Set the read-only attribute on a restored database.

Parameters:
readOnly If true, a restored database will be read-only, otherwise it will be read-write.
    public void setRestoreReadOnly(boolean readOnly) {
        this. = readOnly;
    }


    
Creates and returns the "backup" service request buffer for the Service Manager.

Parameters:
options THe options to be used for the backup operation
Returns:
the "backup" service request buffer for the Service Manager.
    private ServiceRequestBuffer getRestoreSRB(int options) {
        GDS gds = getGds();
        ServiceRequestBuffer restoreSPB = gds
        // backup files without sizes
        for (Iterator iter = .iterator(); iter.hasNext();) {
            PathSizeStruct pathSize = (PathSizeStructiter.next();
            
            restoreSPB.addArgument(.pathSize.getPath());            
        }
        
        // restore files with sizes except the last one
        for (Iterator iter = .iterator(); iter.hasNext();) {
            PathSizeStruct pathSize = (PathSizeStructiter.next();
            
            restoreSPB.addArgument(.pathSize.getPath());
            
            if (iter.hasNext() && pathSize.getSize() != -1)
                restoreSPB.addArgument(.pathSize.getSize());
        }
        
        if ( != -1)
            restoreSPB.addArgument(.
                    );
        if ( != -1)
            restoreSPB.addArgument(.
                    );
                    ((byte)( 
                        ? .
                        : .)));
        if ()
            restoreSPB.addArgument(.);
        if ((options & ) !=  
                && (options & ) != ){
            options |=  ?  : ;
        }
        
        restoreSPB.addArgument(.options);
        return restoreSPB;
    }
New to GrepCode? Check out our FAQ X