AcceptManagerImpl.java 3.9 KB
Newer Older
Matt Tucker's avatar
Matt Tucker committed
1 2 3 4 5
/**
 * $RCSfile$
 * $Revision$
 * $Date$
 *
6
 * Copyright (C) 2004 Jive Software. All rights reserved.
Matt Tucker's avatar
Matt Tucker committed
7
 *
8 9
 * This software is published under the terms of the GNU Public License (GPL),
 * a copy of which is included in this distribution.
Matt Tucker's avatar
Matt Tucker committed
10
 */
11

Matt Tucker's avatar
Matt Tucker committed
12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128
package org.jivesoftware.net.spi;

import org.jivesoftware.net.ConnectionManager;
import org.jivesoftware.net.policies.BasicAcceptPolicy;

import java.io.IOException;
import java.net.InetSocketAddress;
import java.util.Iterator;
import java.util.LinkedList;

import org.jivesoftware.net.AcceptManager;
import org.jivesoftware.net.AcceptPort;
import org.jivesoftware.net.*;
import org.jivesoftware.messenger.container.*;
import org.jivesoftware.util.*;

public class AcceptManagerImpl extends BasicModule implements AcceptManager {

    private ModuleContext context;
    private LinkedList ports = new LinkedList();
    private AcceptPolicy policy = new BasicAcceptPolicy(true);

    private ConnectionManager connManager;

    public AcceptManagerImpl() {
        super("Accept Manager");
    }

    public AcceptPolicy getGlobalAcceptPolicy() {
        if (policy == null){
            throw new IllegalStateException(
                    "Must initialize and start module before use");
        }
        return policy;
    }

    public int getAcceptPortCount() {
        return ports.size();
    }

    public Iterator getAcceptPorts() {
        return ports.iterator();
    }

    public Iterator getAcceptPorts(BasicResultFilter filter) {
        return filter.filter(ports.iterator());
    }

    public AcceptPort getAcceptPort(InetSocketAddress portAddress)
            throws NotFoundException {
        AcceptPort matchedPort = null;
        Iterator portIter = ports.iterator();
        while (portIter.hasNext()){
            AcceptPort acceptPort = (AcceptPort) portIter.next();
            if (acceptPort.getInetSocketAddress().equals(portAddress)){
                matchedPort = acceptPort;
            }
        }
        if (matchedPort == null){
            throw new NotFoundException(portAddress.toString());
        }
        return matchedPort;
    }

    public AcceptPort createAcceptPort(InetSocketAddress portAddress)
            throws AlreadyExistsException {

        if (connManager == null){
            throw new IllegalStateException("Connection Manager not ready");
        }

        Iterator portIter = ports.iterator();
        while (portIter.hasNext()){
            AcceptPort acceptPort = (AcceptPort) portIter.next();
            if (acceptPort.getInetSocketAddress().equals(portAddress)){
                throw new AlreadyExistsException(portAddress.toString());
            }
        }
        return new AcceptPortImpl(context.createChildProperty("accept.port"),
                                  connManager,
                                  portAddress);
    }

    public void deleteAcceptPort(AcceptPort acceptPort) {
        ports.remove(acceptPort);
        // TODO: save remaining ports

        try {
            acceptPort.close();
        } catch (IOException e) {
            Log.error(LocaleUtils.getLocalizedString("admin.error"), e);
        }
    }

    protected TrackInfo getTrackInfo() {
        TrackInfo trackInfo = new TrackInfo();
        trackInfo.getTrackerClasses().put(ConnectionManager.class,"");
        return trackInfo;
    }

    public void initialize(ModuleContext context, Container container) {
        super.initialize(context, container);
        this.context = context;
    }

    protected void serviceAdded(Object service) {
        if (service instanceof ConnectionManager){
            connManager = (ConnectionManager) service;
            Iterator portIter = context.getChildProperties("portIter.port");
            while (portIter.hasNext()){
                AcceptPort port = new AcceptPortImpl((ModuleProperties) portIter.next(),
                                                     connManager);
                ports.add(port);
            }
        }
    }
}