ob.PO.TaskPO.java Source code

Java tutorial

Introduction

Here is the source code for ob.PO.TaskPO.java

Source

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package ob.PO;

import com.fasterxml.jackson.annotation.JsonFilter;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Properties;
import ob.util.AESUtil;
import ob.util.GetUserTimeline;
import ob.util.Sina;
import ob.util.UpdateStatus;
import org.apache.commons.mail.SimpleEmail;
import org.apache.commons.net.pop3.POP3MessageInfo;
import org.apache.commons.net.pop3.POP3SClient;

/**
 * ?
 * @author oubeichen
 */
@JsonFilter("taskFilter")
public class TaskPO extends Thread implements Cloneable {
    private String tid;//
    private int uid;//
    private String taskname;
    private String ctime;
    private String status = "Stopped";
    private boolean isrunning = false;

    private int thistype;
    //API??????
    private String thisstr1;//type == 0 date//type == 1 email//type == 2 weiboid
    private String thisstr2;//type == 0 time//type == 1 emailpass//type ==2 weibopass
    private String thistext;//??

    private int thattype;

    private String thatusername;//type==0 weiboname//type==1 src email
    private String thatpassword;//type==0 weiboname//type==1 src email
    private String thattext;

    public TaskPO() {

    }

    @Override
    public Object clone() throws CloneNotSupportedException {
        Object obj = super.clone();
        //((TaskPO)obj).email = email.clone();//Email???
        return obj;//string???super.clone
    }

    /**
     * @return the tid
     */
    public String getTid() {
        return tid;
    }

    /**
     * @param tid the tid to set
     */
    public void setTid(String tid) {
        this.tid = tid;
    }

    /**
     * @return the uid
     */
    public int getUid() {
        return uid;
    }

    /**
     * @param uid the uid to set
     */
    public void setUid(int uid) {
        this.uid = uid;
    }

    /**
     * @return the taskname
     */
    public String getTaskname() {
        return taskname;
    }

    /**
     * @param taskname the taskname to set
     */
    public void setTaskname(String taskname) {
        this.taskname = taskname;
    }

    /**
     * @return the thistype
     */
    public int getThistype() {
        return thistype;
    }

    /**
     * @param thistype the thistype to set
     */
    public void setThistype(int thistype) {
        this.thistype = thistype;
    }

    /**
     * @return the thisstr1
     */
    public String getThisstr1() {
        return thisstr1;
    }

    /**
     * @param thisstr1 the thisstr1 to set
     */
    public void setThisstr1(String thisstr1) {
        this.thisstr1 = thisstr1;
    }

    /**
     * @return the thisstr2
     */
    public String getThisstr2() {
        return thisstr2;
    }

    /**
     * @param thisstr2 the thisstr2 to set
     */
    public void setThisstr2(String thisstr2) {
        this.thisstr2 = thisstr2;
    }

    /**
     * @return the thistext
     */
    public String getThistext() {
        return thistext;
    }

    /**
     * @param thistext the thistext to set
     */
    public void setThistext(String thistext) {
        this.thistext = thistext;
    }

    /**
     * @return the thattype
     */
    public int getThattype() {
        return thattype;
    }

    /**
     * @param thattype the thattype to set
     */
    public void setThattype(int thattype) {
        this.thattype = thattype;
    }

    /**
     * @return the thatusername
     */
    public String getThatusername() {
        return thatusername;
    }

    /**
     * @param thatusername the thatusername to set
     */
    public void setThatusername(String thatusername) {
        this.thatusername = thatusername;
    }

    /**
     * @return the thatpassword
     */
    public String getThatpassword() {
        return thatpassword;
    }

    /**
     * @param thatpassword the thatpassword to set
     */
    public void setThatpassword(String thatpassword) {
        this.thatpassword = thatpassword;
    }

    /**
     * @return the thattext
     */
    public String getThattext() {
        return thattext;
    }

    /**
     * @param thattext the thattext to set
     */
    public void setThattext(String thattext) {
        this.thattext = thattext;
    }

    /**
     * @return the ctime
     */
    public String getCtime() {
        return ctime;
    }

    /**
     * @param ctime the ctime to set
     */
    public void setCtime(String ctime) {
        this.ctime = ctime;
    }

    /**
     * @return the status
     */
    public String getStatus() {
        return status;
    }

    /**
     * @param status the status to set
     */
    public void setStatus(String status) {
        this.status = status;
    }

    /**
     * @return the isrunning
     */
    public boolean isIsrunning() {
        return isrunning;
    }

    /**
     * @param isrunning the isrunning to set
     */
    public void setIsrunning(boolean isrunning) {
        this.isrunning = isrunning;
    }

    @Override
    public void run() {
        setTaskLog("");
        if (thistype == 0) {//
            Date tasktime;
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-ddHH:mm:ss"); //????
            try {
                tasktime = sdf.parse(thisstr1 + thisstr2);
                //if(cl.)
            } catch (ParseException ex) {
                setTaskLog("");
                return;
            }
            Date nowtime = new Date();
            if (nowtime.after(tasktime)) {
                setTaskLog("??");
                return;
            }
            while (nowtime.before(tasktime)) {
                long deltaminute = (tasktime.getTime() - nowtime.getTime()) / (1000 * 60); //
                long day = deltaminute / (24 * 60);
                long hour = (deltaminute / 60) % 24;
                long minute = deltaminute % 60;
                setTaskLog("\n" + day + "" + hour + "" + minute
                        + "");
                try {
                    Thread.sleep(5000);
                } catch (InterruptedException ex) {
                    setTaskLog("");
                    return;
                }
                nowtime = new Date();
            }
            if (nowtime.getTime() - tasktime.getTime() > 60 * 1000) {//?
                setTaskLog("?");
                return;
            }
            setTaskLog("");
        } else if (thistype == 1) {//
            int lastmessage_num = -1;
            POP3SClient pop3 = new POP3SClient(true);
            POP3MessageInfo[] messages;
            pop3.setDefaultTimeout(600000);
            while (true) {
                try {
                    pop3.connect("pop." + thisstr1.split("@")[1]);//pop. + domain
                    if (!pop3.login(thisstr1, AESUtil.Decryptor(thisstr2))) {
                        pop3.disconnect();
                        setTaskLog("?.");
                        return;
                    }
                    messages = pop3.listMessages();
                    if (lastmessage_num < 0) {//?
                        lastmessage_num = messages.length;//?
                        setTaskLog("?" + lastmessage_num);
                    } else if (lastmessage_num < messages.length) {//
                        break;
                    }
                    setTaskLog("?" + messages.length);
                    Thread.sleep(10000);
                } catch (Exception ex) {
                    setTaskLog("");
                    return;
                }
            }
            setTaskLog("");
        } else if (thistype == 2) {//?
            setTaskLog("??");
            while (true) {
                try {
                    String Access_token;
                    if ((Access_token = Sina.getToken(thisstr1, AESUtil.Decryptor(thisstr2))
                            .getAccessToken()) == null) {
                        setTaskLog("???");
                        return;
                    }
                    String lasttimeline = GetUserTimeline.getTimeline(Access_token);
                    if (lasttimeline == null) {
                    } else {
                        if (lasttimeline.contains(thistext)) {//?
                            break;
                        }
                    }
                    setTaskLog("??");
                    Thread.sleep(10000);
                } catch (Exception ex) {
                    ex.printStackTrace();
                    setTaskLog("??");
                    return;
                }
            }
            setTaskLog("???");
        }
        if (thattype == 1) {//?
            setTaskLog("??");
            SimpleEmail email = new SimpleEmail();
            Properties props = new Properties();
            String user, pass;
            try {
                props.load(
                        Thread.currentThread().getContextClassLoader().getResourceAsStream("adminmail.properties"));
                if ((user = (String) props.get("user")) == null || (pass = (String) props.get("pass")) == null) {
                    setTaskLog("?");
                    return;
                }
                email.setHostName("smtp." + user.split("@")[1]);//?  smtp. + domain
                email.setAuthentication(user, pass);//smtp????  
                email.setSSLOnConnect(true);
                email.addTo(thatusername, "JAVA IFTTT RECEIVER");//  
                email.setFrom(user, "JAVA IFTTT SENDER");//?  
                email.setSubject("JAVA IFTTT SEND EMAIL");//  
                email.setCharset("UTF-8");//??  
                email.setMsg(thattext);//  
                email.send();//??  
            } catch (Exception ex) {
                setTaskLog("??");
                return;
            }
            setTaskLog("????");
        } else if (thattype == 0) {//??
            setTaskLog("???");
            try {
                String Access_token;
                if ((Access_token = Sina.getToken(thatusername, AESUtil.Decryptor(thatpassword))
                        .getAccessToken()) == null) {
                    setTaskLog("???");
                    return;
                }
                UpdateStatus.Update(Access_token, thattext);
            } catch (Exception ex) {
                ex.printStackTrace();
                setTaskLog("???");
                return;
            }
            setTaskLog("?????");
        }
    }

    /**
     * ?
     *
     * @param Info 
     */
    private void setTaskLog(String Info) {
        status = Info;//??
    }
    /*
    @Deprecated
    class AutoRemoveRunningTaskThread extends Thread {
        
    TaskPO task_to_remove;
    int code;//?? RunningTask
    int time = 5000;//5000
        
    public AutoRemoveRunningTaskThread(TaskPO task, int cod) {
        task_to_remove = task;
        code = cod;
    }
        
    public AutoRemoveRunningTaskThread(RunningTask task, int cod, int t) {
        task_to_remove = task;
        code = cod;
        time = t;
    }
        
    @Override
    public void run() {
        try {
            Thread.sleep(time);//?Task
        } catch (InterruptedException ex) {
            task_to_remove.setaskLog(ex.getMessage());
        }
        String TaskInfo;
        switch (code) {//???code???
            case TaskPO.SUCCESS:
                TaskInfo = "???";
                break;
            case TaskPO.TIMEOUT:
                TaskInfo = "???";
                break;
            case TaskPO.RUNTIMEERROR:
                TaskInfo = "???";
                break;
            default:
                TaskInfo = "?";
        }
        RemoveRunningTask(task_to_remove, TaskInfo);
    }
    }
    */
}