Java tutorial
/* * Copyright (C) 2014 a5834099147(lxd) <a5834099147@126.com> * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program. If not, see <http://www.gnu.org/licenses/>. */ package com.lxd.client.monitor; import java.io.File; import org.apache.commons.io.monitor.FileAlterationMonitor; import org.apache.commons.io.monitor.FileAlterationObserver; import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.Logger; import com.lxd.algorithm.MD5; import com.lxd.client.monitor.MonitorMsg.Type; import com.lxd.client.monitor.util.FileUtil; import com.lxd.client.resource.ClientResource; import com.lxd.client.resource.RequestPackage; import com.lxd.client.resource.property.ServerAddFile; import com.lxd.client.resource.property.ServerDeleteFile; import com.lxd.client.resource.property.ServerUpdateFile; import com.lxd.client.view.control.UiSingleton; import com.lxd.protobuf.msg.Msg.Msg_; import com.lxd.protobuf.msg.request.Request.Request_; import com.lxd.protobuf.msg.request.console.AddFile.AddFile_; import com.lxd.protobuf.msg.request.console.Console.Console_; import com.lxd.protobuf.msg.request.console.DeleteFile.DeleteFile_; import com.lxd.protobuf.msg.request.console.UpdateFile.UpdateFile_; import com.lxd.utils.Define; /** * ? * @author: a5834099147 * @mailto: a5834099147@126.com * @date: 201514 * @blog : http://a5834099147.github.io/ * @review */ public class MonitorDir extends Thread { private static final Logger log = LogManager.getLogger(MonitorDir.class); ///< ? private String filePath = null; public MonitorDir(String filePath) { this.filePath = filePath; } public void run() { fileWatch(); } private void fileWatch() { //? FileAlterationMonitor monitor = new FileAlterationMonitor(Define.SCANNING); //?? // FileAlterationObserver observer = new FileAlterationObserver(new File(filePath)); ///< ?? observer.addListener(new AdamFileListener()); ///< ?? monitor.addObserver(observer); try { monitor.start(); } catch (Exception e) { e.printStackTrace(); } ///< ?? MonitorMsg msg = null; while ((msg = ClientResource.getSingleton().takeMonitorMsg()) != null) { if (msg.getStart() == 0 || msg.getStart() <= System.currentTimeMillis()) { ///< ? if (msg.getStart() == 0 && msg.getType() == Type.UPDATE) { ///< , ??, ? msg.setStart(System.currentTimeMillis() + Define.UPDATETIME); ClientResource.getSingleton().submitMonitorMsg(msg); } else { ///< ?? if (msg.getType() == Type.DELETE || fileCanRead(msg.getFile())) { ///< if (msg.getType() == Type.ADD && msg.getState().equals(com.lxd.client.monitor.MonitorMsg.State.BEGIN)) { msg.setStart(System.currentTimeMillis() + Define.CREATEFILETIME); msg.setState(com.lxd.client.monitor.MonitorMsg.State.ACCESS); ClientResource.getSingleton().submitMonitorMsg(msg); continue; } msg = ClientResource.getSingleton().reviseMsg(msg); msgPacking(msg); } else { log.info("" + msg.getFile() + ", ?"); msg.setStart(System.currentTimeMillis() + Define.ERRORTIME); ClientResource.getSingleton().submitMonitorMsg(msg); } } } else { ///< ?? ClientResource.getSingleton().submitMonitorMsg(msg); try { ///< ??, , ? log.info("" + msg.getFile() + ", ?"); sleep(Define.SLEEPTIME); } catch (InterruptedException e) { e.printStackTrace(); } } } } private void msgPacking(MonitorMsg msg) { switch (msg.getType()) { case DELETE: deleteFilePacking(msg.getFile()); break; case ADD: addFilePacking(msg.getFile()); break; case UPDATE: updateFilePacking(msg.getFile()); break; } } private void deleteFilePacking(File file) { ///< ?? Console_.Builder console = Console_.newBuilder(); ///< ? console.setUserName(UiSingleton.getSingleton().getUser()); ///< ?? DeleteFile_.Builder deleteFile = DeleteFile_.newBuilder(); ///< ? deleteFile.setPath(file.getAbsolutePath().substring(Define.CLIENT.length())); ///< ??? console.setDeleteFile(deleteFile); ///< Msg_ msg_ = packing(console.build()); ClientResource.getSingleton() .submitRequest(new RequestPackage(msg_, new ServerDeleteFile(file.getAbsolutePath()))); } /* * ? */ private void addFilePacking(File file) { ///< ?? Console_.Builder console = Console_.newBuilder(); ///< ? console.setUserName(UiSingleton.getSingleton().getUser()); ///< ?? AddFile_.Builder addFile = AddFile_.newBuilder(); ///< md5 String md5 = null; try { ///< MD5 md5 = MD5.getFileMD5String(file); addFile.setMd5(md5); } catch (Exception e) { ///< log.error(e.getMessage()); return; } ///< addFile.setLength(file.length()); ///< ? addFile.setLast(file.lastModified()); ///< addFile.setPath(file.getAbsolutePath().substring(Define.CLIENT.length())); ///< ? addFile.setTotal(FileUtil.getTotal(file)); ///< ??? console.setAddFile(addFile); ///< Msg_ msg_ = packing(console.build()); ClientResource.getSingleton() .submitRequest(new RequestPackage(msg_, new ServerAddFile(file.getAbsolutePath(), md5, file.length(), file.lastModified(), FileUtil.getTotal(file)))); } /* * ? */ private void updateFilePacking(File file) { ///< ?? Console_.Builder console = Console_.newBuilder(); ///< ? console.setUserName(UiSingleton.getSingleton().getUser()); ///< ??? UpdateFile_.Builder updateFile = UpdateFile_.newBuilder(); ///< updateFile.setLength(file.length()); ///< ? updateFile.setLast(file.lastModified()); ///< md5 String md5 = null; try { ///< MD5 md5 = MD5.getFileMD5String(file); updateFile.setMd5(md5); } catch (Exception e) { ///< , IO log.error(e.getMessage()); return; } ///< ? updateFile.setPath(file.getAbsolutePath().substring(Define.CLIENT.length())); ///< ? updateFile.setTotal(FileUtil.getTotal(file)); ///< ??? console.setUpdateFile(updateFile); ///< Msg_ msg_ = packing(console.build()); ClientResource.getSingleton() .submitRequest(new RequestPackage(msg_, new ServerUpdateFile(file.getAbsolutePath(), md5, file.length(), file.lastModified(), FileUtil.getTotal(file)))); } /* * ? */ private Msg_ packing(Console_ console) { Msg_.Builder msg = Msg_.newBuilder(); Request_.Builder request = Request_.newBuilder(); request.setConsole(console); msg.setRequest(request); msg.setJobId(-1L); return msg.build(); } /* * ?? */ private boolean fileCanRead(File file) { if (file.renameTo(file)) { return true; } else { return false; } } }