All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.jdiameter.client.impl.SessionFactoryImpl Maven / Gradle / Ivy

There is a newer version: 1.7.1-123
Show newest version
/*
 * JBoss, Home of Professional Open Source
 * Copyright 2010, Red Hat, Inc. and individual contributors
 * by the @authors tag. See the copyright.txt in the distribution for a
 * full listing of individual contributors.
 *
 * This 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 (at your option) any later version.
 *
 * This software 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 software; if not, write to the Free
 * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
 * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
 */

package org.jdiameter.client.impl;

import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

import org.jdiameter.api.ApplicationId;
import org.jdiameter.api.InternalException;
import org.jdiameter.api.RawSession;
import org.jdiameter.api.Session;
import org.jdiameter.api.app.AppSession;
import org.jdiameter.client.api.IContainer;
import org.jdiameter.client.api.ISessionFactory;
import org.jdiameter.client.api.StackState;
import org.jdiameter.client.impl.helpers.UIDGenerator;
import org.jdiameter.common.api.app.IAppSessionFactory;
import org.jdiameter.common.api.data.ISessionDatasource;

/**
 * Implementation for {@link ISessionFactory}
 * 
 * @author [email protected]
 * @author  Bartosz Baranowski 
 * @author  Alexandre Mendonca 
 */
public class SessionFactoryImpl implements ISessionFactory {

  private IContainer stack;

  @SuppressWarnings("rawtypes")
  private Map appFactories = new ConcurrentHashMap();
  private ISessionDatasource dataSource;

  protected static UIDGenerator uid = new UIDGenerator();

  public SessionFactoryImpl(IContainer stack) {
    this.stack = stack;
    this.dataSource = this.stack.getAssemblerFacility().getComponentInstance(ISessionDatasource.class);
  }

  public String getSessionId(String custom) {
    long id = uid.nextLong();
    long high32 = (id & 0xffffffff00000000L) >> 32;
    long low32 = (id & 0xffffffffL);
    StringBuilder sb = new StringBuilder();
    sb.append(stack.getMetaData().getLocalPeer().getUri().getFQDN()).
    append(";").append(high32).append(";").append(low32);
    if(custom!=null)
    {
      //FIXME: add checks for not allowed chars?
      sb.append(";").append(custom);
    }
    return sb.toString();
  }

  public String getSessionId() {
    return this.getSessionId(null);
  }

  public RawSession getNewRawSession() throws InternalException {
    if (stack.getState() == StackState.IDLE) {
      throw new InternalException("Illegal state of stack");
    }
    return new RawSessionImpl(stack);
  }

  public Session getNewSession() throws InternalException {
    if (stack.getState() == StackState.IDLE) {
      throw new InternalException("Illegal state of stack");
    }

    // FIXME: store this! Properly handle in ISessiondata
    SessionImpl session = new SessionImpl(stack); 
    this.dataSource.addSession(session);
    return session;
  }

  public Session getNewSession(String sessionId) throws InternalException {
    if (stack.getState() == StackState.IDLE) {
      throw new InternalException("Illegal state of stack");
    }
    SessionImpl session = new SessionImpl(stack);
    if (sessionId != null && sessionId.length() > 0) {
      session.sessionId = sessionId;
    }
    // FIXME: store this! Properly handle in ISessiondata
    this.dataSource.addSession(session);
    return session;
  }

  @SuppressWarnings("unchecked")
  public  T getNewAppSession(ApplicationId applicationId, Class aClass) throws InternalException {
    return (T) getNewAppSession(null, applicationId, aClass, new Object[0]);
  }

  @SuppressWarnings("unchecked")
  public  T getNewAppSession(String sessionId, ApplicationId applicationId, Class aClass) throws InternalException {
    return (T) getNewAppSession(sessionId, applicationId,aClass, new Object[0]);
  }

  @SuppressWarnings("unchecked")
  public  T getNewAppSession(String sessionId,  ApplicationId applicationId, Class aClass, Object... args) throws InternalException {
    T session = null;
    if (stack.getState() == StackState.IDLE)
      throw new InternalException("Illegal state of stack");
    if (appFactories.containsKey(aClass)) {
      session = (T) ((IAppSessionFactory) appFactories.get(aClass)).getNewSession(sessionId, aClass, applicationId, args);
      // FIXME: add check if it exists already!
      //dataSource.addSession(session);
    }
    return session;
  }

  public void registerAppFacory(Class sessionClass, IAppSessionFactory factory) {
    appFactories.put(sessionClass, factory);
  }

  public void unRegisterAppFacory(Class sessionClass) {
    appFactories.remove(sessionClass);
  }

  /* (non-Javadoc)
   * @see org.jdiameter.client.api.ISessionFactory#getAppSessionFactory(java.lang.Class)
   */
  public IAppSessionFactory getAppSessionFactory(Class sessionClass) {
    return appFactories.get(sessionClass);
  }

  public IContainer getContainer() {
    return stack;
  }

}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy