View Javadoc

1   /*
2    * Copyright (c) 2005 Regents of the University of California (Regents). Created
3    * by TELS, Graduate School of Education, University of California at Berkeley.
4    *
5    * This software is distributed under the GNU Lesser General Public License, v2.
6    *
7    * Permission is hereby granted, without written agreement and without license
8    * or royalty fees, to use, copy, modify, and distribute this software and its
9    * documentation for any purpose, provided that the above copyright notice and
10   * the following two paragraphs appear in all copies of this software.
11   *
12   * REGENTS SPECIFICALLY DISCLAIMS ANY WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
13   * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
14   * PURPOSE. THE SOFTWAREAND ACCOMPANYING DOCUMENTATION, IF ANY, PROVIDED
15   * HEREUNDER IS PROVIDED "AS IS". REGENTS HAS NO OBLIGATION TO PROVIDE
16   * MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS.
17   *
18   * IN NO EVENT SHALL REGENTS BE LIABLE TO ANY PARTY FOR DIRECT, INDIRECT,
19   * SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES, INCLUDING LOST PROFITS,
20   * ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN IF
21   * REGENTS HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
22   */
23  package net.sf.sail.common.persistance.memory;
24  
25  import java.util.HashSet;
26  import java.util.Set;
27  
28  import net.sf.sail.core.entity.IAgent;
29  import net.sf.sail.core.entity.Role;
30  import net.sf.sail.core.uuid.AgentUuid;
31  import net.sf.sail.core.uuid.UserUuid;
32  
33  import org.doomdark.uuid.UUIDGenerator;
34  
35  
36  /**
37   * <p>
38   * A set of users, in a given role, that are responsible for some events or
39   * data.
40   * </p>
41   * 
42   * @author turadg
43   */
44  public class MemoryAgent implements IAgent {
45  
46  	transient static int agentCount = 0;
47  
48  	transient int agentSerial = ++agentCount;
49  
50  	private final Role role;
51  	private AgentUuid id;
52  	
53  	/**
54  	 * This can be used to store an id or cookie so this Agent
55  	 * can be matched up again with its representation in a portal.  
56  	 * Alternatively the portal can provide its own AgentService implementation
57  	 * which in turn can provide a custom IAgent implementation. 
58  	 */
59  	private String agentToken;
60  	
61  	public Role getRole() {
62  		return role;
63  	}
64  
65  	/**
66  	 * element type: User
67  	 */
68  	Set<UserUuid> users;
69  
70  	public MemoryAgent(Role role, AgentUuid id) {
71  		this.role = role;
72  		users = new HashSet<UserUuid>();
73  		this.id = id;
74  	}
75  	
76  	public MemoryAgent(Role role) {
77  		this(role, new AgentUuid(UUIDGenerator.getInstance().generateTimeBasedUUID().asByteArray()));
78  	}
79  
80  	// TODO check this against rules of the role
81  	public void addUser(UserUuid user) {
82  		users.add(user);
83  	}
84  
85  	public void removeUser(UserUuid user) {
86  		users.remove(user);
87  	}
88  
89  	public UserUuid[] getUserArray() {
90  		UserUuid[] userArray = new UserUuid[users.size()];
91  		users.toArray(userArray);
92  		return userArray;
93  	}
94  
95  	@Override
96  	public String toString() {
97  		return "MemoryAgent@" + agentSerial + "{role=" + role + "}";
98  	}
99  	
100 	/* (non-Javadoc)
101 	 * @see net.sf.sail.core.entity.IAgent#getAgentId()
102 	 */
103 	public AgentUuid getAgentId()
104 	{
105 		return id;
106 	}
107 
108 	public String getAgentToken() {
109 		return agentToken;
110 	}
111 
112 	public void setAgentToken(String agentToken) {
113 		this.agentToken = agentToken;
114 	}
115 }