1 /*------------------------------------------------------------------------------
2 Name: FileInfo.java
3 Project: xmlBlaster.org
4 Copyright: xmlBlaster.org, see xmlBlaster-LICENSE file
5 ------------------------------------------------------------------------------*/
6
7 package org.xmlBlaster.client.filepoller;
8
9 import java.io.File;
10 import java.sql.Timestamp;
11
12 import java.util.logging.Logger;
13 import java.util.logging.Level;
14
15 /**
16 * FileInfo is a placeholder for the information necessary to the poller about
17 * each file.
18 *
19 * @author <a href="mailto:michele@laghi.eu">Michele Laghi</a>
20 * @deprectated it is now replaced by the corresponding class in org.xmlBlaster.contrib.filewatcher
21 */
22 public class FileInfo {
23
24 private String name;
25 private long timestamp;
26 private long size;
27 private long lastChange;
28
29 /**
30 * Default Constructor
31 *
32 */
33 public FileInfo() {
34 }
35
36 /**
37 * Convenience constructor
38 * @param file
39 */
40 public FileInfo(File file, Logger log) {
41 this();
42 update(file, log);
43 //log.info("Found new file '"+this.name+"' timestamp=" + getTimestampStr() + " size=" + this.size + " lastChange=" + getLastChangeStr());
44 }
45
46 /**
47 * updates this info object with the data contained in file. If file is
48 * null, then the method silently returns.
49 * @param file
50 */
51 public void update(File file, Logger log) {
52 if (file == null)
53 return;
54 if (this.name == null) {
55 try {
56 this.name = file.getCanonicalPath();
57 }
58 catch (java.io.IOException ex) {
59 log.warning("could not set the absolute name for file '" + file.getName() + "' " + ex.getMessage());
60 }
61 }
62 long newTimestamp = file.lastModified();
63 long newSize = file.length();
64 if (this.size != newSize) {
65 this.lastChange = System.currentTimeMillis();
66 if (log.isLoggable(Level.FINEST))
67 log.finest("'" + this.name + "' changed: size='" + this.size + "' new size='" + newSize + "'");
68 this.size = newSize;
69 }
70 if (this.timestamp != newTimestamp) {
71 this.lastChange = System.currentTimeMillis();
72 if (log.isLoggable(Level.FINEST))
73 log.finest("'" + this.name + "' changed: time='" + getTimestampStr() + "' new time='" + (new Timestamp(newTimestamp).toString()) + "'");
74 this.timestamp = newTimestamp;
75 }
76 }
77
78 /**
79 * @return Returns the lastChange.
80 */
81 public long getLastChange() {
82 return this.lastChange;
83 }
84
85 public String getTimestampStr() {
86 return new Timestamp(this.timestamp).toString();
87 }
88
89 public String getLastChangeStr() {
90 return new Timestamp(this.lastChange).toString();
91 }
92
93 /**
94 * @return Returns the name.
95 */
96 public String getName() {
97 return this.name;
98 }
99
100 public static String getRelativeName(String name) {
101 int pos = name.lastIndexOf(File.separatorChar);
102 if (pos < 0) return name;
103 return name.substring(pos+1);
104 }
105
106 public String getRelativeName() {
107 return getRelativeName(this.name);
108 }
109
110 /**
111 * @return Returns the size.
112 */
113 public long getSize() {
114 return size;
115 }
116 /**
117 * @return Returns the timestamp.
118 */
119 public long getTimestamp() {
120 return timestamp;
121 }
122
123
124
125 public String toXml(String offset) {
126 StringBuffer buf = new StringBuffer();
127 buf.append(offset).append("<file");
128 if (this.name != null)
129 buf.append("name='").append(this.name).append("' ");
130 buf.append("time='" + this.timestamp + "' size='").append(this.size).append("' lastChanged='").append(this.lastChange).append("'/>\n");
131 return buf.toString();
132 }
133
134 /**
135 * @see java.lang.Object#equals(java.lang.Object)
136 */
137 public boolean equals(Object obj) {
138 if (obj instanceof FileInfo) {
139 FileInfo other = (FileInfo)obj;
140 if (other.name == null && this.name == null)
141 return true;
142 if (other.name != null && this.name != null) {
143 return this.name.equals(other.name);
144 }
145 }
146 return false;
147 }
148 }
syntax highlighted by Code2HTML, v. 0.9.1