Package net.sphene.goim.components

Source Code of net.sphene.goim.components.GalenaExternalComponentManager

/*
* Gamers Own Instant Messenger
* Copyright (C) 2005-2006 Herbert Poul (kahless@sphene.net)
* http://goim.sphene.net
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA
*/
package net.sphene.goim.components;

import java.util.Hashtable;
import java.util.Map;
import java.util.prefs.Preferences;

import javax.net.SocketFactory;

import org.jivesoftware.whack.ExternalComponent;
import org.jivesoftware.whack.ExternalComponentManager;
import org.jivesoftware.whack.container.ServerContainer;
import org.xmpp.component.Component;
import org.xmpp.component.ComponentException;
import org.xmpp.component.ComponentManagerFactory;
import org.xmpp.component.Log;
import org.xmpp.packet.JID;
import org.xmpp.packet.Packet;

/**
* Code copied from ExternalComponentManager to customize behaviour
*/
public class GalenaExternalComponentManager extends ExternalComponentManager {
  /**
   * Keeps the IP address or hostname of the server. This value will be used only for creating
   * connections.
   */
  private String host;
  /**
   * Port of the server used for establishing new connections.
   */
  private int port;
  /**
   * Keeps the domain of the XMPP server. The domain may or may not match the host. The domain
   * will be used mainly for the XMPP packets while the host is used mainly for creating
   * connections to the server.
   */
  private String domain;
  /**
   * This is a global secret key that will be used during the handshake with the server. If a
   * secret key was not defined for the specific component then the global secret key will be
   * used.
   */
  private String defaultSecretKey;
  /**
   * Keeps the secret keys to use for each subdomain. If a key was not found for a specific
   * subdomain then the global secret key will used for the handshake with the server.
   */
  private Map<String, String> secretKeys = new Hashtable<String,String>();
 
  Preferences preferences = Preferences.userRoot();
  private String preferencesPrefix;
 
  /**
   * Keeps a map that associates a domain with the external component thas is handling the domain.
   */
  private Map<String, ExternalComponent> componentsByDomain = new Hashtable<String,ExternalComponent>();
  /**
   * Keeps a map that associates a component with the wrapping ExternalComponent.
   */
  private Map<Component, ExternalComponent> components  = new Hashtable<Component,ExternalComponent>();
 
  private Log logger;
 
  /**
   * Constructs a new ExternalComponentManager that will make connections
   * to the specified XMPP server on the default port (5222).
   *
   * @param host the IP address or name of the XMPP server to connect to (e.g. "example.com").
   */
  public GalenaExternalComponentManager(String host) {
    this(host, 5222);
  }
 
  /**
   * Constructs a new ExternalComponentManager that will make connections to
   * the specified XMPP server on the given port.
   *
   * @param host the IP address or name of the XMPP server to connect to (e.g. "example.com").
   * @param port the port to connect on.
   */
  public GalenaExternalComponentManager(String host, int port) {
    super(host,port);
    this.host = host;
    this.port = port;
    this.domain = host;
   
    createDummyLogger();
   
    // Set this ComponentManager as the current component manager
    ComponentManagerFactory.setComponentManager(this);
  }
 
  /**
   * Sets a secret key for a sub-domain, for future use by a component
   * connecting to the server. Keys are used as an authentication mechanism
   * when connecting to the server. Some servers may require a different
   * key for each component, while others may use a global secret key.
   *
   * @param subdomain the sub-domain.
   * @param secretKey the secret key
   */
  public void setSecretKey(String subdomain, String secretKey) {
    secretKeys.put(subdomain, secretKey);
  }
 
  /**
   * Returns the secret key for a sub-domain. If no key was found then the default secret key
   * will be returned.
   *
   * @param subdomain the subdomain to return its secret key.
   * @return the secret key for a sub-domain.
   */
  public String getSecretKey(String subdomain) {
    // Find the proper secret key to connect as the subdomain.
    String secretKey = secretKeys.get(subdomain);
    if (secretKey == null) {
      secretKey = defaultSecretKey;
    }
    return secretKey;
  }
 
  /**
   * Sets the default secret key, which will be used when connecting if a
   * specific secret key for the component hasn't been sent. Keys are used
   * as an authentication mechanism when connecting to the server. Some servers
   * may require a different key for each component, while others may use
   * a global secret key.
   *
   * @param secretKey the default secret key.
   */
  public void setDefaultSecretKey(String secretKey) {
    this.defaultSecretKey = secretKey;
  }
 
  public void addComponent(String subdomain, Component component) throws ComponentException {
    addComponent(subdomain,component,port);
  }
  public void addComponent(String subdomain, Component component, int port) throws ComponentException {
    if (componentsByDomain.containsKey(subdomain)) {
      if (componentsByDomain.get(subdomain).getComponent() == component) {
        // Do nothing since the component has already been registered
        return;
      }
      else {
        throw new IllegalArgumentException("Subdomain already in use by another component " + subdomain);
      }
    }
    // Find the proper secret key to connect as the subdomain.
    String secretKey = secretKeys.get(subdomain);
    if (secretKey == null) {
      secretKey = defaultSecretKey;
    }
    // Create a wrapping ExternalComponent on the component
    ExternalComponent externalComponent = new ExternalComponent(component, this);
    try {
      // Register the new component
      componentsByDomain.put(subdomain, externalComponent);
      components.put(component, externalComponent);
      // Ask the ExternalComponent to connect with the remote server
      externalComponent.connect(host, port, SocketFactory.getDefault(), subdomain);
      // Initialize the component
      JID componentJID = new JID(null, externalComponent.getDomain(), null);
      externalComponent.initialize(componentJID, this);
    } catch (ComponentException e) {
      // Unregister the new component
      componentsByDomain.remove(subdomain);
      components.remove(component);
      // Re-throw the exception
      throw e;
    }
    // Ask the external component to start processing incoming packets
    externalComponent.start();
  }
 
  public void removeComponent(String subdomain) throws ComponentException {
    ExternalComponent externalComponent = componentsByDomain.remove(subdomain);
    components.remove(externalComponent.getComponent());
    if (externalComponent != null) {
      externalComponent.shutdown();
    }
  }
 
  public void sendPacket(Component component, Packet packet) {
    // Get the ExternalComponent that is wrapping the specified component and ask it to
    // send the packet
    components.get(component).send(packet);
  }
 
  public String getProperty(String name) {
    return preferences.get(getPreferencesPrefix() + name, null);
  }
 
  public void setProperty(String name, String value) {
    preferences.put(getPreferencesPrefix() + name, value);
  }
 
  private String getPreferencesPrefix() {
    if (preferencesPrefix == null) {
      preferencesPrefix = "whack." + domain + ".";
    }
    return preferencesPrefix;
  }
 
  /**
   * Sets the domain of the XMPP server. The domain may or may not match the host. The domain
   * will be used mainly for the XMPP packets while the host is used mainly for creating
   * connections to the server.
   *
   * @param domain the domain of the XMPP server.
   */
  public void setServerName(String domain) {
    this.domain = domain;
  }
 
  public String getServerName() {
    return domain;
  }
 
  public boolean isExternalMode() {
    return true;
  }
 
  /**
   * Returns the location of the <code>home</code> directory.
   *
   * @return the location of the home directory.
   */
  public String getHomeDirectory() {
    return ServerContainer.getInstance().getHomeDirectory();
  }
 
  public Log getLog() {
    return logger;
  }
 
  private void createDummyLogger() {
    this.logger = new Log() {
      public void error(String message) {
        System.out.println(message);
      }
     
      public void error(String message, Throwable throwable) {
        System.err.println(message);
        throwable.printStackTrace();
      }
     
      public void error(Throwable throwable) {
        throwable.printStackTrace();
      }
     
      public void warn(String message) {
        System.out.println(message);
      }
     
      public void warn(String message, Throwable throwable) {
        System.out.println(message);
        throwable.printStackTrace();
      }
     
      public void warn(Throwable throwable) {
        throwable.printStackTrace();
      }
     
      public void info(String message) {
        System.out.println(message);
      }
     
      public void info(String message, Throwable throwable) {
        System.out.println(message);
        throwable.printStackTrace();
      }
     
      public void info(Throwable throwable) {
        throwable.printStackTrace();
      }
     
      public void debug(String message) {
        System.out.println(message);
      }
     
      public void debug(String message, Throwable throwable) {
        System.out.println(message);
        throwable.printStackTrace();
      }
     
      public void debug(Throwable throwable) {
        throwable.printStackTrace();
      }
    };
  }
}
TOP

Related Classes of net.sphene.goim.components.GalenaExternalComponentManager

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.