1 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 ; 31 import java.io.FileOutputStream ; 32 import java.io.IOException ; 33 import java.util.Map ; 34 import java.util.Properties ; 35 36 41 42 public class FileConnector extends AbstractServiceEnabledConnector 43 { 44 47 private static Log logger = LogFactory.getLog(FileConnector.class); 48 49 public static final String PROPERTY_POLLING_FREQUENCY = "pollingFrequency"; 52 public static final String PROPERTY_FILE_AGE = "fileAge"; 53 public static final String PROPERTY_FILENAME = "filename"; 54 public static final String PROPERTY_ORIGINAL_FILENAME = "originalFilename"; 55 public static final String PROPERTY_OUTPUT_PATTERN = "outputPattern"; 56 public static final String PROPERTY_MOVE_TO_PATTERN = "moveToPattern"; 57 public static final String PROPERTY_MOVE_TO_DIRECTORY = "moveToDirectory"; 58 public static final String PROPERTY_DELETE_ON_READ = "autoDelete"; 59 public static final String PROPERTY_DIRECTORY = "directory"; 60 public static final String PROPERTY_SERVICE_OVERRIDE = "serviceOverrides"; 61 public static final String PROPERTY_WRITE_TO_DIRECTORY = "writeToDirectoryName"; 62 63 public static final long DEFAULT_POLLING_FREQUENCY = 1000; 64 65 68 private long pollingFrequency = 0; 69 70 private String moveToPattern = null; 71 72 private String writeToDirectoryName = null; 73 74 private String moveToDirectoryName = null; 75 76 private String 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 outputStream = null; 87 88 private boolean serialiseObjects = false; 89 90 public FilenameParser filenameParser; 91 92 97 public FileConnector() 98 { 99 filenameParser = new SimpleFilenameParser(); 100 } 101 102 protected Object 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 122 public UMOMessageReceiver createReceiver(UMOComponent component, UMOEndpoint endpoint) throws Exception 123 { 124 String readDir = endpoint.getEndpointURI().getAddress(); 125 long polling = this.pollingFrequency; 126 127 String moveTo = moveToDirectoryName; 128 String moveToPattern = getMoveToPattern(); 129 130 Map props = endpoint.getProperties(); 131 if (props != null) 132 { 133 String move = (String )props.get(PROPERTY_MOVE_TO_DIRECTORY); 135 if (move != null) 136 { 137 moveTo = move; 138 } 139 String tempMoveToPattern = (String )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 tempPolling = (String )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 tempFileAge = (String )props.get(PROPERTY_FILE_AGE); 165 if (tempFileAge != null) 166 { 167 try 168 { 169 setFileAge(Long.parseLong(tempFileAge)); 170 } 171 catch (Exception ex1) 172 { 173 logger.error("Failed to set fileAge", ex1); 174 } 175 } 176 Map srvOverride = (Map ) 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 []{readDir, 191 moveTo, moveToPattern, new Long (polling)}); 192 193 } 194 catch (Exception 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 206 protected synchronized void doStop() throws UMOException 207 { 208 if (outputStream != null) 209 { 210 try 211 { 212 outputStream.close(); 213 } 214 catch (IOException e) 215 { 216 logger.warn("Failed to close file output stream on stop: " + e); 217 } 218 } 219 } 220 221 226 public String 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 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 261 public String getMoveToDirectory() 262 { 263 return moveToDirectoryName; 264 } 265 266 269 public void setMoveToDirectory(String dir) 270 { 271 this.moveToDirectoryName = dir; 272 } 273 274 277 public boolean isOutputAppend() 278 { 279 return outputAppend; 280 } 281 282 285 public void setOutputAppend(boolean outputAppend) 286 { 287 this.outputAppend = outputAppend; 288 } 289 290 293 public String getOutputPattern() 294 { 295 return outputPattern; 296 } 297 298 301 public void setOutputPattern(String outputPattern) 302 { 303 this.outputPattern = outputPattern; 304 } 305 306 309 public FileOutputStream getOutputStream() 310 { 311 return outputStream; 312 } 313 314 317 public void setOutputStream(FileOutputStream outputStream) 318 { 319 this.outputStream = outputStream; 320 } 321 322 325 public long getPollingFrequency() 326 { 327 return pollingFrequency; 328 } 329 330 333 public void setPollingFrequency(long pollingFrequency) 334 { 335 this.pollingFrequency = pollingFrequency; 336 } 337 338 341 public long getFileAge() 342 { 343 return fileAge; 344 } 345 346 public boolean getCheckFileAge() 347 { 348 return checkFileAge; 349 } 350 351 354 public void setFileAge(long fileAge) 355 { 356 this.fileAge = fileAge; 357 this.checkFileAge = true; 358 } 359 360 363 public String getWriteToDirectory() 364 { 365 return writeToDirectoryName; 366 } 367 368 371 public void setWriteToDirectory(String dir) throws IOException 372 { 373 this.writeToDirectoryName = dir; 374 if (writeToDirectoryName != null) 375 { 376 File writeToDirectory = FileUtils.openDirectory((writeToDirectoryName)); 377 if (!(writeToDirectory.canRead()) || !writeToDirectory.canWrite()) 378 { 379 throw new IOException ( 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 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 getMoveToPattern() 431 { 432 return moveToPattern; 433 } 434 435 public void setMoveToPattern(String moveToPattern) 436 { 437 this.moveToPattern = moveToPattern; 438 } 439 } 440 | Popular Tags |