package org.com.gather;
/**
* author Adam
* [email protected]
*/
import java.util.*;
import java.io.*;
import java.sql.Timestamp;
import java.net.InetAddress;
public class GatherImpl implements Serializable,Gather {
private Properties pro;
//存放记录为7的集合!
private ArrayList list = new ArrayList();
private RandomAccessFile raf;
private Log log = null;
public GatherImpl(){}
public GatherImpl(Properties pro) {
this.pro = pro;
}
public Collection getData(){
ArrayList arrayList = new ArrayList();
ArrayList aList = new ArrayList();
//先看一下上次是否有客户还没有下线,如有,则把
//该信息载入内存,继续比较!
if(this.loadTempFile()!=null){
list = loadTempFile();
}
try{
//获得上次标志位!
long point = this.getPoint();
System.out.println("当前指针是--- "+point);
//获取解析文件路径!
String wtmpxPath = pro.getProperty("wtmpx_path");
File file = new File(wtmpxPath);
//为了防止出现找不到文件的异常,用file.createNewFile();
//来创建!
if (file.createNewFile()) {
System.out.println("wtmpx file is first creating");
}
//用RandomAccessFile对File进行包装!
raf = new RandomAccessFile(file, "r");
//将指针移到上次的标志位!
raf.seek(point);
//布尔值为真,直接进入循环体!
boolean run = true;
//循环读取该文件
while(run){
String data = raf.readLine();
//如果读到空行就退出!
if(data == null){
run = false;
break;
}
//先检验该记录是否为有效记录,如果无效则不做
//任何处理!
if (validate(data)) {
TempBIDR obj = this.parseWtmpx(data);
//先判断一下该状态位置是否为空!
if (obj.getStatus() != null) {
if (obj.getStatus().equals("7")) {
//如果读到的记录状态位为7的,就把该记录
//一起放到记录为7的集合里!
list.add(obj);
}
//如果读到的记录状态位为8,则直接和记录为7
//的集合进行匹配,如果有匹配的则进行合并!
else if (obj.getStatus().equals("8")) {
Iterator iter = list.iterator();
while (iter.hasNext()) {
//把记录放到一个另一个的TempBIDR对象里!
TempBIDR temp = (TempBIDR) iter.next();
//如果线程号相同,则认为是同一个用户!
if (obj.getThreadID().equals(temp.getThreadID()) && obj.getUserName().equals(obj.getUserName())) {
//得到登陆时间和登出时间,然后登出时间
//减去登陆时间,得出上网时长!
long loginTime = Long.parseLong(temp.getTime());
long logoutTime = Long.parseLong(obj.getTime());
long timeDuration = logoutTime - loginTime;
BIDR bidr = new BIDR(obj.getUserName(),
obj.getLocalIP(),
new Timestamp(loginTime * 1000),
new Timestamp(logoutTime * 1000),
InetAddress.getLocalHost().
getHostAddress(),
timeDuration * 1000);
//把成功合并的记录存放放到集合!
arrayList.add(bidr);
bidr = null;
//把已经成功配对的7记录从集合里删除!
iter.remove();
}
}
}
}
}
}
//把状态为7的记录存放到临时文件里,下次再比较!
this.storeTempFile(list);
//获取文件尾的指针,保存起来,做为下次读文件的标志为位!
long filePoint = raf.getFilePointer();
//把当前的指针位置保存起来!
this.storePoint(filePoint);
//该方法是对读出来的集合再一次过滤,把交叉时间给屏蔽调!
// aList = this.parseArrayList(arrayList);
log.writeDebug("采集成功!");
if(arrayList.isEmpty()){
System.out.println("=="+arrayList);
}
return arrayList;
}catch(Exception e){
e.printStackTrace();
return null;
}
}
//辅助方法
public ArrayList parseArrayList(ArrayList aList){
ArrayList list = new ArrayList();
list = aList;
for(int i = 0; i < list.size(); i++){
for(int j = i+1; j < list.size(); j++){
if(!(list.get(i)==null) && !(list.get(j)==null)){
BIDR bidr_I = (BIDR) list.get(i);
BIDR bidr_J = (BIDR) list.get(j);
//如果两个对象的名字一样,而且有交错时间的话就整合
//把i 改变掉,然后与他比较的那个对象就删除.
if(bidr_I.getLoginName().equals(bidr_J.getLoginName())&&testBIDR(bidr_I,bidr_J)){
list.set(i,this.combineBIDR(bidr_I,bidr_J));
list.set(j,null);
}
}
}
}
Iterator iter = list.iterator();
while(iter.hasNext()){
if(iter.next()==null){
iter.remove();
}
}
if(list==null){
list = aList;
}
return list;
}
public BIDR combineBIDR(BIDR bidr_I,BIDR bidr_J){
long s1Login_date = bidr_I.getLoginDate().getTime();
long s2Login_date = bidr_J.getLoginDate().getTime();
long s1Logout_date = bidr_I.getLogoutDate().getTime();
long s2Logout_date = bidr_J.getLogoutDate().getTime();
long login_date = 0l;
long logout_date = 0l;
login_date = (s1Login_date <= s2Login_date ? s1Login_date : s2Login_date);
logout_date = (s1Logout_date >= s2Logout_date ? s1Logout_date : s2Logout_date);
long timeduration = logout_date - login_date;
Timestamp login_Date = new Timestamp(login_date);
Timestamp logout_Date = new Timestamp(logout_date);
BIDR bidr = new BIDR(bidr_I.getLoginName(),bidr_I.getLoginIP(),
login_Date,logout_Date,bidr_I.getLabIP(),timeduration);
return bidr;
}
public boolean testBIDR(BIDR bidr_I,BIDR bidr_J){
boolean status = false;
long ILogin_date = bidr_I.getLoginDate().getTime();
long JLogin_date = bidr_J.getLoginDate().getTime();
long ILogout_date = bidr_I.getLogoutDate().getTime();
long JLogout_date = bidr_J.getLogoutDate().getTime();
if(ILogin_date >= JLogin_date && ILogin_date <= JLogout_date ||
JLogin_date >= ILogin_date && JLogin_date <= ILogout_date){
status = true;
}
return status;
}
public void storePoint(long point){
String data = Long.toString(point);
try{
OutputStream os = new FileOutputStream(pro.getProperty("point_file"));
BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(os));
bw.write(data);
bw.flush();
os.close();
bw.close();
}catch(Exception e){
e.printStackTrace();
}
}
public void storeTempFile(ArrayList list){
File file = new File(pro.getProperty("log_backup"));
try{
OutputStream os = new FileOutputStream(file);
ObjectOutputStream oos = new ObjectOutputStream(os);
oos.writeObject(list);
}catch(Exception e){
e.printStackTrace();
}
}
public TempBIDR parseWtmpx(String data){
StringTokenizer st = new StringTokenizer(data,":");
String[]record = new String[11];
int i = 0;
while(st.hasMoreTokens()){
record[i] = st.nextToken();
i++;
}
TempBIDR tempBIDR = new TempBIDR(record[0], record[3], record[4],
record[5], record[8]);
return tempBIDR;
}
public boolean validate(String data){
boolean status = true;
if(data!=null){
StringTokenizer st = new StringTokenizer(data, ":");
String da = null;
while (st.hasMoreTokens()) {
da = st.nextToken();
break;
}
if (da == null) {
status = false;
}
else {
String[] stringSet = {
"root", "LOGIN", "system boot", "run-level", "rc2", "rc3", "sac",
"ttymon", "zsmon", "shutdown"};
for (int i = 0; i < stringSet.length; i++) {
if (da != null) {
if (da.equals(stringSet[i])) {
status = false;
break;
}
}
}
}
}
return status;
}
public long getPoint(){
long point = 0l;
File file = new File(pro.getProperty("point_file"));
try{
if (file.createNewFile()) {}
if(file.length()>0){
InputStream is = new FileInputStream(file);
BufferedReader br = new BufferedReader(new InputStreamReader(is));
String p = br.readLine();
point = Long.parseLong(p);
br.close();
}
return point;
}catch(Exception e){
e.printStackTrace();
return 0;
}
}
public ArrayList loadTempFile(){
File file = new File(pro.getProperty("log_backup"));
ArrayList list = null;
try{
if (file.createNewFile()) {
}
//如果不判断一下,会出现异常,因为如果该文件长度为零的话,
//会返回一个XX异常.
if(file.length()>0){
InputStream is = new FileInputStream(file);
ObjectInputStream ois = new ObjectInputStream(is);
list = (ArrayList) ois.readObject();
is.close();
ois.close();
}
return list;
}catch(Exception e){
e.printStackTrace();
return null;
}
}
public void setLog(Log log){
this.log = log;
}
}
////辅助类
package org.bazu.gathersystem.gather;
import java.sql.Timestamp;
import java.io.*;
public class Bidr implements Serializable
{
private String Login_name = null;
private String Login_ip = null;
private Timestamp login_date = null;
private Timestamp logout_date = null;
private String Lab_ip = null;
private long Time_duration = 0; /* Minute */
public Bidr(String Login_name,String Login_ip,Timestamp login_date,
Timestamp logout_date,String Lab_ip,long Time_duration)
{
this.Login_name=Login_name;
this.Login_ip=Login_ip;
this.login_date=login_date;
this.logout_date=logout_date;
this.Lab_ip=Lab_ip;
this.Time_duration=Time_duration;
}
public String getLab_ip()
{
return Lab_ip;
}
public Timestamp getLogin_date()
{
return login_date;
}
public String getLogin_ip()
{
return Login_ip;
}
public String getLogin_name()
{
return Login_name;
}
public Timestamp getLogout_date()
{
return logout_date;
}
public long getTime_duration()
{
return Time_duration;
}
public void setTime_duration(long Time_duration)
{
this.Time_duration = Time_duration;
}
public void setLogout_date(Timestamp logout_date)
{
this.logout_date = logout_date;
}
public void setLogin_name(String Login_name)
{
this.Login_name = Login_name;
}
public void setLogin_ip(String Login_ip)
{
this.Login_ip = Login_ip;
}
public void setLogin_date(Timestamp login_date)
{
this.login_date = login_date;
}
public void setLab_ip(String Lab_ip)
{
this.Lab_ip = Lab_ip;
}
}
//接口提供了一个getData方法
<?xml version="1.0" encoding="UTF-8"?>
<netctoss>
<db single="false">
<className>lianxi.DBImpl</className>
<methodName>getInstance</methodName>
<batchSize>1000</batchSize>
<driver>com.pointbase.jdbc.jdbcUniversalDriver</driver>
<url>jdbc:pointbase:server://localhost/Adam</url>
<username>pbpublic</username>
<password>pbpublic</password>
</db>
<backup single="false">
<className>lianxi.BackUpImpl</className>
</backup>
</netctoss>