View Javadoc
1 /* ====================================================================
2 * License:
3 *
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
6 * are met:
7 *
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 *
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in
13 * the documentation and/or other materials provided with the
14 * distribution.
15 *
16 * 3. The end-user documentation included with the redistribution,
17 * if any, must include the following acknowledgment:
18 * "This product includes software developed by
19 * Robert Half International (http://www.rhi.com/)."
20 * Alternately, this acknowledgment may appear in the software itself,
21 * if and wherever such third-party acknowledgments normally appear.
22 *
23 * 4. The names "Parc", "RHI", and "Robert Half International" must
24 * not be used to endorse or promote products derived from this
25 * software without prior written permission. For written
26 * permission, please contact pete.mckinstry@rhi.com.
27 *
28 * 5. Products derived from this software may not be called "PARC",
29 * nor may "PARC" appear in their name, without prior written
30 * permission of Robert Half International.
31 *
32 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
33 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
34 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
35 * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
36 * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
37 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
38 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
39 * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
40 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
41 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
42 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
43 * SUCH DAMAGE.
44 * ====================================================================
45 *
46 */
47 package com.rhi.architecture.parc.adapter.file;
48
49 import java.io.BufferedReader;
50 import java.io.FileNotFoundException;
51 import java.io.FileReader;
52 import java.util.Properties;
53
54 import com.rhi.architecture.logging.LogUtil;
55 import com.rhi.architecture.logging.Logger;
56 import com.rhi.architecture.parc.PARCConfiguration;
57 import com.rhi.architecture.resource.InitializationException;
58
59 /***
60 * Generic Flat File InputAdapter.
61 * The basic function of this flat file input adapter is to facilitate a reading of
62 * a flat file in the batches, instead of reading a whole file in a single fetch.
63 *
64 * @author Sandeep Sahai
65 * @copyright 2002, Robert Half Int'l., Inc. All rights reserved.
66 *
67 * @since 1.0
68 *
69 * MOD HISTORY
70 * DATE USER COMMENTS
71 * 06/20/2002 Gaurang Patel 1) Changed the variable names according to the coding standard.
72 * 2) Imports of classes are changed.
73 * 3) removed unused variables and unwanted code.
74 * 4) Changed the java docs.
75 * 07/08/2002 Gaurang Patel 1) The file information hardcoding is removed.
76 * 2) The keys of batchsize and delimiter has been changed according
77 * to the standard.
78 */
79
80 public abstract class FlatFileInputAdapter extends AbstractFileInputAdapter {
81
82 private static Logger log = LogUtil.getLogger( FlatFileInputAdapter.class );
83
84 /*
85 * Holds the name of the file to be read.
86 */
87 private String fileName;
88
89 /*
90 * Reader is initialized in the init() method and is kept open for loadBatch()
91 * calls and then closed in cleanup(). This facilitates batching of input.
92 */
93 private BufferedReader reader;
94
95 /***
96 * Default Constructor
97 *
98 * @since 1.0
99 */
100 public FlatFileInputAdapter() {
101 // no-op
102 }
103
104 public void init( Properties props ) throws InitializationException {
105 super.init( props );
106 initFileName( props );
107 initFileReader( props );
108 }
109
110 /***
111 * Provides name of the file to which this input adapter instance is tied.
112 * @return file name
113 */
114 public String getFileName() {
115 return fileName;
116 }
117
118 /***
119 * Provides reader created during init()
120 * @see com.rhi.architecture.parc.adapter.file.AbstractFileInputAdapter#getFileReader()
121 */
122 public BufferedReader getFileReader() {
123 return reader;
124 }
125
126 /***
127 * Prepares file name from the input parameters supplied from properties.
128 * Refactored from init() into a method of its own so that derived classes
129 * can still reuse most of the functionality provided by this adapter and
130 * selectively change only the logic to pickup file for processing.
131 *
132 * @param props
133 * @return
134 * @throws InitializationException
135 */
136 private void initFileName( Properties props ) throws InitializationException {
137 fileName = props.getProperty( PARCConfiguration.INPUT_FILE_NAME );
138 if ( fileName != null ) {
139 return;
140 }
141
142 String path = props.getProperty( PARCConfiguration.INPUT_FILE_PATH1 );
143 if ( path == null ) {
144 throw new InitializationException(
145 "FlatFileInputAdapter Initialization error. "
146 + PARCConfiguration.INPUT_FILE_PATH1
147 + " File Path not found.");
148 }
149
150 String name = props.getProperty( PARCConfiguration.INPUT_FILE_NAME1 );
151 if ( name == null ) {
152 throw new InitializationException(
153 "FlatFileInputAdapter Initialization error. "
154 + PARCConfiguration.INPUT_FILE_NAME1
155 + " File Name not found.");
156 }
157
158 fileName = path + name;
159 log.info( "intiFileName() : full file name read = " + fileName );
160 }
161
162 /***
163 * Creates file reader to be passed to super class.
164 * @param props
165 */
166 private void initFileReader( Properties props ) throws InitializationException {
167 try {
168 reader = new BufferedReader( new FileReader( fileName ) );
169 }
170 catch (FileNotFoundException exFileNotFound) {
171 log.error(
172 "Application is not able to read file : '"
173 + fileName
174 + "' ");
175 throw new InitializationException(
176 "Application is not able to read file : '" + fileName + "' ",
177 exFileNotFound);
178 }
179 }
180
181 } // End of File
This page was automatically generated by Maven