KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > net > suberic > pooka > StoreManager


1 package net.suberic.pooka;
2
3 import javax.mail.*;
4 import java.util.*;
5 import net.suberic.util.*;
6
7 /**
8  * This class manages the a list of StoreInfos. It also provides some
9  * convenience methods for accessing FolderInfos within the StoreInfos,
10  * and for adding and removing StoreInfos.
11  */

12
13 public class StoreManager implements ItemCreator, ItemListChangeListener {
14
15   private ItemManager manager;
16   private LinkedList listenerList = new LinkedList();
17
18   public StoreManager() {
19     createStoreList();
20   }
21
22   //-----------------------
23
// public interface.
24

25   /**
26    * As defined in net.suberic.util.ValueChangeListener.
27    *
28    * This listens for ItemListChangeEvents, which result from changes to the
29    * "Store" property. The result is that refrestStoreInfos() is called,
30    * and then the event is passed to listeners to this object.
31    */

32   public void itemListChanged(ItemListChangeEvent e) {
33     fireItemListChanged(e);
34     refreshStoreInfos(e);
35   }
36
37   /**
38    * This returns a Vector with all the currently registered StoreInfo
39    * objects.
40    */

41   public java.util.Vector JavaDoc getStoreList() {
42     return manager.getItems();
43   }
44
45   /**
46    * This adds the store with the given storeName to the allStores list.
47    */

48   public void addStore(String JavaDoc storeName) {
49     manager.addItem(storeName);
50   }
51
52   /**
53    * This adds the stores with the given storeNames to the allStores list.
54    */

55   public void addStore(String JavaDoc[] storeName) {
56     manager.addItem(storeName);
57   }
58
59   /**
60    * This removes the store with the given storeName.
61    */

62   public void removeStore(String JavaDoc storeName) {
63     manager.removeItem(storeName);
64   }
65
66   /**
67    * This removes the stores with the given storeNames.
68    */

69   public void removeStore(String JavaDoc[] storeNames) {
70     manager.removeItem(storeNames);
71   }
72
73   /**
74    * This removes the given StoreInfo.
75    */

76   public void removeStore(StoreInfo store) {
77     manager.removeItem(store);
78   }
79
80   /**
81    * This removes the given StoreInfos.
82    */

83   public void removeStore(StoreInfo[] stores) {
84     manager.removeItem(stores);
85   }
86
87   /**
88    * This compares the storeList object with the Store property, and
89    * updates the storeList appropriately.
90    */

91   public void refreshStoreInfos(ItemListChangeEvent e) {
92     Item[] removedStores = e.getRemoved();
93     for (int i = 0; removedStores != null && i < removedStores.length; i++) {
94       ((StoreInfo) removedStores[i]).remove();
95     }
96   }
97
98   /**
99    * This returns the FolderInfo which corresponds to the given folderName.
100    * The folderName should be in the form "/storename/folder/subfolder".
101    */

102   public FolderInfo getFolder(String JavaDoc folderName) {
103     if (folderName != null && folderName.length() >= 1) {
104       int divider = folderName.indexOf('/', 1);
105       while (divider == 0) {
106   folderName = folderName.substring(1);
107   divider = folderName.indexOf('/');
108       }
109
110       if (divider > 0) {
111   String JavaDoc storeName = folderName.substring(0, divider);
112   StoreInfo store = getStoreInfo(storeName);
113   if (store != null) {
114     return store.getChild(folderName.substring(divider +1));
115   }
116       }
117     }
118
119     return null;
120   }
121
122   /**
123    * This returns the FolderInfo which corresponds to the given folderID.
124    * The folderName should be in the form "storename.folderID.folderID".
125    */

126   public FolderInfo getFolderById(String JavaDoc folderID) {
127     // hurm. the problem here is that '.' is a legal value in a name...
128

129     java.util.Vector JavaDoc allStores = getStoreList();
130
131     for (int i = 0; i < allStores.size(); i++) {
132       StoreInfo currentStore = (StoreInfo) allStores.elementAt(i);
133       if (folderID.startsWith(currentStore.getStoreID())) {
134   FolderInfo possibleMatch = currentStore.getFolderById(folderID);
135   if (possibleMatch != null) {
136     return possibleMatch;
137   }
138       }
139     }
140
141     return null;
142   }
143
144   /**
145    * Gets all of the open and available folders known by the system.
146    */

147   public Vector getAllOpenFolders() {
148     Vector returnValue = new Vector();
149     Vector currentStores = getStoreList();
150     for (int i = 0; i < currentStores.size(); i++) {
151       returnValue.addAll(((StoreInfo) currentStores.elementAt(i)).getAllFolders());
152     }
153
154     return returnValue;
155   }
156
157   /**
158    * This returns the StoreInfo with the given storeName if it exists
159    * in the allStores Vector; otherwise, returns null.
160    */

161   public StoreInfo getStoreInfo(String JavaDoc storeID) {
162     return (StoreInfo) manager.getItem(storeID);
163   }
164
165   /**
166    * This loads all the Sent Folders on the UserProfile object. This must
167    * be called separately because UserProfiles have references to StoreInfos
168    * and StoreInfos have references to UserProfiles.
169    */

170   public void loadAllSentFolders() {
171     List profileList = Pooka.getPookaManager().getUserProfileManager().getUserProfileList();
172
173     for (int i = 0; i < profileList.size(); i++) {
174       ((UserProfile)profileList.get(i)).loadSentFolder();
175     }
176   }
177
178   /**
179    * This adds a ItemListChangeListener to the local listener list.
180    */

181   public void addItemListChangeListener(ItemListChangeListener ilcl) {
182     if (! listenerList.contains(ilcl))
183       listenerList.add(ilcl);
184   }
185
186   /**
187    * This removes a ItemListChangeListener from the local listener list.
188    */

189   public void removeItemListChangeListener(ItemListChangeListener ilcl) {
190     listenerList.remove(ilcl);
191   }
192
193   /**
194    * This notifies all listeners that the StoreList has changed.
195    */

196   public void fireItemListChanged(ItemListChangeEvent e) {
197     for (int i = 0; i < listenerList.size(); i++)
198       ((ItemListChangeListener)listenerList.get(i)).itemListChanged(e);
199   }
200
201
202   /**
203    * This creates a new StoreInfo.
204    *
205    * As defined by interface net.suberic.util.ItemCreator.
206    */

207   public Item createItem(VariableBundle sourceBundle, String JavaDoc resourceString, String JavaDoc itemID) {
208     return new StoreInfo(itemID);
209   }
210
211   //---------------------------
212
// the background stuff.
213

214   /**
215    * This loads and creates all the Stores using the "Store" property
216    * of the main Pooka VariableBundle.
217    */

218   private void createStoreList() {
219     manager = new ItemManager("Store", Pooka.getResources(), this);
220     manager.addItemListChangeListener(this);
221   }
222
223   /**
224    * Sets up SSL connections.
225    */

226   public static void setupSSL() {
227     // set up the SSL socket factory.
228

229     // we have to configure this or the sun jdks will fail. however, this
230
// also will make ibm jdks fail since they don't have the com.sun
231
// classes. so we have to be sneaky.
232

233     try {
234       Object JavaDoc provider = Class.forName("com.sun.net.ssl.internal.ssl.Provider").newInstance();
235
236       java.security.Security.addProvider((java.security.Provider JavaDoc) provider);
237
238       //java.security.Security.addProvider(new com.sun.net.ssl.internal.ssl.Provider());
239
} catch (Exception JavaDoc e) {
240       // if we catch an exception for this then we're probably running with
241
// a non-sun jdk, in which case we shouldn't need to add a provider
242
// explicitly
243
}
244     //java.security.Security.setProperty("ssl.SocketFactory.provider","net.suberic.pooka.ssl.PookaSSLSocketFactory");
245

246   }
247
248   /**
249    * Cleans up the StoreManager.
250    */

251   public void cleanup() {
252     Pooka.getResources().removeValueChangeListener(manager);
253   }
254 }
255
256
Popular Tags