BasicServer.java 3.99 KB
Newer Older
Matt Tucker's avatar
Matt Tucker committed
1 2 3 4 5
/**
 * $RCSfile$
 * $Revision$
 * $Date$
 *
Matt Tucker's avatar
Matt Tucker committed
6
 * Copyright (C) 2004 Jive Software. All rights reserved.
Matt Tucker's avatar
Matt Tucker committed
7
 *
Matt Tucker's avatar
Matt Tucker committed
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
 */
Matt Tucker's avatar
Matt Tucker committed
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
package org.jivesoftware.messenger.spi;

import org.jivesoftware.messenger.container.BasicModule;
import org.jivesoftware.messenger.container.Container;
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
 */
Matt Tucker's avatar
Matt Tucker committed
32
public class BasicServer extends BasicModule implements XMPPServer, BasicServerMBean {
Matt Tucker's avatar
Matt Tucker committed
33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51

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

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

    public XMPPServerInfo getServerInfo() {
        Iterator ports;
        if (connectionManager == null) {
Matt Tucker's avatar
Matt Tucker committed
52
            connectionManager = (ConnectionManager)lookup.lookup(ConnectionManager.class);
Matt Tucker's avatar
Matt Tucker committed
53 54 55 56 57 58 59 60 61 62
        }
        if (connectionManager == null) {
            ports = Collections.EMPTY_LIST.iterator();
        }
        else {
            ports = connectionManager.getPorts();
        }
        if (!initialized) {
            throw new IllegalStateException("Not initialized yet");
        }
63
        return new XMPPServerInfoImpl(name, version, startDate, stopDate, ports);
Matt Tucker's avatar
Matt Tucker committed
64 65 66 67 68 69 70 71 72 73 74 75 76 77
    }

    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);
    }

Matt Tucker's avatar
Matt Tucker committed
78 79
    private Session serverSession = new ServerSession(new XMPPAddress(null, name, null),
            new BasicStreamIDFactory().createStreamID(name));
Matt Tucker's avatar
Matt Tucker committed
80 81 82 83 84 85 86 87 88

    public Session getSession() {
        return serverSession;
    }

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

89 90
    public void initialize(Container container) {
        super.initialize(container);
Matt Tucker's avatar
Matt Tucker committed
91 92
        try {
            lookup = container.getServiceLookup();
Matt Tucker's avatar
Matt Tucker committed
93
            name = JiveGlobals.getProperty("xmpp.domain");
Matt Tucker's avatar
Matt Tucker committed
94 95 96 97
            if (name == null) {
                name = "127.0.0.1";
            }

Bill Lynch's avatar
Bill Lynch committed
98
            version = new Version(2, 0, 1, Version.ReleaseStatus.Release, -1);
Matt Tucker's avatar
Matt Tucker committed
99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116
            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(startupBanner);
        System.out.println(startupBanner);

Matt Tucker's avatar
Matt Tucker committed
117 118 119
//        params.clear();
//        params.add(name);
//        Log.info(LocaleUtils.getLocalizedString("startup.starting", params));
Matt Tucker's avatar
Matt Tucker committed
120 121 122 123 124 125 126 127 128 129 130 131

        // 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();
    }
Matt Tucker's avatar
Matt Tucker committed
132
}