Skip to content

Commit

Permalink
[ignore] code cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
adamretter committed Oct 14, 2016
1 parent 51d6b5d commit 778ac93
Show file tree
Hide file tree
Showing 8 changed files with 100 additions and 111 deletions.
19 changes: 6 additions & 13 deletions src/org/exist/backup/ExportGUI.java
Expand Up @@ -29,14 +29,9 @@
import org.exist.util.MimeType;
import org.exist.xquery.TerminatedException;

import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.io.*;

import java.nio.file.Files;
import java.nio.file.Path;
import java.util.List;
import java.util.Optional;
Expand Down Expand Up @@ -582,14 +577,12 @@ public void displayMessage( String message )
}


private void openLog( String dir )
private void openLog(final String dir)
{
final Path file = SystemExport.getUniqueFile("report", ".log", dir);
try {
final File file = SystemExport.getUniqueFile( "report", ".log", dir );
final OutputStream os = new BufferedOutputStream( new FileOutputStream( file ) );
logWriter = new PrintWriter( new OutputStreamWriter( os, UTF_8 ) );
}
catch( final FileNotFoundException e ) {
logWriter = new PrintWriter(Files.newBufferedWriter(file, UTF_8));
} catch( final IOException e ) {
System.err.println( "ERROR: failed to create log file" );
}
}
Expand Down
17 changes: 9 additions & 8 deletions src/org/exist/backup/SystemExport.java
Expand Up @@ -73,7 +73,9 @@
import javax.xml.stream.XMLStreamReader;
import javax.xml.transform.OutputKeys;
import java.io.*;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.text.SimpleDateFormat;
import java.util.*;

Expand Down Expand Up @@ -716,17 +718,16 @@ private void writeXML( DocumentImpl doc, Receiver receiver )
}
}

public static File getUniqueFile( String base, String extension, String dir )
{
public static Path getUniqueFile(final String base, final String extension, final String dir) {
final SimpleDateFormat creationDateFormat = new SimpleDateFormat(DataBackup.DATE_FORMAT_PICTURE);
final String filename = base + '-' + creationDateFormat.format( Calendar.getInstance().getTime() );
File file = new File( dir, filename + extension );
int version = 0;
final String filename = base + '-' + creationDateFormat.format(Calendar.getInstance().getTime());
Path file = Paths.get(dir, filename + extension);
int version = 0;

while( file.exists() ) {
file = new File( dir, filename + '_' + version++ + extension );
while(Files.exists(file)) {
file = Paths.get(dir, filename + '_' + version++ + extension);
}
return( file );
return file;
}


Expand Down
22 changes: 9 additions & 13 deletions src/org/exist/storage/BackupSystemTask.java
Expand Up @@ -71,19 +71,21 @@ public class BackupSystemTask implements SystemTask {
// purge old zip backup files
private int zipFilesMax = -1;

public void configure(Configuration config, Properties properties) throws EXistException {
@Override
public void configure(final Configuration config, final Properties properties) throws EXistException {
user = properties.getProperty("user", "guest");
password = properties.getProperty("password", "guest");
String collName = properties.getProperty("collection", "xmldb:exist:///db");
if (!collName.startsWith("xmldb:exist:"))
{collName = "xmldb:exist://" + collName;}
if (!collName.startsWith("xmldb:exist:")) {
collName = "xmldb:exist://" + collName;
}
collection = XmldbURI.create(collName);
LOG.debug("Collection to backup: " + collection.toString() + ". User: " + user);

suffix = properties.getProperty("suffix", "");
prefix = properties.getProperty("prefix", "");

String dir = properties.getProperty("dir", "backup");
final String dir = properties.getProperty("dir", "backup");
directory = Paths.get(dir);
if (!directory.isAbsolute()) {
directory = ((Path)config.getProperty(BrokerPool.PROPERTY_DATA_DIR)).resolve(dir);
Expand All @@ -107,21 +109,15 @@ public void configure(Configuration config, Properties properties) throws EXistE
}
}


public void execute(DBBroker broker) throws EXistException {
@Override
public void execute(final DBBroker broker) throws EXistException {
final String dateTime = creationDateFormat.format(Calendar.getInstance().getTime());
final Path dest = directory.resolve(prefix + dateTime + suffix);

final Backup backup = new Backup(user, password, dest, collection);
try {
backup.backup(false, null);
} catch (final XMLDBException e) {
LOG.debug(e.getMessage(), e);
throw new EXistException(e.getMessage(), e);
} catch (final IOException e) {
LOG.debug(e.getMessage(), e);
throw new EXistException(e.getMessage(), e);
} catch (final SAXException e) {
} catch (final XMLDBException | SAXException | IOException e) {
LOG.debug(e.getMessage(), e);
throw new EXistException(e.getMessage(), e);
}
Expand Down
69 changes: 35 additions & 34 deletions src/org/exist/storage/ConsistencyCheckTask.java
Expand Up @@ -21,13 +21,16 @@
*/
package org.exist.storage;

import java.io.*;
import java.io.IOException;
import java.io.PrintWriter;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.List;
import java.util.Properties;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.exist.EXistException;
import org.exist.backup.ConsistencyCheck;
import org.exist.backup.ErrorReport;
Expand All @@ -43,6 +46,8 @@

public class ConsistencyCheckTask implements SystemTask {

private final static Logger LOG = LogManager.getLogger(ConsistencyCheckTask.class);

private String exportDir;
private boolean createBackup = false;
private boolean createZip = true;
Expand Down Expand Up @@ -70,9 +75,9 @@ public class ConsistencyCheckTask implements SystemTask {
public boolean afterCheckpoint() {
return false;
}
public void configure(Configuration config, Properties properties) throws EXistException {

@Override
public void configure(final Configuration config, final Properties properties) throws EXistException {
exportDir = properties.getProperty(OUTPUT_PROP_NAME, "export");
Path dir = Paths.get(exportDir);
if (!dir.isAbsolute()) {
Expand Down Expand Up @@ -115,7 +120,7 @@ public void configure(Configuration config, Properties properties) throws EXistE
}

@Override
public void execute(DBBroker broker) throws EXistException {
public void execute(final DBBroker broker) throws EXistException {
final Agent agentInstance = AgentFactory.getInstance();
final BrokerPool brokerPool = broker.getBrokerPool();
final TaskStatus endStatus = new TaskStatus(TaskStatus.Status.STOPPED_OK);
Expand Down Expand Up @@ -177,13 +182,8 @@ public void execute(DBBroker broker) throws EXistException {
LOG.info("Finished backup");
}

} catch (final TerminatedException e) {
} catch (final TerminatedException | PermissionDeniedException e) {
throw new EXistException(e.getMessage(), e);

} catch (final PermissionDeniedException e) {
//TODO should maybe throw PermissionDeniedException instead!
throw new EXistException(e.getMessage(), e);

} finally {
if (report != null) {
report.close();
Expand All @@ -202,14 +202,14 @@ public Path getLastExportedBackup()
return lastExportedBackup;
}

private boolean fatalErrorsFound(List<ErrorReport> errors) {
private boolean fatalErrorsFound(final List<ErrorReport> errors) {
for (final ErrorReport error : errors) {
switch (error.getErrcode()) {
// the following errors are considered fatal: export the db and
// the following errors are considered fatal: export the db and
// stop the task
case ErrorReport.CHILD_COLLECTION:
case ErrorReport.RESOURCE_ACCESS_FAILED:
return true;
case ErrorReport.CHILD_COLLECTION:
case ErrorReport.RESOURCE_ACCESS_FAILED:
return true;
}
}
// no fatal errors
Expand All @@ -218,43 +218,41 @@ private boolean fatalErrorsFound(List<ErrorReport> errors) {

private PrintWriter openLog() throws EXistException {
try {
final File file = SystemExport.getUniqueFile("report", ".log", exportDir);
final OutputStream os = new BufferedOutputStream(new FileOutputStream(file));
return new PrintWriter(new OutputStreamWriter(os, UTF_8));
} catch (final FileNotFoundException e) {
final Path file = SystemExport.getUniqueFile("report", ".log", exportDir);
return new PrintWriter(Files.newBufferedWriter(file, UTF_8));
} catch (final IOException e) {
throw new EXistException("ERROR: failed to create report file in " + exportDir, e);
}
}

private static class LoggingCallback implements SystemExport.StatusCallback {

public void startCollection(String path) throws TerminatedException {
@Override
public void startCollection(final String path) throws TerminatedException {

}

public void startDocument(String name, int current, int count)
throws TerminatedException {
@Override
public void startDocument(final String name, final int current, final int count) throws TerminatedException {
}

public void error(String message, Throwable exception) {
@Override
public void error(final String message, final Throwable exception) {
LOG.error(message, exception);
}

}

private class CheckCallback implements ConsistencyCheck.ProgressCallback, SystemExport.StatusCallback {

private PrintWriter log;
private final PrintWriter log;
private boolean errorFound = false;

private CheckCallback(PrintWriter log) {
private CheckCallback(final PrintWriter log) {
this.log = log;
}

// public void startDocument(String path) {
// }

public void startDocument(String name, int current, int count) throws TerminatedException {
@Override
public void startDocument(final String name, final int current, final int count) throws TerminatedException {
if (!monitor.proceed()) {
throw new TerminatedException("consistency check terminated");
}
Expand All @@ -268,7 +266,8 @@ public void startDocument(String name, int current, int count) throws Terminated
}
}

public void startCollection(String path) throws TerminatedException {
@Override
public void startCollection(final String path) throws TerminatedException {
if (!monitor.proceed()) {
throw new TerminatedException("consistency check terminated");
}
Expand All @@ -282,14 +281,16 @@ public void startCollection(String path) throws TerminatedException {
log.flush();
}

public void error(ErrorReport error) {
@Override
public void error(final ErrorReport error) {
log.write("----------------------------------------------\n");
log.write(error.toString());
log.write('\n');
log.flush();
}

public void error(String message, Throwable exception) {
@Override
public void error(final String message, final Throwable exception) {
log.write("----------------------------------------------\n");
log.write("EXPORT ERROR: ");
log.write(message);
Expand Down
29 changes: 14 additions & 15 deletions src/org/exist/storage/DataBackup.java
Expand Up @@ -61,11 +61,9 @@ public DataBackup(final Path destination) {
public boolean afterCheckpoint() {
return true;
}

/* (non-Javadoc)
* @see org.exist.storage.SystemTask#configure(java.util.Properties)
*/
public void configure(Configuration config, Properties properties) throws EXistException {

@Override
public void configure(final Configuration config, final Properties properties) throws EXistException {
dest = Paths.get(properties.getProperty("output-dir", "backup"));
if (!dest.isAbsolute()) {
dest = ((Path)config.getProperty(BrokerPool.PROPERTY_DATA_DIR)).resolve(dest);
Expand All @@ -83,12 +81,12 @@ public void configure(Configuration config, Properties properties) throws EXistE

LOG.debug("Setting backup data directory: " + dest);
}
public void execute(DBBroker broker) throws EXistException {
if (!(broker instanceof NativeBroker))
{throw new EXistException("DataBackup system task can only be used " +
"with the native storage backend");}
// NativeBroker nbroker = (NativeBroker) broker;

@Override
public void execute(final DBBroker broker) throws EXistException {
if (!(broker instanceof NativeBroker)) {
throw new EXistException("DataBackup system task can only be used with the native storage backend");
}

LOG.debug("Backing up data files ...");

Expand All @@ -109,18 +107,19 @@ public void execute(DBBroker broker) throws EXistException {
}

private static class Callback implements RawDataBackup {
final private ZipOutputStream zout;

private ZipOutputStream zout;

private Callback(ZipOutputStream out) {
private Callback(final ZipOutputStream out) {
zout = out;
}

public OutputStream newEntry(String name) throws IOException {
@Override
public OutputStream newEntry(final String name) throws IOException {
zout.putNextEntry(new ZipEntry(name));
return zout;
}

@Override
public void closeEntry() throws IOException {
zout.closeEntry();
}
Expand Down
5 changes: 1 addition & 4 deletions src/org/exist/storage/SystemTask.java
Expand Up @@ -24,8 +24,6 @@

import java.util.Properties;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.exist.EXistException;
import org.exist.util.Configuration;

Expand All @@ -51,8 +49,7 @@
*/
public interface SystemTask {

final static Logger LOG = LogManager.getLogger(SystemTask.class);


void configure(Configuration config, Properties properties) throws EXistException;

/**
Expand Down

0 comments on commit 778ac93

Please sign in to comment.