From: <fza...@us...> - 2005-11-29 23:08:38
|
Update of /cvsroot/struts/ajaxchat/WEB-INF/src/org/apache/struts/apps/ajaxchat/dao In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv21020/WEB-INF/src/org/apache/struts/apps/ajaxchat/dao Added Files: AjaxChatDAO.java package.html Log Message: --- NEW FILE: AjaxChatDAO.java --- /* * Copyright 2005 Frank W. Zammetti * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package org.apache.struts.apps.ajaxchat.dao; import java.io.InputStream; import java.io.IOException; import java.util.Date; import java.util.Iterator; import java.util.LinkedHashMap; import java.util.Vector; import org.apache.commons.digester.Digester; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; import org.apache.struts.apps.ajaxchat.dto.MessageDTO; import org.apache.struts.apps.ajaxchat.dto.RoomDTO; import org.apache.struts.apps.ajaxchat.dto.UserDTO; import org.xml.sax.SAXException; /** * This Data Access Object (DAO) is really the heart and soul of the app. All * the real work is done here in terms of recording messages, dealing with * users and rooms and most everything else. * * @author <a href="mailto:fza...@om...">Frank W. Zammetti</a>. */ public final class AjaxChatDAO { /** * Log instance. */ private static Log log = LogFactory.getLog(AjaxChatDAO.class); /** * This class is a singleton, so here's the one and only instance. */ private static AjaxChatDAO instance; /** * Collection of RoomDTO objects. */ private LinkedHashMap rooms = new LinkedHashMap(); /** * Collection of UserDTO objects of currently logged in users. */ private Vector users = new Vector(); /** * Make sure instances of this class can't be created. */ private AjaxChatDAO() { } /** * Complete the singleton pattern. This method is the only way to get an * instance of this class. * * @return The one and only instance of this class. */ public static AjaxChatDAO getInstance() { log.info("AjaxChatDAO getInstance()..."); if (instance == null) { instance = new AjaxChatDAO(); instance.init(null); } log.info("AjaxChatDAO getInstance() Done"); return instance; } // End getInstance(). /** * Initialize. Read in room-list.xml file and create RoomDTOs for each * and add it to the collection of rooms. Note that the first time * getInstance() is called, we pass in null for the isConfigFile parameter, * and hence the config file is not read. Before this DAO can really be * used, init() must be called, handing it an InputStream to the config * file. This is done from ContextListener. * * @param isConfigFile InputStream to the config file. */ public synchronized void init(InputStream isConfigFile) { log.info("AjaxChatDAO init()..."); if (isConfigFile != null) { // Read in rooms config and create beans, hand off to DAO. Digester digester = new Digester(); digester.setValidating(false); digester.push(this); digester.addObjectCreate("rooms/room", "org.apache.struts.apps.ajaxchat.dto.RoomDTO"); digester.addSetProperties("rooms/room"); digester.addSetNext("rooms/room", "addRoom"); try { digester.parse(isConfigFile); log.info("***** Rooms = " + rooms); } catch (IOException ioe) { ioe.printStackTrace(); } catch (SAXException se) { se.printStackTrace(); } } log.info("AjaxChatDAO init() Done"); } // End init(). /** * Adds a room to the collection of rooms. * * @param inRoom The room to add. */ public synchronized void addRoom(RoomDTO inRoom) { log.info("AjaxChatDAO addRoom()..."); log.info("Adding room " + inRoom); rooms.put(inRoom.getName(), inRoom); log.info("AjaxChatDAO addRoom() Done"); } // End addRoom(). /** * Removes a room from the collection of rooms. * * @param inRoomName The namr of the room to remove. */ public synchronized void removeRoom(String inRoomName) { log.info("AjaxChatDAO removeRoom()..."); log.info("Removing room " + inRoomName); RoomDTO room = (RoomDTO)rooms.get(inRoomName); if (room.getUserList().size() == 0) { rooms.remove(inRoomName); log.info("AjaxChatDAO removeRoom() Done"); } else { log.info("AjaxChatDAO removeRoom() Room not removed because " + "there are users in it"); } } // End removeRoom(). /** * Add a message to the list of messages in the named room. * * @param inRoom The name of the room to post the message to. * @param inMessage The message to post. */ public synchronized void postMessage(String inRoom, MessageDTO inMessage) { log.info("AjaxChatDAO postMessage() : inRoom = " + inRoom + " - inMessage = " + inMessage); RoomDTO room = (RoomDTO)rooms.get(inRoom); room.postMessage(inMessage); } // End postMessage(). /** * Gets all messages in a named room newer than the specified datetime. * * @param inRoom The name of the room to get messages for. * @param inDateTime The date/time to start retrieval from. We will actually * get any message subsequent to this datetime. * @return List of messages for the named room. */ public synchronized Vector getMessages(String inRoom, Date inDateTime) { log.info("AjaxChatDAO getMessages() : inRoom = " + inRoom + " - inDateTime = " + inDateTime); RoomDTO room = (RoomDTO)rooms.get(inRoom); return room.getMessages(inDateTime); } // End getMessages(). /** * Returns a list of all rooms. Note that this returns the room name only, * it DOES NOT return a list of RoomDTOs. * * @return List of all rooms names. */ public synchronized Vector getRoomList() { log.info("AjaxChatDAO getRoomList()"); Vector roomList = new Vector(); for (Iterator it = rooms.keySet().iterator(); it.hasNext();) { roomList.add((String)it.next()); } log.info("AjaxChatDAO roomList = " + roomList); return roomList; } // End getRoomList(). /** * Returns a Map of rooms, keyed by room name, with the number of users * chatting in each as the value. * * @return List of all rooms and user counts. */ public synchronized LinkedHashMap getRoomUserCounts() { log.info("getRoomUserCounts getMessages()"); LinkedHashMap roomList = new LinkedHashMap(); for (Iterator it = rooms.keySet().iterator(); it.hasNext();) { String roomName = (String)it.next(); roomList.put(roomName, new Integer(((RoomDTO)rooms.get(roomName)).getUserList().size())); } log.info("getRoomUserCounts roomList = " + roomList); return roomList; } // End getRoomUserCounts(). /** * Returns a list of all users currently chatting in a given room. Note that * this returns the username only, it DOES NOT return a list of UserDTOs. * * @param inRoom The name of the room to get the user list for. * @return List of all usernames chatting in a named room. */ public synchronized Vector getUserList(String inRoom) { log.info("AjaxChatDAO getUserList() : inRoom = " + inRoom); Vector userList = null; RoomDTO room = (RoomDTO)rooms.get(inRoom); userList = room.getUserList(); log.info("AjaxChatDAO userList = " + userList); return userList; } // End getUserList(). /** * Adds a user to the specified room. * * @param inRoom The room to add to. * @param inUser The user to add. */ public synchronized void addUserToRoom(String inRoom, UserDTO inUser) { log.info("AjaxChatDAO addUserToRoom()..."); RoomDTO room = (RoomDTO)rooms.get(inRoom); room.addUser(inUser); log.info("AjaxChatDAO addUserToRoom() Done"); } // End addUserToRoom(). /** * Removes a user from the specified room. * * @param inRoom The room to add to. * @param inUser The user to remove. */ public synchronized void removeUserFromRoom(String inRoom, UserDTO inUser) { log.info("AjaxChatDAO removeUserFromRoom()..."); RoomDTO room = (RoomDTO)rooms.get(inRoom); room.removeUser(inUser); log.info("AjaxChatDAO removeUserFromRoom() Done"); } // End removeUserFromRoom(). /** * Removes a user from all rooms. This is kind of a safety net when a * users' session is destroyed. * * @param inUser The user to remove. */ public synchronized void removeUserFromAllRooms(UserDTO inUser) { log.info("AjaxChatDAO removeUserFromAllRooms()..."); for (Iterator it = rooms.keySet().iterator(); it.hasNext();) { String roomName = (String)it.next(); RoomDTO room = (RoomDTO)rooms.get(roomName); room.removeUser(inUser); } log.info("AjaxChatDAO removeUserFromAllRooms() Done"); } // End removeUserFromAllRooms(). /** * Adds a user to the list of logged on users. * * @param inUser The user to log in. */ public synchronized void logUserIn(UserDTO inUser) { log.info("AjaxChatDAO logUserIn()..."); users.add(inUser); log.info(inUser.getUsername() + " logged in"); } // End logUserIn(). /** * Removes a user from the list of logged on users. * * @param inUser The user to log out. */ public synchronized void logUserOut(UserDTO inUser) { log.info("AjaxChatDAO logUserOut()..."); String usernameToLogOut = inUser.getUsername(); int i = 0; int indexToRemove = -1; for (Iterator it = users.iterator(); it.hasNext();) { UserDTO user = (UserDTO)it.next(); if (usernameToLogOut.equalsIgnoreCase(user.getUsername())) { indexToRemove = i; } i++; } if (indexToRemove != -1) { users.remove(indexToRemove); log.info(usernameToLogOut + " logged out"); } } // End logUserIn(). /** * Checks to see if a given username is in use in any room. * * @param inUsername The name to check. * @return True if the name is in use, false if not. */ public synchronized boolean isUsernameInUse(String inUsername) { log.info("AjaxChatDAO isUsernameInUse()..."); boolean retVal = false; for (Iterator it = users.iterator(); it.hasNext();) { UserDTO user = (UserDTO)it.next(); if (inUsername.equalsIgnoreCase(user.getUsername())) { retVal = true; } } log.info("AjaxChatDAO isUsernameInUse() Done (" + retVal + ")"); return retVal; } // End isUsernameInUse(). } // End class. --- NEW FILE: package.html --- <body> The org.apache.struts.apps.ajaxchat.dao package contains the Data Access Object (DAO) where the majority of the work of the AjaxChat application happens, at least as far as the back-end goes. </body> |