Package org.olat.instantMessaging

Source Code of org.olat.instantMessaging.InstantMessagingModule

/**
* OLAT - Online Learning and Training<br />
* http://www.olat.org
* <p>
* Licensed under the Apache License, Version 2.0 (the "License"); <br />
* you may not use this file except in compliance with the License.<br />
* You may obtain a copy of the License at
* <p>
* http://www.apache.org/licenses/LICENSE-2.0
* <p>
* Unless required by applicable law or agreed to in writing,<br />
* software distributed under the License is distributed on an "AS IS" BASIS, <br />
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. <br />
* See the License for the specific language governing permissions and <br />
* limitations under the License.
* <p>
* Copyright (c) since 2004 at Multimedia- & E-Learning Services (MELS),<br />
* University of Zurich, Switzerland.
* <p>
*/

package org.olat.instantMessaging;

import org.jivesoftware.smack.ConnectionConfiguration;
import org.jivesoftware.smack.XMPPConnection;
import org.olat.admin.user.delete.service.UserDeletionManager;
import org.olat.basesecurity.Authentication;
import org.olat.basesecurity.Manager;
import org.olat.basesecurity.ManagerFactory;
import org.olat.configuration.ConfigurationManager;
import org.olat.core.CoreSpringFactory;
import org.olat.core.commons.persistence.DBFactory;
import org.olat.core.configuration.OLATModule;
import org.olat.core.id.Identity;
import org.olat.core.logging.OLog;
import org.olat.core.logging.Tracing;
import org.olat.user.UserDataDeletable;

import com.anthonyeden.lib.config.Configuration;

/**
* Description: <br />
* Configuration class that is instantiated by the framework if configured in
* olat_config.xml. For configuration issues see olat.properties and run the
* ant task config-all to affect the changes. Do not edit the
* olat_config.xml manually! All classes from the instant messaging package
* obtain there configuration values by calling the methods of this class.
* <P>
* Initial Date: 14.10.2004
*
* @author Guido Schnider
*/
public class InstantMessagingModule implements OLATModule,UserDataDeletable {

  private static ConnectionConfiguration connConfig;
  private static XMPPConnection adminConnection;
  private static final String CONFIG_SYNCED_KEY = "issynced";
  OLog log = Tracing.createLoggerFor(this.getClass());
 
 
  /**
   * call superclass constructor
   */
  public InstantMessagingModule() {
    super();
    UserDeletionManager.getInstance().registerDeletableUserData(this);
  }

  /**
   * @see org.olat.core.configuration.OLATModule#init(com.anthonyeden.lib.config.Configuration)
   */
  @SuppressWarnings("unchecked")
  public void init(Configuration configuration) {
    IMConfig config = InstantMessagingModule.getAdapter().getConfig();
   
    if (config.isEnabled()) {
     
      //create test accounts local and on the IM server
      if (InstantMessagingModule.getAdapter().getConfig().generateTestUsers()) checkAndCreateTestUsers();

      // synchronizing of existing buddygroups with the instant messaging
      // server
      // if done we set a property (it gets only done once, to reactivate delete
      // entry in table o_property)
      /**
       * delete from o_property where name='org.olat.instantMessaging.InstantMessagingModule::issynced';
       */
     
      //the new way to get the (singleton) ConfigurationManager
      ConfigurationManager cman = (ConfigurationManager)CoreSpringFactory.getBean("olatconfigbean");
      boolean isSynced = cman.findOrCreateBooleanProperty(InstantMessagingModule.class, CONFIG_SYNCED_KEY, false);
      if (!isSynced) {
       
        if (config.isSyncPersonalGroups()) InstantMessagingModule.getAdapter().synchronizeAllBuddyGroupsWithIMServer();
        if (config.isSyncLearningGroups()) InstantMessagingModule.getAdapter().synchronizeLearningGroupsWithIMServer();
        cman.setBooleanProperty(InstantMessagingModule.class, CONFIG_SYNCED_KEY, true);
      }

      // Cleanup, otherwise this subjects will have problems in normal OLAT
      // operation
      DBFactory.getInstance().intermediateCommit();
     
    }// end if enabled

  }



  /**
   * if enabled in the configuration some testusers for IM are created in the
   * database. It has nothing to do with accounts on the jabber server itself.
   */
  private void checkAndCreateTestUsers() {
    Identity identity;
    Authentication auth;
    Manager securityManager = ManagerFactory.getManager();
    identity = securityManager.findIdentityByName("author");
    auth = ManagerFactory.getManager().findAuthentication(identity, ClientManager.PROVIDER_INSTANT_MESSAGING);
    if (auth == null) { // create new authentication for provider
      ManagerFactory.getManager().createAndPersistAuthentication(identity, ClientManager.PROVIDER_INSTANT_MESSAGING, identity.getName(),
          "test");
      InstantMessagingModule.getAdapter().createAccount("author", "test", "Aurich Throw", "author@olat-newinstallation.org");
    }

    identity = securityManager.findIdentityByName("administrator");
    auth = ManagerFactory.getManager().findAuthentication(identity, ClientManager.PROVIDER_INSTANT_MESSAGING);
    if (auth == null) { // create new authentication for provider
      ManagerFactory.getManager().createAndPersistAuthentication(identity, ClientManager.PROVIDER_INSTANT_MESSAGING, identity.getName(),
          "olat");
      InstantMessagingModule.getAdapter().createAccount("administrator", "olat", "Administrator", "administrator@olat-newinstallation.org");
    }

    identity = securityManager.findIdentityByName("learner");
    auth = ManagerFactory.getManager().findAuthentication(identity, ClientManager.PROVIDER_INSTANT_MESSAGING);
    if (auth == null) { // create new authentication for provider
      ManagerFactory.getManager().createAndPersistAuthentication(identity, ClientManager.PROVIDER_INSTANT_MESSAGING, identity.getName(),
          "test");
      InstantMessagingModule.getAdapter().createAccount("learner", "test", "Leise Arnerich", "learner@olat-newinstallation.org");
    }

    identity = securityManager.findIdentityByName("test");
    auth = ManagerFactory.getManager().findAuthentication(identity, ClientManager.PROVIDER_INSTANT_MESSAGING);
    if (auth == null) { // create new authentication for provider
      ManagerFactory.getManager().createAndPersistAuthentication(identity, ClientManager.PROVIDER_INSTANT_MESSAGING, identity.getName(),
          "test");
      InstantMessagingModule.getAdapter().createAccount("test", "test", "Thomas Est", "test@olat-newinstallation.org");
    }
  }

  /**
   * @see org.olat.core.configuration.OLATModule#destroy()
   */
  public void destroy() {
   if (adminConnection != null) {
     adminConnection.disconnect();
   }
  }


  /**
   * @return the adapter instance
   */
  public static InstantMessaging getAdapter() {
    return (InstantMessaging) CoreSpringFactory.getBean(InstantMessaging.class);
  }

  /**
   * @return Returns the enabled.
   */
  public static boolean isEnabled() {
    return InstantMessagingModule.getAdapter().getConfig().isEnabled();
  }


  /**
   * @return a reused connection configuration for connecting to the im server
   */
  protected static ConnectionConfiguration getConnectionConfiguration() {
    if (connConfig == null) {
      // 5222 is the default unsecured jabber server port
      connConfig = new ConnectionConfiguration(InstantMessagingModule.getAdapter().getConfig().getServername(), 5222);
      connConfig.setNotMatchingDomainCheckEnabled(false);
      connConfig.setSASLAuthenticationEnabled(false);
      connConfig.setReconnectionAllowed(false);
    }
    return connConfig;
  }

  /**
   *
   * @see org.olat.user.UserDataDeletable#deleteUserData(org.olat.core.id.Identity)
   */
  public void deleteUserData(Identity identity, String newDeletedUserName) {
    if (InstantMessagingModule.getAdapter().getConfig().isEnabled()) {
      String imUsername = InstantMessagingModule.getAdapter().getIMUsername(identity.getName());
      InstantMessagingModule.getAdapter().deleteAccount(imUsername);
      log.debug("Deleted IM account for identity=" + identity);
    }
  }

  /**
   * @return Returns the iDLE_POLLTIME.
   */
  public static int getIDLE_POLLTIME() {
    return InstantMessagingModule.getAdapter().getConfig().getIdlePolltime();
  }

  /**
   * @param idle_polltime The iDLE_POLLTIME to set.
   */
  public static void setIDLE_POLLTIME(int idle_polltime) {
    InstantMessagingModule.getAdapter().getConfig().setIdlePolltime(idle_polltime);
  }

  /**
   * @return Returns the cHAT_POLLTIME.
   */
  public static int getCHAT_POLLTIME() {
    return InstantMessagingModule.getAdapter().getConfig().getChatPolltime();
  }

  /**
   * @param chat_polltime The cHAT_POLLTIME to set.
   */
  public static void setCHAT_POLLTIME(int chat_polltime) {
    InstantMessagingModule.getAdapter().getConfig().setChatPolltime(chat_polltime);
  }


  public static boolean isSyncLearningGroups() {
    return InstantMessagingModule.getAdapter().getConfig().isSyncLearningGroups();
  }
}
TOP

Related Classes of org.olat.instantMessaging.InstantMessagingModule

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.