KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > drftpd > plugins > XferLog


1 /*
2  * This file is part of DrFTPD, Distributed FTP Daemon.
3  *
4  * DrFTPD is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation; either version 2 of the License, or
7  * (at your option) any later version.
8  *
9  * DrFTPD is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with DrFTPD; if not, write to the Free Software
16  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17  */

18 package org.drftpd.plugins;
19
20 import java.io.File JavaDoc;
21 import java.io.FileOutputStream JavaDoc;
22 import java.io.IOException JavaDoc;
23 import java.io.PrintStream JavaDoc;
24 import java.text.SimpleDateFormat JavaDoc;
25 import java.util.Date JavaDoc;
26 import java.util.Locale JavaDoc;
27
28 import net.sf.drftpd.FatalException;
29 import net.sf.drftpd.event.Event;
30 import net.sf.drftpd.event.FtpListener;
31 import net.sf.drftpd.event.TransferEvent;
32 import net.sf.drftpd.master.ConnectionManager;
33
34 /**
35  * @see http://www.wu-ftpd.org/man/xferlog.html
36  * @author mog
37  * @version $Id: XferLog.java,v 1.2 2004/04/23 00:47:25 mog Exp $
38  */

39 public class XferLog implements FtpListener {
40
41     public XferLog() {
42         super();
43         new File JavaDoc("logs").mkdirs();
44         try {
45             //APPEND
46
_out = new PrintStream JavaDoc(new FileOutputStream JavaDoc("logs/xferlog", true));
47         } catch (IOException JavaDoc e) {
48             throw new FatalException(e);
49         }
50     }
51
52     public void actionPerformed(Event event) {
53         if (event instanceof TransferEvent)
54             actionPerformed((TransferEvent) event);
55     }
56
57     /**
58      * xferlog.log - Contains all the upload/download information for all files
59               transferred (if logging of that is enabled). The format is the
60               following: current time, transfer time, user's hostname, number
61               of bytes sent, filename, 'a' if transfer was in ASCII mode or
62               'b' if BINARY, _ (meaningless), 'i' if incoming (user uploading)
63               or 'o' if outgoing (user downloading), 'r' (no meaning), user's
64               name, user's group, 1 if user had ident or 0 if not, user's ident
65     
66                   current-time transfer-time remote-host file-
67               size filename transfer-type special-action-
68               flag direction access-mode username ser?
69               vice-name authentication-method authenticated-
70               user-id completion-status
71     
72     example lines:
73     Mon Aug 11 14:03:30 2003 20 hostname 15000000 /path/to/file b _ i r user group 0 *
74     Mon Aug 11 14:03:31 2003 33 hostname 15000000 /path/to/file b _ i r user group 1 user
75     Mon Aug 11 14:03:44 2003 13 hostname 15000000 /path/to/file b _ i r user group 0 *
76      */

77     public static SimpleDateFormat JavaDoc DATE_FMT =
78         new SimpleDateFormat JavaDoc("EEE MMM d HH:mm:ss yyyy", Locale.ENGLISH);
79     private PrintStream JavaDoc _out;
80
81     public void actionPerformed(TransferEvent event) {
82         char direction;
83         if (event.getCommand().equals("STOR")) {
84             direction = 'i';
85         } else if (event.getCommand().equals("RETR")) {
86             direction = 'o';
87         } else {
88             return;
89         }
90
91         char transferType;
92         if (event.getType() == 'I') { // IMAGE
93
transferType = 'b';
94         } else if (event.getType() == 'A') { // ASCII
95
transferType = 'a';
96         } else {
97             throw new FatalException("Invalid transfer type");
98         }
99
100         char completed = event.isComplete() ? 'c' : 'i';
101         _out.println(
102             DATE_FMT.format(new Date JavaDoc(event.getTime()))
103                 + " "
104                 + event.getDirectory().getXfertime() / 1000
105                 + " "
106                 + event.getPeer().getHostName()
107                 + " "
108                 + event.getDirectory().length()
109                 + " "
110                 + event.getDirectory().getPath()
111                 + " "
112                 + transferType
113                 + " _ "
114                 + direction
115                 + " r "
116                 + event.getUser().getUsername()
117                 + " "
118                 + event.getUser().getGroupName()
119                 + " 0 * " // authentication-method authenticated-user-id
120
+ completed);
121     }
122
123     public void init(ConnectionManager mgr) {
124     }
125
126     public void unload() {
127     }
128
129 }
130
Popular Tags