1 /*
2 * Copyright 2012-2013 Steven Swor.
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17 package com.github.sworisbreathing.sfmf4j.commonsio;
18
19 import com.github.sworisbreathing.sfmf4j.api.FileMonitorService;
20 import com.github.sworisbreathing.sfmf4j.api.FileMonitorServiceFactory;
21 import java.util.concurrent.TimeUnit;
22 import org.apache.commons.io.monitor.FileAlterationMonitor;
23
24 /**
25 * Commons-IO implementation of FileMonitorServiceFactory.
26 * @author Steven Swor
27 */
28 public class CommonsIOFileMonitorServiceFactory implements FileMonitorServiceFactory {
29
30 /**
31 * The polling interval.
32 */
33 private volatile long pollingInterval = 1;
34
35 /**
36 * The polling time interval.
37 */
38 private volatile TimeUnit pollingTimeUnit = TimeUnit.MINUTES;
39
40 /**
41 * Gets the polling interval.
42 * @return the polling interval
43 */
44 public long getPollingInterval() {
45 return pollingInterval;
46 }
47
48 /**
49 * Sets the polling interval.
50 * @param pollingInterval the polling interval
51 */
52 public void setPollingInterval(long pollingInterval) {
53 this.pollingInterval = pollingInterval;
54 }
55
56 /**
57 * Gets the polling time unit.
58 * @return the polling time unit
59 */
60 public TimeUnit getPollingTimeUnit() {
61 return pollingTimeUnit;
62 }
63
64 /**
65 * Sets the polling time unit.
66 * @param pollingTimeUnit the polling time unit
67 */
68 public void setPollingTimeUnit(TimeUnit pollingTimeUnit) {
69 this.pollingTimeUnit = pollingTimeUnit;
70 }
71
72 /**
73 * Creates a new {@link CommonsIOFileMonitorServiceImpl}.
74 * @param pollingInterval the polling interval
75 * @param pollingTimeUnit the time unit for the polling interval.
76 * @return a new {@link CommonsIOFileMonitorServiceImpl}, which wraps a
77 * newly-created {@link FileAlterationMonitor}
78 */
79 @Override
80 public FileMonitorService createFileMonitorService() {
81 long pollingIntervalMillis = getPollingTimeUnit().toMillis(getPollingInterval());
82 FileAlterationMonitor fileAlterationMonitor = new FileAlterationMonitor(pollingIntervalMillis);
83 return new CommonsIOFileMonitorServiceImpl(fileAlterationMonitor);
84 }
85 }