⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 gatherimpl.java

📁 java的团队合作代码
💻 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 + -