Docjar: A Java Source and Docuemnt Enginecom.*    java.*    javax.*    org.*    all    new    plug-in

Quick Search    Search Deep

Source code: jsd/ftp/server/ftp/FtpConfig.java


1   /*
2    * ----------------------------------------------------------------------------
3    * JStrangeDownloader and all accompanying source code files are
4    * Copyright (C) 2002 Dusty Davidson (dustyd@iastate.edu)
5    * 
6    * This program is free software; you can redistribute it and/or
7    * modify it under the terms of the GNU General Public License
8    * as published by the Free Software Foundation; either version 2
9    * of the License, or (at your option) any later version.
10   * 
11   * This program is distributed in the hope that it will be useful,
12   * but WITHOUT ANY WARRANTY; without even the implied warranty of
13   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14   * GNU General Public License for more details.
15   * 
16   * You should have received a copy of the GNU General Public License
17   * along with this program; if not, write to the Free Software
18   * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
19   * ----------------------------------------------------------------------------
20   *  
21   * Please see gpl.txt for the full text of the GNU General Public
22   * License.
23   */
24  
25  package jsd.ftp.server.ftp;
26  
27  import java.io.File;
28  import java.net.InetAddress;
29  import java.lang.reflect.Constructor;
30  
31  import jsd.ftp.io.LogFile;
32  import jsd.ftp.util.BaseProperties;
33  import jsd.ftp.util.AsyncMessageQueue;
34  import jsd.ftp.server.IpRestrictor;
35  import jsd.ftp.server.ftp.usermanager.UserManager;
36  import jsd.ftp.server.ftp.usermanager.PropertiesUserManager;
37  
38  
39  /**
40   * Ftp configuration class. It has all ftp server configuration 
41   * parameters. This is not hot-editable. Parameters will be loaded 
42   * once during server startup. We can add our own config parameters.
43   * 
44   * @author <a href="mailto:rana_b@yahoo.com">Rana Bhattacharyya</a>
45   */
46  public
47  class FtpConfig extends BaseProperties {
48      
49      /**
50       * Config properties prefix.
51       */
52      public final static String PREFIX    = "FtpServer.server.config.";
53      
54      
55      private final static String LOG_FILE = "ftp.log";
56      private final static String IP_PROP  = "ip.properties";
57      
58      private File mCfgFile                 = null;
59      
60      private LogFile mFtpLog               = null;
61      
62      private FtpStatus  mStatus            = null;
63      private ConnectionService mConService = null;
64      private IpRestrictor mIpRestrictor    = null;
65      private UserManager mUserManager      = null;
66      
67      private InetAddress  mServerAddress   = null;
68      private InetAddress  mSelfAddress     = null;
69      
70      private FtpStatistics mStatistics     = null;   
71      
72      private int miServerPort;
73      private int miMaxThread;
74      private int miMaxLogin;
75      private int miAnonLogin;
76      private int miPollInterval;
77      private int miDefaultIdle;
78      
79      private int miLogLevel;
80      private long mlLogMaxSize;
81      private boolean mbLogFlush;
82      private boolean mbAnonAllowed;
83      private boolean mbAllowIp;
84      
85      private File mDataDir;
86      private File mDefaultRoot;
87      
88      private AsyncMessageQueue mQueue;
89           
90      /**
91       * Constructor - read the configuration file.
92       */
93      public FtpConfig(File cfgFile) throws Exception {
94          super(cfgFile);
95          mCfgFile = cfgFile;
96          
97          // get config properties
98  //        mServerAddress = getInetAddress(PREFIX + "ip.external", null);
99          mServerAddress = InetAddress.getByName(jsd.options.OptionPane.getFtpIP());
100         mSelfAddress   = null;
101 //        mSelfAddress   = getInetAddress(PREFIX + "ip.internal", null);
102         miServerPort   = getInteger(PREFIX + "port", 21);
103         miMaxThread    = getInteger(PREFIX + "thread", 25);
104         miMaxLogin     = getInteger(PREFIX + "login", 20);
105         mbAnonAllowed  = getBoolean(PREFIX + "anonymous", true);
106         miAnonLogin    = getInteger(PREFIX + "anonymous.login", 10);
107         miPollInterval = getInteger(PREFIX + "poll.interval", 60);
108         mlLogMaxSize   = getLong(PREFIX + "log.size", 1024)*1024;
109         mbLogFlush     = getBoolean(PREFIX + "log.flush", false);
110         miLogLevel     = getInteger(PREFIX + "log.level", 1);
111         mDefaultRoot   = new File(jsd.options.OptionPane.getInstallDir() + File.separator + "Downloads");
112 //        mDefaultRoot   = getFile(PREFIX + "root.dir", new File("/"));
113         miDefaultIdle  = getInteger(PREFIX + "idle.time", 300);
114         mDataDir       = new File(jsd.options.OptionPane.getInstallDir() + File.separator);
115 //        mDataDir       = getFile(PREFIX + "data", new File("./data"));
116         mbAllowIp      = getBoolean(PREFIX + "ip.allow", false);
117         
118         // get self address
119 //        mSelfAddress = mServerAddress;
120         if (mSelfAddress == null) {
121             mSelfAddress = InetAddress.getLocalHost();
122         }
123         if (mServerAddress == null) {
124           mServerAddress = mSelfAddress;
125         }
126         
127         // open log
128         File logDir = new File(mDataDir, "log");
129         if (!logDir.exists()) {
130             logDir.mkdirs();
131         }
132         mFtpLog = new LogFile(new File(logDir, LOG_FILE));
133         mFtpLog.setMaxSize(mlLogMaxSize);
134         mFtpLog.setAutoFlush(mbLogFlush);
135         mFtpLog.setLogLevel(miLogLevel);
136         
137         // open ip restrictor
138         File ipDat = new File(mDataDir, IP_PROP);
139         if (!ipDat.exists()){
140             ipDat.createNewFile();
141         }
142         mIpRestrictor = new IpRestrictor(ipDat, mbAllowIp);
143         
144         // instantiate user manager
145         Class managerClass = getClass(PREFIX + "user.manager", PropertiesUserManager.class);
146         Constructor cons   = managerClass.getConstructor(new Class[] {getClass()});
147         mUserManager       = (UserManager)cons.newInstance(new Object[] {this});
148         
149         // open global statistics object
150         mStatistics = new FtpStatistics(this);
151         
152         // open user service
153         mConService = new ConnectionService(this);
154         
155         // open ftp status
156         mStatus = new FtpStatus();
157         
158         // open message queue
159         mQueue = new AsyncMessageQueue();
160         mQueue.setMaxSize(4096);
161         mFtpLog.info("Configuration loaded " + mCfgFile.getAbsolutePath());
162     }
163     
164     /**
165      * Get config file.
166      */
167     public File getConfigFile() {
168         return mCfgFile;
169     } 
170     
171     /**
172      * Get server port.
173      */
174     public int getServerPort()  {
175         return miServerPort;
176     }
177     
178     /**
179      * Get server bind address.
180      */
181     public InetAddress getServerAddress() {
182         return mServerAddress;
183     }
184      
185     /**
186      * Get self address
187      */ 
188     public InetAddress getSelfAddress() {
189         return mSelfAddress;
190     } 
191      
192     /**
193      * Get thread count
194      */
195     public int getThreadCount() {
196         return miMaxThread;
197     }
198     
199     /**
200      * Check annonymous login support.
201      */
202     public boolean isAnonymousLoginAllowed() {
203         return mbAnonAllowed;
204     } 
205     
206     /**
207      * Get resource directory.
208      */
209     public File getDataDir() {
210         return mDataDir;
211     }
212     
213     /**
214      * Allow Ip
215      */
216     public boolean isAllowIp() {
217         return mbAllowIp;
218     }
219     
220     /**
221      * Get ftp status resource.
222      */
223     public FtpStatus getStatus() {
224         return mStatus;
225     }
226     
227     /**
228      * Get connection service.
229      */
230     public ConnectionService getConnectionService() {
231         return mConService;
232     } 
233     
234     /**
235      * Get user manager.
236      */
237     public UserManager getUserManager() {
238         return mUserManager;
239     }
240     
241     /**
242      * Get maximum number of connections.
243      */
244     public int getMaxConnections() {
245         return miMaxLogin;
246     }
247     
248     /**
249      * Get maximum number of anonymous connections.
250      */
251     public int getMaxAnonymousLogins() {
252         if(!isAnonymousLoginAllowed()) {
253             return 0;
254         }
255         return miAnonLogin;
256     }
257     
258     /**
259      * Get poll interval in seconds.
260      */
261     public int getSchedulerInterval() {
262         return miPollInterval;
263     } 
264      
265     /**
266      * Get default idle time in seconds.
267      */ 
268     public int getDefaultIdleTime() {
269         return miDefaultIdle;
270     } 
271      
272     /**
273      * Get default root directory
274      */ 
275     public File getDefaultRoot() {
276         return mDefaultRoot;
277     }
278      
279     /**
280      * Get ftp log to write log entries.
281      */
282     public LogFile getLogger() {
283         return mFtpLog;
284     }
285     
286     /**
287      * Get IP restrictor object.
288      */
289     public IpRestrictor getIpRestrictor() {
290         return mIpRestrictor;
291     }
292      
293     /**
294      * Get global statistics object.
295      */
296     public FtpStatistics getStatistics() {
297         return mStatistics;
298     } 
299      
300     /**
301      * Get message queue
302      */ 
303     public AsyncMessageQueue getMessageQueue() {
304         return mQueue;
305     } 
306      
307     /**
308      * Get the system name.
309      */
310     public String getSystemName() {
311         String systemName = System.getProperty("os.name");
312         if(systemName == null) {
313             systemName = "UNKNOWN";
314         }
315         else {
316             systemName = systemName.toUpperCase();
317             systemName = systemName.replace(' ', '-');
318         }
319         return systemName;
320     }
321      
322     /**
323      * Close this config and all the related resources. Ftp server
324      * <code>FtpServer.dispose()</code> method will call this method.
325      */
326     public void dispose() {
327         if(mConService != null) {
328             mFtpLog.info("Closing connection service.");
329             mConService.dispose();
330             mConService = null;
331         }
332         
333         if (mUserManager != null) {
334             mFtpLog.info("Closing user manager.");
335             mUserManager.dispose();
336             mUserManager = null;
337         }
338         
339         if (mQueue != null) {
340             mFtpLog.info("Closing message queue.");
341             mQueue.dispose();
342             mQueue = null;
343         }
344         
345         if(mFtpLog != null) {
346             mFtpLog.info("Closing log file.");
347             mFtpLog.dispose();
348             mFtpLog = null;
349         }
350     } 
351      
352 }
353