View Javadoc
1   /*
2    * This file is part of Waarp Project (named also Waarp or GG).
3    *
4    *  Copyright (c) 2019, Waarp SAS, and individual contributors by the @author
5    *  tags. See the COPYRIGHT.txt in the distribution for a full listing of
6    * individual contributors.
7    *
8    *  All Waarp Project is free software: you can redistribute it and/or
9    * modify it under the terms of the GNU General Public License as published by
10   * the Free Software Foundation, either version 3 of the License, or (at your
11   * option) any later version.
12   *
13   * Waarp is distributed in the hope that it will be useful, but WITHOUT ANY
14   * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
15   * A PARTICULAR PURPOSE. See the GNU General Public License for more details.
16   *
17   *  You should have received a copy of the GNU General Public License along with
18   * Waarp . If not, see <http://www.gnu.org/licenses/>.
19   */
20  package org.waarp.common.filemonitor;
21  
22  import org.waarp.common.filemonitor.FileMonitor.FileItem;
23  import org.waarp.common.filemonitor.FileMonitor.Status;
24  
25  import java.util.Date;
26  
27  import static org.waarp.common.database.DbConstant.*;
28  
29  /**
30   * Command run when a new file item is validated
31   */
32  public abstract class FileMonitorCommandRunnableFuture implements Runnable {
33    private FileItem fileItem;
34    private Thread currentThread;
35    private FileMonitor monitor;
36  
37    /**
38     *
39     */
40    protected FileMonitorCommandRunnableFuture() {
41    }
42  
43    public final void setMonitor(final FileMonitor monitor) {
44      this.monitor = monitor;
45    }
46  
47    /**
48     * @param fileItem
49     */
50    protected FileMonitorCommandRunnableFuture(final FileItem fileItem) {
51      setFileItem(fileItem);
52    }
53  
54    public final void setFileItem(final FileItem fileItem) {
55      this.fileItem = fileItem;
56    }
57  
58    @Override
59    public void run() {
60      currentThread = Thread.currentThread();
61      if (getFileItem() != null) {
62        run(getFileItem());
63      }
64    }
65  
66    /**
67     * @param fileItem fileItem on which the command will be executed.
68     */
69    public abstract void run(FileItem fileItem);
70  
71    /**
72     * To be called at the beginning of the primary action (only for
73     * commandValidFile).
74     *
75     * @param ignoreAlreadyUsed
76     */
77    protected final void checkReuse(final boolean ignoreAlreadyUsed) {
78      if (!ignoreAlreadyUsed && fileItem.used &&
79          fileItem.specialId != ILLEGALVALUE &&
80          fileItem.status == Status.RESTART) {
81        fileItem.used = false;
82      }
83    }
84  
85    protected final boolean isReuse() {
86      return fileItem.status == Status.RESTART && !fileItem.used &&
87             fileItem.specialId != ILLEGALVALUE;
88    }
89  
90    protected final boolean isIgnored(final boolean ignoreAlreadyUsed) {
91      return !ignoreAlreadyUsed && fileItem.used;
92    }
93  
94    /**
95     * To be overriden to adapt to the situation for extra check
96     *
97     * @param fileItem
98     *
99     * @return True if the file has to be run (or re-run), False in other
100    *     conditions (ignored)
101    */
102   protected boolean checkFileItemBusiness(final FileItem fileItem) {
103     return fileItem.status == Status.VALID || fileItem.status == Status.RESTART;
104   }
105 
106   protected final void setValid(final FileItem fileItem) {
107     fileItem.specialId = ILLEGALVALUE;
108     fileItem.used = false;
109     fileItem.status = Status.VALID;
110   }
111 
112   /**
113    * To be called at the end of the primary action (only for
114    * commandValidFile).
115    *
116    * @param status
117    * @param specialId the specialId associated with the task
118    */
119   protected final void finalizeValidFile(final boolean status,
120                                          final long specialId) {
121     if (getMonitor() != null) {
122       final Date date = new Date();
123       if (date.after(getMonitor().nextDay)) {
124         // midnight is after last check
125         getMonitor().setNextDay();
126         getMonitor().todayok.set(0);
127         getMonitor().todayerror.set(0);
128       }
129     }
130     if (status) {
131       getFileItem().used = true;
132       getFileItem().status = Status.DONE;
133       // Keep the hash: fileItem.hash = null
134       getFileItem().specialId = specialId;
135       if (getMonitor() != null) {
136         getMonitor().globalok.incrementAndGet();
137         getMonitor().todayok.incrementAndGet();
138       }
139     } else {
140       // execution in error, will retry later on
141       getFileItem().used = false;
142       getFileItem().status = Status.VALID;
143       getFileItem().hash = null;
144       getFileItem().specialId = specialId;
145       if (getMonitor() != null) {
146         getMonitor().globalerror.incrementAndGet();
147         getMonitor().todayerror.incrementAndGet();
148       }
149     }
150   }
151 
152   public final void cancel() {
153     if (currentThread != null) {
154       currentThread.interrupt();
155     }
156   }
157 
158   /**
159    * @return the fileItem
160    */
161   public final FileItem getFileItem() {
162     return fileItem;
163   }
164 
165   /**
166    * @return the monitor
167    */
168   public final FileMonitor getMonitor() {
169     return monitor;
170   }
171 }