View Javadoc

1   /*
2    * ------------------------------------------------------------------------------
3    * Hermes FTP Server
4    * Copyright (c) 2005-2007 Lars Behnke
5    * ------------------------------------------------------------------------------
6    * 
7    * This file is part of Hermes FTP Server.
8    * 
9    * Hermes FTP Server is free software; you can redistribute it and/or modify
10   * it under the terms of the GNU General Public License as published by
11   * the Free Software Foundation; either version 2 of the License, or
12   * (at your option) any later version.
13   * 
14   * Hermes FTP Server is distributed in the hope that it will be useful,
15   * but WITHOUT ANY WARRANTY; without even the implied warranty of
16   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17   * GNU General Public License for more details.
18   * 
19   * You should have received a copy of the GNU General Public License
20   * along with Hermes FTP Server; if not, write to the Free Software
21   * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
22   * ------------------------------------------------------------------------------
23   */
24  
25  package net.sf.hermesftp.console;
26  
27  import java.util.List;
28  import java.util.Map;
29  import java.util.Properties;
30  
31  import net.sf.hermesftp.common.FtpConstants;
32  import net.sf.hermesftp.usermanager.UserManager;
33  import net.sf.hermesftp.usermanager.model.UserData;
34  
35  /***
36   * Provides an overview of system and application properties.
37   * 
38   * @author Administrator
39   */
40  public class UserServlet extends AbstractConsoleServlet {
41  
42      private static final String NOT_APPLICABLE   = "n/a";
43  
44      private static final long   serialVersionUID = -594524060863329206L;
45  
46      private UserManager         userManager;
47  
48      /***
49       * {@inheritDoc}
50       */
51      protected Properties getContentProperties() {
52          Properties result = new Properties();
53          String s = userManager != null ? createUserTable() : NOT_APPLICABLE;
54          result.put("user.table", s);
55          return result;
56      }
57  
58      private String createUserTable() {
59  
60          List<UserData> list = null;
61          try {
62              list = getUserManager().getUserDataList();
63          } catch (Exception e) {
64              return "not available";
65          }
66          StringBuffer sb = new StringBuffer();
67          sb.append(ConsoleConstants.TABLE_START_TAG);
68          createColumnHeaders(sb);
69  
70          int rowCount = 0;
71          for (UserData userData : list) {
72              rowCount++;
73              fillRow(sb, rowCount, userData);
74          }
75  
76          sb.append(ConsoleConstants.TABLE_END_TAG);
77          return sb.toString();
78      }
79  
80      private void fillRow(StringBuffer sb, int rowCount, UserData user) {
81          String band = rowCount % 2 == 0 ? ConsoleConstants.TR_START_TAG_CLASS_A
82                  : ConsoleConstants.TR_START_TAG_CLASS_B;
83          sb.append(band);
84  
85          fillField(sb, user.getUid());
86          fillField(sb, user.getFullName());
87          fillNumField(sb, getUserStat(user.getUid(), FtpConstants.STAT_BYTES_DOWNLOADED));
88          fillNumField(sb, getUserStat(user.getUid(), FtpConstants.STAT_BYTES_UPLOADED));
89          fillNumField(sb, getUserStat(user.getUid(), FtpConstants.STAT_FILES_DOWNLOADED));
90          fillNumField(sb, getUserStat(user.getUid(), FtpConstants.STAT_FILES_UPLOADED));
91          fillNumField(sb, getUserStat(user.getUid(), FtpConstants.STAT_DOWNLOAD_RATE));
92          fillNumField(sb, getUserStat(user.getUid(), FtpConstants.STAT_UPLOAD_RATE));
93  
94          sb.append(ConsoleConstants.TR_END_TAG);
95      }
96  
97      private void createColumnHeaders(StringBuffer sb) {
98          sb.append(ConsoleConstants.TR_START_TAG);
99          sb.append(ConsoleConstants.TH_START_TAG + "User ID" + ConsoleConstants.TH_END_TAG);
100         sb.append(ConsoleConstants.TH_START_TAG + "Name" + ConsoleConstants.TH_END_TAG);
101         sb.append(ConsoleConstants.TH_START_TAG + "DL Bytes" + ConsoleConstants.TH_END_TAG);
102         sb.append(ConsoleConstants.TH_START_TAG + "UL Bytes" + ConsoleConstants.TH_END_TAG);
103         sb.append(ConsoleConstants.TH_START_TAG + "DL Files" + ConsoleConstants.TH_END_TAG);
104         sb.append(ConsoleConstants.TH_START_TAG + "UL Files" + ConsoleConstants.TH_END_TAG);
105         sb.append(ConsoleConstants.TH_START_TAG + "DL KB/s" + ConsoleConstants.TH_END_TAG);
106         sb.append(ConsoleConstants.TH_START_TAG + "UL KB/s" + ConsoleConstants.TH_END_TAG);
107         sb.append(ConsoleConstants.TR_END_TAG);
108     }
109 
110     private String getUserStat(String uid, String key) {
111         String result;
112         Map<String, Long> userStats = getUserManager().getUserStatistics(uid);
113         if (userStats != null) {
114             Long val = userStats.get(key);
115             result = val == null ? "0" : formatNum(val);
116         } else {
117             result = NOT_APPLICABLE;
118         }
119         return result;
120     }
121 
122     /***
123      * Getter methode for property <code>userManager</code>.
124      * 
125      * @return Property <code>userManager</code>.
126      */
127     public UserManager getUserManager() {
128         return userManager;
129     }
130 
131     /***
132      * Setter methode for property <code>userManager</code>.
133      * 
134      * @param userManager Value for <code>userManager</code>.
135      */
136     public void setUserManager(UserManager userManager) {
137         this.userManager = userManager;
138     }
139 
140 }