001: /*
002: * ChainBuilder ESB
003: * Visual Enterprise Integration
004: *
005: * Copyright (C) 2006 Bostech Corporation
006: *
007: * This program is free software; you can redistribute it and/or modify it
008: * under the terms of the GNU General Public License as published by the
009: * Free Software Foundation; either version 2 of the License, or (at your option)
010: * any later version.
011: *
012: * This program is distributed in the hope that it will be useful,
013: * but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
014: * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
015: * for more details.
016: *
017: * You should have received a copy of the GNU General Public License along with
018: * this program; if not, write to the Free Software Foundation, Inc.,
019: * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
020: *
021: *
022: * $Id: FileEndpoint.java 9746 2007-10-26 11:05:03Z lzheng $
023: */
024: package com.bostechcorp.cbesb.runtime.component.file;
025:
026: import java.util.ArrayList;
027: import java.util.List;
028:
029: import javax.jbi.messaging.MessageExchange.Role;
030:
031: import org.apache.commons.logging.Log;
032: import org.apache.commons.logging.LogFactory;
033:
034: import com.bostechcorp.cbesb.runtime.ccsl.jbi.messaging.CbEmbeddedSchedulerConsumerProcessor;
035: import com.bostechcorp.cbesb.runtime.ccsl.jbi.messaging.IComponentProcessor;
036: import com.bostechcorp.cbesb.runtime.ccsl.jbi.messaging.ScheduledEndpointProcessor;
037: import com.bostechcorp.cbesb.runtime.component.file.processors.FileConsumerHandler;
038: import com.bostechcorp.cbesb.runtime.component.file.processors.FileProviderProcessor;
039:
040: public class FileEndpoint extends ScheduledEndpointProcessor {
041:
042: protected final transient Log logger = LogFactory
043: .getLog(getClass());
044:
045: /**
046: * Source location of data files.
047: */
048: private String sourceDir;
049:
050: /**
051: * For reader: Staging directory where files are moved for processing.<p>
052: * For writer: Staging directory where files are created and written to.
053: */
054: private String stageDir;
055:
056: /**
057: * Archive location to place files when done processing.
058: */
059: private String archiveDir;
060:
061: /**
062: * Hold location to place files when an error occurs during processing.
063: */
064: private String holdDir;
065:
066: /**
067: * Value in milliseconds that determines how often the source directory is scanned for
068: * new data files.
069: */
070: private long scanInterval = 5000L;
071:
072: /**
073: * Boolean value. Ture means that if an error occurs while processing a file, the file
074: * will be moved to the Hold directory.
075: */
076: private boolean hold = false;
077:
078: /**
079: * For reader: Glob style file pattern to determine which files in the Source
080: * directory will be processed.<p>
081: * For writer: Describes a file pattern to use to name the file when being written.
082: */
083: private String filePattern = "*";
084:
085: private String matchMode = "";
086:
087: /**
088: * Boolean value. Two pass mode causes the component to check the size of the files
089: * in the Source directory, wait for a set interval and check the sizes again.
090: */
091: private boolean twoPass = false;
092:
093: /**
094: * Value in milliseconds to wait between scans during Two Pass mode.
095: */
096: private long twoPassInterval = 2000L;
097:
098: /**
099: * Value determines what to do with the file after all data is read from it.
100: */
101: private String fileCompleteAction = "delete";
102:
103: /**
104: * Integer value that determines the number of records from a file will be placed in
105: * an individual Normalized Message.
106: */
107: private int recordsPerMessage = 0;
108:
109: /**
110: * Value determines how to read a record from the file.
111: */
112: private String readStyle = "raw";
113:
114: /**
115: * Value determines the type of data each record contains.
116: */
117: private String recordType = "string";
118:
119: /**
120: * For reader: Value is the name of the charset to use to read in character data.<p>
121: * For writer: Value is the name of the charset to use to write character data to the
122: * file.
123: */
124: private String charset;
125:
126: /**
127: * Describes a file pattern to use to rename the file when being archived.
128: */
129: private String archiveFilePattern = null;
130:
131: /**
132: * Directory where the out message is written
133: */
134: private String replyDir;
135:
136: /**
137: * Value is the name of the charset to use to write character data to the file.
138: */
139: private String replyCharset;
140:
141: /**
142: * Value determines how records are written to a file. Acceptable values are:
143: * raw - Each record from a Normalized Message is written to an individual file.
144: * newline - Each record from a Normalized Message is written to the same file separated by a newline.
145: */
146: private String replyWriteStyle = "raw";
147:
148: /**
149: * Describes a file pattern to use to name the file when being written. This can be used to add a date/time stamp to the file. The pattern may contain literal characters as well as the following macros that will be replaced with values at runtime:
150: * {DATE} - The system date formatted as yyyymmdd
151: * {TIME} - The system time formatted as hhmmss
152: * {BASENAME} - The original file's base name (name without extension).
153: * {EXT} - The original file's extension.
154: * {COUNT} - An automatically incremented value that starts from 1 when the component is started.
155: */
156: private String replyFilePattern;
157:
158: /**
159: * Destination location for data files.
160: */
161: private String destDir;
162:
163: /**
164: * Value determines how records are written to a file.
165: */
166: private String writeStyle = "raw";
167:
168: public FileEndpoint() {
169: handler = new FileConsumerHandler(this );
170: }
171:
172: /**
173: * Get the sourceDir.
174: *
175: * @return the sourceDir
176: */
177: public String getSourceDir() {
178: return sourceDir;
179: }
180:
181: /**
182: * Set the sourceDir.
183: *
184: * @param sourceDir the sourceDir to set
185: */
186: public void setSourceDir(String sourceDir) {
187: this .sourceDir = sourceDir;
188: }
189:
190: /**
191: * Get the stageDir.
192: *
193: * @return the stageDir
194: */
195: public String getStageDir() {
196: return stageDir;
197: }
198:
199: /**
200: * Set the stageDir.
201: *
202: * @param stageDir the stageDir to set
203: */
204: public void setStageDir(String stageDir) {
205: this .stageDir = stageDir;
206: }
207:
208: /**
209: * Get the archiveDir.
210: *
211: * @return the archiveDir
212: */
213: public String getArchiveDir() {
214: return archiveDir;
215: }
216:
217: /**
218: * Set the archiveDir.
219: *
220: * @param archiveDir the archiveDir to set
221: */
222: public void setArchiveDir(String archiveDir) {
223: this .archiveDir = archiveDir;
224: }
225:
226: /**
227: * Get the holdDir.
228: *
229: * @return the holdDir
230: */
231: public String getHoldDir() {
232: return holdDir;
233: }
234:
235: /**
236: * Set the holdDir.
237: *
238: * @param holdDir the holdDir to set
239: */
240: public void setHoldDir(String holdDir) {
241: this .holdDir = holdDir;
242: }
243:
244: /**
245: * Get the scanInterval.
246: *
247: * @return the scanInterval
248: */
249: public long getScanInterval() {
250: return scanInterval;
251: }
252:
253: /**
254: * Set the scanInterval.
255: *
256: * @param scanInterval the scanInterval to set
257: */
258: public void setScanInterval(long scanInterval) {
259: this .scanInterval = scanInterval;
260: }
261:
262: /**
263: * Set the scanInterval.
264: *
265: * @param scanInterval the scanInterval to set
266: */
267: public void setScanInterval(String scanInterval) {
268: if (scanInterval != null && !"".equals(scanInterval)) {
269: this .scanInterval = Integer.parseInt(scanInterval);
270: }
271: }
272:
273: /**
274: * Get the hold.
275: *
276: * @return the hold
277: */
278: public boolean isHold() {
279: return hold;
280: }
281:
282: /**
283: * Set the hold.
284: *
285: * @param hold the hold to set
286: */
287: public void setHold(boolean hold) {
288: this .hold = hold;
289: }
290:
291: /**
292: * Set the hold.
293: *
294: * @param hold the hold to set
295: */
296: public void setHold(String hold) {
297: if (hold != null && hold.compareToIgnoreCase("true") == 0) {
298: this .hold = true;
299: }
300: }
301:
302: /**
303: * Get the filePattern.
304: *
305: * @return the filePattern
306: */
307: public String getFilePattern() {
308: return filePattern;
309: }
310:
311: /**
312: * Set the filePattern.
313: *
314: * @param filePattern the filePattern to set
315: */
316: public void setFilePattern(String filePattern) {
317: if (filePattern != null && !"".equals(filePattern)) {
318: this .filePattern = filePattern;
319: }
320: }
321:
322: public String getMatchMode() {
323: return matchMode;
324: }
325:
326: public void setMatchMode(String matchMode) {
327: this .matchMode = matchMode;
328: }
329:
330: /**
331: * Get the twoPass.
332: *
333: * @return the twoPass
334: */
335: public boolean isTwoPass() {
336: return twoPass;
337: }
338:
339: /**
340: * Set the twoPass.
341: *
342: * @param twoPass the twoPass to set
343: */
344: public void setTwoPass(boolean twoPass) {
345: this .twoPass = twoPass;
346: }
347:
348: /**
349: * Set the twoPass.
350: *
351: * @param twoPass the twoPass to set
352: */
353: public void setTwoPass(String twoPass) {
354: if (twoPass != null && twoPass.compareToIgnoreCase("true") == 0) {
355: this .twoPass = true;
356: }
357: }
358:
359: /**
360: * Get the twoPassInterval.
361: *
362: * @return the twoPassInterval
363: */
364: public long getTwoPassInterval() {
365: return twoPassInterval;
366: }
367:
368: /**
369: * Set the twoPassInterval.
370: *
371: * @param twoPassInterval the twoPassInterval to set
372: */
373: public void setTwoPassInterval(long twoPassInterval) {
374: this .twoPassInterval = twoPassInterval;
375: }
376:
377: /**
378: * Set the twoPassInterval.
379: *
380: * @param twoPassInterval the twoPassInterval to set
381: */
382: public void setTwoPassInterval(String twoPassInterval) {
383: if (twoPassInterval != null && !"".equals(twoPassInterval)) {
384: this .twoPassInterval = Integer.parseInt(twoPassInterval);
385: }
386: }
387:
388: /**
389: * Get the fileCompleteAction.
390: *
391: * @return the fileCompleteAction
392: */
393: public String getFileCompleteAction() {
394: return fileCompleteAction;
395: }
396:
397: /**
398: * Set the fileCompleteAction.
399: *
400: * @param fileCompleteAction the fileCompleteAction to set
401: */
402: public void setFileCompleteAction(String fileCompleteAction) {
403: if (fileCompleteAction != null
404: && !"".equals(fileCompleteAction)) {
405: this .fileCompleteAction = fileCompleteAction;
406: }
407: }
408:
409: /**
410: * Get the recordsPerMessage.
411: *
412: * @return the recordsPerMessage
413: */
414: public int getRecordsPerMessage() {
415: return recordsPerMessage;
416: }
417:
418: /**
419: * Set the recordsPerMessage.
420: *
421: * @param recordsPerMessage the recordsPerMessage to set
422: */
423: public void setRecordsPerMessage(int recordsPerMessage) {
424: this .recordsPerMessage = recordsPerMessage;
425: }
426:
427: /**
428: * Set the recordsPerMessage.
429: *
430: * @param recordsPerMessage the recordsPerMessage to set
431: */
432: public void setRecordsPerMessage(String recordsPerMessage) {
433: if (recordsPerMessage != null && !"".equals(recordsPerMessage)) {
434: this .recordsPerMessage = Integer
435: .parseInt(recordsPerMessage);
436: }
437: }
438:
439: /**
440: * Get the readStyle.
441: *
442: * @return the readStyle
443: */
444: public String getReadStyle() {
445: return readStyle;
446: }
447:
448: /**
449: * Set the readStyle.
450: *
451: * @param readStyle the readStyle to set
452: */
453: public void setReadStyle(String readStyle) {
454: if (readStyle != null && !"".equals(readStyle)) {
455: this .readStyle = readStyle;
456: }
457: }
458:
459: /**
460: * Get the recordType.
461: *
462: * @return the recordType
463: */
464: public String getRecordType() {
465: return recordType;
466: }
467:
468: /**
469: * Set the recordType.
470: *
471: * @param recordType the recordType to set
472: */
473: public void setRecordType(String recordType) {
474: if (recordType != null & !"".equals(recordType)) {
475: this .recordType = recordType;
476: }
477: }
478:
479: /**
480: * Get the charset.
481: *
482: * @return the charset
483: */
484: public String getCharset() {
485: return charset;
486: }
487:
488: /**
489: * Set the charset.
490: *
491: * @param charset the charset to set
492: */
493: public void setCharset(String charset) {
494: this .charset = charset;
495: }
496:
497: /**
498: * Get the archiveFilePattern.
499: *
500: * @return the archiveFilePattern
501: */
502: public String getArchiveFilePattern() {
503: return archiveFilePattern;
504: }
505:
506: /**
507: * Set the archiveFilePattern.
508: *
509: * @param archiveFilePattern the archiveFilePattern to set
510: */
511: public void setArchiveFilePattern(String archiveFilePattern) {
512: if (!"".equals(archiveFilePattern)) {
513: this .archiveFilePattern = archiveFilePattern;
514: }
515: }
516:
517: /**
518: * Get the destDir.
519: *
520: * @return the destDir
521: */
522: public String getDestDir() {
523: return destDir;
524: }
525:
526: /**
527: * Set the destDir.
528: *
529: * @param destDir the destDir to set
530: */
531: public void setDestDir(String destDir) {
532: this .destDir = destDir;
533: }
534:
535: /**
536: * Get the writeStyle.
537: *
538: * @return the writeStyle
539: */
540: public String getWriteStyle() {
541: return writeStyle;
542: }
543:
544: /**
545: * Set the writeStyle.
546: *
547: * @param writeStyle the writeStyle to set
548: */
549: public void setWriteStyle(String writeStyle) {
550: this .writeStyle = writeStyle;
551: }
552:
553: /**
554: * @return the replyCharset
555: */
556: public String getReplyCharset() {
557: return replyCharset;
558: }
559:
560: /**
561: * @param replyCharset the replyCharset to set
562: */
563: public void setReplyCharset(String replyCharset) {
564: this .replyCharset = replyCharset;
565: }
566:
567: /**
568: * @return the replyDir
569: */
570: public String getReplyDir() {
571: return replyDir;
572: }
573:
574: /**
575: * @param replyDir the replyDir to set
576: */
577: public void setReplyDir(String replyDir) {
578: this .replyDir = replyDir;
579: }
580:
581: /**
582: * @return the replyFilePattern
583: */
584: public String getReplyFilePattern() {
585: return replyFilePattern;
586: }
587:
588: /**
589: * @param replyFilePattern the replyFilePattern to set
590: */
591: public void setReplyFilePattern(String replyFilePattern) {
592: this .replyFilePattern = replyFilePattern;
593: }
594:
595: /**
596: * @return the replyWriteStyle
597: */
598: public String getReplyWriteStyle() {
599: return replyWriteStyle;
600: }
601:
602: /**
603: * @param replyWriteStyle the replyWriteStyle to set
604: */
605: public void setReplyWriteStyle(String replyWriteStyle) {
606: if (replyWriteStyle != null && !"".equals(replyWriteStyle)) {
607: this .replyWriteStyle = replyWriteStyle;
608: }
609: }
610:
611: /* (non-Javadoc)
612: * @see com.bostechcorp.cbesb.runtime.ccsl.jbi.messaging.BaseEndpoint#createProviderProcessor()
613: */
614: protected IComponentProcessor createProviderProcessor() {
615: //This code was used with the separate Scheduler Component and base classes
616: //It is not used with the embedded scheduler
617: //
618: // if (this.mode == ScheduledEndpointProcessor.MODE_POLL) {
619: // //Poll mode
620: // return new FileProviderProcessor(this);
621: // } else {
622: // //Trigger mode
623: // return new CbScheduledProviderProcessor(this);
624: // }
625:
626: //This is used with the embedded scheduler
627: return new FileProviderProcessor(this );
628: }
629:
630: /* (non-Javadoc)
631: * @see com.bostechcorp.cbesb.runtime.ccsl.jbi.messaging.BaseEndpoint#createConsumerProcessor()
632: */
633: protected IComponentProcessor createConsumerProcessor() {
634: // This code was used with the separate Scheduler Component and base classes
635: // It is not used with the embedded scheduler
636: // return new CbScheduledConsumerProcessor(this);
637: return new CbEmbeddedSchedulerConsumerProcessor(this );
638:
639: }
640:
641: /**********************************************************************************
642: * These attributes and methods customize the LifeCycleEndpoint for this component
643: ***********************************************************************************/
644:
645: /*
646: * The display parameters are general information for the admin console to display.
647: */
648: public String[] getDisplayParameterTitles() {
649: return new String[] { "Directory", "StageDir",
650: FilePropertiesEnumeration.FILE_PATTERN.name(),
651: FilePropertiesEnumeration.CHARSET.name(),
652: FilePropertiesEnumeration.RECORD_TYPE.name() };
653:
654: }
655:
656: /*
657: * The values returned here correspond to the titles above.
658: */
659: public String[] getDisplayParameters() {
660: if (this .getRole().equals(Role.CONSUMER)) {
661: return new String[] {
662: FilePropertiesEnumeration.SOURCE_DIRECTORY
663: .getValue(this ),
664: FilePropertiesEnumeration.STAGE_DIRECTORY
665: .getValue(this ),
666: FilePropertiesEnumeration.FILE_PATTERN
667: .getValue(this ),
668: FilePropertiesEnumeration.CHARSET.getValue(this ),
669: FilePropertiesEnumeration.RECORD_TYPE
670: .getValue(this ) };
671:
672: } else {
673: return new String[] {
674: FilePropertiesEnumeration.DESTINATION_DIRECTORY
675: .getValue(this ),
676: FilePropertiesEnumeration.STAGE_DIRECTORY
677: .getValue(this ),
678: FilePropertiesEnumeration.FILE_PATTERN
679: .getValue(this ),
680: FilePropertiesEnumeration.CHARSET.getValue(this ),
681: FilePropertiesEnumeration.RECORD_TYPE
682: .getValue(this ) };
683:
684: }
685: }
686:
687: /*
688: * This returns a list of properties that can be read.
689: */
690: public String[] getGetableProperties() {
691: FilePropertiesEnumeration[] fps = FilePropertiesEnumeration
692: .values();
693: List<String> result = new ArrayList<String>();
694: for (int i = 0; i < fps.length; i++) {
695: result.add(fps[i].name());
696: }
697: String[] sr = new String[result.size()];
698: return result.toArray(sr);
699: }
700:
701: /*
702: * This gets one property from the list above.
703: */
704: public String getProperty(int index) {
705: return FilePropertiesEnumeration.values()[index].getValue(this );
706: }
707:
708: /*
709: * This gets one property from the list above.
710: */
711: public String getProperty(String property) {
712: return FilePropertiesEnumeration.valueOf(property).getValue(
713: this );
714: }
715:
716: /*
717: * This returns a list of properties that can be set.
718: */
719: public String[] getSetableProperties() {
720: FilePropertiesEnumeration[] fps = FilePropertiesEnumeration
721: .values();
722: List<String> result = new ArrayList<String>();
723: for (int i = 0; i < fps.length; i++) {
724: if (fps[i].isSetable())
725: result.add(fps[i].name());
726: }
727: String[] sr = new String[result.size()];
728: return result.toArray(sr);
729: }
730:
731: /*
732: * This sets one property from the list above.
733: */
734: public void setProperty(int index, String value) {
735: FilePropertiesEnumeration.values()[index].setValue(this , value);
736: }
737:
738: /*
739: * This sets one property from the list above.
740: */
741: public void setProperty(String property, String value) {
742: FilePropertiesEnumeration.valueOf(property).setValue(this ,
743: value);
744: }
745:
746: /**************************************************
747: * Done with LifeCycleEndpoint methods
748: ***************************************************/
749:
750: }
|