KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > mule > providers > file > FileConnector


1 /*
2  * $Id: FileConnector.java 3937 2006-11-20 16:04:25Z lajos $
3  * --------------------------------------------------------------------------------------
4  * Copyright (c) MuleSource, Inc. All rights reserved. http://www.mulesource.com
5  *
6  * The software in this package is published under the terms of the MuleSource MPL
7  * license, a copy of which has been included with this distribution in the
8  * LICENSE.txt file.
9  */

10
11 package org.mule.providers.file;
12
13 import org.apache.commons.logging.Log;
14 import org.apache.commons.logging.LogFactory;
15 import org.mule.config.MuleProperties;
16 import org.mule.config.i18n.Message;
17 import org.mule.config.i18n.Messages;
18 import org.mule.providers.AbstractServiceEnabledConnector;
19 import org.mule.providers.file.filters.FilenameWildcardFilter;
20 import org.mule.transformers.NoActionTransformer;
21 import org.mule.transformers.simple.ByteArrayToSerializable;
22 import org.mule.transformers.simple.SerializableToByteArray;
23 import org.mule.umo.UMOComponent;
24 import org.mule.umo.UMOException;
25 import org.mule.umo.endpoint.UMOEndpoint;
26 import org.mule.umo.lifecycle.InitialisationException;
27 import org.mule.umo.provider.UMOMessageReceiver;
28 import org.mule.util.FileUtils;
29
30 import java.io.File JavaDoc;
31 import java.io.FileOutputStream JavaDoc;
32 import java.io.IOException JavaDoc;
33 import java.util.Map JavaDoc;
34 import java.util.Properties JavaDoc;
35
36 /**
37  * <code>FileConnector</code> is used for setting up listeners on a directory and
38  * for writing files to a directory. The connecotry provides support for defining
39  * file output patterns and filters for receiving files.
40  */

41
42 public class FileConnector extends AbstractServiceEnabledConnector
43 {
44     /**
45      * logger used by this class
46      */

47     private static Log logger = LogFactory.getLog(FileConnector.class);
48
49     // These are properties that can be overridden on the Receiver by the endpoint
50
// declaration
51
public static final String JavaDoc PROPERTY_POLLING_FREQUENCY = "pollingFrequency";
52     public static final String JavaDoc PROPERTY_FILE_AGE = "fileAge";
53     public static final String JavaDoc PROPERTY_FILENAME = "filename";
54     public static final String JavaDoc PROPERTY_ORIGINAL_FILENAME = "originalFilename";
55     public static final String JavaDoc PROPERTY_OUTPUT_PATTERN = "outputPattern";
56     public static final String JavaDoc PROPERTY_MOVE_TO_PATTERN = "moveToPattern";
57     public static final String JavaDoc PROPERTY_MOVE_TO_DIRECTORY = "moveToDirectory";
58     public static final String JavaDoc PROPERTY_DELETE_ON_READ = "autoDelete";
59     public static final String JavaDoc PROPERTY_DIRECTORY = "directory";
60     public static final String JavaDoc PROPERTY_SERVICE_OVERRIDE = "serviceOverrides";
61     public static final String JavaDoc PROPERTY_WRITE_TO_DIRECTORY = "writeToDirectoryName";
62
63     public static final long DEFAULT_POLLING_FREQUENCY = 1000;
64
65     /**
66      * Time in milliseconds to poll. On each poll the poll() method is called
67      */

68     private long pollingFrequency = 0;
69
70     private String JavaDoc moveToPattern = null;
71
72     private String JavaDoc writeToDirectoryName = null;
73
74     private String JavaDoc moveToDirectoryName = null;
75
76     private String JavaDoc outputPattern = null;
77
78     private boolean outputAppend = false;
79
80     private boolean autoDelete = true;
81
82     private boolean checkFileAge = false;
83
84     private long fileAge = 0;
85
86     private FileOutputStream JavaDoc outputStream = null;
87
88     private boolean serialiseObjects = false;
89
90     public FilenameParser filenameParser;
91
92     /*
93      * (non-Javadoc)
94      *
95      * @see org.mule.providers.AbstractConnector#doInitialise()
96      */

97     public FileConnector()
98     {
99         filenameParser = new SimpleFilenameParser();
100     }
101
102     protected Object JavaDoc getReceiverKey(UMOComponent component, UMOEndpoint endpoint)
103     {
104         if (endpoint.getFilter() != null)
105         {
106             return endpoint.getEndpointURI().getAddress() + "/"
107                    + ((FilenameWildcardFilter)endpoint.getFilter()).getPattern();
108         }
109         return endpoint.getEndpointURI().getAddress();
110     }
111
112     /**
113      * Registers a listener for a particular directory The following properties can
114      * be overriden in the endpoint declaration
115      * <ul>
116      * <li>moveToDirectory</li>
117      * <li>filterPatterns</li>
118      * <li>filterClass</li>
119      * <li>pollingFrequency</li>
120      * </ul>
121      */

122     public UMOMessageReceiver createReceiver(UMOComponent component, UMOEndpoint endpoint) throws Exception JavaDoc
123     {
124         String JavaDoc readDir = endpoint.getEndpointURI().getAddress();
125         long polling = this.pollingFrequency;
126
127         String JavaDoc moveTo = moveToDirectoryName;
128         String JavaDoc moveToPattern = getMoveToPattern();
129
130         Map JavaDoc props = endpoint.getProperties();
131         if (props != null)
132         {
133             // Override properties on the endpoint for the specific endpoint
134
String JavaDoc move = (String JavaDoc)props.get(PROPERTY_MOVE_TO_DIRECTORY);
135             if (move != null)
136             {
137                 moveTo = move;
138             }
139             String JavaDoc tempMoveToPattern = (String JavaDoc)props.get(PROPERTY_MOVE_TO_PATTERN);
140             if (tempMoveToPattern != null)
141             {
142                 if (logger.isDebugEnabled())
143                 {
144                     logger.debug("set moveTo Pattern to: " + tempMoveToPattern);
145                 }
146                 moveToPattern = tempMoveToPattern;
147             }
148
149             String JavaDoc tempPolling = (String JavaDoc)props.get(PROPERTY_POLLING_FREQUENCY);
150             if (tempPolling != null)
151             {
152                 polling = Long.parseLong(tempPolling);
153             }
154
155             if (polling <= 0)
156             {
157                 polling = DEFAULT_POLLING_FREQUENCY;
158             }
159
160             if (logger.isDebugEnabled())
161             {
162                 logger.debug("set polling frequency to: " + polling);
163             }
164             String JavaDoc tempFileAge = (String JavaDoc)props.get(PROPERTY_FILE_AGE);
165             if (tempFileAge != null)
166             {
167                 try
168                 {
169                     setFileAge(Long.parseLong(tempFileAge));
170                 }
171                 catch (Exception JavaDoc ex1)
172                 {
173                     logger.error("Failed to set fileAge", ex1);
174                 }
175             }
176             Map JavaDoc srvOverride = (Map JavaDoc) props.get(PROPERTY_SERVICE_OVERRIDE);
177             if (srvOverride != null) {
178                 if (serviceOverrides == null) {
179                     serviceOverrides = new Properties();
180                 }
181                 serviceOverrides.setProperty(MuleProperties.CONNECTOR_INBOUND_TRANSFORMER,
182                     NoActionTransformer.class.getName());
183                 serviceOverrides.setProperty(MuleProperties.CONNECTOR_OUTBOUND_TRANSFORMER,
184                     NoActionTransformer.class.getName());
185             }
186         }
187
188         try
189         {
190             return serviceDescriptor.createMessageReceiver(this, component, endpoint, new Object JavaDoc[]{readDir,
191                 moveTo, moveToPattern, new Long JavaDoc(polling)});
192
193         }
194         catch (Exception JavaDoc e)
195         {
196             throw new InitialisationException(new Message(Messages.FAILED_TO_CREATE_X_WITH_X,
197                 "Message Receiver", serviceDescriptor.getMessageReceiver()), e, this);
198         }
199     }
200
201     /*
202      * (non-Javadoc)
203      *
204      * @see org.mule.providers.UMOConnector#stop()
205      */

206     protected synchronized void doStop() throws UMOException
207     {
208         if (outputStream != null)
209         {
210             try
211             {
212                 outputStream.close();
213             }
214             catch (IOException JavaDoc e)
215             {
216                 logger.warn("Failed to close file output stream on stop: " + e);
217             }
218         }
219     }
220
221     /*
222      * (non-Javadoc)
223      *
224      * @see org.mule.providers.UMOConnector#getProtocol()
225      */

226     public String JavaDoc getProtocol()
227     {
228         return "file";
229     }
230
231     public FilenameParser getFilenameParser()
232     {
233         return filenameParser;
234     }
235
236     public void setFilenameParser(FilenameParser filenameParser)
237     {
238         this.filenameParser = filenameParser;
239     }
240
241     /*
242      * (non-Javadoc)
243      *
244      * @see org.mule.providers.AbstractConnector#doDispose()
245      */

246     protected void doDispose()
247     {
248         try
249         {
250             doStop();
251         }
252         catch (UMOException e)
253         {
254             logger.error(e.getMessage(), e);
255         }
256     }
257
258     /**
259      * @return Returns the moveToDirectoryName.
260      */

261     public String JavaDoc getMoveToDirectory()
262     {
263         return moveToDirectoryName;
264     }
265
266     /**
267      * @param dir The moveToDirectoryName to set.
268      */

269     public void setMoveToDirectory(String JavaDoc dir)
270     {
271         this.moveToDirectoryName = dir;
272     }
273
274     /**
275      * @return Returns the outputAppend.
276      */

277     public boolean isOutputAppend()
278     {
279         return outputAppend;
280     }
281
282     /**
283      * @param outputAppend The outputAppend to set.
284      */

285     public void setOutputAppend(boolean outputAppend)
286     {
287         this.outputAppend = outputAppend;
288     }
289
290     /**
291      * @return Returns the outputPattern.
292      */

293     public String JavaDoc getOutputPattern()
294     {
295         return outputPattern;
296     }
297
298     /**
299      * @param outputPattern The outputPattern to set.
300      */

301     public void setOutputPattern(String JavaDoc outputPattern)
302     {
303         this.outputPattern = outputPattern;
304     }
305
306     /**
307      * @return Returns the outputStream.
308      */

309     public FileOutputStream JavaDoc getOutputStream()
310     {
311         return outputStream;
312     }
313
314     /**
315      * @param outputStream The outputStream to set.
316      */

317     public void setOutputStream(FileOutputStream JavaDoc outputStream)
318     {
319         this.outputStream = outputStream;
320     }
321
322     /**
323      * @return Returns the pollingFrequency.
324      */

325     public long getPollingFrequency()
326     {
327         return pollingFrequency;
328     }
329
330     /**
331      * @param pollingFrequency The pollingFrequency to set.
332      */

333     public void setPollingFrequency(long pollingFrequency)
334     {
335         this.pollingFrequency = pollingFrequency;
336     }
337
338     /**
339      * @return Returns the fileAge.
340      */

341     public long getFileAge()
342     {
343         return fileAge;
344     }
345
346     public boolean getCheckFileAge()
347     {
348         return checkFileAge;
349     }
350
351     /**
352      * @param fileAge The fileAge in seconds to set.
353      */

354     public void setFileAge(long fileAge)
355     {
356         this.fileAge = fileAge;
357         this.checkFileAge = true;
358     }
359
360     /**
361      * @return Returns the writeToDirectory.
362      */

363     public String JavaDoc getWriteToDirectory()
364     {
365         return writeToDirectoryName;
366     }
367
368     /**
369      * @param dir The writeToDirectory to set.
370      */

371     public void setWriteToDirectory(String JavaDoc dir) throws IOException JavaDoc
372     {
373         this.writeToDirectoryName = dir;
374         if (writeToDirectoryName != null)
375         {
376             File JavaDoc writeToDirectory = FileUtils.openDirectory((writeToDirectoryName));
377             if (!(writeToDirectory.canRead()) || !writeToDirectory.canWrite())
378             {
379                 throw new IOException JavaDoc(
380                     "Error on initialization, Write To directory does not exist or is not read/write");
381             }
382         }
383     }
384
385     public boolean isSerialiseObjects()
386     {
387         return serialiseObjects;
388     }
389
390     public void setSerialiseObjects(boolean serialiseObjects)
391     {
392         // set serialisable transformers on the connector if this is set
393
if (serialiseObjects)
394         {
395             if (serviceOverrides == null)
396             {
397                 serviceOverrides = new Properties();
398             }
399             serviceOverrides.setProperty(MuleProperties.CONNECTOR_INBOUND_TRANSFORMER,
400                 ByteArrayToSerializable.class.getName());
401             serviceOverrides.setProperty(MuleProperties.CONNECTOR_OUTBOUND_TRANSFORMER,
402                 SerializableToByteArray.class.getName());
403         }
404
405         this.serialiseObjects = serialiseObjects;
406     }
407
408     public boolean isAutoDelete()
409     {
410         return autoDelete;
411     }
412
413     public void setAutoDelete(boolean autoDelete)
414     {
415         this.autoDelete = autoDelete;
416         if (!autoDelete)
417         {
418             if (serviceOverrides == null)
419             {
420                 serviceOverrides = new Properties();
421             }
422             if (serviceOverrides.getProperty(MuleProperties.CONNECTOR_MESSAGE_ADAPTER) == null)
423             {
424                 serviceOverrides.setProperty(MuleProperties.CONNECTOR_MESSAGE_ADAPTER,
425                     FileMessageAdapter.class.getName());
426             }
427         }
428     }
429
430     public String JavaDoc getMoveToPattern()
431     {
432         return moveToPattern;
433     }
434
435     public void setMoveToPattern(String JavaDoc moveToPattern)
436     {
437         this.moveToPattern = moveToPattern;
438     }
439 }
440
Popular Tags