/** * OLAT - Online Learning and Training
* http://www.olat.org *

* 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. *

* Copyright (c) since 2004 at Multimedia- & E-Learning Services (MELS),
* University of Zurich, Switzerland. *

*/ package org.olat.instantMessaging; import java.util.Collections; import java.util.Date; import java.util.HashMap; import java.util.Map; import org.apache.commons.lang.RandomStringUtils; import org.jivesoftware.smack.Chat; import org.jivesoftware.smack.ChatManager; import org.jivesoftware.smack.MessageListener; import org.jivesoftware.smack.PacketListener; import org.jivesoftware.smack.filter.PacketTypeFilter; import org.jivesoftware.smack.packet.Message; import org.jivesoftware.smack.packet.Packet; import org.jivesoftware.smack.packet.Presence; import org.olat.basesecurity.Authentication; import org.olat.basesecurity.ManagerFactory; import org.olat.core.gui.control.Controller; import org.olat.core.id.Identity; import org.olat.core.id.UserConstants; import org.olat.core.logging.OLog; import org.olat.core.logging.Tracing; import org.olat.core.util.event.GenericEventListener; /** * Description:
* Manager class for creation and manipulation of the instant messaging client * for each user. *

* Initial Date: 14.10.2004 * * @author Guido Schnider */ public class ClientManagerImpl implements ClientManager { OLog log = Tracing.createLoggerFor(this.getClass()); // each client gets stored in a map under the username // o_clusterNOK cache ?? what if the clientmanager runs on each vm? ok? why? private Map clients = Collections.synchronizedMap(new HashMap()); Map listeners = Collections.synchronizedMap(new HashMap()); /** * empty constructor */ private ClientManagerImpl() { // nothing to do } /** * Creates an new instant messaging client and connects automatically * to the server. This method should only be used in a constructor and not * be triggered by GUI events, otherwise clients that lost connections may get recreated * again. * @param username * @return JabberClient even if IM service is down */ public InstantMessagingClient getInstantMessagingClient(String username) { // no need to sync over whole method (get/put) since only called once per // UserRequest and username. // we do not sync since "new InstantMessagingClient(...)" may last quite // long. InstantMessagingClient client; client = clients.get(username); if (client == null) { String password = getInstantMessagingCredentialsForUser(username); client = new InstantMessagingClient(username, password); clients.put(username, client); return client; } else { return client; } } /** * Check whether a user has already an IM client running * Use this method when fetching clients outside a controller constructor as users may have several sessions * to avoid reconnection of an duplicate session * @param username * @return */ public boolean hasActiveInstantMessagingClient(String username){ return clients.containsKey(username); } /** * * @param username * @param listener * @param listenToAllMessages - only the main controller needs to listen to all messages */ public void registerEventListener(String username, GenericEventListener listener, boolean listenToAllMessages) { if (listenToAllMessages) { listeners.put(username, listener); } else { listeners.put(username+listener.hashCode(), listener); } } public GenericEventListener getRegisteredEventListeners(String username, Controller controller) { return listeners.get(username+controller.hashCode()); } public void deregisterControllerListener(String username, Controller controller) { log.debug("OLAT-4146: deregisterControllerListener: start"); listeners.remove(username+controller.hashCode()); log.debug("OLAT-4146: deregisterControllerListener: end"); } public Chat createChat(final String username, String chatPartnerJid, final Controller controller) { ChatManager chatmanager = getInstantMessagingClient(username).getConnection().getChatManager(); Chat chat = chatmanager.createChat(chatPartnerJid, new MessageListener() { public void processMessage(Chat chat, Message message) { message.setProperty("receiveTime", new Long(new Date().getTime())); GenericEventListener listener = listeners.get(username+controller.hashCode()); listener.event(new InstantMessagingEvent(message, "chatmessage")); } }); return chat; } /** * @param username */ public void addMessageListener(final String username) { PacketListener packetListener = new PacketListener() { public void processPacket(Packet packet) { Message jabbmessage = (Message) packet; //TODO:gs:b see issue: http://bugs.olat.org/jira/browse/OLAT-2966 //filter