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: ReadErrorTableImpl.java 3829 2006-12-14 21:10:05Z elu $
023: */
024: package com.bostechcorp.cbesb.console.server;
025:
026: import java.io.File;
027: import java.io.FileNotFoundException;
028: import java.io.IOException;
029: import java.io.RandomAccessFile;
030: import java.util.ArrayList;
031: import java.util.List;
032: import java.util.regex.Matcher;
033: import java.util.regex.Pattern;
034:
035: import javax.servlet.http.HttpSession;
036:
037: import com.bostechcorp.cbesb.common.util.EsbPathHelper;
038: import com.bostechcorp.cbesb.console.common.Constants;
039: import com.bostechcorp.cbesb.console.common.LogData;
040: import com.bostechcorp.cbesb.console.common.LogViewProperties;
041: import com.bostechcorp.cbesb.console.common.ServerSideException;
042: import com.bostechcorp.cbesb.console.help.FileHelper;
043: import com.bostechcorp.cbesb.console.rpc.ViewLog;
044:
045: //TODO we need to handle the polling of log to support then session timeout
046: //public class ViewLogImpl extends ConsoleRemoteServiceServlet implements ViewLog {
047: public class ViewLogImpl extends ConsoleRemoteServiceServlet implements
048: ViewLog {
049:
050: private static final long serialVersionUID = 1L;
051:
052: private static RandomAccessFile cbserverRAF = null;
053:
054: private static RandomAccessFile tcwrapperRAF = null;
055:
056: private static RandomAccessFile smwrapperRAF = null;
057:
058: public static FileHelper cbserverRAFFileHelper = new FileHelper();
059:
060: public static FileHelper tcwrapperRAFFileHelper = new FileHelper();
061:
062: public static FileHelper smwrapperRAFFileHelper = new FileHelper();
063:
064: private static long cbserverLength = 0;
065:
066: private static long tcwrapperLength = 0;
067:
068: private static long smwrapperLength = 0;
069:
070: private static boolean flag;
071:
072: private File cbesbServerFile;
073:
074: private File smwrapperFile;
075:
076: private File tcwrapperFile;
077: public LogViewProperties properties = null;
078:
079: String path = EsbPathHelper.getCbesbHomeDir();
080:
081: public ViewLogImpl() {
082:
083: if (!flag) {
084:
085: cbesbServerFile = new File(path + File.separator + "log"
086: + File.separator + "cbesb_server.log");
087: smwrapperFile = new File(path + File.separator + "log"
088: + File.separator + "sm_wrapper.log");
089: tcwrapperFile = new File(path + File.separator + "log"
090: + File.separator + "tc_wrapper.log");
091: try {
092: if (!cbesbServerFile.exists())
093: cbesbServerFile.createNewFile();
094: if (!smwrapperFile.exists())
095: smwrapperFile.createNewFile();
096: if (!tcwrapperFile.exists())
097: tcwrapperFile.createNewFile();
098: cbserverRAF = new RandomAccessFile(cbesbServerFile, "r");
099: tcwrapperRAF = new RandomAccessFile(tcwrapperFile, "r");
100: smwrapperRAF = new RandomAccessFile(smwrapperFile, "r");
101: cbserverLength = cbesbServerFile.length();
102: tcwrapperLength = tcwrapperFile.length();
103: smwrapperLength = smwrapperFile.length();
104: } catch (FileNotFoundException e1) {
105: e1.printStackTrace();
106: } catch (IOException e) {
107: // TODO Auto-generated catch block
108: e.printStackTrace();
109: }
110: flag = true;
111: }
112: }
113:
114: public LogData read() {
115: return null;
116: }
117:
118: public LogData update(int length, String currentFile)
119: throws ServerSideException {
120: LogData logData = new LogData();
121: int sizeOld = 0;
122: if (properties == null) {
123: sizeOld = 0;
124: } else
125: sizeOld = properties.getBufferSize();
126: properties = getProperties();
127: int size = properties.getBufferSize();
128: if (properties.getBufferSize() <= 0)
129: size = 0;
130:
131: if (sizeOld < size) {
132: cbserverRAFFileHelper.init();
133: tcwrapperRAFFileHelper.init();
134: smwrapperRAFFileHelper.init();
135: }
136: String result = "";
137:
138: if (currentFile.equalsIgnoreCase("cbesb_server.log")) {
139: result = cbserverRAFFileHelper.tail(path + File.separator
140: + "log" + File.separator + currentFile, size);
141:
142: } else if (currentFile.equalsIgnoreCase("tc_wrapper.log")) {
143: result = tcwrapperRAFFileHelper.tail(path + File.separator
144: + "log" + File.separator + currentFile, size);
145: } else if (currentFile.equalsIgnoreCase("sm_wrapper.log")) {
146: result = smwrapperRAFFileHelper.tail(path + File.separator
147: + "log" + File.separator + currentFile, size);
148: }
149:
150: logData.setData(result);
151: logData.setCurrentFile(currentFile);
152: logData.setProperties(properties);
153: return logData;
154: }
155:
156: private byte[] getFileText(String fileName, int length) {
157: byte[] result = new byte[length];
158: try {
159: File file = new File(path + File.separator + "log"
160: + File.separator + fileName);
161:
162: RandomAccessFile raf = new RandomAccessFile(file, "r");
163: long len = file.length();
164:
165: int lineCount = 0;
166: List list = new ArrayList();
167: String s = null;
168: int lastSize = 0;
169: s = raf.readLine();
170: if (s == null)
171: return null;
172: list.add(s.length() + 2);
173: lastSize = s.length() + 2;
174: while ((s = raf.readLine()) != null) {
175: int size = s.length() + 2 + lastSize;
176: list.add(size);
177: lastSize = size;
178:
179: lineCount++;
180: }
181:
182: int beginningLine = 0;
183:
184: if (lineCount > length) {
185: beginningLine = lineCount - length;
186: }
187:
188: int index = 0;
189: if (beginningLine == 0)
190: index = 0;
191: else {
192: index = Integer.parseInt(list.get(beginningLine)
193: .toString());
194: }
195:
196: result = new byte[(int) (len - index)];
197: raf.seek(index);
198: raf.read(result);
199: } catch (Exception e) {
200: return null;
201: }
202: return result;
203: }
204:
205: public LogData pollingUpdate(int length, String currentFile)
206: throws ServerSideException {
207: return update(length, currentFile);
208: }
209:
210: public Boolean validateLogConfigureText(String string) {
211: if (string.equals("")) {
212: return true;
213: }
214: Pattern pattern = Pattern.compile("[0-9]+");
215: Matcher matcher = pattern.matcher(string);
216: return matcher.matches();
217: }
218:
219: public void reset() {
220: cbserverLength = cbesbServerFile.length();
221: tcwrapperLength = tcwrapperFile.length();
222: smwrapperLength = smwrapperFile.length();
223: }
224:
225: public LogData inital(int length, String log) {
226: String path = EsbPathHelper.getCbesbHomeDir();
227: /*File file = new File(path + File.separator + "log"
228: + File.separator + log);*/
229:
230: LogViewProperties properties = getProperties();
231: int size = properties.getBufferSize();
232: if (properties.getBufferSize() <= 0)
233: size = 0;
234:
235: byte[] temp = getFileText(log, size);
236:
237: String[] string = new String[2];
238: string[0] = log;
239: if (temp == null)
240: string[1] = new String("");
241: else
242: string[1] = new String(temp);
243:
244: LogData logData = new LogData();
245: logData.setData(string[1]);
246: logData.setProperties(properties);
247: return logData;
248: }
249:
250: public LogViewProperties getProperties() {
251:
252: int numOfLines = 0;
253: int refreshRate = 0;
254: int bufferSize = 0;
255:
256: try {
257: numOfLines = Integer.parseInt(ConsoleSettingUtil
258: .getConsoleUserSetting(super .getCurrentUserId())
259: .getLogViewerNumOfLine());
260: refreshRate = Integer.parseInt(ConsoleSettingUtil
261: .getConsoleUserSetting(super .getCurrentUserId())
262: .getLogRefreshRate());
263: bufferSize = Integer.parseInt(ConsoleSettingUtil
264: .getConsoleUserSetting(super .getCurrentUserId())
265: .getLogViewerLineBufferSize());
266:
267: } catch (Exception e) {
268: numOfLines = 10;
269: refreshRate = 5;
270: bufferSize = 600;
271: e.printStackTrace();
272: }
273: HttpSession session = this .getThreadLocalRequest().getSession();
274: LogViewProperties logProperties = (LogViewProperties) session
275: .getAttribute(Constants.LOG_VIEW_PROPERTIES);
276: if (logProperties == null) {
277: logProperties = new LogViewProperties();
278:
279: }
280:
281: logProperties.setInterval(refreshRate);
282: logProperties.setBufferSize(bufferSize);
283: logProperties.setNumOfLines(numOfLines);
284: setProperties(logProperties);
285:
286: return logProperties;
287: }
288:
289: public Boolean setProperties(LogViewProperties props) {
290: HttpSession session = this.getThreadLocalRequest().getSession();
291: session.setAttribute(Constants.LOG_VIEW_PROPERTIES, props);
292: return Boolean.TRUE;
293: }
294:
295: }
|