001: /*
002: * Created on May 1, 2003
003: *
004: * Dbmjui is free software; you can redistribute it and/or
005: * modify it under the terms of the GNU General Public License version 2 as
006: * published by the Free Software Foundation.
007: *
008: * Dbmjui is distributed in the hope that it will be useful,
009: * but WITHOUT ANY WARRANTY; without even the implied warranty of
010: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
011: * General Public License for more details.
012: *
013: * You should have received a copy of the GNU General Public
014: * License along with dbmjui; see the file COPYING. If not,
015: * write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
016: * Boston, MA 02111-1307, USA.
017: *
018: */
019: package fr.aliacom.dbmjui.components.backup;
020:
021: import java.text.ParseException;
022: import java.text.SimpleDateFormat;
023: import java.util.ArrayList;
024:
025: import org.apache.log4j.Logger;
026:
027: import com.sap.dbtech.powertoys.DBM;
028: import com.sap.dbtech.powertoys.DBMException;
029: import com.sap.dbtech.rte.comm.RTEException;
030:
031: import fr.aliacom.dbmjui.BackupHistory;
032: import fr.aliacom.dbmjui.DbInstance;
033: import fr.aliacom.dbmjui.beans.BackupMedium;
034: import fr.aliacom.dbmjui.driver.IBackupHelper;
035:
036: /**
037: * @author tom
038: *
039: * (c) 2001, 2003 Thomas Cataldo
040: */
041: public class BackupHelper implements IBackupHelper {
042:
043: private SimpleDateFormat formatter;
044: private Logger log;
045:
046: public BackupHelper() {
047: log = Logger.getLogger(BackupHelper.class);
048: formatter = new SimpleDateFormat("yyyyMMddhhmmss");
049: }
050:
051: public ArrayList getMediumList(DbInstance dbi, int mediumType)
052: throws RTEException, DBMException {
053: DBM dbm = dbi.getDbmConnection();
054: String mediumList;
055: try {
056: mediumList = dbm.cmd("medium_getall");
057: } finally {
058: dbm.release();
059: }
060: return parseMediumList(mediumList, mediumType);
061: }
062:
063: /**
064: * Creates a list of {@link BackupMedium} using the
065: * output a the <code>medium_getall</code> dbm command
066: *
067: * @param mediumList the output of <code>medium_getall</code>
068: * @return a list of {@link BackupMedium}
069: */
070: protected ArrayList parseMediumList(String mediumList,
071: int backupType) {
072: ArrayList ret = new ArrayList();
073: String lines[] = mediumList.split("\n");
074: for (int i = 0; i < lines.length; i++) {
075: BackupMedium medium = parseLine(lines[i]);
076: if ((backupType & medium.getBackupType()) == medium
077: .getBackupType()) {
078: ret.add(medium);
079: }
080: }
081: return ret;
082: }
083:
084: /**
085: * @param mediumLine
086: * @return a backup medium
087: */
088: protected BackupMedium parseLine(String mediumLine) {
089: String[] line = mediumLine.split("\t");
090: BackupMedium bm = new BackupMedium();
091: bm.setName(line[0]);
092: bm.setLocation(line[1]);
093: try {
094: bm.setDate(formatter.parse(line[9]));
095: } catch (ParseException pe) {
096: pe.printStackTrace();
097: }
098:
099: // parse medium
100: bm.setDeviceType(parseMediumType(line[2]));
101:
102: // parse backup type
103: bm.setBackupType(parseBackupType(line[3]));
104:
105: bm.setMediumSize(Integer.parseInt(line[4].trim()));
106: bm.setBlockSize(Integer.parseInt(line[5].trim()));
107:
108: bm.setOverwritable(line[6].trim().equals("YES"));
109: bm.setUseAutoloader(line[7].trim().equals("YES"));
110: return bm;
111: }
112:
113: /**
114: * @param mType
115: * @return one of the medium type constants
116: */
117: protected int parseMediumType(String mType) {
118: int mediumType = BackupMedium.TYPE_UNKNOWN;
119: if (mType.equals("TAPE")) {
120: mediumType = BackupMedium.TYPE_TAPE;
121: } else if (mType.equals("FILE")) {
122: mediumType = BackupMedium.TYPE_FILE;
123: } else if (mType.equals("PIPE")) {
124: mediumType = BackupMedium.TYPE_PIPE;
125: } else if (mType.equals("NO-REWIND")) {
126: mediumType = BackupMedium.TYPE_NO_REWIND;
127: } else if (mType.equals("AUTOLOADER")) {
128: mediumType = BackupMedium.TYPE_AUTOLOADER;
129: }
130: return mediumType;
131: }
132:
133: /**
134: * @param bType
135: * @return one of the backup type constants
136: */
137: protected int parseBackupType(String bType) {
138: int backupType = BackupMedium.BACKUP_TYPE_UNKNOWN;
139: if (bType.equals("DATA")) {
140: backupType = BackupMedium.BACKUP_TYPE_DATA;
141: } else if (bType.equals("PAGES")) {
142: backupType = BackupMedium.BACKUP_TYPE_PAGES;
143: } else if (bType.equals("LOG")) {
144: backupType = BackupMedium.BACKUP_TYPE_LOG;
145: } else if (bType.equals("AUTO")) {
146: backupType = BackupMedium.BACKUP_TYPE_AUTO;
147: }
148: return backupType;
149: }
150:
151: /**
152: * Method getBackupHistory.
153: * Will force a backup history reload.
154: * @return BackupHistory
155: */
156: public BackupHistory getBackupHistory(DbInstance dbi) {
157: BackupHistory history = new BackupHistory();
158: try {
159: final String cmd = "backup_history_list -c " + "KEY,"
160: + "LABEL," + "ACTION," + "STAMP1," + "STAMP2,"
161: + "START," + "STOP," + "FIRSTLOG," + "LASTLOG,"
162: + "LOG," + "MEDIA," + "PAGES," + "VOLUMES," + "RC,"
163: + "ERROR";
164: log.debug(cmd);
165: DBM dbm = dbi.getPlainDbmConnection();
166: String ret = dbm.cmd(cmd);
167: dbm.release();
168: String[] backups = ret.split("\n");
169: for (int i = backups.length - 1; i > 0; i--) {
170: history.addBackup(backups[i]);
171: }
172: } catch (Exception e) {
173: // ignore errors in history creation
174: // no history throws an exception
175: // I should create a BackupHistoryException for proper handling in log backup
176: }
177: return history;
178: }
179:
180: }
|