Java tutorial
/** * 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.IOException; import java.io.InputStream; import java.util.ArrayList; import java.util.List; import org.codehaus.jackson.JsonParseException; import org.codehaus.jackson.annotate.JsonProperty; import org.codehaus.jackson.map.JsonMappingException; import org.codehaus.jackson.map.ObjectMapper; import org.codehaus.jackson.map.annotate.JsonSerialize; import org.codehaus.jackson.map.annotate.JsonSerialize.Inclusion; import com.amazonaws.services.glacier.model.DescribeVaultOutput; import com.thoughtworks.xstream.annotations.XStreamAlias; import com.thoughtworks.xstream.annotations.XStreamImplicit; @JsonSerialize(include = Inclusion.NON_NULL) @XStreamAlias("vault") public class Vault { private String arn; private String name; private String creationDate; private String inventoryDate; private Long numArchives; private Long sizeInBytes; @XStreamImplicit private List<Archive> archives; @SuppressWarnings("unused") private Vault() { //required for Jackson to be able to deserialize the JSON } /** * Creates a Vault instance from the description returned as part * of a ListVault command. Note that this won't contain a breakdown * of the archives contained with the vault as this information * isn't returned as part of a vault listing; you need to request * a vault inventory if you need to find out the details of each * archive in the vault. * @param vault */ public Vault(DescribeVaultOutput vault) { this.arn = vault.getVaultARN(); this.name = vault.getVaultName(); this.creationDate = vault.getCreationDate(); this.inventoryDate = vault.getLastInventoryDate(); this.numArchives = vault.getNumberOfArchives(); this.sizeInBytes = vault.getSizeInBytes(); //a simple vault description doesn't contain the list //of archives, you have to request a vault inventory //to get the full list, which is why you should be //maintaining a local index of your glacier vaults archives = new ArrayList<Archive>(); } @JsonProperty("ArchiveList") private void setArchiveList(List<Archive> archives) { this.archives = archives; } @JsonProperty("ArchiveList") public List<Archive> getArchiveList() { return archives; } public Archive getArchive(int index) { return archives.get(index); } @JsonProperty("VaultARN") public String getVaultARN() { return arn; } @JsonProperty("VaultARN") private void setVaultARN(String arn) { this.arn = arn; } @JsonProperty("VaultName") public String getName() { return name; } @JsonProperty("VaultName") private void setName(String name) { this.name = name; } @JsonProperty("CreationDate") public String getCreationDate() { return creationDate; } @JsonProperty("CreationDate") private void setCreationDate(String creationDate) { this.creationDate = creationDate; } @JsonProperty("InventoryDate") public String getInventoryDate() { return inventoryDate; } @JsonProperty("InventoryDate") private void setInventoryDate(String inventoryDate) { this.inventoryDate = inventoryDate; } @JsonProperty("NumberOfArchives") private void setNumberOfArchives(Long numArchives) { this.numArchives = numArchives; } @JsonProperty("NumberOfArchives") public Long getNumberOfArchives() { return numArchives; } @JsonProperty("SizeInBytes") public Long getSizeInBytes() { return sizeInBytes; } @JsonProperty("SizeInBytes") private void setSizeInBytes(Long sizeInBytes) { this.sizeInBytes = sizeInBytes; } public static Vault fromJSON(InputStream in) throws JsonParseException, JsonMappingException, IOException { ObjectMapper om = new ObjectMapper(); Vault v = om.readValue(in, Vault.class); v.sizeInBytes = 0L; for (Archive a : v.archives) { v.sizeInBytes += a.getSizeInBytes(); } v.numArchives = (long) v.archives.size(); return v; } @Override public int hashCode() { final int prime = 31; int result = 1; result = prime * result + ((arn == null) ? 0 : arn.hashCode()); return result; } @Override public boolean equals(Object obj) { if (this == obj) return true; if (obj == null) return false; if (getClass() != obj.getClass()) return false; Vault other = (Vault) obj; if (arn == null) { if (other.arn != null) return false; } else if (!arn.equals(other.arn)) return false; return true; } }