KickJava   Java API By Example, From Geeks To Geeks.

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


1 package net.suberic.pooka;
2
3 import java.util.*;
4
5 import javax.mail.*;
6 import javax.mail.internet.*;
7
8 import net.suberic.util.*;
9
10 /**
11  * <p>An object which manages UserProfile resources.</p>
12  *
13  * @author Allen Petersen
14  * @version $Revision: 1619 $
15  */

16 public class UserProfileManager implements ItemCreator, ItemListChangeListener {
17
18   private ItemManager manager;
19   private LinkedList listenerList = new LinkedList();
20
21   private List mailPropertiesList = null;
22
23   /**
24    * <p>Creates a new UserProfileManager.</p>
25    */

26   public UserProfileManager(VariableBundle sourceBundle) {
27     createMailPropertiesList(sourceBundle);
28     createUserProfileList(sourceBundle);
29   }
30
31   //-----------------------
32
// public interface.
33

34   /**
35    * This listens for ItemListChangeEvents, which result from changes to the
36    * "UserProfile" property. The event is passed to listeners to this object.
37    */

38   public void itemListChanged(ItemListChangeEvent e) {
39     fireItemListChanged(e);
40   }
41
42   /**
43    * This returns a List with all the currently registered UserProfile
44    * objects.
45    */

46   public java.util.List JavaDoc getUserProfileList() {
47     return manager.getItems();
48   }
49
50   /**
51    * This adds the UserProfile with the given UserProfileName to the
52    * allUserProfiles list.
53    */

54   public void addUserProfile(String JavaDoc UserProfileName) {
55     manager.addItem(UserProfileName);
56   }
57
58   /**
59    * This adds the UserProfiles with the given UserProfileNames to the allUserProfiles list.
60    */

61   public void addUserProfile(String JavaDoc[] UserProfileName) {
62     manager.addItem(UserProfileName);
63   }
64
65   /**
66    * This removes the UserProfile with the given UserProfileName.
67    */

68   public void removeUserProfile(String JavaDoc UserProfileName) {
69     manager.removeItem(UserProfileName);
70   }
71
72   /**
73    * This removes the UserProfiles with the given UserProfileNames.
74    */

75   public void removeUserProfile(String JavaDoc[] UserProfileNames) {
76     manager.removeItem(UserProfileNames);
77   }
78
79   /**
80    * This removes the given UserProfile.
81    */

82   public void removeUserProfile(UserProfile UserProfile) {
83     manager.removeItem(UserProfile);
84   }
85
86   /**
87    * This removes the given UserProfiles.
88    */

89   public void removeUserProfile(UserProfile[] UserProfiles) {
90     manager.removeItem(UserProfiles);
91   }
92
93   /**
94    * This returns the UserProfile with the given UserProfileName if it
95    * exists; otherwise, returns null.
96    */

97   public UserProfile getUserProfile(String JavaDoc UserProfileID) {
98     return (UserProfile) manager.getItem(UserProfileID);
99   }
100
101   /**
102    * This returns the UserProfile with the given UserProfileName if it
103    * exists; otherwise, returns null.
104    */

105   public UserProfile getProfile(String JavaDoc UserProfileID) {
106     return getUserProfile(UserProfileID);
107   }
108
109   /**
110    * This returns the UserProfile with the given UserProfileName if it
111    * exists; otherwise, returns null.
112    */

113   public UserProfile getDefaultProfile() {
114     UserProfile defaultUser = getUserProfile(Pooka.getProperty("UserProfile.default", ""));
115     if (defaultUser == null) {
116       List profileList = manager.getItems();
117       if (profileList != null && profileList.size() > 0) {
118         defaultUser = (UserProfile) profileList.get(0);
119       }
120     }
121     return defaultUser;
122   }
123
124   /**
125    * This adds a ItemListChangeListener to the local listener list.
126    */

127   public void addItemListChangeListener(ItemListChangeListener ilcl) {
128     if (! listenerList.contains(ilcl))
129       listenerList.add(ilcl);
130   }
131
132   /**
133    * This removes a ItemListChangeListener from the local listener list.
134    */

135   public void removeItemListChangeListener(ItemListChangeListener ilcl) {
136     listenerList.remove(ilcl);
137   }
138
139   /**
140    * This notifies all listeners that the UserProfileList has changed.
141    */

142   public void fireItemListChanged(ItemListChangeEvent e) {
143     for (int i = 0; i < listenerList.size(); i++)
144       ((ItemListChangeListener)listenerList.get(i)).itemListChanged(e);
145   }
146
147
148   /**
149    * This creates a new UserProfile.
150    */

151   public Item createItem(VariableBundle sourceBundle, String JavaDoc resourceString, String JavaDoc itemID) {
152     UserProfile newProfile = new UserProfile(itemID, sourceBundle);
153     newProfile.initializeFromProperties(sourceBundle, mailPropertiesList);
154     return newProfile;
155
156   }
157
158   //---------------------------
159
// the background stuff.
160

161   /**
162    * This loads and creates all the UserProfiles using the "UserProfile"
163    * property of the main Pooka VariableBundle.
164    */

165   private void createUserProfileList(VariableBundle sourceBundle) {
166     manager = new ItemManager("UserProfile", sourceBundle, this);
167
168     manager.addItemListChangeListener(this);
169   }
170
171   /**
172    * This creates the profile map that we'll use to create new
173    * Profile objects.
174    */

175   public void createMailPropertiesList(VariableBundle mainProperties) {
176     mailPropertiesList = new ArrayList();
177
178     // Initialize Profile Map
179

180     StringTokenizer tokens = new StringTokenizer(mainProperties.getProperty("UserProfile.mailHeaders.fields", "From:FromPersonal:ReplyTo:ReplyToPersonal:Organization"), ":");
181     while (tokens.hasMoreTokens()) {
182       mailPropertiesList.add(tokens.nextToken());
183     }
184
185   }
186
187   public List getMailPropertiesList() {
188     return mailPropertiesList;
189   }
190
191   /**
192    * Removes all UserProfiles from this Manager.
193    */

194   public void shutdownManager() {
195     VariableBundle resources = Pooka.getResources();
196     List profiles = getUserProfileList();
197     for (int i = 0; i < profiles.size(); i++) {
198       UserProfile up = (UserProfile) profiles.get(i);
199       resources.removeValueChangeListener(up);
200     }
201     resources.removeValueChangeListener(manager);
202   }
203
204 }
205
Popular Tags