englishcoffeedrinker.corpse.CORPSE.java Source code

Java tutorial

Introduction

Here is the source code for englishcoffeedrinker.corpse.CORPSE.java

Source

/**
 * This file is part of CORPSE.
 * Copyright (C) 2013, Mark A. Greenwood
 * 
 * CORPSE is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * CORPSE 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 CORPSE. If not, see <http://www.gnu.org/licenses/>.
 **/

package englishcoffeedrinker.corpse;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileReader;
import java.util.Properties;

import com.amazonaws.auth.BasicAWSCredentials;
import com.thoughtworks.xstream.XStream;

public class CORPSE {
    public static void main(String args[]) throws Exception {
        Properties properties = new Properties();
        properties.load(new FileReader("corpse.properties"));

        String accessKey = properties.getProperty("accessKey");
        String secretKey = properties.getProperty("secretKey");

        Glacier glacier = new Glacier(AWSEndpoint.EU_WEST_1, new BasicAWSCredentials(accessKey, secretKey));

        System.out.println(
                glacier.getEndpoint() + " Glacier contains " + humanReadableByteCount(glacier.getSizeInBytes())
                        + " in " + glacier.getNumberOfVaults() + " vaults");

        for (Vault vault : glacier.getVaults()) {
            System.out.println("   " + vault.getName() + ": " + vault.getInventoryDate() + " "
                    + humanReadableByteCount(vault.getSizeInBytes()) + " in " + vault.getNumberOfArchives()
                    + " archives");
        }

        System.out.println("Free Monthly Retrieval: " + humanReadableByteCount(glacier.getFreeMonthlyAllowance()));
        System.out.println("Free Daily Retrieval: " + humanReadableByteCount(glacier.getFreeDailyAllowance()));

        Vault v = Vault.fromJSON(new FileInputStream(new File("vault-inventory.json")));

        System.out.println("\n" + v.getVaultARN() + ": " + v.getInventoryDate() + " contains "
                + humanReadableByteCount(v.getSizeInBytes()) + " in " + v.getNumberOfArchives());

        XStream xstream = new XStream();
        xstream.processAnnotations(Glacier.class);
        xstream.processAnnotations(RetrieveVaultInventory.class);
        System.out.println("\n" + xstream.toXML(glacier));
    }

    public static String humanReadableByteCount(Long bytes) {
        //http://stackoverflow.com/questions/3758606/how-to-convert-byte-size-into-human-readable-format-in-java

        int unit = 1024;
        if (bytes < unit)
            return bytes + " B";
        int exp = (int) (Math.log(bytes) / Math.log(unit));
        String pre = "KMGTPE".charAt(exp - 1) + "";
        return String.format("%.2f %sB", bytes / Math.pow(unit, exp), pre);
    }
}