001: package net.suberic.pooka;
002:
003: import javax.mail.*;
004: import java.util.*;
005: import net.suberic.util.*;
006:
007: /**
008: * This class manages the a list of StoreInfos. It also provides some
009: * convenience methods for accessing FolderInfos within the StoreInfos,
010: * and for adding and removing StoreInfos.
011: */
012:
013: public class StoreManager implements ItemCreator,
014: ItemListChangeListener {
015:
016: private ItemManager manager;
017: private LinkedList listenerList = new LinkedList();
018:
019: public StoreManager() {
020: createStoreList();
021: }
022:
023: //-----------------------
024: // public interface.
025:
026: /**
027: * As defined in net.suberic.util.ValueChangeListener.
028: *
029: * This listens for ItemListChangeEvents, which result from changes to the
030: * "Store" property. The result is that refrestStoreInfos() is called,
031: * and then the event is passed to listeners to this object.
032: */
033: public void itemListChanged(ItemListChangeEvent e) {
034: fireItemListChanged(e);
035: refreshStoreInfos(e);
036: }
037:
038: /**
039: * This returns a Vector with all the currently registered StoreInfo
040: * objects.
041: */
042: public java.util.Vector getStoreList() {
043: return manager.getItems();
044: }
045:
046: /**
047: * This adds the store with the given storeName to the allStores list.
048: */
049: public void addStore(String storeName) {
050: manager.addItem(storeName);
051: }
052:
053: /**
054: * This adds the stores with the given storeNames to the allStores list.
055: */
056: public void addStore(String[] storeName) {
057: manager.addItem(storeName);
058: }
059:
060: /**
061: * This removes the store with the given storeName.
062: */
063: public void removeStore(String storeName) {
064: manager.removeItem(storeName);
065: }
066:
067: /**
068: * This removes the stores with the given storeNames.
069: */
070: public void removeStore(String[] storeNames) {
071: manager.removeItem(storeNames);
072: }
073:
074: /**
075: * This removes the given StoreInfo.
076: */
077: public void removeStore(StoreInfo store) {
078: manager.removeItem(store);
079: }
080:
081: /**
082: * This removes the given StoreInfos.
083: */
084: public void removeStore(StoreInfo[] stores) {
085: manager.removeItem(stores);
086: }
087:
088: /**
089: * This compares the storeList object with the Store property, and
090: * updates the storeList appropriately.
091: */
092: public void refreshStoreInfos(ItemListChangeEvent e) {
093: Item[] removedStores = e.getRemoved();
094: for (int i = 0; removedStores != null
095: && i < removedStores.length; i++) {
096: ((StoreInfo) removedStores[i]).remove();
097: }
098: }
099:
100: /**
101: * This returns the FolderInfo which corresponds to the given folderName.
102: * The folderName should be in the form "/storename/folder/subfolder".
103: */
104: public FolderInfo getFolder(String folderName) {
105: if (folderName != null && folderName.length() >= 1) {
106: int divider = folderName.indexOf('/', 1);
107: while (divider == 0) {
108: folderName = folderName.substring(1);
109: divider = folderName.indexOf('/');
110: }
111:
112: if (divider > 0) {
113: String storeName = folderName.substring(0, divider);
114: StoreInfo store = getStoreInfo(storeName);
115: if (store != null) {
116: return store.getChild(folderName
117: .substring(divider + 1));
118: }
119: }
120: }
121:
122: return null;
123: }
124:
125: /**
126: * This returns the FolderInfo which corresponds to the given folderID.
127: * The folderName should be in the form "storename.folderID.folderID".
128: */
129: public FolderInfo getFolderById(String folderID) {
130: // hurm. the problem here is that '.' is a legal value in a name...
131:
132: java.util.Vector allStores = getStoreList();
133:
134: for (int i = 0; i < allStores.size(); i++) {
135: StoreInfo currentStore = (StoreInfo) allStores.elementAt(i);
136: if (folderID.startsWith(currentStore.getStoreID())) {
137: FolderInfo possibleMatch = currentStore
138: .getFolderById(folderID);
139: if (possibleMatch != null) {
140: return possibleMatch;
141: }
142: }
143: }
144:
145: return null;
146: }
147:
148: /**
149: * Gets all of the open and available folders known by the system.
150: */
151: public Vector getAllOpenFolders() {
152: Vector returnValue = new Vector();
153: Vector currentStores = getStoreList();
154: for (int i = 0; i < currentStores.size(); i++) {
155: returnValue.addAll(((StoreInfo) currentStores.elementAt(i))
156: .getAllFolders());
157: }
158:
159: return returnValue;
160: }
161:
162: /**
163: * This returns the StoreInfo with the given storeName if it exists
164: * in the allStores Vector; otherwise, returns null.
165: */
166: public StoreInfo getStoreInfo(String storeID) {
167: return (StoreInfo) manager.getItem(storeID);
168: }
169:
170: /**
171: * This loads all the Sent Folders on the UserProfile object. This must
172: * be called separately because UserProfiles have references to StoreInfos
173: * and StoreInfos have references to UserProfiles.
174: */
175: public void loadAllSentFolders() {
176: List profileList = Pooka.getPookaManager()
177: .getUserProfileManager().getUserProfileList();
178:
179: for (int i = 0; i < profileList.size(); i++) {
180: ((UserProfile) profileList.get(i)).loadSentFolder();
181: }
182: }
183:
184: /**
185: * This adds a ItemListChangeListener to the local listener list.
186: */
187: public void addItemListChangeListener(ItemListChangeListener ilcl) {
188: if (!listenerList.contains(ilcl))
189: listenerList.add(ilcl);
190: }
191:
192: /**
193: * This removes a ItemListChangeListener from the local listener list.
194: */
195: public void removeItemListChangeListener(ItemListChangeListener ilcl) {
196: listenerList.remove(ilcl);
197: }
198:
199: /**
200: * This notifies all listeners that the StoreList has changed.
201: */
202: public void fireItemListChanged(ItemListChangeEvent e) {
203: for (int i = 0; i < listenerList.size(); i++)
204: ((ItemListChangeListener) listenerList.get(i))
205: .itemListChanged(e);
206: }
207:
208: /**
209: * This creates a new StoreInfo.
210: *
211: * As defined by interface net.suberic.util.ItemCreator.
212: */
213: public Item createItem(VariableBundle sourceBundle,
214: String resourceString, String itemID) {
215: return new StoreInfo(itemID);
216: }
217:
218: //---------------------------
219: // the background stuff.
220:
221: /**
222: * This loads and creates all the Stores using the "Store" property
223: * of the main Pooka VariableBundle.
224: */
225: private void createStoreList() {
226: manager = new ItemManager("Store", Pooka.getResources(), this );
227: manager.addItemListChangeListener(this );
228: }
229:
230: /**
231: * Sets up SSL connections.
232: */
233: public static void setupSSL() {
234: // set up the SSL socket factory.
235:
236: // we have to configure this or the sun jdks will fail. however, this
237: // also will make ibm jdks fail since they don't have the com.sun
238: // classes. so we have to be sneaky.
239:
240: try {
241: Object provider = Class.forName(
242: "com.sun.net.ssl.internal.ssl.Provider")
243: .newInstance();
244:
245: java.security.Security
246: .addProvider((java.security.Provider) provider);
247:
248: //java.security.Security.addProvider(new com.sun.net.ssl.internal.ssl.Provider());
249: } catch (Exception e) {
250: // if we catch an exception for this then we're probably running with
251: // a non-sun jdk, in which case we shouldn't need to add a provider
252: // explicitly
253: }
254: java.security.Security.setProperty(
255: "ssl.SocketFactory.provider",
256: "net.suberic.pooka.ssl.PookaSSLSocketFactory");
257:
258: }
259:
260: /**
261: * Cleans up the StoreManager.
262: */
263: public void cleanup() {
264: Pooka.getResources().removeValueChangeListener(manager);
265: }
266: }
|