BasicServer.java 4.36 KB
Newer Older
Matt Tucker's avatar
Matt Tucker committed
1 2 3 4 5 6 7 8 9 10 11 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 129 130 131 132 133 134 135 136 137 138 139 140
/**
 * $RCSfile$
 * $Revision$
 * $Date$
 *
 * Copyright (C) 1999-2003 CoolServlets, Inc. All rights reserved.
 *
 * This software is the proprietary information of CoolServlets, Inc.
 * Use is subject to license terms.
 */
package org.jivesoftware.messenger.spi;

import org.jivesoftware.messenger.container.BasicModule;
import org.jivesoftware.messenger.container.Container;
import org.jivesoftware.messenger.container.ModuleContext;
import org.jivesoftware.messenger.container.ServiceLookup;
import org.jivesoftware.util.LocaleUtils;
import org.jivesoftware.util.Log;
import org.jivesoftware.util.Version;
import org.jivesoftware.messenger.*;
import org.jivesoftware.messenger.auth.UnauthorizedException;
import java.text.DateFormat;
import java.util.*;

/**
 * Main entry point into the Jive xmpp server powered by a JDBC backend. Most of this code
 * is actually generic to any server so should be moved to a base class when we have
 * a separate server implementation.
 *
 * @author Iain Shigeoka
 */
public class BasicServer
        extends BasicModule
        implements XMPPServer, BasicServerMBean {

    private String name;
    private Version version;
    private Date startDate;
    private Date stopDate;
    private ServiceLookup lookup;
    private ConnectionManager connectionManager;
    private boolean initialized = false;
    private ModuleContext modContext = null;

    /**
     * Create a default loopback test server.
     */
    public BasicServer() {
        super("XMPP Server");
    }

    public XMPPServerInfo getServerInfo() {
        Iterator ports;
        if (connectionManager == null) {
            connectionManager =
                    (ConnectionManager)lookup.lookup(ConnectionManager.class);
        }
        if (connectionManager == null) {
            ports = Collections.EMPTY_LIST.iterator();
        }
        else {
            ports = connectionManager.getPorts();
        }
        if (!initialized) {
            throw new IllegalStateException("Not initialized yet");
        }
        return new XMPPServerInfoImpl(modContext, name, version, startDate, stopDate, ports);
    }

    public boolean isLocal(XMPPAddress jid) {
        boolean local = false;
        if (jid != null && name != null && name.equalsIgnoreCase(jid.getHost())) {
            local = true;
        }
        return local;
    }

    public XMPPAddress createAddress(String username, String resource) {
        return new XMPPAddress(username, name, resource);
    }

    private Session serverSession =
            new ServerSession(new XMPPAddress(null, name, null),
                    new BasicStreamIDFactory().createStreamID(name));

    public Session getSession() {
        return serverSession;
    }

    public String getName() {
        return "XMPP Server Kernel";
    }

    public void initialize(ModuleContext context, Container container) {
        super.initialize(context, container);
        modContext = context;
        try {
            lookup = container.getServiceLookup();
            name = context.getProperty("xmpp.domain");
            if (name == null) {
                name = "127.0.0.1";
            }

            version = new Version(2, 0, 0, Version.ReleaseStatus.Release, 1);
            initialized = true;
        }
        catch (UnauthorizedException e) {
            Log.error(LocaleUtils.getLocalizedString("admin.error"), e);
        }
    }

    public void start() {
        super.start();
        DateFormat formatter = DateFormat.getDateTimeInstance(DateFormat.MEDIUM,
                DateFormat.MEDIUM);
        List params = new ArrayList();
        params.add(version.getVersionString());
        params.add(formatter.format(new Date()));
        String startupBanner = LocaleUtils.getLocalizedString("startup.name", params);
        Log.info("####################################################################");
        Log.info(startupBanner);
        Log.info("--------------------------------------------------------------------");
        System.out.println(startupBanner);

        params.clear();
        params.add(name);
        Log.info(LocaleUtils.getLocalizedString("startup.starting", params));

        // Register the server as an MBean.
//            MBeanManager.registerMBean(this, ("Server:Name=" + getName()));

        startDate = new Date();
        stopDate = null;
    }

    public void stop() {
        super.stop();
        stopDate = new Date();
    }
}