KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jivesoftware > messenger > transport > TransportHandler


1 /**
2  * $RCSfile: TransportHandler.java,v $
3  * $Revision: 1.5 $
4  * $Date: 2005/04/04 06:36:48 $
5  *
6  * Copyright (C) 2004 Jive Software. All rights reserved.
7  *
8  * This software is published under the terms of the GNU Public License (GPL),
9  * a copy of which is included in this distribution.
10  */

11
12 package org.jivesoftware.messenger.transport;
13
14 import org.jivesoftware.messenger.container.BasicModule;
15 import org.jivesoftware.util.LocaleUtils;
16 import org.jivesoftware.util.Log;
17 import org.jivesoftware.messenger.*;
18 import org.jivesoftware.messenger.auth.UnauthorizedException;
19 import org.xmpp.packet.JID;
20 import org.xmpp.packet.Packet;
21 import org.xmpp.packet.PacketError;
22
23 import java.util.Map JavaDoc;
24 import java.util.concurrent.ConcurrentHashMap JavaDoc;
25
26 /**
27  * Routes packets to the appropriate transport gateway or drops the packet.
28  *
29  * @author Iain Shigeoka
30  */

31 public class TransportHandler extends BasicModule implements ChannelHandler {
32
33     private Map JavaDoc<String JavaDoc, Channel> transports = new ConcurrentHashMap JavaDoc<String JavaDoc, Channel>();
34
35     private PacketDeliverer deliverer;
36
37     public TransportHandler() {
38         super("Transport handler");
39     }
40
41     public void addTransport(Channel transport) {
42         transports.put(transport.getName(), transport);
43     }
44
45     public void process(Packet packet) throws UnauthorizedException, PacketException {
46         boolean handled = false;
47         String JavaDoc host = packet.getTo().getDomain();
48         for (Channel channel : transports.values()) {
49             if (channel.getName().equalsIgnoreCase(host)) {
50                 channel.add(packet);
51                 handled = true;
52             }
53         }
54         if (!handled) {
55             JID recipient = packet.getTo();
56             JID sender = packet.getFrom();
57             packet.setError(PacketError.Condition.remote_server_timeout);
58             packet.setFrom(recipient);
59             packet.setTo(sender);
60             try {
61                 deliverer.deliver(packet);
62             }
63             catch (PacketException e) {
64                 Log.error(LocaleUtils.getLocalizedString("admin.error"), e);
65             }
66         }
67     }
68
69     public void initialize(XMPPServer server) {
70         super.initialize(server);
71         deliverer = server.getPacketDeliverer();
72     }
73 }
Popular Tags