LocalSessionStore.java [src/csip] Revision: default  Date:
/*
 * $Id$
 *
 * This file is part of the Cloud Services Integration Platform (CSIP),
 * a Model-as-a-Service framework, API and application suite.
 *
 * 2012-2022, Olaf David and others, OMSLab, Colorado State University.
 *
 * OMSLab licenses this file to you under the MIT license.
 * See the LICENSE file in the project root for more information.
 */
package csip;

import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;

/**
 * This is a local session store. One instance only, no failover.
 */
class LocalSessionStore implements SessionStore {

  Map<String, ModelSession> m = Collections.synchronizedMap(new HashMap<String, ModelSession>());


  public LocalSessionStore() {
    Config.LOG.info("Using local session store.");
  }


  @Override
  public void close() throws Exception {
    m.clear();
  }


  @Override
  public void setSession(String key, ModelSession session) throws Exception {
    m.put(key, session);
  }


  @Override
  public ModelSession getSession(String key) throws Exception {
    return m.get(key);
  }


  @Override
  public void removeSession(String key) {
    m.remove(key);
  }


  @Override
  public Set<String> keys(int skip, int limit, String sortby, boolean sortAscending) {
    return m.keySet();
  }


  @Override
  public long getCount() {
    return m.size();
  }


  @Override
  public void ping() {
    if (m == null) 
      throw new NullPointerException("Illegal hashtable store.");
  }


  @Override
  public boolean hasSession(String suid) throws Exception {
    return m.containsKey(suid);
  }


  @Override
  public long countSessionsByState(String state) {
    long l = 0;
    for (ModelSession s : m.values()) {
      if (s.getStatus().equals(state)) {
        l++;
      }
    }
    return l;
  }


  @Override
  public void registerResources(boolean register) {
    Config.LOG.warning("Not implemented.");
  }

}