FileDocCategorySizeDatePackage
ChatServiceImpl.javaAPI DocApache Struts 2.0.9 Apps4451Mon Jul 23 13:43:26 BST 2007org.apache.struts2.showcase.chat

ChatServiceImpl

public class ChatServiceImpl extends Object implements ChatService

Fields Summary
private Map
availableUsers
private Map
availableRooms
Constructors Summary
Methods Summary
public voidaddRoom(Room room)

        if (availableRooms.containsKey(room.getName())) {
            throw new ChatException("room ["+room.getName()+"] is already available", ChatException.ErrorType.valueOf("ROOM_ALREADY_EXISTS"));
        }
        availableRooms.put(room.getName(), room);
    
public voidenterRoom(User user, java.lang.String roomName)

        assert(roomName != null);
        assert(roomName.trim().length() > 0);
        if (! availableRooms.containsKey(roomName)) {
            throw new ChatException("No such room exists ["+roomName+"]", ChatException.ErrorType.NO_SUCH_ROOM_EXISTS);
        }
        Room room = availableRooms.get(roomName);
        room.memberEnter(user);
    
public voidexitRoom(java.lang.String userName, java.lang.String roomName)

        assert(roomName != null);
        assert(roomName.trim().length()> 0);

        if (availableRooms.containsKey(roomName)) {
            Room room = availableRooms.get(roomName);
            room.memberExit(userName);
        }
    
public java.util.ListgetAvailableRooms()

        return new ArrayList<Room>(availableRooms.values());
    
public java.util.ListgetAvailableUsers()



       
        return new ArrayList<User>(availableUsers.values());
    
public java.util.ListgetMessagesInRoom(java.lang.String roomName)

        assert(roomName != null);
        assert(roomName.trim().length() > 0);
        if (! availableRooms.containsKey(roomName)) {
            throw new ChatException("No such room exists ["+roomName+"]", ChatException.ErrorType.NO_SUCH_ROOM_EXISTS);
        }
        Room room = availableRooms.get(roomName);
        return room.getChatMessages();
    
public java.util.ListgetUsersAvailableInRoom(java.lang.String roomName)

        assert(roomName != null);
        if (! availableRooms.containsKey(roomName)) {
            throw new ChatException("No such room exists ["+roomName+"]", ChatException.ErrorType.NO_SUCH_ROOM_EXISTS);
        }
        Room room = availableRooms.get(roomName);
        return room.getMembers();
    
public voidlogin(User user)

        assert(user != null);
        if (availableUsers.containsKey(user.getName())) {
            throw new ChatException("User ["+user.getName()+"] already exists", ChatException.ErrorType.valueOf("USER_ALREADY_EXISTS"));
        }
        availableUsers.put(user.getName(), user);
    
public voidlogout(java.lang.String name)

        assert(name != null);
        assert(name.trim().length() > 0);
        availableUsers.remove(name);
        for (Room room : availableRooms.values()) {
            if (room.hasMember(name)) {
                room.memberExit(name);
            }
        }
    
public voidsendMessageToRoom(java.lang.String roomName, User user, java.lang.String message)

        assert(roomName != null);
        if (! availableRooms.containsKey(roomName)) {
            throw new ChatException("No such room exists ["+roomName+"]", ChatException.ErrorType.NO_SUCH_ROOM_EXISTS);
        }
        Room room = availableRooms.get(roomName);
        room.addMessage(new ChatMessage(message, user));