Melhorando os logs
[DistributedFileSystem.git] / src / main / java / org / hnaves / dfs / repository / Container.java
blobf2ef8ab9c77aacdfee081923337d0685f4f57be5
1 package org.hnaves.dfs.repository;
3 import java.io.File;
4 import java.io.Serializable;
5 import java.util.ArrayList;
6 import java.util.HashMap;
7 import java.util.logging.Logger;
9 import org.hnaves.dfs.Configurator;
10 import org.hnaves.dfs.election.Node;
12 public class Container implements Serializable {
13 private static final long serialVersionUID = 4190763929963328455L;
14 private static final Logger LOG = Logger.getLogger(Container.class.getName());
16 private final Node location;
17 private final ArrayList<ContentItem> content = new ArrayList<ContentItem>();
18 private transient long lastUpdate;
20 public Container(Node location) {
21 if (location == null) throw new NullPointerException("Location is null");
22 this.location = location;
25 public Node getLocation() {
26 return location;
29 public ContentItem[] getContent() {
30 return content.toArray(new ContentItem[0]);
33 public int getSize() {
34 return content.size();
37 public void clearContent() {
38 content.clear();
41 public void addItem(ContentItem item) {
42 if (item == null) throw new NullPointerException("Item is null");
43 content.add(item);
46 public boolean containsItem(ContentItem item) {
47 return content.contains(item);
50 public void addAllItems(Container c) {
51 if (c == null) throw new NullPointerException("Container is null");
52 content.addAll(c.content);
55 public void update() {
56 this.lastUpdate = System.currentTimeMillis();
59 public boolean hasExpired() {
60 return (System.currentTimeMillis() > lastUpdate + Configurator.getExpireTime());
63 public void updateFiles(File directory) {
64 if (directory == null) throw new NullPointerException("Directory is null");
65 if (!directory.isDirectory()) throw new IllegalArgumentException("Invalid directory " + directory);
67 HashMap<String, ContentItem> oldContent = new HashMap<String, ContentItem>();
68 for(ContentItem item : content) {
69 oldContent.put(item.getName(), item);
72 content.clear();
73 for(File file : directory.listFiles()) {
74 if (!file.isFile()) continue;
75 if (file.getName().startsWith(".")) continue;
76 if (file.getName().startsWith("dfs.log")) continue;
77 if (file.getName().equals("index.dat") || file.getName().equals("config.ini")) continue;
79 LOG.fine("Found file " + file.getName());
80 ContentItem oldItem = oldContent.get(file.getName());
81 if (oldItem != null && oldItem.getLastModified().getTime() == file.lastModified() &&
82 (oldItem.getSize() == (int) file.length())) {
83 LOG.fine("Using old hash " + oldItem.getMd5Hash());
84 content.add(oldItem);
85 } else {
86 LOG.fine("New item!");
87 ContentItem item = new ContentItem(file);
88 content.add(item);
93 @Override
94 public boolean equals(Object obj) {
95 if (obj == this) return true;
96 if (!(obj instanceof Container)) return false;
97 Container other = (Container) obj;
98 return location.equals(other.location);
101 @Override
102 public int hashCode() {
103 return location.hashCode();
106 @Override
107 public String toString() {
108 StringBuilder sb = new StringBuilder();
109 sb.append("Location: ").append(location).append("\n");
110 for(ContentItem item : content) {
111 sb.append(item).append('\n');
113 return sb.toString();