defaultftpstatistics.java
来自「JAVA FTP 上传下载 的源文件」· Java 代码 · 共 583 行 · 第 1/2 页
JAVA
583 行
/* * Licensed to the Apache Software Foundation (ASF) under one * or more contributor license agreements. See the NOTICE file * distributed with this work for additional information * regarding copyright ownership. The ASF licenses this file * to you under the Apache License, Version 2.0 (the * "License"); you may not use this file except in compliance * with the License. You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, * software distributed under the License is distributed on an * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY * KIND, either express or implied. See the License for the * specific language governing permissions and limitations * under the License. */package org.apache.ftpserver.impl;import java.net.InetAddress;import java.net.InetSocketAddress;import java.util.Date;import java.util.Map;import java.util.concurrent.ConcurrentHashMap;import java.util.concurrent.atomic.AtomicInteger;import java.util.concurrent.atomic.AtomicLong;import org.apache.ftpserver.ftplet.FtpFile;import org.apache.ftpserver.ftplet.User;/** * <strong>Internal class, do not use directly.</strong> * * This is FTP statistics implementation. * * TODO revisit concurrency, right now we're a bit over zealous with both Atomic* * counters and synchronization * * @author The Apache MINA Project (dev@mina.apache.org) * @version $Rev: 733158 $, $Date: 2009-01-09 22:37:49 +0100 (Fri, 09 Jan 2009) $ */public class DefaultFtpStatistics implements ServerFtpStatistics { private StatisticsObserver observer = null; private FileObserver fileObserver = null; private Date startTime = new Date(); private AtomicInteger uploadCount = new AtomicInteger(0); private AtomicInteger downloadCount = new AtomicInteger(0); private AtomicInteger deleteCount = new AtomicInteger(0); private AtomicInteger mkdirCount = new AtomicInteger(0); private AtomicInteger rmdirCount = new AtomicInteger(0); private AtomicInteger currLogins = new AtomicInteger(0); private AtomicInteger totalLogins = new AtomicInteger(0); private AtomicInteger totalFailedLogins = new AtomicInteger(0); private AtomicInteger currAnonLogins = new AtomicInteger(0); private AtomicInteger totalAnonLogins = new AtomicInteger(0); private AtomicInteger currConnections = new AtomicInteger(0); private AtomicInteger totalConnections = new AtomicInteger(0); private AtomicLong bytesUpload = new AtomicLong(0L); private AtomicLong bytesDownload = new AtomicLong(0L); private static class UserLogins { private Map<InetAddress, AtomicInteger> perAddress = new ConcurrentHashMap<InetAddress, AtomicInteger>(); public UserLogins(InetAddress address) { // init with the first connection totalLogins = new AtomicInteger(1); perAddress.put(address, new AtomicInteger(1)); } public AtomicInteger loginsFromInetAddress(InetAddress address) { AtomicInteger logins = perAddress.get(address); if (logins == null) { logins = new AtomicInteger(0); perAddress.put(address, logins); } return logins; } public AtomicInteger totalLogins; } /** *The user login information. */ private Map<String, UserLogins> userLoginTable = new ConcurrentHashMap<String, UserLogins>(); public static final String LOGIN_NUMBER = "login_number"; /** * Set the observer. */ public void setObserver(final StatisticsObserver observer) { this.observer = observer; } /** * Set the file observer. */ public void setFileObserver(final FileObserver observer) { fileObserver = observer; } // ////////////////////////////////////////////////////// // /////////////// All getter methods ///////////////// /** * Get server start time. */ public Date getStartTime() { if (startTime != null) { return (Date) startTime.clone(); } else { return null; } } /** * Get number of files uploaded. */ public int getTotalUploadNumber() { return uploadCount.get(); } /** * Get number of files downloaded. */ public int getTotalDownloadNumber() { return downloadCount.get(); } /** * Get number of files deleted. */ public int getTotalDeleteNumber() { return deleteCount.get(); } /** * Get total number of bytes uploaded. */ public long getTotalUploadSize() { return bytesUpload.get(); } /** * Get total number of bytes downloaded. */ public long getTotalDownloadSize() { return bytesDownload.get(); } /** * Get total directory created. */ public int getTotalDirectoryCreated() { return mkdirCount.get(); } /** * Get total directory removed. */ public int getTotalDirectoryRemoved() { return rmdirCount.get(); } /** * Get total number of connections. */ public int getTotalConnectionNumber() { return totalConnections.get(); } /** * Get current number of connections. */ public int getCurrentConnectionNumber() { return currConnections.get(); } /** * Get total number of logins. */ public int getTotalLoginNumber() { return totalLogins.get(); } /** * Get total failed login number. */ public int getTotalFailedLoginNumber() { return totalFailedLogins.get(); } /** * Get current number of logins. */ public int getCurrentLoginNumber() { return currLogins.get(); } /** * Get total number of anonymous logins. */ public int getTotalAnonymousLoginNumber() { return totalAnonLogins.get(); } /** * Get current number of anonymous logins. */ public int getCurrentAnonymousLoginNumber() { return currAnonLogins.get(); } /** * Get the login number for the specific user */ public synchronized int getCurrentUserLoginNumber(final User user) { UserLogins userLogins = userLoginTable.get(user.getName()); if (userLogins == null) {// not found the login user's statistics info return 0; } else { return userLogins.totalLogins.get(); } } /** * Get the login number for the specific user from the ipAddress * * @param user * login user account * @param ipAddress * the ip address of the remote user */ public synchronized int getCurrentUserLoginNumber(final User user, final InetAddress ipAddress) { UserLogins userLogins = userLoginTable.get(user.getName()); if (userLogins == null) {// not found the login user's statistics info return 0; } else { return userLogins.loginsFromInetAddress(ipAddress).get(); } } // ////////////////////////////////////////////////////// // /////////////// All setter methods ///////////////// /** * Increment upload count. */ public synchronized void setUpload(final FtpIoSession session, final FtpFile file, final long size) { uploadCount.incrementAndGet(); bytesUpload.addAndGet(size); notifyUpload(session, file, size); } /** * Increment download count. */ public synchronized void setDownload(final FtpIoSession session, final FtpFile file, final long size) { downloadCount.incrementAndGet(); bytesDownload.addAndGet(size); notifyDownload(session, file, size); } /** * Increment delete count. */ public synchronized void setDelete(final FtpIoSession session, final FtpFile file) { deleteCount.incrementAndGet(); notifyDelete(session, file); }
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?