FileState.java 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  1. package info.fetter.logstashforwarder;
  2. /*
  3. * Copyright 2015 Didier Fetter
  4. *
  5. * Licensed under the Apache License, Version 2.0 (the "License");
  6. * you may not use this file except in compliance with the License.
  7. * You may obtain a copy of the License at
  8. *
  9. * http://www.apache.org/licenses/LICENSE-2.0
  10. *
  11. * Unless required by applicable law or agreed to in writing, software
  12. * distributed under the License is distributed on an "AS IS" BASIS,
  13. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. * See the License for the specific language governing permissions and
  15. * limitations under the License.
  16. *
  17. */
  18. import info.fetter.logstashforwarder.util.RandomAccessFile;
  19. import java.io.File;
  20. import java.io.FileNotFoundException;
  21. import java.io.IOException;
  22. //import java.io.RandomAccessFile;
  23. import org.apache.commons.lang.builder.ToStringBuilder;
  24. import com.fasterxml.jackson.annotation.JsonIgnore;
  25. import java.util.Map;
  26. public class FileState {
  27. @JsonIgnore
  28. private File file;
  29. private String directory;
  30. private String fileName;
  31. @JsonIgnore
  32. private long lastModified;
  33. @JsonIgnore
  34. private long size;
  35. @JsonIgnore
  36. private boolean deleted = false;
  37. private long signature;
  38. private int signatureLength;
  39. @JsonIgnore
  40. private boolean changed = false;
  41. @JsonIgnore
  42. private RandomAccessFile randomAccessFile;
  43. private long pointer = 0;
  44. @JsonIgnore
  45. private FileState oldFileState;
  46. @JsonIgnore
  47. private Event fields;
  48. @JsonIgnore
  49. private Multiline multiline;
  50. @JsonIgnore
  51. private boolean matchedToNewFile = false;
  52. public FileState() {
  53. }
  54. public FileState(File file) throws IOException {
  55. this.file = file;
  56. directory = file.getCanonicalFile().getParent();
  57. fileName = file.getName();
  58. randomAccessFile = new RandomAccessFile(file.getPath(), "r");
  59. lastModified = file.lastModified();
  60. size = file.length();
  61. }
  62. private void setFileFromDirectoryAndName() throws FileNotFoundException {
  63. file = new File(directory + File.separator + fileName);
  64. if(file.exists()) {
  65. randomAccessFile = null;
  66. lastModified = file.lastModified();
  67. size = file.length();
  68. } else {
  69. deleted = true;
  70. }
  71. }
  72. public File getFile() {
  73. return file;
  74. }
  75. public long getLastModified() {
  76. return lastModified;
  77. }
  78. public long getSize() {
  79. return size;
  80. }
  81. public String getDirectory() {
  82. return directory;
  83. }
  84. public void setDirectory(String directory) throws FileNotFoundException {
  85. this.directory = directory;
  86. if(fileName != null && directory != null) {
  87. setFileFromDirectoryAndName();
  88. }
  89. }
  90. public String getFileName() {
  91. return fileName;
  92. }
  93. public void setFileName(String fileName) throws FileNotFoundException {
  94. this.fileName = fileName;
  95. if(fileName != null && directory != null) {
  96. setFileFromDirectoryAndName();
  97. }
  98. }
  99. public boolean isDeleted() {
  100. return deleted;
  101. }
  102. public void setDeleted() {
  103. deleted = true;
  104. }
  105. public boolean hasChanged() {
  106. return changed;
  107. }
  108. public void setChanged(boolean changed) {
  109. this.changed = changed;
  110. }
  111. public long getSignature() {
  112. return signature;
  113. }
  114. public void setSignature(long signature) {
  115. this.signature = signature;
  116. }
  117. public RandomAccessFile getRandomAccessFile() {
  118. return randomAccessFile;
  119. }
  120. public long getPointer() {
  121. return pointer;
  122. }
  123. public void setPointer(long pointer) {
  124. this.pointer = pointer;
  125. }
  126. public int getSignatureLength() {
  127. return signatureLength;
  128. }
  129. public void setSignatureLength(int signatureLength) {
  130. this.signatureLength = signatureLength;
  131. }
  132. public FileState getOldFileState() {
  133. return oldFileState;
  134. }
  135. public void setOldFileState(FileState oldFileState) {
  136. this.oldFileState = oldFileState;
  137. oldFileState.setMatchedToNewFile(true);
  138. }
  139. public void deleteOldFileState() {
  140. try {
  141. oldFileState.getRandomAccessFile().close();
  142. oldFileState = null;
  143. } catch(Exception e) {}
  144. }
  145. public Event getFields() {
  146. return fields;
  147. }
  148. public void setFields(Event fields) {
  149. this.fields = fields;
  150. }
  151. public Multiline getMultiline() {
  152. return multiline;
  153. }
  154. public void setMultiline(Multiline multiline) {
  155. this.multiline = multiline;
  156. }
  157. public boolean isMatchedToNewFile() {
  158. return matchedToNewFile;
  159. }
  160. public void setMatchedToNewFile(boolean matchedToNewFile) {
  161. this.matchedToNewFile = matchedToNewFile;
  162. }
  163. @Override
  164. public String toString() {
  165. return new ToStringBuilder(this).
  166. append("fileName", fileName).
  167. append("directory", directory).
  168. append("pointer", pointer).
  169. append("signature", signature).
  170. append("signatureLength", signatureLength).
  171. toString();
  172. }
  173. }