/*
 * JORAM: Java(TM) Open Reliable Asynchronous Messaging
 * Copyright (C) 2009 - 2026 ScalAgent Distributed Technologies
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or any later version.
 * 
 * This library 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
 * Lesser General Public License for more details.
 * 
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307
 * USA.
 *
 * Initial developer(s): ScalAgent Distributed Technologies
 */
package org.ow2.joram.jakarta.jms.local;

import java.util.ArrayList;
import java.util.List;

import org.objectweb.joram.mom.proxies.ConnectionManager;
import org.objectweb.util.monolog.api.BasicLevel;
import org.objectweb.util.monolog.api.Logger;

import fr.dyade.aaa.common.Configuration;
import fr.dyade.aaa.common.Debug;
import fr.dyade.aaa.common.ExponentialMovingAverage;

/**
 * Class used to check off local connections.
 */
public class LocalConnections implements LocalConnectionsMBean {
  static final Logger logmon = Debug.getLogger("org.objectweb.joram.mom.proxies.local.monitor");
  static final boolean MONITOR = logmon.isLoggable(BasicLevel.INFO);
  private static final int PERIODS = Configuration.getInteger("org.objectweb.joram.mom.proxies.AverageConnectionsPeriods", 100);
  
  private static final String MBEAN_NAME = "type=Connection,mode=local";

  /** Unique LocalConnections instance. */
  private static LocalConnections currentInstance;
  
  /** Lists the opened local connections. */
  private List<LocalRequestChannel> connections = new ArrayList<LocalRequestChannel>();

  /** If true, creation of new connections is available. */
  private boolean activated = true;

  /** Number of failed login attempts. */
  private int failedLoginCount;

  /** Number of local connections initiated since agent server start. */
  private int initiatedConnectionCount;

  private LocalConnections() {}
  
  public void activate() {
    activated = true;
  }

  public void closeAllConnections() {
    LocalRequestChannel[] array = connections.toArray(new LocalRequestChannel[connections.size()]);
    for (int i = 0; i < array.length; i++) {
      array[i].close();
    }
  }

  public void deactivate() {
    activated = false;
  }

  public int getRunningConnectionsCount() {
    return connections.size();
  }

  public boolean isActivated() {
    return activated;
  }

  public void addLocalConnection(LocalRequestChannel localRequestChannel) {
    connections.add(localRequestChannel);
  }
  
  public void removeLocalConnection(LocalRequestChannel localRequestChannel) {
    connections.remove(localRequestChannel);
  }
  
  public static synchronized LocalConnections getCurrentInstance() {
    if (currentInstance == null) {
      currentInstance = new LocalConnections();
      ConnectionManager.getCurrentInstance().addManager(currentInstance);
    }
    return currentInstance;
  }

  public String getMBeanName() {
    return MBEAN_NAME;
  }
  
  public String getVersion() {
    return ConnectionManager.version;
  }

  public int getFailedLoginCount() {
    return failedLoginCount;
  }

  public synchronized void increaseFailedLoginCount() {
    failedLoginCount++;
  }

  public int getInitiatedConnectionCount() {
    return initiatedConnectionCount;
  }

  public synchronized void increaseInitiatedConnectionCount() {
    initiatedConnectionCount++;
  }
  
  static final ExponentialMovingAverage average;
  
  static {
    if (MONITOR)
      average = new ExponentialMovingAverage(PERIODS);
    else
      average = null;
  }
  
  /**
   * Gets the average time for incoming local connections.
   * 
   * @return the average time for incoming local connections.
   */
  public long getAverageConnectionTime() {
    if (MONITOR)
      return average.getLongAverage();
    return 0;
  }
}
