001: /*
002: * CoadunationLib: The coaduntion implementation library.
003: * Copyright (C) 2006 Rift IT Contracting
004: *
005: * This library is free software; you can redistribute it and/or
006: * modify it under the terms of the GNU Lesser General Public
007: * License as published by the Free Software Foundation; either
008: * version 2.1 of the License, or (at your option) any later version.
009: *
010: * This library is distributed in the hope that it will be useful,
011: * but WITHOUT ANY WARRANTY; without even the implied warranty of
012: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
013: * Lesser General Public License for more details.
014: *
015: * You should have received a copy of the GNU Lesser General Public
016: * License along with this library; if not, write to the Free Software
017: * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
018: *
019: * ThreadPermissionSession.java
020: *
021: * This object stores the thread permissions for the current session.
022: */
023:
024: // package definition
025: package com.rift.coad.lib.security;
026:
027: // the java imports
028: import java.util.Vector;
029: import java.util.HashSet;
030: import java.util.Set;
031:
032: /**
033: * This object stores the thread permissions for the current session.
034: *
035: * @author Brett Chaldecott
036: */
037: public class ThreadPermissionSession {
038:
039: // The list of containers for which the principals are being stored.
040: private Long threadId = null;
041: private UserSession user = null;
042: private Vector containerList = null;
043:
044: /**
045: * Creates a new instance of ThreadSecuritySession
046: *
047: * @param threadId The id of the thread.
048: * @param user The reference to the user object.
049: */
050: public ThreadPermissionSession(Long threadId, UserSession user) {
051: this .threadId = threadId;
052: this .user = user;
053: containerList = new Vector();
054: containerList.add(user);
055: }
056:
057: /**
058: * The getter method for the thread id information.
059: *
060: * @return The long object containing the thread id information.
061: * @exception SecurityException
062: */
063: public Long getThreadId() throws SecurityException {
064: try {
065: user.touch();
066: } catch (Exception ex) {
067: throw new SecurityException(
068: "Failed to touch the users session : "
069: + ex.getMessage(), ex);
070: }
071: return threadId;
072: }
073:
074: /**
075: * This method retrieves the use information.
076: *
077: * @return The object containing the user information associated with this
078: * thread.
079: * @exception SecurityException
080: */
081: public UserSession getUser() throws SecurityException {
082: try {
083: user.touch();
084: } catch (Exception ex) {
085: throw new SecurityException(
086: "Failed to touch the users session : "
087: + ex.getMessage(), ex);
088: }
089: return user;
090: }
091:
092: /**
093: * This method returns the list of principals.
094: *
095: * @return The set containing the list of principals.
096: * @exception SecurityException
097: */
098: public Set getPrincipals() throws SecurityException {
099: Set principals = new HashSet();
100: for (int i = 0; i < containerList.size(); i++) {
101: principals.addAll(((PrincipalContainer) containerList
102: .get(i)).getPrincipals());
103: }
104: try {
105: user.touch();
106: } catch (Exception ex) {
107: throw new SecurityException(
108: "Failed to touch the users session : "
109: + ex.getMessage(), ex);
110: }
111: return principals;
112: }
113:
114: /**
115: * This method adds the specified role to the list principal containers.
116: *
117: * @param role The object containining the role information.
118: * @exception SecurityException
119: */
120: public void addRole(Role role) throws SecurityException {
121: try {
122: user.touch();
123: } catch (Exception ex) {
124: throw new SecurityException(
125: "Failed to touch the users session : "
126: + ex.getMessage(), ex);
127: }
128: containerList.add(role);
129: }
130:
131: /**
132: * This method removes the role from the container list.
133: *
134: * @param role The object containing the role information.
135: */
136: public void removeRole(Role role) {
137: for (int i = 0; i < containerList.size(); i++) {
138: if (((PrincipalContainer) containerList.get(containerList
139: .size()
140: - (i + 1))).getName().equals(role.getName())) {
141: containerList.remove(containerList.size() - (i + 1));
142: break;
143: }
144: }
145: }
146: }
|