1 package org.webdocwf.util.loader;
2
3
4 /***
5 EchoElement - Element in parallel to importDefinitions and sql's.
6 The message is to be printed in the log output (screen and logfile) in
7 logmode normal and logmode full.
8
9 Copyright (C) 2002-2003 Together
10
11 This library is free software; you can redistribute it and/or
12 modify it under the terms of the GNU Lesser General Public
13 License as published by the Free Software Foundation; either
14 version 2.1 of the License, or (at your option) any later version.
15
16 This library is distributed in the hope that it will be useful,
17 but WITHOUT ANY WARRANTY; without even the implied warranty of
18 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
19 Lesser General Public License for more details.
20
21 You should have received a copy of the GNU Lesser General Public
22 License along with this library; if not, write to the Free Software
23 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
24
25 EchoElement.java
26 Date: 20.2.2003.
27 @version 1.0.0
28 @author: Milosevic Sinisa sinisa@prozone.co.yu
29 */
30
31 import java.io.File;
32 import java.util.Calendar;
33 import java.util.Date;
34 import java.io.RandomAccessFile;
35 import java.io.FileNotFoundException;
36 import java.util.Hashtable;
37
38 /***
39 EchoElement - Element in parallel to importDefinitions and sql's.
40 The message is to be printed in the log output (screen and logfile) in
41 logmode normal and logmode full.
42 */
43 public class EchoElement {
44
45 public final static String LOGMODE_NORMAL="normal";
46 public final static String LOGMODE_NONE="none";
47 public final static String LOGMODE_FULL="full";
48
49 private String logMode=LOGMODE_NORMAL;
50 private String echoMessage = "";
51 private File logFile;
52 private Hashtable messages= new Hashtable();
53
54
55 /***
56 * Public constructor of EchoElement class.
57 */
58 public EchoElement() {
59 }
60
61 /***
62 * Public constructor of EchoElement class.
63 * Constructor set log file.
64 * @param File log file.
65 */
66 public EchoElement(File output) {
67 this.logFile=output;
68 }
69
70 /***
71 * Public constructor of EchoElement class.
72 * Constructor set log mode value.
73 * @param String log mode.
74 */
75 public EchoElement(String mode) {
76 this.logMode=mode;
77 }
78
79 /***
80 * Public constructor of EchoElement class.
81 * Constructor set log mode and log file.
82 * @param String log mode.
83 * @param File cache log file.
84 */
85 public EchoElement(String mode, File output) {
86 this.logMode=mode;
87 this.logFile=output;
88 }
89
90 /***
91 * Read logMode
92 * @return String mode value
93 */
94 public String getLogMode() {
95 return this.logMode;
96 }
97
98 /***
99 * Set log mode.
100 * @param String mode value
101 */
102 public void setLogMode(String mode) {
103 this.logMode=mode;
104 }
105
106 /***
107 * Read log file
108 * @return File log file
109 */
110 public File getLogFile() {
111 return this.logFile;
112 }
113
114 /***
115 * Set Log file.
116 * @param File log file
117 */
118
119 public void setLogFile(File output) {
120 this.logFile=output;
121 }
122
123
124 /***
125 * Metod setMessage is used to put echo message into Hashtable
126 * @param key Key value for messages (e.g number of echo element in import definition.
127 * @param value String message.
128 */
129 public boolean setMessage(String key, String value) {
130 if(value!=null && key!=null) {
131 this.messages.put(key,value);
132 return true;
133 }
134 else
135 return false;
136 }
137
138 /***
139 * Metod getMessage is used to read echo message from Hashtable for key value
140 * @param key Key value for message.
141 * @return value String message.
142 */
143 public String getMessage(String key) {
144 if(key!=null) {
145 return (String)this.messages.get(key);
146 }
147 else
148 return null;
149 }
150
151 /***
152 * Metod writeEcho is used to put strLogTxt into log file and on screen
153 * depending on the logMode. If status param logMode is equal or greater
154 * then the logMode defined in XML, the exportToLOg method will be done.
155 * @param strLogMode Log modes defined in XML file are "none", "normal" or "full"
156 * @param strLogTxt Text for the screen and log.
157 */
158 public boolean writeEcho (String strLogTxt) {
159 if(!this.logMode.equalsIgnoreCase(LOGMODE_NONE)) {
160 writeToLog(LOGMODE_NORMAL,strLogTxt);
161 return true;
162 }
163 else
164 return false;
165 }
166
167 /***
168 * Metod writeToLog is used to put strLogTxt into log file and on screen
169 * depending on the logMode. If status param logMode is equal or greater
170 * then the logMode defined in XML, the exportToLOg method will be done.
171 * @param strLogMode Log modes defined in XML file are "none", "normal" or "full"
172 * @param strLogTxt Text for the screen and log.
173 */
174 public boolean writeToLog (String strLogMode, String strLogTxt) {
175 int iLogModeInt;
176 int iImportDefinitionLogModeInt;
177 if (strLogMode.equalsIgnoreCase(LOGMODE_NONE))
178 iLogModeInt = 1;
179 else if (strLogMode.equalsIgnoreCase(LOGMODE_NORMAL))
180 iLogModeInt = 2;
181 else
182 iLogModeInt = 3;
183 if (this.logMode.equalsIgnoreCase(LOGMODE_NONE))
184 iImportDefinitionLogModeInt = 1;
185 else if (this.logMode.equalsIgnoreCase(LOGMODE_NORMAL))
186 iImportDefinitionLogModeInt = 2;
187 else
188 iImportDefinitionLogModeInt = 3;
189 if (iLogModeInt <= iImportDefinitionLogModeInt) {
190 try {
191 if(this.logFile!=null) {
192 RandomAccessFile fileLogr = new RandomAccessFile(this.logFile, "rw");
193 fileLogr.seek(fileLogr.length());
194 System.out.println(strLogTxt + "\n");
195 fileLogr.writeBytes(strLogTxt + "\n");
196 fileLogr.close();
197 return true;
198 }
199 else
200 return false;
201 } catch (Exception e) {
202 e.printStackTrace();
203 }
204 }
205 else
206 return false;
207 return true;
208 }
209
210
211 /***
212 * Metod createLog is used to create log file. Name of the file include day, month,
213 * year, our and minute of the moment the load method is started. File is created in
214 * Directory LoaderLog that is subdirectory of working directory or directory that is argument of Loader.
215 * @param logDir log directory
216 * @param fileName Name of log file - if value is "default" Loader creates default lo file.
217 */
218 public boolean createLog (String logDir, String fileName) throws LoaderException{
219 String strPi;
220 Calendar calendar = Calendar.getInstance();
221 Date currentDate = new Date();
222 calendar.setTime(currentDate);
223 int year, month, iDate, iDay, hours, minutes, seconds;
224 int y, h, min, s;
225 y = calendar.get(Calendar.YEAR);
226 month = calendar.get(Calendar.MONTH);
227 month = month + 1;
228 String strMonth = null;
229 if(month<10)
230 strMonth="0"+month;
231 else
232 strMonth=""+month;
233
234 String[] months = new String[] {
235 "January", "February", "March", "April", "May", "June", "July",
236 "August", "September", "October", "November", "December"
237 };
238 iDate = calendar.get(Calendar.DAY_OF_MONTH);
239 String strDate="";
240 if(iDate<10)
241 strDate="0"+iDate;
242 else
243 strDate=""+iDate;
244
245 iDay = calendar.get(Calendar.DAY_OF_WEEK) - 1;
246 String[] days = new String[] {
247 "Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday",
248 "Saturday"
249 };
250 hours = calendar.get(Calendar.HOUR_OF_DAY);
251 String strHours="";
252 if(hours<10)
253 strHours="0"+hours;
254 else
255 strHours=""+hours;
256
257 h = hours + 1;
258
259 minutes = calendar.get(Calendar.MINUTE);
260 String strMinutes="";
261 if(minutes<10)
262 strMinutes="0"+minutes;
263 else
264 strMinutes=""+minutes;
265
266 h = hours + 1;
267
268 min = minutes + 1;
269 seconds = calendar.get(Calendar.SECOND);
270 String strSeconds="";
271 if(seconds<10)
272 strSeconds="0"+seconds;
273 else
274 strSeconds=""+seconds;
275
276 s = seconds + 1;
277 if (fileName.equalsIgnoreCase("default"))
278 strPi = "LoaderLog" + y + "-" + strMonth + "-" + strDate
279 + "-" + strHours + "-" + strMinutes + "-" + strSeconds + ".txt";
280 else
281 strPi = fileName;
282 try {
283 File filea = new File(logDir, "LoaderLog");
284 if (!filea.exists())
285 filea.mkdir();
286 filea = new File(filea.getAbsolutePath(), strPi);
287 RandomAccessFile filear = new RandomAccessFile(filea, "rw");
288 this.logFile = filea.getAbsoluteFile();
289 System.out.println("Created: " + strPi);
290 filear.writeBytes("Date: " + days[iDay] + ", " + iDate + ". " +
291 months[month-1] + " " + y + ".\n" + "Time: " + hours + ":"
292 + minutes + ":" + seconds + "\n");
293 filear.close();
294 return true;
295 } catch (FileNotFoundException en) {
296 System.out.println("Wrong name of Log Directory path " + logDir);
297 LoaderException le = new LoaderException("FileNotFoundException: ",
298 (Throwable)en);
299 throw le;
300 } catch (Exception e) {
301 this.writeToLog(LOGMODE_NONE, e.toString());
302 LoaderException le = new LoaderException("Exception: ",
303 (Throwable)e);
304 throw le;
305 }
306 }
307 }
This page automatically generated by Maven