View Javadoc

1   /*
2    *  XNap - A P2P framework and client.
3    *
4    *  See the file AUTHORS for copyright information.
5    *
6    *  This program is free software; you can redistribute it and/or modify
7    *  it under the terms of the GNU General Public License as published by
8    *  the Free Software Foundation.
9    *
10   *  This program is distributed in the hope that it will be useful,
11   *  but WITHOUT ANY WARRANTY; without even the implied warranty of
12   *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13   *  GNU General Public License for more details.
14   *
15   *  You should have received a copy of the GNU General Public License
16   *  along with this program; if not, write to the Free Software
17   *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
18   */
19  
20  package org.xnap.transfer;
21  
22  import java.beans.PropertyChangeEvent;
23  import java.beans.PropertyChangeListener;
24  
25  import org.apache.log4j.Logger;
26  
27  /***
28   * This class acts as data container for {@link Upload} objects.
29   *
30   * <p>The class makes use of the singleton pattern.</p>
31   */
32  public class UploadManager extends AbstractTransferManager
33      implements PropertyChangeListener
34  {
35  
36      //--- Constant(s) ---
37  
38      //--- Data field(s) ---
39  
40      private static UploadManager singleton = new UploadManager();
41  
42      //--- Constructor(s) ---
43  
44      private UploadManager()
45      {
46  		logger = Logger.getLogger(UploadManager.class);
47  
48  		prefs.addPropertyChangeListener("limitUploads", this);
49  		prefs.addPropertyChangeListener("maxUploads", this);
50  		prefs.addPropertyChangeListener("autoClearTransfers", this);
51  		prefs.addPropertyChangeListener("autoClearTransfersInterval", this);
52  
53  		updateLimit();
54      }
55  
56      //--- Method(s) ---
57  	
58      public static UploadManager getInstance()
59      {
60  		return singleton;
61      }
62  
63      public synchronized void add(Upload d) 
64      {
65  		super.add(d);
66      }
67  
68      public void propertyChange(PropertyChangeEvent e)
69      {
70  		updateLimit();
71      }
72  
73      public synchronized void remove(Upload d) 
74      {
75  		super.remove(d);
76      }
77         
78      private void updateLimit()
79      {
80  		getQueue().setMaxSlots(prefs.getLimitUploads() 
81  							   ? prefs.getMaxUploads() 
82  							   : Integer.MAX_VALUE);
83  		setAutoClearInterval(prefs.getAutoClearTransfers() 
84  							 ? prefs.getAutoClearTransfersInterval()
85  							 : -1);
86      }
87  
88  }