?? gatherimpl.java
字號:
package com.briup.impl.gather;
import java.util.*;
import java.io.*;
import java.net.*;
import java.sql.Date;
import com.briup.BIDR;
import com.briup.Gather;
import com.briup.exception.GatherException;
/**
* @author renqs
* @company Briup Technology Inc,.(Shanghai)
* @date JUL 2, 2008 2:58:31 AM
*/
public class GatherImpl implements Gather {
private Map<String, BIDR> loginData = new HashMap<String, BIDR>();
private List<BIDR> bidrs = new ArrayList<BIDR>();
private String dataSourceFile = "src/com/briup/resource/radwtmp";
private String dataBakFile = "src/com/briup/resource/dataBakFile";
private String dataPositionFile = "src/com/briup/resource/dataPositionFile";
private static String labIp = "";
public GatherImpl(Properties props) throws UnknownHostException {
String pDataSourceFile = null;
String pDataBakFile = null;
String pDataPositionFile = null;
pDataSourceFile = props.getProperty("data_source_file_url");
pDataBakFile = props.getProperty("data_bak_file_url");
pDataPositionFile = props.getProperty("data_position_file_url");
if (pDataSourceFile != null)
dataSourceFile = pDataSourceFile;
if (pDataBakFile != null)
dataBakFile = pDataBakFile;
if (pDataPositionFile != null)
dataPositionFile = pDataPositionFile;
labIp = InetAddress.getLocalHost().getHostAddress().toString();
}
/**
* 將解析得到的BIRD對象存放到集合中
*
* @return Collection<BIDR> 存放BIRD對象的List集合
*/
public Collection<BIDR> doGather() throws GatherException {
try {
getLoginDataFromFile(dataBakFile);
gather(dataSourceFile);
saveSingleLogin2File(dataBakFile);
} catch (Exception e) {
e.printStackTrace();
throw new GatherException("gather error!");
}
return getbidrs();
}
/**
* 從備份文件中讀取上次解析沒有匹配成功的記錄
*/
@SuppressWarnings("unchecked")
private void getLoginDataFromFile(String dataBakFile)
throws FileNotFoundException, IOException, ClassNotFoundException {
File f = new File(dataBakFile);
if (f.exists() && f.canRead()) {
Map<String, BIDR> bakMap = null;
ObjectInputStream ois = null;
try {
ois = new ObjectInputStream(new FileInputStream(f));
bakMap = (Map) ois.readObject();
if (bakMap != null && bakMap.size() > 0)
loginData = bakMap;
} catch (IOException e) {
e.printStackTrace();
} finally {
if (ois != null)
try {
ois.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}
}
/**
* 解析radWtmpx文件,封裝成數組對象后存放到Map中,并記錄文件讀取到的位置
*/
private void gather(String dataSourceFile) throws IOException {
File f = new File(dataSourceFile);
RandomAccessFile raf = null;
if (f.exists() && f.canRead()) {
raf = new RandomAccessFile(f, "rw");// "r"表示以只讀方式打開
}
long position = 0l;
//position = getPositionFromFile(dataPositionFile);
raf.seek(position); // 表示從此文件指針偏移量所在該位置開始讀取或寫入文件
String source = null;
while ((source = raf.readLine()) != null) {
String sArray[] = getArrayFromString(source);
String key = sArray[1].trim() + sArray[4];
BIDR bidr = null;
if (sArray[2].trim().equals("7")) {
if (!loginData.containsKey(key)) {
bidr = getArray2bird(sArray);
loginData.put(key, bidr);
}
}
if (sArray[2].trim().equals("8")) {
String sDate = sArray[3];
bidrs.add(matchbidrs(key, sDate));
loginData.remove(key);
}
}
position = raf.getFilePointer();
//savePosition2File(dataPositionFile, position);
}
/**
* 從map中取出BIRD對象,設定登出時間與撥號時長,并返回該BIRD對象
*/
public BIDR matchbidrs(String key, String date) {
BIDR bidr = null;
bidr = (BIDR) loginData.get(key);
Date loginDate = bidr.getLoginDate();
Date logoutDate = getDate(date);
long timeDuration = (logoutDate.getTime() - loginDate.getTime())
/ (1000 * 60);
if (timeDuration < 1)
timeDuration = 1;
bidr.setLogoutDate(logoutDate);
bidr.setTimeDuration(timeDuration);
return bidr;
}
/**
* 讀取數組中的元素,將其封裝成BIRD對象
*/
private BIDR getArray2bird(String[] sArray) {
BIDR bidr = null;
return bidr;
}
/**
* 將一個用long類型表示的毫秒值封裝成java.sql.Date對象
*/
private Date getDate(String sDate) {
return new Date(Long.parseLong(sDate+"000"));
}
/**
* 將讀取出來的一行記錄解析封裝成數組
*/
private String[] getArrayFromString(String source) {
return null;
}
/**
* 從文件中讀取上次讀取到的文件的位置
*/
private long getPositionFromFile(String dataPositionFile) {
long position = 0L;
File file = new File(dataPositionFile);
RandomAccessFile raf = null;
if (file.exists() && file.canRead()) {
try {
raf = new RandomAccessFile(file,"rw");
position = raf.readLong();
} catch (IOException e) {
e.printStackTrace();
} finally {
if (raf != null)
try {
raf.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
return position;
}
/**
* 將本次解析到的文件的位置保存到文件中
*/
private void savePosition2File(String dataPositionFile, long position) {
File file = new File(dataPositionFile);
DataOutputStream dos = null;
try {
dos = new DataOutputStream(
new FileOutputStream(file));
try {
dos.writeLong(position);
} catch (IOException e1) {
e1.printStackTrace();
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} finally {
if (dos != null)
try {
dos.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
/**
* 將沒有匹配的記錄(Map<String, BIDR>)保存在文件中
*/
private void saveSingleLogin2File(String dataBakFile)
throws FileNotFoundException, IOException {
File f = new File(dataBakFile);
ObjectOutputStream oos = null;
try {
oos = new ObjectOutputStream(new FileOutputStream(f));
oos.writeObject(loginData);
} catch (IOException e) {
e.printStackTrace();
} finally {
if (oos != null)
try {
oos.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}
public Map<String, BIDR> getLoginData() {
return loginData;
}
public void setLoginData(Map<String, BIDR> loginData) {
this.loginData = loginData;
}
public List<BIDR> getbidrs() {
return bidrs;
}
public void setbidrs(List<BIDR> bidrs) {
this.bidrs = bidrs;
}
public static void main(String[] args){
Properties props = new Properties();
props.setProperty("data_source_file_url",
"src/com/briup/resource/radwtmp");
props.setProperty("data_bak_file_url", "src/com/briup/resource/dataBakFile");
props.setProperty("data_position_file_url",
"src/com/briup/resource/dataPositionFile");
List<BIDR> list = new ArrayList<BIDR>();
try {
GatherImpl gatherimpl = new GatherImpl(props);
list = (List<BIDR>) gatherimpl.doGather();
System.out.println("SUM:" + list.size());
} catch (UnknownHostException e) {
e.printStackTrace();
} catch (GatherException e) {
e.printStackTrace();
}
}
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -