Source Code Cross Referenced for UserManager.java in  » Content-Management-System » contelligent » de » finix » contelligent » client » base » Java Source Code / Java DocumentationJava Source Code and Java Documentation

Java Source Code / Java Documentation
1. 6.0 JDK Core
2. 6.0 JDK Modules
3. 6.0 JDK Modules com.sun
4. 6.0 JDK Modules com.sun.java
5. 6.0 JDK Modules sun
6. 6.0 JDK Platform
7. Ajax
8. Apache Harmony Java SE
9. Aspect oriented
10. Authentication Authorization
11. Blogger System
12. Build
13. Byte Code
14. Cache
15. Chart
16. Chat
17. Code Analyzer
18. Collaboration
19. Content Management System
20. Database Client
21. Database DBMS
22. Database JDBC Connection Pool
23. Database ORM
24. Development
25. EJB Server geronimo
26. EJB Server GlassFish
27. EJB Server JBoss 4.2.1
28. EJB Server resin 3.1.5
29. ERP CRM Financial
30. ESB
31. Forum
32. GIS
33. Graphic Library
34. Groupware
35. HTML Parser
36. IDE
37. IDE Eclipse
38. IDE Netbeans
39. Installer
40. Internationalization Localization
41. Inversion of Control
42. Issue Tracking
43. J2EE
44. JBoss
45. JMS
46. JMX
47. Library
48. Mail Clients
49. Net
50. Parser
51. PDF
52. Portal
53. Profiler
54. Project Management
55. Report
56. RSS RDF
57. Rule Engine
58. Science
59. Scripting
60. Search Engine
61. Security
62. Sevlet Container
63. Source Control
64. Swing Library
65. Template Engine
66. Test Coverage
67. Testing
68. UML
69. Web Crawler
70. Web Framework
71. Web Mail
72. Web Server
73. Web Services
74. Web Services apache cxf 2.0.1
75. Web Services AXIS2
76. Wiki Engine
77. Workflow Engines
78. XML
79. XML UI
Java
Java Tutorial
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
Photoshop Tutorials
Maya Tutorials
Flash Tutorials
3ds-Max Tutorials
Illustrator Tutorials
GIMP Tutorials
C# / C Sharp
C# / CSharp Tutorial
C# / CSharp Open Source
ASP.Net
ASP.NET Tutorial
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
Ruby
PHP
Python
Python Tutorial
Python Open Source
SQL Server / T-SQL
SQL Server / T-SQL Tutorial
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Flash / Flex / ActionScript
VBA / Excel / Access / Word
XML
XML Tutorial
Microsoft Office PowerPoint 2007 Tutorial
Microsoft Office Excel 2007 Tutorial
Microsoft Office Word 2007 Tutorial
Java Source Code / Java Documentation » Content Management System » contelligent » de.finix.contelligent.client.base 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


001:        /*
002:         * Copyright 2001-2006 C:1 Financial Services GmbH
003:         *
004:         * This software is free software; you can redistribute it and/or
005:         * modify it under the terms of the GNU Lesser General Public
006:         * License Version 2.1, as published by the Free Software Foundation.
007:         *
008:         * This software is distributed in the hope that it will be useful,
009:         * but WITHOUT ANY WARRANTY; without even the implied warranty of
010:         * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
011:         * Lesser General Public License for more details.
012:         *
013:         * You should have received a copy of the GNU Lesser General Public
014:         * License along with this library; if not, write to the Free Software
015:         * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA
016:         */
017:
018:        package de.finix.contelligent.client.base;
019:
020:        import java.io.IOException;
021:        import java.io.StringWriter;
022:        import java.util.ArrayList;
023:        import java.util.Arrays;
024:        import java.util.Comparator;
025:        import java.util.HashSet;
026:        import java.util.Iterator;
027:        import java.util.List;
028:        import java.util.Set;
029:        import java.util.logging.Level;
030:        import java.util.logging.Logger;
031:
032:        import org.w3c.dom.Element;
033:        import org.w3c.dom.NodeList;
034:
035:        import de.finix.contelligent.client.event.TaskFinishedEvent;
036:        import de.finix.contelligent.client.event.TaskStartedEvent;
037:        import de.finix.contelligent.client.i18n.Resources;
038:        import de.finix.contelligent.client.modules.preferences.PreferencesModule;
039:        import de.finix.contelligent.client.remote.ActionResult;
040:        import de.finix.contelligent.client.remote.Actions;
041:        import de.finix.contelligent.client.remote.RemoteActionException;
042:        import de.finix.contelligent.client.util.list.NameValueListModel;
043:        import de.finix.contelligent.client.util.xml.XMLUtil;
044:        import de.zeigermann.xml.XMLWriter;
045:
046:        public final class UserManager implements  SessionListener {
047:
048:            private static Logger logger = Logger.getLogger(UserManager.class
049:                    .getName());
050:
051:            private static UserManager userManager = null;
052:
053:            private final static String roleHead = ContelligentConstants.XML_HEADER
054:                    + "<!DOCTYPE role PUBLIC \"-//finix AG/contelligent/Principal Data V1.0/EN\" \"http://www.c1-fse.de/contelligent/dtds/principaldata_1_0.dtd\">";
055:
056:            private final static String userHead = ContelligentConstants.XML_HEADER
057:                    + "<!DOCTYPE user PUBLIC \"-//finix AG/contelligent/Principal Data V1.0/EN\" \"http://www.c1-fse.de/contelligent/dtds/principaldata_1_0.dtd\">";
058:
059:            private final static String foot = "";
060:
061:            private List<User> users;
062:
063:            private List<Role> roles;
064:
065:            private List administrableRoleGroups;
066:
067:            private List<String> userGroups;
068:
069:            private List<String> roleGroups;
070:
071:            private Set<User> changedUsers;
072:
073:            private Set<User> deletedUsers;
074:
075:            private Set<Role> changedRoles;
076:
077:            private Set<Role> deletedRoles;
078:
079:            private String cSession = ""; // The last session id we reloaded for
080:
081:            private UserManager() {
082:            }
083:
084:            public static UserManager getInstance() {
085:                if (userManager == null) {
086:                    userManager = new UserManager();
087:                }
088:                Session.getInstance().addSessionListener(userManager);
089:                return userManager;
090:            }
091:
092:            public void sessionChanged(Session session) {
093:                try {
094:                    if (!session.getContelligentSessionHandle()
095:                            .equals(cSession)) {
096:                        load();
097:                    }
098:                } catch (RemoteActionException rae) {
099:                    logger.log(Level.SEVERE, "Could not reload UserManager",
100:                            rae);
101:                }
102:            }
103:
104:            public synchronized void load() throws RemoteActionException {
105:                try {
106:                    TaskStartedEvent startedEvent = new TaskStartedEvent(this ,
107:                            Resources.getLocalString("loading_principals"));
108:                    Session.getInstance().fireTaskStartEvent(startedEvent);
109:                    cSession = Session.getInstance()
110:                            .getContelligentSessionHandle();
111:                    users = new ArrayList<User>();
112:                    roles = new ArrayList<Role>();
113:
114:                    changedRoles = new HashSet<Role>();
115:                    changedUsers = new HashSet<User>();
116:                    deletedRoles = new HashSet<Role>();
117:                    deletedUsers = new HashSet<User>();
118:
119:                    List[] groups = getAdministrableUserGroups();
120:                    // FIXME for performance reasons, only load login groups
121:                    if (PreferencesModule.getPreferences().getBoolean(
122:                            PreferencesModule.EDIT_ALL_GROUPS,
123:                            PreferencesModule.DEFAULT_EDIT_ALL_GROUPS)) {
124:                        userGroups = ServerInfo.getInstance().getUserGroups();
125:                    } else {
126:                        userGroups = ServerInfo.getInstance()
127:                                .getLoginUserGroups();
128:                    }
129:                    // userGroups = groups[0];
130:                    administrableRoleGroups = groups[1];
131:                    roleGroups = ServerInfo.getInstance().getRoleGroups();
132:
133:                    for (Iterator it = userGroups.iterator(); it.hasNext();) {
134:                        String groupId = (String) it.next();
135:                        loadUserGroup(groupId);
136:                    }
137:
138:                    for (Iterator it = roleGroups.iterator(); it.hasNext();) {
139:                        String groupId = (String) it.next();
140:                        loadRoleGroup(groupId);
141:                    }
142:                } finally {
143:                    TaskFinishedEvent finishedEvent = new TaskFinishedEvent(
144:                            this );
145:                    Session.getInstance().fireTaskFinishEvent(finishedEvent);
146:                }
147:            }
148:
149:            public synchronized void reloadUserGroup(String groupId)
150:                    throws RemoteActionException {
151:                for (Iterator<User> i = users.iterator(); i.hasNext();) {
152:                    if (i.next().getGroupId().equals(groupId)) {
153:                        i.remove();
154:                    }
155:                }
156:                loadUserGroup(groupId);
157:            }
158:
159:            public synchronized void reloadRoleGroup(String groupId)
160:                    throws RemoteActionException {
161:                for (Iterator<Role> i = roles.iterator(); i.hasNext();) {
162:                    if (i.next().getGroupId().equals(groupId)) {
163:                        i.remove();
164:                    }
165:                }
166:                loadRoleGroup(groupId);
167:            }
168:
169:            private void loadUserGroup(String groupId)
170:                    throws RemoteActionException {
171:                ActionResult actionResult = Actions.exportPrincipals(groupId);
172:                if (!actionResult.getState().equals("ok")) {
173:                    actionResult.showErrors();
174:                }
175:                Element userDescription = actionResult.getContent();
176:
177:                NodeList userList = userDescription
178:                        .getElementsByTagName("user");
179:                for (int i = 0; i < userList.getLength(); i++) {
180:                    Element userElement = (Element) userList.item(i);
181:                    String name = userElement.getAttributes().getNamedItem(
182:                            "name").getNodeValue();
183:                    String displayName = userElement.getAttributes()
184:                            .getNamedItem("displayName").getNodeValue();
185:                    User user = new User();
186:                    user.groupId = groupId;
187:                    user.userId = name;
188:                    user.displayName = displayName;
189:
190:                    NodeList userRoles = userElement
191:                            .getElementsByTagName("hasRole");
192:                    for (int j = 0; j < userRoles.getLength(); j++) {
193:                        Element roleElement = (Element) userRoles.item(j);
194:                        String roleId = roleElement.getAttributes()
195:                                .getNamedItem("name").getNodeValue();
196:                        String roleGroupId = roleElement.getAttributes()
197:                                .getNamedItem("group").getNodeValue();
198:                        Role role = new Role();
199:                        role.groupId = roleGroupId;
200:                        role.roleId = roleId;
201:                        user.roles.add(role);
202:                    }
203:                    users.add(user);
204:                }
205:            }
206:
207:            private void loadRoleGroup(String groupId)
208:                    throws RemoteActionException {
209:                ActionResult actionResult = Actions.exportPrincipals(groupId);
210:                if (!actionResult.getState().equals("ok")) {
211:                    actionResult.showErrors();
212:                }
213:                Element userDescription = actionResult.getContent();
214:
215:                NodeList roleList = userDescription
216:                        .getElementsByTagName("role");
217:                for (int i = 0; i < roleList.getLength(); i++) {
218:                    Element roleElement = (Element) roleList.item(i);
219:                    String name = roleElement.getAttributes().getNamedItem(
220:                            "name").getNodeValue();
221:                    String displayName = roleElement.getAttributes()
222:                            .getNamedItem("displayName").getNodeValue();
223:                    Role role = new Role();
224:                    role.groupId = groupId;
225:                    role.roleId = name;
226:                    role.displayName = displayName;
227:                    roles.add(role);
228:                }
229:            }
230:
231:            private List[] getAdministrableUserGroups()
232:                    throws RemoteActionException {
233:                ActionResult actionResult = Actions
234:                        .exportAdministrablePrincipalGroups();
235:                if (!actionResult.getState().equals("ok")) {
236:                    actionResult.showErrors();
237:                }
238:
239:                List<String> userGroups = new ArrayList<String>();
240:                List<String> roleGroups = new ArrayList<String>();
241:
242:                Element root = actionResult.getContent();
243:
244:                if (XMLUtil.tagExists(root, "subject-groups")) {
245:                    Element subjectsGroups = (Element) root
246:                            .getElementsByTagName("subject-groups").item(0);
247:                    NodeList usergroupList = subjectsGroups
248:                            .getElementsByTagName("userGroup");
249:                    for (int i = 0; i < usergroupList.getLength(); i++) {
250:                        Element usergroupElement = (Element) usergroupList
251:                                .item(i);
252:                        String groupId = usergroupElement.getAttributes()
253:                                .getNamedItem("groupId").getNodeValue();
254:                        userGroups.add(groupId);
255:                    }
256:                    NodeList rolegroupList = subjectsGroups
257:                            .getElementsByTagName("roleGroup");
258:                    for (int i = 0; i < rolegroupList.getLength(); i++) {
259:                        Element usergroupElement = (Element) rolegroupList
260:                                .item(i);
261:                        roleGroups.add(usergroupElement.getAttributes()
262:                                .getNamedItem("groupId").getNodeValue());
263:                    }
264:                }
265:                List[] answer = new List[2];
266:
267:                answer[0] = userGroups;
268:                answer[1] = roleGroups;
269:                return answer;
270:            }
271:
272:            public synchronized void save() throws RemoteActionException {
273:                for (Iterator it = changedRoles.iterator(); it.hasNext();) {
274:                    Role role = (Role) it.next();
275:                    String xml = roleHead + role.toXML() + foot;
276:                    logger.log(Level.FINEST, "xml: " + xml);
277:                    ActionResult actionResult = Actions.updateOrAddPrincipal(
278:                            role.groupId, xml);
279:                    if (!actionResult.getState().equals("ok")) {
280:                        actionResult.showErrors();
281:                    }
282:                }
283:                changedRoles = new HashSet<Role>();
284:
285:                for (Iterator it = deletedRoles.iterator(); it.hasNext();) {
286:                    Role role = (Role) it.next();
287:                    String xml = roleHead + role.toXML() + foot;
288:                    logger.log(Level.FINEST, "xml: " + xml);
289:                    ActionResult actionResult = Actions.removePrincipal(
290:                            role.groupId, xml);
291:                    if (!actionResult.getState().equals("ok")) {
292:                        actionResult.showErrors();
293:                    }
294:                }
295:                deletedRoles = new HashSet<Role>();
296:
297:                for (Iterator it = changedUsers.iterator(); it.hasNext();) {
298:                    User user = (User) it.next();
299:                    String xml = userHead + user.toXML() + foot;
300:                    logger.log(Level.FINEST, "xml: " + xml);
301:                    ActionResult actionResult = Actions.updateOrAddPrincipal(
302:                            user.groupId, xml);
303:                    if (!actionResult.getState().equals("ok")) {
304:                        actionResult.showErrors();
305:                    }
306:                }
307:                changedUsers = new HashSet<User>();
308:
309:                for (Iterator it = deletedUsers.iterator(); it.hasNext();) {
310:                    User user = (User) it.next();
311:                    String xml = userHead + user.toXML() + foot;
312:                    logger.log(Level.FINEST, "xml: " + xml);
313:                    ActionResult actionResult = Actions.removePrincipal(
314:                            user.groupId, xml);
315:                    if (!actionResult.getState().equals("ok")) {
316:                        actionResult.showErrors();
317:                    }
318:                }
319:                deletedUsers = new HashSet<User>();
320:
321:            }
322:
323:            public void cancel() throws RemoteActionException {
324:                load();
325:            }
326:
327:            public NameValueListModel getGroupsDescriptions() {
328:                List<String> groups = UserManager.getInstance().getGroups();
329:                return NameValueListModel.buildNameValuePairs(groups);
330:            }
331:
332:            public NameValueListModel getUserGroupsDescriptions() {
333:                List<String> groups = getUserGroups();
334:                String[] groupsArray = (String[]) groups
335:                        .toArray(new String[groups.size()]);
336:                return NameValueListModel.buildNameValuePairs(groupsArray);
337:            }
338:
339:            public NameValueListModel getRoleGroupsDescriptions() {
340:                List<String> groups = getRoleGroups();
341:                String[] groupsArray = (String[]) groups
342:                        .toArray(new String[groups.size()]);
343:                return NameValueListModel.buildNameValuePairs(groupsArray);
344:            }
345:
346:            public NameValueListModel getPrincipalsDescriptions(String groupId) {
347:                return getPrincipalsDescriptions(groupId);
348:            }
349:
350:            public NameValueListModel getPrincipalsDescriptions(String groupId,
351:                    final boolean displayName, boolean sort) {
352:                List principals = getPrincipals(groupId);
353:                if (principals != null) {
354:                    Principal[] principalsArray = new Principal[principals
355:                            .size()];
356:                    String[] principalsDisplayArray = new String[principals
357:                            .size()];
358:                    int i = 0;
359:                    for (Iterator it = principals.iterator(); it.hasNext(); i++) {
360:                        Principal principal = (Principal) it.next();
361:                        principalsArray[i] = principal;
362:                        principalsDisplayArray[i] = (displayName ? principal
363:                                .display() : principal.getId());
364:                    }
365:                    if (sort) {
366:                        Arrays.sort(principalsArray,
367:                                new Comparator<Principal>() {
368:                                    public int compare(Principal principal1,
369:                                            Principal principal2) {
370:                                        String name1 = (displayName ? principal1
371:                                                .display()
372:                                                : principal1.getId());
373:                                        String name2 = (displayName ? principal2
374:                                                .display()
375:                                                : principal2.getId());
376:
377:                                        return name1.compareToIgnoreCase(name2);
378:                                    }
379:                                });
380:                        Arrays.sort(principalsDisplayArray,
381:                                new Comparator<String>() {
382:                                    public int compare(String name1,
383:                                            String name2) {
384:                                        return name1.compareToIgnoreCase(name2);
385:                                    }
386:                                });
387:                    }
388:
389:                    return NameValueListModel.buildNameValuePairs(
390:                            principalsArray, principalsDisplayArray);
391:                } else {
392:                    return NameValueListModel.buildEmptyNameValuePairs();
393:                }
394:            }
395:
396:            public NameValueListModel getUsersDescriptions(String groupId) {
397:                return getUsersDescriptions(groupId, true);
398:            }
399:
400:            public NameValueListModel getUsersDescriptions(String groupId,
401:                    boolean sort) {
402:                List users = getUsers(groupId);
403:                if (users != null) {
404:                    UserManager.User[] usersArray = new UserManager.User[users
405:                            .size()];
406:                    String[] usersDisplayArray = new String[users.size()];
407:                    int i = 0;
408:                    for (Iterator it = users.iterator(); it.hasNext(); i++) {
409:                        UserManager.User user = (UserManager.User) it.next();
410:                        usersArray[i] = user;
411:                        usersDisplayArray[i] = user.display();
412:                    }
413:
414:                    if (sort) {
415:                        Arrays.sort(usersArray, new Comparator<User>() {
416:                            public int compare(User user1, User user2) {
417:                                String name1 = user1.getGroupId() + ":"
418:                                        + user1.getId();
419:                                String name2 = user2.getGroupId() + ":"
420:                                        + user2.getId();
421:
422:                                return name1.compareToIgnoreCase(name2);
423:                            }
424:                        });
425:                        Arrays.sort(usersDisplayArray,
426:                                new Comparator<String>() {
427:                                    public int compare(String name1,
428:                                            String name2) {
429:                                        return name1.compareToIgnoreCase(name2);
430:                                    }
431:                                });
432:                    }
433:
434:                    return NameValueListModel.buildNameValuePairs(usersArray,
435:                            usersDisplayArray);
436:                } else {
437:                    return NameValueListModel.buildEmptyNameValuePairs();
438:                }
439:            }
440:
441:            public NameValueListModel getRolesDescriptions(String groupId,
442:                    boolean sort) {
443:                return getRolesDescriptions(groupId, true, sort);
444:            }
445:
446:            public NameValueListModel getRolesDescriptions(String groupId,
447:                    final boolean displayName, boolean sort) {
448:                List roles = getRoles(groupId);
449:                if (roles != null) {
450:                    UserManager.Role[] rolesArray = new UserManager.Role[roles
451:                            .size()];
452:                    String[] rolesDisplayArray = new String[roles.size()];
453:                    int i = 0;
454:                    for (Iterator it = roles.iterator(); it.hasNext(); i++) {
455:                        UserManager.Role role = (UserManager.Role) it.next();
456:                        rolesArray[i] = role;
457:                        rolesDisplayArray[i] = (displayName ? role.display()
458:                                : role.roleId);
459:                    }
460:
461:                    if (sort) {
462:                        Arrays.sort(rolesArray, new Comparator<Role>() {
463:                            public int compare(Role role1, Role role2) {
464:                                String name1 = (displayName ? role1.display()
465:                                        : role1.roleId);
466:                                String name2 = (displayName ? role2.display()
467:                                        : role2.roleId);
468:
469:                                return name1.compareToIgnoreCase(name2);
470:                            }
471:                        });
472:                        Arrays.sort(rolesDisplayArray,
473:                                new Comparator<String>() {
474:                                    public int compare(String name1,
475:                                            String name2) {
476:                                        return name1.compareToIgnoreCase(name2);
477:                                    }
478:                                });
479:                    }
480:
481:                    return NameValueListModel.buildNameValuePairs(rolesArray,
482:                            rolesDisplayArray);
483:                } else {
484:                    return NameValueListModel.buildEmptyNameValuePairs();
485:                }
486:            }
487:
488:            public NameValueListModel getRolesForUserDescriptions(
489:                    UserManager.User user) {
490:                return getRolesForUserDescriptions(user, true);
491:            }
492:
493:            public NameValueListModel getRolesForUserDescriptions(
494:                    UserManager.User user, boolean displayName) {
495:                if (user != null) {
496:                    List roles = user.roles;
497:                    UserManager.Role[] rolesArray = new UserManager.Role[roles
498:                            .size()];
499:                    String[] rolesDisplayArray = new String[roles.size()];
500:                    int i = 0;
501:                    for (Iterator it = roles.iterator(); it.hasNext(); i++) {
502:                        UserManager.Role role = (UserManager.Role) it.next();
503:                        rolesArray[i] = role;
504:                        rolesDisplayArray[i] = (displayName ? role.display()
505:                                : role.roleId);
506:                    }
507:                    return NameValueListModel.buildNameValuePairs(rolesArray,
508:                            rolesDisplayArray);
509:                } else {
510:                    return NameValueListModel.buildEmptyNameValuePairs();
511:                }
512:            }
513:
514:            public synchronized List<String> getGroups() {
515:                List<String> res = new ArrayList<String>();
516:                res.addAll(getUserGroups());
517:                res.addAll(getRoleGroups());
518:                return res;
519:            }
520:
521:            public synchronized List<String> getUserGroups() {
522:                return new ArrayList<String>(userGroups);
523:            }
524:
525:            public synchronized List<String> getRoleGroups() {
526:                return new ArrayList<String>(roleGroups);
527:            }
528:
529:            public synchronized List<Principal> getPrincipals(String groupId) {
530:                List<Principal> res = new ArrayList<Principal>();
531:                res.addAll(getUsers(groupId));
532:                res.addAll(getRoles(groupId));
533:                return res;
534:            }
535:
536:            public synchronized List<User> getUsers() {
537:                return new ArrayList<User>(users);
538:            }
539:
540:            public synchronized List<User> getUsers(String groupId) {
541:                List<User> res = new ArrayList<User>();
542:                for (Iterator it = users.iterator(); it.hasNext();) {
543:                    User user = (User) it.next();
544:                    if (user.groupId.equals(groupId)) {
545:                        res.add(user);
546:                    }
547:                }
548:                return res;
549:            }
550:
551:            public synchronized User getUser(String userId, String groupId) {
552:                for (Iterator it = users.iterator(); it.hasNext();) {
553:                    User user = (User) it.next();
554:                    if (user.groupId.equals(groupId)
555:                            && user.userId.equals(userId)) {
556:                        return user;
557:                    }
558:                }
559:                return null;
560:            }
561:
562:            public synchronized boolean deleteUser(User user) {
563:                String groupId = user.groupId;
564:                String name = user.userId;
565:                if (groupId != null && name != null
566:                        && getUser(name, groupId) != null) {
567:                    users.remove(user);
568:                    deletedUsers.add(user);
569:                    return true;
570:                }
571:                return false;
572:            }
573:
574:            public synchronized boolean updateUser(User user) {
575:                String groupId = user.groupId;
576:                String name = user.userId;
577:                if (groupId != null && name != null) {
578:                    User oldUser = getUser(name, groupId);
579:                    if (oldUser != null) {
580:                        oldUser.updateByUser(user);
581:                        changedUsers.add(oldUser);
582:                        return true;
583:                    }
584:                }
585:                return false;
586:            }
587:
588:            public synchronized boolean addUser(User user) {
589:                String groupId = user.groupId;
590:                String name = user.userId;
591:                if (groupId != null && name != null
592:                        && getUser(name, groupId) == null) {
593:                    users.add(user);
594:                    changedUsers.add(user);
595:                    return true;
596:                }
597:                return false;
598:            }
599:
600:            public synchronized List<Role> getRoles() {
601:                return new ArrayList<Role>(roles);
602:            }
603:
604:            public synchronized List<Role> getRoles(String groupId) {
605:                List<Role> res = new ArrayList<Role>();
606:                for (Iterator it = roles.iterator(); it.hasNext();) {
607:                    Role role = (Role) it.next();
608:                    if (role.groupId.equals(groupId)) {
609:                        res.add(role);
610:                    }
611:                }
612:                return res;
613:            }
614:
615:            public synchronized Role getRole(String roleId, String groupId) {
616:                for (Iterator it = roles.iterator(); it.hasNext();) {
617:                    Role role = (Role) it.next();
618:                    if (role.groupId.equals(groupId)
619:                            && role.roleId.equals(roleId)) {
620:                        return role;
621:                    }
622:                }
623:                return null;
624:            }
625:
626:            public synchronized boolean deleteRole(Role role) {
627:                String groupId = role.groupId;
628:                String name = role.roleId;
629:                if (groupId != null && name != null
630:                        && getRole(name, groupId) != null) {
631:                    roles.remove(role);
632:                    deletedRoles.add(role);
633:                    removeRoleFromUsers(role);
634:                    return true;
635:                }
636:                return false;
637:            }
638:
639:            public synchronized void removeRoleFromUsers(Role role) {
640:                for (Iterator it = users.iterator(); it.hasNext();) {
641:                    User user = (User) it.next();
642:                    if (user.removeRole(role)) {
643:                        changedUsers.add(user);
644:                    }
645:                }
646:            }
647:
648:            public synchronized boolean updateRole(Role role) {
649:                String groupId = role.groupId;
650:                String name = role.roleId;
651:                if (groupId != null && name != null) {
652:                    Role oldRole = getRole(name, groupId);
653:                    if (oldRole != null) {
654:                        oldRole.updateByRole(role);
655:                        changedRoles.add(oldRole);
656:                        return true;
657:                    }
658:                }
659:                return false;
660:            }
661:
662:            public synchronized boolean addRole(Role role) {
663:                String groupId = role.groupId;
664:                String name = role.roleId;
665:                if (groupId != null && name != null
666:                        && getRole(name, groupId) == null) {
667:                    roles.add(role);
668:                    changedRoles.add(role);
669:                    return true;
670:                }
671:                return false;
672:            }
673:
674:            public static abstract class Principal {
675:
676:                abstract public String display();
677:
678:                abstract public String getId();
679:
680:                abstract public String getGroupId();
681:
682:                abstract public String getDisplayName();
683:
684:                abstract public String toXML();
685:
686:            }
687:
688:            public final static class User extends Principal {
689:                public String groupId;
690:
691:                public String userId, displayName, newPassword;
692:
693:                public List<Role> roles = new ArrayList<Role>();
694:
695:                public String display() {
696:                    return groupId + ": " + userId;
697:                    // return userId;
698:                }
699:
700:                public String getId() {
701:                    return userId;
702:                }
703:
704:                public String getGroupId() {
705:                    return groupId;
706:                }
707:
708:                public String getDisplayName() {
709:                    return displayName;
710:                }
711:
712:                public void updateByUser(User user) {
713:                    groupId = user.groupId;
714:                    userId = user.userId;
715:                    displayName = user.displayName;
716:                    newPassword = user.newPassword;
717:                    roles = user.roles;
718:                }
719:
720:                public void addRole(String groupId, String roleId) {
721:                    Role role = new Role();
722:                    role.roleId = roleId;
723:                    role.groupId = groupId;
724:                    roles.add(role);
725:                }
726:
727:                public boolean removeRole(String groupId, String roleId) {
728:                    Role role = new Role();
729:                    role.roleId = roleId;
730:                    role.groupId = groupId;
731:                    return roles.remove(role);
732:                }
733:
734:                public boolean removeRole(Role role) {
735:                    return roles.remove(role);
736:                }
737:
738:                public boolean equals(Object o) {
739:                    return (o != null && groupId.equals(((User) o).groupId) && userId
740:                            .equals(((User) o).userId));
741:                }
742:
743:                public int hashCode() {
744:                    return -1;
745:                }
746:
747:                public String toXML() {
748:                    StringWriter sw = new StringWriter();
749:                    try {
750:                        XMLWriter xmlWriter = new XMLWriter(sw);
751:                        xmlWriter.writeStartTag(XMLWriter.createStartTag(
752:                                "user", new String[][] { { "name", userId },
753:                                        { "displayName", displayName },
754:                                        { "newPassword", newPassword } }));
755:                        if (roles != null) {
756:                            for (Iterator it = roles.iterator(); it.hasNext();) {
757:                                Role role = (Role) it.next();
758:                                xmlWriter
759:                                        .writeEmptyElement(XMLWriter
760:                                                .createEmptyTag(
761:                                                        "hasRole",
762:                                                        new String[][] {
763:                                                                {
764:                                                                        "name",
765:                                                                        role.roleId },
766:                                                                {
767:                                                                        "group",
768:                                                                        role.groupId } }));
769:                            }
770:                        }
771:                        xmlWriter.writeEndTag(XMLWriter.createEndTag("user"));
772:                        xmlWriter.flush();
773:                        return sw.toString();
774:                    } catch (IOException ioe) {
775:                        logger.log(Level.SEVERE,
776:                                "Could not convert description to XML", ioe);
777:                        return null;
778:                    }
779:                }
780:            }
781:
782:            public final static class Role extends Principal {
783:                public String groupId;
784:
785:                public String roleId, displayName;
786:
787:                public String display() {
788:                    return groupId + ": " + roleId;
789:                }
790:
791:                public String getId() {
792:                    return roleId;
793:                }
794:
795:                public String getGroupId() {
796:                    return groupId;
797:                }
798:
799:                public String getDisplayName() {
800:                    return displayName;
801:                }
802:
803:                public void updateByRole(Role role) {
804:                    groupId = role.groupId;
805:                    roleId = role.roleId;
806:                    displayName = role.displayName;
807:                }
808:
809:                public boolean equals(Object o) {
810:                    return (o != null && groupId.equals(((Role) o).groupId) && roleId
811:                            .equals(((Role) o).roleId));
812:                }
813:
814:                public int hashCode() {
815:                    return -1;
816:                }
817:
818:                public String toXML() {
819:                    StringWriter sw = new StringWriter();
820:                    try {
821:                        XMLWriter xmlWriter = new XMLWriter(sw);
822:                        xmlWriter.writeEmptyElement(XMLWriter.createEmptyTag(
823:                                "role", new String[][] { { "name", roleId },
824:                                        { "displayName", displayName } }));
825:                        xmlWriter.flush();
826:                        return sw.toString();
827:                    } catch (IOException ioe) {
828:                        logger.log(Level.SEVERE,
829:                                "Could not convert description to XML", ioe);
830:                        return null;
831:                    }
832:                }
833:            }
834:
835:            public boolean mayEditRoleGroup(String groupId) {
836:                logger.finest("mayEditRoleGroup " + groupId + " "
837:                        + administrableRoleGroups.contains(groupId));
838:                return administrableRoleGroups.contains(groupId);
839:            }
840:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.