KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > servicemix > components > file > FilePoller


1 /*
2  * Licensed to the Apache Software Foundation (ASF) under one or more
3  * contributor license agreements. See the NOTICE file distributed with
4  * this work for additional information regarding copyright ownership.
5  * The ASF licenses this file to You under the Apache License, Version 2.0
6  * (the "License"); you may not use this file except in compliance with
7  * the License. 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 package org.apache.servicemix.components.file;
18
19 import org.apache.commons.logging.Log;
20 import org.apache.commons.logging.LogFactory;
21 import org.apache.servicemix.components.util.DefaultFileMarshaler;
22 import org.apache.servicemix.components.util.FileMarshaler;
23 import org.apache.servicemix.components.util.PollingComponentSupport;
24
25 import edu.emory.mathcs.backport.java.util.concurrent.CopyOnWriteArraySet;
26
27 import javax.jbi.JBIException;
28 import javax.jbi.messaging.InOnly;
29 import javax.jbi.messaging.NormalizedMessage;
30 import javax.resource.spi.work.Work JavaDoc;
31 import javax.resource.spi.work.WorkException JavaDoc;
32 import java.io.BufferedInputStream JavaDoc;
33 import java.io.File JavaDoc;
34 import java.io.FileFilter JavaDoc;
35 import java.io.FileInputStream JavaDoc;
36 import java.io.IOException JavaDoc;
37 import java.io.InputStream JavaDoc;
38 import java.util.Set JavaDoc;
39
40 /**
41  * A polling component which looks for a file or files in a directory
42  * and sends the files into the JBI bus as messages, deleting the files
43  * by default when they are processed.
44  *
45  * @version $Revision: 426415 $
46  */

47 public class FilePoller extends PollingComponentSupport {
48     private static final Log log = LogFactory.getLog(FilePoller.class);
49
50     private File JavaDoc file;
51     private FileFilter JavaDoc filter;
52     private boolean deleteFile = true;
53     private boolean recursive = true;
54     private boolean autoCreateDirectory = true;
55     private FileMarshaler marshaler = new DefaultFileMarshaler();
56     private Set workingSet = new CopyOnWriteArraySet();
57
58     public void poll() throws Exception JavaDoc {
59         pollFileOrDirectory(file);
60     }
61
62     // Properties
63
//-------------------------------------------------------------------------
64
public File JavaDoc getFile() {
65         return file;
66     }
67
68     /**
69      * Sets the file to poll, which can be a directory or a file.
70      *
71      * @param file
72      */

73     public void setFile(File JavaDoc file) {
74         this.file = file;
75     }
76
77     public FileFilter JavaDoc getFilter() {
78         return filter;
79     }
80
81     /**
82      * Sets the optional filter to choose which files to process
83      */

84     public void setFilter(FileFilter JavaDoc filter) {
85         this.filter = filter;
86     }
87
88     /**
89      * Returns whether or not we should delete the file when its processed
90      */

91     public boolean isDeleteFile() {
92         return deleteFile;
93     }
94
95     public void setDeleteFile(boolean deleteFile) {
96         this.deleteFile = deleteFile;
97     }
98
99     public boolean isRecursive() {
100         return recursive;
101     }
102
103     public void setRecursive(boolean recursive) {
104         this.recursive = recursive;
105     }
106
107     public boolean isAutoCreateDirectory() {
108         return autoCreateDirectory;
109     }
110
111     public void setAutoCreateDirectory(boolean autoCreateDirectory) {
112         this.autoCreateDirectory = autoCreateDirectory;
113     }
114
115     public FileMarshaler getMarshaler() {
116         return marshaler;
117     }
118
119     public void setMarshaler(FileMarshaler marshaler) {
120         this.marshaler = marshaler;
121     }
122
123     /**
124      * The set of FTPFiles that this component is currently working on
125      *
126      * @return
127      */

128     public Set getWorkingSet() {
129         return workingSet;
130     }
131
132     // Implementation methods
133
//-------------------------------------------------------------------------
134
protected void init() throws JBIException {
135         if (file == null) {
136             throw new IllegalArgumentException JavaDoc("You must specify a file property");
137         }
138         if (isAutoCreateDirectory() && !file.exists()) {
139             file.mkdirs();
140         }
141         super.init();
142     }
143
144     protected void pollFileOrDirectory(File JavaDoc fileOrDirectory) throws WorkException JavaDoc {
145         pollFileOrDirectory(fileOrDirectory, true);
146     }
147     
148     protected void pollFileOrDirectory(File JavaDoc fileOrDirectory, boolean processDir) throws WorkException JavaDoc {
149         if (!fileOrDirectory.isDirectory()) {
150             pollFile(fileOrDirectory); // process the file
151
} else if (processDir) {
152             log.debug("Polling directory " + fileOrDirectory);
153             File JavaDoc[] files = fileOrDirectory.listFiles(getFilter());
154             for (int i = 0; i < files.length; i++) {
155                 pollFileOrDirectory(files[i], isRecursive()); // self-recursion
156
}
157         } else {
158             log.debug("Skipping directory " + fileOrDirectory);
159         }
160     }
161
162     protected void pollFile(final File JavaDoc aFile) throws WorkException JavaDoc {
163         if (!workingSet.contains(aFile)) {
164             workingSet.add(aFile);
165             if (log.isDebugEnabled()) {
166                 log.debug("Scheduling file " + aFile + " for processing");
167             }
168             getWorkManager().scheduleWork(new Work JavaDoc() {
169                 public void run() {
170                     processFileAndDelete(aFile);
171                 }
172
173                 public void release() {
174                 }
175             });
176         }
177     }
178
179     protected void processFileAndDelete(File JavaDoc aFile) {
180         try {
181             if (log.isDebugEnabled()) {
182                 log.debug("Processing file " + aFile);
183             }
184             if (aFile.exists()) {
185                 processFile(aFile);
186                 if (isDeleteFile()) {
187                     if (!aFile.delete()) {
188                         throw new IOException JavaDoc("Could not delete file " + aFile);
189                     }
190                 }
191             }
192         }
193         catch (Exception JavaDoc e) {
194             log.error("Failed to process file: " + aFile + ". Reason: " + e, e);
195         }
196         finally {
197             workingSet.remove(aFile);
198         }
199     }
200
201     protected void processFile(File JavaDoc aFile) throws Exception JavaDoc {
202         String JavaDoc name = aFile.getCanonicalPath();
203         InputStream JavaDoc in = new BufferedInputStream JavaDoc(new FileInputStream JavaDoc(aFile));
204         InOnly exchange = getExchangeFactory().createInOnlyExchange();
205         NormalizedMessage message = exchange.createMessage();
206         exchange.setInMessage(message);
207         marshaler.readMessage(exchange, message, in, name);
208         getDeliveryChannel().sendSync(exchange);
209         in.close();
210     }
211 }
212
Popular Tags