VOBackupFile.java Source code

Java tutorial

Introduction

Here is the source code for VOBackupFile.java

Source

/*
This softtware use BSD licence (http://opensource.org/licenses/BSD-3-Clause)
    
Copyright (c) 2013, Martin Lebeda
All rights reserved.
    
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:
    
1. Redistributions of source code must retain the above copyright notice, this
list of conditions and the following disclaimer.
    
2. Redistributions in binary form must reproduce the above copyright notice,
this list of conditions and the following disclaimer in the documentation
and/or other materials provided with the distribution.
    
3. Neither the name of the Martin Lebeda nor the names of its contributors may
be used to endorse or promote products derived from this software without
specific prior written permission.
    
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/

import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.LinkOption;
import java.nio.file.attribute.DosFileAttributeView;
import java.nio.file.attribute.DosFileAttributes;
import java.nio.file.attribute.PosixFileAttributeView;
import java.nio.file.attribute.PosixFileAttributes;
import java.nio.file.attribute.PosixFilePermissions;
import java.util.Date;

import org.apache.commons.lang3.builder.EqualsBuilder;
import org.apache.commons.lang3.builder.HashCodeBuilder;
import org.apache.commons.lang3.builder.ToStringBuilder;
import org.apache.commons.lang3.builder.ToStringStyle;
import org.simpleframework.xml.Element;
import org.simpleframework.xml.Root;

/**
 * @author <a href="mailto:martin.lebeda@gmail.com">Martin Lebeda</a>
 *         Date: 11.11.13
 */
@Root
public class VOBackupFile {

    @Element
    private String path;
    @Element
    private long size;
    @Element
    private Date modify;
    @Element
    private boolean directory;
    @Element(required = false)
    private boolean symLink;

    @Element(required = false)
    private String symlinkTarget;

    @Element(required = false)
    private String fileHash;
    @Element(required = false)
    private String checkSumBck;

    @Element(required = false)
    private String owner;

    @Element(required = false)
    private Boolean dosAttr;
    @Element(required = false)
    private Boolean dosHidden;
    @Element(required = false)
    private Boolean dosArchive;
    @Element(required = false)
    private Boolean dosSystem;
    @Element(required = false)
    private Boolean dosReadOnly;

    @Element(required = false)
    private Boolean posixAttr;
    @Element(required = false)
    private String posixGroup;
    @Element(required = false)
    private String posixPermitions;

    public VOBackupFile() {
        super();
    }

    /**
     * Wrapper for metadata of backuped file.
     *
     * @param file backuped file
     */
    public VOBackupFile(File file) {
        this.path = file.getAbsolutePath();
        this.directory = file.isDirectory();
        this.symLink = Files.isSymbolicLink(file.toPath());

        this.size = ((file.isDirectory() || symLink) ? 0 : file.length());
        this.modify = new Date(file.lastModified());

        if (symLink) {
            try {
                symlinkTarget = Files.readSymbolicLink(file.toPath()).toString();
            } catch (IOException e) {
                e.printStackTrace(); //TODO Lebeda - oetit do logu
            }
        } else {
            // advanced attributes

            try {

                owner = Files.getOwner(file.toPath(), LinkOption.NOFOLLOW_LINKS).getName();

                if (Files.getFileStore(file.toPath()).supportsFileAttributeView(DosFileAttributeView.class)) {
                    dosAttr = Boolean.TRUE;
                    final DosFileAttributes dosFileAttributes = Files.readAttributes(file.toPath(),
                            DosFileAttributes.class, LinkOption.NOFOLLOW_LINKS);

                    dosArchive = dosFileAttributes.isArchive();
                    dosHidden = dosFileAttributes.isHidden();
                    dosSystem = dosFileAttributes.isSystem();
                    dosReadOnly = dosFileAttributes.isReadOnly();
                } else {
                    dosAttr = Boolean.FALSE;
                }

                if (Files.getFileStore(file.toPath()).supportsFileAttributeView(PosixFileAttributeView.class)) {
                    posixAttr = Boolean.TRUE;
                    final PosixFileAttributes posixFileAttributes = Files.readAttributes(file.toPath(),
                            PosixFileAttributes.class, LinkOption.NOFOLLOW_LINKS);
                    posixGroup = posixFileAttributes.group().getName();
                    posixPermitions = PosixFilePermissions.toString(posixFileAttributes.permissions());
                } else {
                    posixAttr = Boolean.FALSE;
                }

            } catch (IOException e) {
                e.printStackTrace(); //Todo implementovat
            }
        }

    }

    /**
     * Return if file is prepared for backup or not.
     *
     * @return true if file need prepare to backup
     */
    public boolean unPrepared() {
        return (size > 0) && (!isDirectory()) && (fileHash == null);
    }

    @Override
    public boolean equals(final Object other) {
        if (other != null && other instanceof VOBackupFile) {
            return new EqualsBuilder().append(this.getPath(), ((VOBackupFile) other).getPath())
                    .append(this.getModify(), ((VOBackupFile) other).getModify())
                    .append(this.getSize(), ((VOBackupFile) other).getSize())
                    .append(this.isDirectory(), ((VOBackupFile) other).isDirectory()).isEquals();
        } else {
            return false;
        }
    }

    @Override
    public int hashCode() {
        return new HashCodeBuilder(17, 37).append(path).append(size).append(modify).append(directory).toHashCode();
    }

    @Override
    public String toString() {
        return ToStringBuilder.reflectionToString(this, ToStringStyle.SIMPLE_STYLE);
    }

    public void setFileHash(String fileHash) {
        this.fileHash = fileHash;
    }

    public void setDirectory(boolean directory) {
        this.directory = directory;
    }

    public void setModify(Date modify) {
        this.modify = modify;
    }

    public void setSize(long size) {
        this.size = size;
    }

    public void setPath(String path) {
        this.path = path;
    }

    public String getPath() {
        return path;
    }

    public long getSize() {
        return size;
    }

    public Date getModify() {
        return modify;
    }

    public boolean isDirectory() {
        return directory;
    }

    public String getFileHash() {
        return fileHash;
    }

    public void setCheckSumBck(String checkSumBck) {
        this.checkSumBck = checkSumBck;
    }

    public String getCheckSumBck() {
        return checkSumBck;
    }

    public boolean isSymLink() {
        return symLink;
    }

    public void setSymLink(final boolean symLink) {
        this.symLink = symLink;
    }

    public String getSymlinkTarget() {
        return symlinkTarget;
    }

    public void setSymlinkTarget(final String symlinkTarget) {
        this.symlinkTarget = symlinkTarget;
    }

    public String getOwner() {
        return owner;
    }

    public void setOwner(final String owner) {
        this.owner = owner;
    }

    public Boolean getDosAttr() {
        return dosAttr;
    }

    public void setDosAttr(final Boolean dosAttr) {
        this.dosAttr = dosAttr;
    }

    public Boolean getDosHidden() {
        return dosHidden;
    }

    public void setDosHidden(final Boolean dosHidden) {
        this.dosHidden = dosHidden;
    }

    public Boolean getDosArchive() {
        return dosArchive;
    }

    public void setDosArchive(final Boolean dosArchive) {
        this.dosArchive = dosArchive;
    }

    public Boolean getDosSystem() {
        return dosSystem;
    }

    public void setDosSystem(final Boolean dosSystem) {
        this.dosSystem = dosSystem;
    }

    public Boolean getPosixAttr() {
        return posixAttr;
    }

    public void setPosixAttr(final Boolean posixAttr) {
        this.posixAttr = posixAttr;
    }

    public String getPosixGroup() {
        return posixGroup;
    }

    public void setPosixGroup(final String posixGroup) {
        this.posixGroup = posixGroup;
    }

    public String getPosixPermitions() {
        return posixPermitions;
    }

    public void setPosixPermitions(final String posixPermitions) {
        this.posixPermitions = posixPermitions;
    }

    public Boolean getDosReadOnly() {
        return dosReadOnly;
    }

    public void setDosReadOnly(final Boolean dosReadOnly) {
        this.dosReadOnly = dosReadOnly;
    }
}