📄 backupworker.java
字号:
// You can redistribute this software and/or modify it under the terms of// the Ozone Library License version 1 published by ozone-db.org.//// The original code and portions created by SMB are// Copyright (C) 1997-@year@ by SMB GmbH. All rights reserved.//// $Id: BackupWorker.java,v 1.2 2003/03/27 16:50:11 per_nyfelt Exp $package org.ozoneDB.adminGui.feature.data;import org.ozoneDB.ExternalDatabase;import org.ozoneDB.ExternalTransaction;import org.ozoneDB.adminGui.main.AdminGui;import org.ozoneDB.xml.util.SAXChunkConsumer;import org.ozoneDB.xml.util.SAXChunkProducer;import org.ozoneDB.xml.util.SAXChunkProducerDelegate;import org.ozoneDB.core.admin.Admin;import org.apache.xml.serialize.XMLSerializer;import org.apache.xml.serialize.OutputFormat;import org.xml.sax.InputSource;import org.xml.sax.helpers.ParserAdapter;import javax.xml.parsers.SAXParserFactory;import javax.xml.parsers.SAXParser;import java.io.*;/** * @author Per Nyfelt */public final class BackupWorker { public static void backup(File backupFile) throws Exception { ExternalDatabase db = AdminGui.instance().getDb(); Admin admin = db.admin(); ExternalTransaction tx = db.newTransaction(); tx.begin(); try { admin.beginBackup(); if (backupFile.exists() && !backupFile.canWrite()) { throw new Exception("File " + backupFile.getAbsolutePath() + " exists and is not writable"); } OutputStream out = new FileOutputStream(backupFile); OutputFormat outputFormat = new OutputFormat("xml", "UTF-8", true); //System.out.println("outputformat = " + outputFormat); XMLSerializer serializer = new XMLSerializer(out, outputFormat); //System.out.println("serializer = " + serializer); SAXChunkConsumer consumer = new SAXChunkConsumer(serializer.asContentHandler()); byte[] bytes = null; int i = 0; while ((bytes = admin.nextBackupChunk()) != null) { System.out.println("writing chunk " + ++i); consumer.processChunk(bytes); } out.flush(); out.close(); tx.commit(); System.out.println("done"); } catch (Exception e) { tx.rollback(); throw e; } } public static void restore(File backupFile) throws Exception { ExternalDatabase db = AdminGui.instance().getDb(); final Admin admin = db.admin(); ExternalTransaction tx = db.newTransaction(); tx.begin(); try { if (!backupFile.exists() || !backupFile.canRead()) { throw new Exception("File " + backupFile.getAbsolutePath() + " is not accessible"); } admin.beginRestore(); try { InputStream in = new FileInputStream(backupFile); InputSource xmlSource = new InputSource(in); SAXChunkProducer producer = new SAXChunkProducer(new SAXChunkProducerDelegate() { public void processChunk(SAXChunkProducer producer) throws Exception { admin.processRestoreChunk(producer.chunkStream().toByteArray()); } }); SAXParserFactory parserFactory = SAXParserFactory.newInstance(); SAXParser parser = parserFactory.newSAXParser(); ParserAdapter adapter = new ParserAdapter(parser.getParser()); adapter.setContentHandler(producer); adapter.parse(xmlSource); tx.commit(); } finally { admin.processRestoreChunk(null); } } catch (Exception e) { tx.rollback(); throw e; } }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -