KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > freecs > core > Membership


1 /**
2  * Copyright (C) 2004 Manfred Andres
3  * Created: 11.10.2004 (15:50:57)
4  *
5  * This program is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU General Public License
7  * as published by the Free Software Foundation; either version 2
8  * of the License, or (at your option) any later version.
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 package freecs.core;
20
21 import java.util.Iterator JavaDoc;
22 import java.util.Properties JavaDoc;
23 import java.util.Vector JavaDoc;
24
25 import freecs.Server;
26 import freecs.interfaces.IContainer;
27 import freecs.interfaces.IMessageDestination;
28
29
30 /**
31  * @author Manfred Andres
32  *
33  * Representation of one single membership. Implements interface IMessageDestination
34  * making it possible to send Messages to alle users belonging to this Membership.
35  * @see freecs.interfaces#IMessageDestination
36  */

37 public class Membership implements IMessageDestination {
38     private Vector JavaDoc members = new Vector JavaDoc();
39     
40     public final String JavaDoc key;
41     private volatile String JavaDoc namePrefix, nameSuffix, stringRepresentation;
42     private boolean displayDefaultVipRighttitle = false, displayDefaultModGuestTitle = true, listedAsOnlinevip = true;
43     private int addedStates = 0, removedStates = 0;
44     private int addedDefaultStates = 0, removedDefaultStates = 0;
45
46     public Membership (String JavaDoc key, Properties JavaDoc p) {
47         if (key == null)
48             throw new RuntimeException JavaDoc ("unable to construct membership without key");
49         this.key = key;
50         if (p == null && !key.equals("undefined"))
51             throw new RuntimeException JavaDoc ("unable to construct membership without properties");
52         init(p);
53     }
54
55     private void init (Properties JavaDoc p) {
56         if (p==null)
57             return;
58         boolean checkNamePrefix = false,checkNameSuffix = false,checkStringRepresentation = false,checkAddedStates = false
59         ,checkRemovedStates = false,checkAddedDefaultStates = false,checkRemovedDefaultStates = false,
60         checkDisplayDefaultVipRighttitle = false,checkDisplayDefaultModGuestTitle = false,checkListedAsOnlinevip = false;
61         for (Iterator JavaDoc i = p.keySet().iterator(); i.hasNext(); ) {
62             String JavaDoc currKey = (String JavaDoc) i.next();
63             if (currKey.equalsIgnoreCase ("usernameprefix")) {
64                 namePrefix = p.getProperty(currKey);
65                 checkNamePrefix = true;
66             } else if (currKey.equalsIgnoreCase ("usernamesuffix")) {
67                 nameSuffix = p.getProperty(currKey);
68                 checkNameSuffix = true;
69             } else if (currKey.equalsIgnoreCase ("stringrepresentation")) {
70                 stringRepresentation = p.getProperty(currKey);
71                 checkStringRepresentation = true;
72             } else if (currKey.equalsIgnoreCase ("addstates")) {
73                 String JavaDoc val = p.getProperty(currKey);
74                 String JavaDoc[] states = val.split(",");
75                 addedStates = 0;
76                 for (int j = 0; j < states.length; j++) {
77                     addedStates = addedStates | UserManager.resolveState(states[j]);
78                 }
79                 checkAddedStates = true;
80             } else if (currKey.equalsIgnoreCase ("removestates")) {
81                 String JavaDoc val = p.getProperty(currKey);
82                 String JavaDoc[] states = val.split(",");
83                 removedStates = 0;
84                 for (int j = 0; j < states.length; j++) {
85                     removedStates = removedStates | UserManager.resolveState(states[j]);
86                 }
87                 checkRemovedStates = true;
88             } else if (currKey.equalsIgnoreCase ("adddefaultstates")) {
89                 String JavaDoc val = p.getProperty(currKey);
90                 String JavaDoc[] states = val.split(",");
91                 addedDefaultStates = 0;
92                 for (int j = 0; j < states.length; j++) {
93                     addedDefaultStates = addedDefaultStates | UserManager.resolveState(states[j]);
94                 }
95                 checkAddedDefaultStates = true;
96             } else if (currKey.equalsIgnoreCase ("removedefaultstates")) {
97                 String JavaDoc val = p.getProperty(currKey);
98                 String JavaDoc[] states = val.split(",");
99                 removedDefaultStates = 0;
100                 for (int j = 0; j < states.length; j++) {
101                     removedDefaultStates = removedDefaultStates | UserManager.resolveState(states[j]);
102                 }
103                 checkRemovedDefaultStates = true;
104             } else if (currKey.equalsIgnoreCase ("displaydefaultviprighttitle")
105                     && p.getProperty(currKey).equalsIgnoreCase ("true")) {
106                 displayDefaultVipRighttitle = true;
107                 checkDisplayDefaultVipRighttitle = true;
108             } else if (currKey.equalsIgnoreCase ("displaydefaultmodguesttitle")
109                     && p.getProperty(currKey).equalsIgnoreCase ("false")) {
110                 displayDefaultModGuestTitle = false;
111                 checkDisplayDefaultModGuestTitle = true;
112             } else if (currKey.equalsIgnoreCase ("listedasonlinevip")
113                     && p.getProperty(currKey).equalsIgnoreCase ("false")) {
114                 listedAsOnlinevip = false;
115                 checkListedAsOnlinevip = true;
116             }
117         }
118         
119         if (!checkNamePrefix)
120             namePrefix = null;
121         if (!checkNameSuffix)
122             nameSuffix = null;
123         if (!checkStringRepresentation)
124             stringRepresentation = null;
125         if (!checkAddedStates)
126             addedStates = 0;
127         if (!checkRemovedStates)
128             removedStates = 0;
129         if (!checkAddedDefaultStates)
130             addedDefaultStates = 0;
131         if (!checkRemovedDefaultStates)
132             removedDefaultStates = 0;
133         if (!checkDisplayDefaultVipRighttitle)
134             displayDefaultVipRighttitle = false;
135         if (!checkDisplayDefaultModGuestTitle)
136             displayDefaultModGuestTitle = true;
137         if (!checkListedAsOnlinevip)
138             displayDefaultModGuestTitle = true;
139         
140     }
141
142     public synchronized void update (Properties JavaDoc p) {
143         init(p);
144         for (Iterator JavaDoc i = members.iterator(); i.hasNext(); ) {
145             User u = (User) i.next();
146             u.rebuildMemberships();
147         }
148     }
149     
150     public void add (User u) {
151         if (!members.contains(u))
152             members.add(u);
153         u.addMembership (this.key, this);
154         
155         // add addedStates and remove removedStates to/from this users permissionmap
156
int pMap = u.getPermissionMap();
157         pMap = pMap | addedStates;
158         pMap = pMap - (pMap & removedStates);
159         Server.log (this, "setting user-state-map for user " + u.toString() + " to value " + pMap, Server.MSG_AUTH, Server.LVL_VERY_VERBOSE);
160         u.setPermission(pMap);
161         
162         int dpMap = u.getDefaultMembershipPermissionMap();
163         dpMap = dpMap | addedDefaultStates;
164         dpMap = dpMap - (dpMap & removedDefaultStates);
165         Server.log (this, "setting user-defaultmembership-state-map for user " + u.toString() + " to value " + dpMap, Server.MSG_AUTH, Server.LVL_VERY_VERBOSE);
166         u.setDefaultMembershipPermission(dpMap);
167         
168         if (dpMap >0 && dpMap != pMap) {
169             Server.log (this, "setting user-defaultstate-map for user " + u.toString() + " to value " + dpMap, Server.MSG_AUTH, Server.LVL_VERY_VERBOSE);
170             u.setDefaultPermissionMap(dpMap);
171         }
172     }
173     
174     public void remove (User u) {
175         members.remove(u);
176     }
177     
178     public void cleanup () {
179         for (Iterator JavaDoc i = members.iterator(); i.hasNext(); ) {
180             User u = (User) i.next();
181             u.removeMembership(this.key);
182         }
183     }
184     
185     
186     // Interface IMessageDestination
187

188     /**
189      * Send a message to all users having this membership
190      * @see freecs.interfaces.IMessageDestination#sendMessage(freecs.interfaces.IContainer)
191      */

192     public void sendMessage(IContainer mc) {
193         User[] uArr = (User[]) members.toArray(new User[0]);
194         for (int i = 0; i < uArr.length; i++) {
195             uArr[i].sendMessage(mc);
196         }
197     }
198
199     /**
200      * Iterator over all users having this membership
201      * @see freecs.interfaces.IMessageDestination#users()
202      */

203     public Iterator JavaDoc users() {
204         return members.iterator();
205     }
206
207     /**
208      * return the name of this membership
209      * @see freecs.interfaces.IMessageDestination#getName()
210      */

211     public String JavaDoc getName() {
212         if (stringRepresentation != null)
213             return stringRepresentation;
214         return key;
215     }
216     
217     public String JavaDoc getNamePrefix() {
218             return namePrefix;
219     }
220     
221     public String JavaDoc getNameSuffix() {
222             return nameSuffix;
223     }
224     
225     public boolean displayDefaultVipRighttitle() {
226         return displayDefaultVipRighttitle;
227     }
228     
229     public boolean displayDefaultModGuesttitle() {
230         return displayDefaultModGuestTitle;
231     }
232     
233     public boolean listedAsOnlinevip() {
234         return listedAsOnlinevip;
235     }
236     
237     public int hashCode() {
238         return key.hashCode();
239     }
240     
241     public boolean equals (Object JavaDoc o) {
242         if (this == o)
243             return true;
244         if (!(o instanceof Membership))
245             return false;
246         Membership foreign = (Membership) o;
247         return foreign.key.equals(this.key);
248     }
249     
250     public String JavaDoc toString() {
251         return this.stringRepresentation;
252     }
253 }
Popular Tags