001: /*
002: * $Id: ChatServiceImpl.java 471756 2006-11-06 15:01:43Z husted $
003: *
004: * Licensed to the Apache Software Foundation (ASF) under one
005: * or more contributor license agreements. See the NOTICE file
006: * distributed with this work for additional information
007: * regarding copyright ownership. The ASF licenses this file
008: * to you under the Apache License, Version 2.0 (the
009: * "License"); you may not use this file except in compliance
010: * with the License. You may obtain a copy of the License at
011: *
012: * http://www.apache.org/licenses/LICENSE-2.0
013: *
014: * Unless required by applicable law or agreed to in writing,
015: * software distributed under the License is distributed on an
016: * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
017: * KIND, either express or implied. See the License for the
018: * specific language governing permissions and limitations
019: * under the License.
020: */
021: package org.apache.struts2.showcase.chat;
022:
023: import java.util.ArrayList;
024: import java.util.LinkedHashMap;
025: import java.util.List;
026: import java.util.Map;
027:
028: public class ChatServiceImpl implements ChatService {
029:
030: private Map<String, User> availableUsers = new LinkedHashMap<String, User>();
031: private Map<String, Room> availableRooms = new LinkedHashMap<String, Room>();
032:
033: public List<User> getAvailableUsers() {
034: return new ArrayList<User>(availableUsers.values());
035: }
036:
037: public List<Room> getAvailableRooms() {
038: return new ArrayList<Room>(availableRooms.values());
039: }
040:
041: public void addRoom(Room room) {
042: if (availableRooms.containsKey(room.getName())) {
043: throw new ChatException("room [" + room.getName()
044: + "] is already available", ChatException.ErrorType
045: .valueOf("ROOM_ALREADY_EXISTS"));
046: }
047: availableRooms.put(room.getName(), room);
048: }
049:
050: public void login(User user) {
051: assert (user != null);
052: if (availableUsers.containsKey(user.getName())) {
053: throw new ChatException("User [" + user.getName()
054: + "] already exists", ChatException.ErrorType
055: .valueOf("USER_ALREADY_EXISTS"));
056: }
057: availableUsers.put(user.getName(), user);
058: }
059:
060: public void logout(String name) {
061: assert (name != null);
062: assert (name.trim().length() > 0);
063: availableUsers.remove(name);
064: for (Room room : availableRooms.values()) {
065: if (room.hasMember(name)) {
066: room.memberExit(name);
067: }
068: }
069: }
070:
071: public void exitRoom(String userName, String roomName) {
072: assert (roomName != null);
073: assert (roomName.trim().length() > 0);
074:
075: if (availableRooms.containsKey(roomName)) {
076: Room room = availableRooms.get(roomName);
077: room.memberExit(userName);
078: }
079: }
080:
081: public void enterRoom(User user, String roomName) {
082: assert (roomName != null);
083: assert (roomName.trim().length() > 0);
084: if (!availableRooms.containsKey(roomName)) {
085: throw new ChatException("No such room exists [" + roomName
086: + "]", ChatException.ErrorType.NO_SUCH_ROOM_EXISTS);
087: }
088: Room room = availableRooms.get(roomName);
089: room.memberEnter(user);
090: }
091:
092: public List<ChatMessage> getMessagesInRoom(String roomName) {
093: assert (roomName != null);
094: assert (roomName.trim().length() > 0);
095: if (!availableRooms.containsKey(roomName)) {
096: throw new ChatException("No such room exists [" + roomName
097: + "]", ChatException.ErrorType.NO_SUCH_ROOM_EXISTS);
098: }
099: Room room = availableRooms.get(roomName);
100: return room.getChatMessages();
101: }
102:
103: public void sendMessageToRoom(String roomName, User user,
104: String message) {
105: assert (roomName != null);
106: if (!availableRooms.containsKey(roomName)) {
107: throw new ChatException("No such room exists [" + roomName
108: + "]", ChatException.ErrorType.NO_SUCH_ROOM_EXISTS);
109: }
110: Room room = availableRooms.get(roomName);
111: room.addMessage(new ChatMessage(message, user));
112: }
113:
114: public List<User> getUsersAvailableInRoom(String roomName) {
115: assert (roomName != null);
116: if (!availableRooms.containsKey(roomName)) {
117: throw new ChatException("No such room exists [" + roomName
118: + "]", ChatException.ErrorType.NO_SUCH_ROOM_EXISTS);
119: }
120: Room room = availableRooms.get(roomName);
121: return room.getMembers();
122: }
123: }
|