defaultftpstatistics.java

来自「JAVA FTP 上传下载 的源文件」· Java 代码 · 共 583 行 · 第 1/2 页

JAVA
583
字号
    /**     * Increment make directory count.     */    public synchronized void setMkdir(final FtpIoSession session,            final FtpFile file) {        mkdirCount.incrementAndGet();        notifyMkdir(session, file);    }    /**     * Increment remove directory count.     */    public synchronized void setRmdir(final FtpIoSession session,            final FtpFile file) {        rmdirCount.incrementAndGet();        notifyRmdir(session, file);    }    /**     * Increment open connection count.     */    public synchronized void setOpenConnection(final FtpIoSession session) {        currConnections.incrementAndGet();        totalConnections.incrementAndGet();        notifyOpenConnection(session);    }    /**     * Decrement open connection count.     */    public synchronized void setCloseConnection(final FtpIoSession session) {        if (currConnections.get() > 0) {            currConnections.decrementAndGet();        }        notifyCloseConnection(session);    }    /**     * New login.     */    public synchronized void setLogin(final FtpIoSession session) {        currLogins.incrementAndGet();        totalLogins.incrementAndGet();        User user = session.getUser();        if ("anonymous".equals(user.getName())) {            currAnonLogins.incrementAndGet();            totalAnonLogins.incrementAndGet();        }        synchronized (user) {// thread safety is needed. Since the login occurrs            // at low frequency, this overhead is endurable            UserLogins statisticsTable = userLoginTable.get(user.getName());            if (statisticsTable == null) {                // the hash table that records the login information of the user                // and its ip address.                InetAddress address = null;                if (session.getRemoteAddress() instanceof InetSocketAddress) {                    address = ((InetSocketAddress) session.getRemoteAddress())                            .getAddress();                }                statisticsTable = new UserLogins(address);                userLoginTable.put(user.getName(), statisticsTable);            } else {                statisticsTable.totalLogins.incrementAndGet();                if (session.getRemoteAddress() instanceof InetSocketAddress) {                    InetAddress address = ((InetSocketAddress) session                            .getRemoteAddress()).getAddress();                    statisticsTable.loginsFromInetAddress(address)                            .incrementAndGet();                }            }        }        notifyLogin(session);    }    /**     * Increment failed login count.     */    public synchronized void setLoginFail(final FtpIoSession session) {        totalFailedLogins.incrementAndGet();        notifyLoginFail(session);    }    /**     * User logout     */    public synchronized void setLogout(final FtpIoSession session) {        User user = session.getUser();        if (user == null) {            return;        }        currLogins.decrementAndGet();        if ("anonymous".equals(user.getName())) {            currAnonLogins.decrementAndGet();        }        synchronized (user) {            UserLogins userLogins = userLoginTable.get(user.getName());            if (userLogins != null) {                userLogins.totalLogins.decrementAndGet();                if (session.getRemoteAddress() instanceof InetSocketAddress) {                    InetAddress address = ((InetSocketAddress) session                            .getRemoteAddress()).getAddress();                    userLogins.loginsFromInetAddress(address).decrementAndGet();                }            }        }        notifyLogout(session);    }    // //////////////////////////////////////////////////////////    // /////////////// all observer methods ////////////////////    /**     * Observer upload notification.     */    private void notifyUpload(final FtpIoSession session,            final FtpFile file, long size) {        StatisticsObserver observer = this.observer;        if (observer != null) {            observer.notifyUpload();        }        FileObserver fileObserver = this.fileObserver;        if (fileObserver != null) {            fileObserver.notifyUpload(session, file, size);        }    }    /**     * Observer download notification.     */    private void notifyDownload(final FtpIoSession session,            final FtpFile file, final long size) {        StatisticsObserver observer = this.observer;        if (observer != null) {            observer.notifyDownload();        }        FileObserver fileObserver = this.fileObserver;        if (fileObserver != null) {            fileObserver.notifyDownload(session, file, size);        }    }    /**     * Observer delete notification.     */    private void notifyDelete(final FtpIoSession session, final FtpFile file) {        StatisticsObserver observer = this.observer;        if (observer != null) {            observer.notifyDelete();        }        FileObserver fileObserver = this.fileObserver;        if (fileObserver != null) {            fileObserver.notifyDelete(session, file);        }    }    /**     * Observer make directory notification.     */    private void notifyMkdir(final FtpIoSession session, final FtpFile file) {        StatisticsObserver observer = this.observer;        if (observer != null) {            observer.notifyMkdir();        }        FileObserver fileObserver = this.fileObserver;        if (fileObserver != null) {            fileObserver.notifyMkdir(session, file);        }    }    /**     * Observer remove directory notification.     */    private void notifyRmdir(final FtpIoSession session, final FtpFile file) {        StatisticsObserver observer = this.observer;        if (observer != null) {            observer.notifyRmdir();        }        FileObserver fileObserver = this.fileObserver;        if (fileObserver != null) {            fileObserver.notifyRmdir(session, file);        }    }    /**     * Observer open connection notification.     */    private void notifyOpenConnection(final FtpIoSession session) {        StatisticsObserver observer = this.observer;        if (observer != null) {            observer.notifyOpenConnection();        }    }    /**     * Observer close connection notification.     */    private void notifyCloseConnection(final FtpIoSession session) {        StatisticsObserver observer = this.observer;        if (observer != null) {            observer.notifyCloseConnection();        }    }    /**     * Observer login notification.     */    private void notifyLogin(final FtpIoSession session) {        StatisticsObserver observer = this.observer;        if (observer != null) {            // is anonymous login            User user = session.getUser();            boolean anonymous = false;            if (user != null) {                String login = user.getName();                anonymous = (login != null) && login.equals("anonymous");            }            observer.notifyLogin(anonymous);        }    }    /**     * Observer failed login notification.     */    private void notifyLoginFail(final FtpIoSession session) {        StatisticsObserver observer = this.observer;        if (observer != null) {            if (session.getRemoteAddress() instanceof InetSocketAddress) {                observer.notifyLoginFail(((InetSocketAddress) session                        .getRemoteAddress()).getAddress());            }        }    }    /**     * Observer logout notification.     */    private void notifyLogout(final FtpIoSession session) {        StatisticsObserver observer = this.observer;        if (observer != null) {            // is anonymous login            User user = session.getUser();            boolean anonymous = false;            if (user != null) {                String login = user.getName();                anonymous = (login != null) && login.equals("anonymous");            }            observer.notifyLogout(anonymous);        }    }    /**     * Reset the cumulative counters.     */    public synchronized void resetStatisticsCounters() {        startTime = new Date();        uploadCount.set(0);        downloadCount.set(0);        deleteCount.set(0);        mkdirCount.set(0);        rmdirCount.set(0);        totalLogins.set(0);        totalFailedLogins.set(0);        totalAnonLogins.set(0);        totalConnections.set(0);        bytesUpload.set(0);        bytesDownload.set(0);    }}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?