BackupHelper.java :  » Database-Client » dbmjui » fr » aliacom » dbmjui » components » backup » Java Open Source

Java Open Source » Database Client » dbmjui 
dbmjui » fr » aliacom » dbmjui » components » backup » BackupHelper.java
/*
 * Created on May 1, 2003
 *
 * Dbmjui is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License version 2 as
 * published by the Free Software Foundation.
 *  
 * Dbmjui 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 GNU
 * General Public License for more details.
 *  
 * You should have received a copy of the GNU General Public
 * License along with dbmjui; see the file COPYING.  If not,
 * write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
 * Boston, MA 02111-1307, USA.
 *
 */
package fr.aliacom.dbmjui.components.backup;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;

import org.apache.log4j.Logger;

import com.sap.dbtech.powertoys.DBM;
import com.sap.dbtech.powertoys.DBMException;
import com.sap.dbtech.rte.comm.RTEException;

import fr.aliacom.dbmjui.BackupHistory;
import fr.aliacom.dbmjui.DbInstance;
import fr.aliacom.dbmjui.beans.BackupMedium;
import fr.aliacom.dbmjui.driver.IBackupHelper;

/**
 * @author tom
 *
 * (c) 2001, 2003 Thomas Cataldo
 */
public class BackupHelper implements IBackupHelper {

    private SimpleDateFormat formatter;
    private Logger log;

    public BackupHelper() {
      log = Logger.getLogger(BackupHelper.class);
        formatter = new SimpleDateFormat("yyyyMMddhhmmss");
    }

    public ArrayList getMediumList(DbInstance dbi, int mediumType)
        throws RTEException, DBMException {
        DBM dbm = dbi.getDbmConnection();
        String mediumList;
        try {
            mediumList = dbm.cmd("medium_getall");
        } finally {
            dbm.release();
        }
        return parseMediumList(mediumList, mediumType);
    }

    /**
     * Creates a list of {@link BackupMedium} using the
     * output a the <code>medium_getall</code> dbm command
     * 
     * @param mediumList the output of <code>medium_getall</code> 
     * @return a list of {@link BackupMedium}
     */
    protected ArrayList parseMediumList(String mediumList, int backupType) {
        ArrayList ret = new ArrayList();
        String lines[] = mediumList.split("\n");
        for (int i = 0; i < lines.length; i++) {
            BackupMedium medium = parseLine(lines[i]);
            if ((backupType & medium.getBackupType())
                == medium.getBackupType()) {
                ret.add(medium);
            }
        }
        return ret;
    }

    /**
     * @param mediumLine
     * @return a backup medium
     */
    protected BackupMedium parseLine(String mediumLine) {
        String[] line = mediumLine.split("\t");
        BackupMedium bm = new BackupMedium();
        bm.setName(line[0]);
        bm.setLocation(line[1]);
        try {
            bm.setDate(formatter.parse(line[9]));
        } catch (ParseException pe) {
            pe.printStackTrace();
        }

        // parse medium
        bm.setDeviceType(parseMediumType(line[2]));

        // parse backup type
        bm.setBackupType(parseBackupType(line[3]));

        bm.setMediumSize(Integer.parseInt(line[4].trim()));
        bm.setBlockSize(Integer.parseInt(line[5].trim()));

        bm.setOverwritable(line[6].trim().equals("YES"));
        bm.setUseAutoloader(line[7].trim().equals("YES"));
        return bm;
    }

    /**
     * @param mType
     * @return one of the medium type constants
     */
    protected int parseMediumType(String mType) {
        int mediumType = BackupMedium.TYPE_UNKNOWN;
        if (mType.equals("TAPE")) {
            mediumType = BackupMedium.TYPE_TAPE;
        } else if (mType.equals("FILE")) {
            mediumType = BackupMedium.TYPE_FILE;
        } else if (mType.equals("PIPE")) {
            mediumType = BackupMedium.TYPE_PIPE;
        } else if (mType.equals("NO-REWIND")) {
            mediumType = BackupMedium.TYPE_NO_REWIND;
        } else if (mType.equals("AUTOLOADER")) {
            mediumType = BackupMedium.TYPE_AUTOLOADER;
        }
        return mediumType;
    }

    /**
     * @param bType
     * @return one of the backup type constants
     */
    protected int parseBackupType(String bType) {
        int backupType = BackupMedium.BACKUP_TYPE_UNKNOWN;
        if (bType.equals("DATA")) {
            backupType = BackupMedium.BACKUP_TYPE_DATA;
        } else if (bType.equals("PAGES")) {
            backupType = BackupMedium.BACKUP_TYPE_PAGES;
        } else if (bType.equals("LOG")) {
            backupType = BackupMedium.BACKUP_TYPE_LOG;
        } else if (bType.equals("AUTO")) {
            backupType = BackupMedium.BACKUP_TYPE_AUTO;
        }
        return backupType;
    }
    
  /**
   * Method getBackupHistory.
   * Will force a backup history reload.
   * @return BackupHistory
   */
  public BackupHistory getBackupHistory(DbInstance dbi) {
      BackupHistory history = new BackupHistory();
    try {
      final String cmd =
        "backup_history_list -c "
          + "KEY,"
          + "LABEL,"
          + "ACTION,"
          + "STAMP1,"
          + "STAMP2,"
          + "START,"
          + "STOP,"
          + "FIRSTLOG,"
          + "LASTLOG,"
          + "LOG,"
          + "MEDIA,"
          + "PAGES,"
          + "VOLUMES,"
          + "RC,"
          + "ERROR";
      log.debug(cmd);
      DBM dbm = dbi.getPlainDbmConnection();
      String ret = dbm.cmd(cmd);
      dbm.release();
      String[] backups = ret.split("\n");
      for (int i = backups.length - 1; i > 0; i--) {
        history.addBackup(backups[i]);
      }
    } catch (Exception e) {
      // ignore errors in history creation
      // no history throws an exception
      // I should create a BackupHistoryException for proper handling in log backup
    }
    return history;
  }

}
java2s.com  | Contact Us | Privacy Policy
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.