论坛首页 入门技术论坛

一个关于重构的问题,求助

浏览 1576 次
该帖已经被评为新手帖
作者 正文
   发表时间:2007-04-09  
近日由于工作原因,需要写一个读取文件的类,目的是读取日志文件中的一条记录。由于近20分钟内的数据可能不准,所以每次都读20分钟之前的数据。如果读取发生错误,则写入日志文件。比如现在为9:55分,则取9:35分那条数据。实现如下。现在需要增加一个设定时间,而不是取当前时间的方法,由于深知复制粘贴的错误,故而想重构后抽出相似方法,不过不知从何处下手。初学乍练,请高手不吝赐教。
public class BillingInforByLogImpl implements BillingInforManager {

private Logger logger;

private String billingLogPath;

private BillingStringTransform billingStringTransform;

private ExceptionFileWriter exceptionFileWriter;

private static final int TIME_BASE = 1000;

private static final int CALENDAR_MINUTE_SET = 12;

private static final int CUT_MINUTE = -20;

public BillingInfor queryBillingInfor(
ChannelContraposeInfor channelContraposeInfor) throws IOException {
PropertyConfigurator.configure("conf" + File.separator
+ "log4j.properties");
logger = Logger.getRootLogger();

String fileName = getFileName(channelContraposeInfor.getIp(),
channelContraposeInfor.getInOid());
logger.info("Read log is " + fileName);

File file = new File(fileName);

FileInputStream fileInputStream;
try {
fileInputStream = new FileInputStream(file);
} catch (FileNotFoundException e) {
writExceptionFile(channelContraposeInfor, " ", e.getMessage());
logger
.error("BillingInforByLogImpl.queryBillingInfor|Can't find log file :"
+ fileName);
throw new IOException(e.getMessage());
}

return queryBillingInforFromLogFile(channelContraposeInfor,
fileInputStream);
}

private void writExceptionFile(
ChannelContraposeInfor channelContraposeInfor,
String billingResult, String errorFlag) throws IOException {
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyyMMddhhmm");
exceptionFileWriter.appendExceptionFile(channelContraposeInfor,
simpleDateFormat.format(new Date()), billingResult, errorFlag);
}

private BillingInfor queryBillingInforFromLogFile(
ChannelContraposeInfor channelContraposeInfor,
FileInputStream fileInputStream) throws IOException {
BufferedReader allFileLines = new BufferedReader(new InputStreamReader(
fileInputStream));
try {
String oneLine;
while ((oneLine = allFileLines.readLine()) != null) {
if (oneLine != null) {
String[] oneLineStrings = oneLine.split(" ");
if (oneLineStrings.length != 5
|| !isNowTimeAddTenMinutesAfterFileTime(oneLineStrings[0])) {
continue;
}
logger.info("File fist line is: " + oneLine);
return billingStringTransform.transformBillingString(
channelContraposeInfor.getChannelID(),
oneLineStrings[1], oneLineStrings[2],oneLineStrings[0]);
}
}
} catch (Exception e) {
logger
.error("BillingInforByLogImpl.queryBillingInfor|Analyse log file error :"
+ e.getMessage());
writExceptionFile(channelContraposeInfor, " ", e.getMessage());
throw new IOException(e.getMessage());
}
writExceptionFile(channelContraposeInfor, " ",
"Log file is no hava log infor");
throw new IOException(
"BillingInforByLogImpl.queryBillingInfor|Log file is no hava log infor");
}

private boolean isNowTimeAddTenMinutesAfterFileTime(String time) {
GregorianCalendar fileTime = new GregorianCalendar();
GregorianCalendar thisTime = new GregorianCalendar();
Date date = new Date(Long.valueOf(time) * TIME_BASE);

thisTime.setTime(new Date());
fileTime.setTime(date);

thisTime.add(CALENDAR_MINUTE_SET, CUT_MINUTE);

if (thisTime.after(fileTime)) {
return true;
}
return false;
}

private String getFileName(String ip, String oid) {
String[] flag = oid.split("\\.");
return billingLogPath + File.separator + ip + "_" + flag[10] + ".old";
}

public void setBillingLogPath(String billingLogPath) {
this.billingLogPath = billingLogPath;
}

public void setBillingStringTransform(
BillingStringTransform billingStringTransform) {
this.billingStringTransform = billingStringTransform;
}

public void setExceptionFileWriter(ExceptionFileWriter exceptionFileWriter) {
this.exceptionFileWriter = exceptionFileWriter;
}
   发表时间:2007-04-09  
为什么新来的都不知道用code标签呢
0 请登录后投票
论坛首页 入门技术版

跳转论坛:
Global site tag (gtag.js) - Google Analytics