View Javadoc
1   //////////////////////////////////////////////////////////////////////////////
2   // dexterIM - Instant Messaging Framework
3   // Copyright (C) 2003  Christoph Walcher
4   //
5   // This program is free software; you can redistribute it and/or modify
6   // it under the terms of the GNU General Public License as published by
7   // the Free Software Foundation; either version 2 of the License, or
8   // (at your option) any later version.
9   //
10  // This program is distributed in the hope that it will be useful,
11  // but WITHOUT ANY WARRANTY; without even the implied warranty of
12  // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  // GNU General Public License for more details.
14  //
15  // You should have received a copy of the GNU General Public License
16  // along with this program; if not, write to the Free Software
17  // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
18  //////////////////////////////////////////////////////////////////////////////
19  package net.sf.dexterim.core;
20  
21  import java.util.HashMap;
22  import java.util.Map;
23  
24  /***
25   *
26   * @author  Christoph Walcher
27   */
28  public class InstantMessenger {
29    private Map protocols;
30  
31    /*** Creates a new instance of InstantMessenger */
32    public InstantMessenger() {
33      protocols = new HashMap();
34    }
35  
36    /***Create a unconnected IMConnection using the given protocol
37     * @param protocolName
38     * @return
39     * @throws NoSuchProtocolException
40     */
41    public IMConnection createConnection(String protocolName)
42      throws NoSuchProtocolException {
43      return lookupProtocol(protocolName).createConnection();
44    }
45  
46    /***
47     * @param protocolName
48     * @return
49     * @throws NoSuchProtocolException
50     */
51    public Protocol lookupProtocol(String protocolName)
52      throws NoSuchProtocolException {
53      if (protocols.containsKey(protocolName)) {
54        return (Protocol)protocols.get(protocolName);
55      }
56      else {
57        throw new NoSuchProtocolException();
58      }
59    }
60  
61    /***
62     * @param protocolName
63     * @param protocol
64     */
65    public void registerProtocol(String protocolName, Protocol protocol) {
66      if (protocolName == null) {
67        throw new IllegalArgumentException("Parameter protocolName " +
68          "must not be null");
69      }
70  
71      if (protocol == null) {
72        throw new IllegalArgumentException("Parameter protocol must not be null. " +
73          "Use unregister to remove a registered protocol.");
74      }
75  
76      protocols.put(protocolName, protocol);
77    }
78  
79    /***
80     * @param protocolName
81     * @return
82     * @throws NoSuchProtocolException
83     */
84    public Protocol unregisterProtocol(String protocolName)
85      throws NoSuchProtocolException {
86      
87      if (!protocols.containsKey(protocolName)) {
88        throw new NoSuchProtocolException();
89      }
90  
91      return (Protocol)protocols.remove(protocolName);
92    }
93  }