/servers/diameter/core/jdiameter/impl/src/main/java/org/jdiameter/common/impl/app/acc/AccSessionFactoryImpl.java
Java | 466 lines | 266 code | 56 blank | 144 comment | 52 complexity | 942673ccf22e428c4d9bccc769ebfb0f MD5 | raw file
Possible License(s): LGPL-3.0, GPL-3.0, LGPL-2.1, GPL-2.0, CC-BY-SA-3.0, CC0-1.0, Apache-2.0, BSD-3-Clause
- /*
- * JBoss, Home of Professional Open Source
- * Copyright 2006, 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.common.impl.app.acc;
-
- import java.util.concurrent.ScheduledFuture;
-
- import org.jdiameter.api.Answer;
- import org.jdiameter.api.ApplicationId;
- import org.jdiameter.api.IllegalDiameterStateException;
- import org.jdiameter.api.InternalException;
- import org.jdiameter.api.OverloadException;
- import org.jdiameter.api.Request;
- import org.jdiameter.api.RouteException;
- import org.jdiameter.api.SessionFactory;
- import org.jdiameter.api.acc.ClientAccSession;
- import org.jdiameter.api.acc.ClientAccSessionListener;
- import org.jdiameter.api.acc.ServerAccSession;
- import org.jdiameter.api.acc.ServerAccSessionListener;
- import org.jdiameter.api.acc.events.AccountAnswer;
- import org.jdiameter.api.acc.events.AccountRequest;
- import org.jdiameter.api.app.AppAnswerEvent;
- import org.jdiameter.api.app.AppRequestEvent;
- import org.jdiameter.api.app.AppSession;
- import org.jdiameter.api.app.StateChangeListener;
- import org.jdiameter.client.api.ISessionFactory;
- import org.jdiameter.client.impl.app.acc.ClientAccSessionImpl;
- import org.jdiameter.client.impl.app.acc.IClientAccSessionData;
- import org.jdiameter.common.api.app.IAppSessionDataFactory;
- import org.jdiameter.common.api.app.acc.IAccMessageFactory;
- import org.jdiameter.common.api.app.acc.IAccSessionData;
- import org.jdiameter.common.api.app.acc.IAccSessionFactory;
- import org.jdiameter.common.api.app.acc.IClientAccActionContext;
- import org.jdiameter.common.api.app.acc.IServerAccActionContext;
- import org.jdiameter.common.api.data.ISessionDatasource;
- import org.jdiameter.server.impl.app.acc.IServerAccSessionData;
- import org.jdiameter.server.impl.app.acc.ServerAccSessionImpl;
- import org.slf4j.Logger;
- import org.slf4j.LoggerFactory;
-
- /**
- * Default Diameter Account Session Factory implementation
- *
- * @author <a href="mailto:baranowb@gmail.com"> Bartosz Baranowski </a>
- * @author <a href="mailto:brainslog@gmail.com"> Alexandre Mendonca </a>
- */
- public class AccSessionFactoryImpl implements IAccSessionFactory,IAccMessageFactory, ServerAccSessionListener, ClientAccSessionListener, IClientAccActionContext, IServerAccActionContext, StateChangeListener<AppSession> {
-
- protected Logger logger = LoggerFactory.getLogger(AccSessionFactoryImpl.class);
-
- protected ServerAccSessionListener serverSessionListener;
- protected StateChangeListener<AppSession> stateListener;
- protected ClientAccSessionListener clientSessionListener;
- protected IClientAccActionContext clientContextListener;
- protected IServerAccActionContext serverContextListener;
-
- protected ISessionDatasource iss;
- protected ISessionFactory sessionFactory = null;
- protected ApplicationId applicationId;
- protected IAccMessageFactory messageFactory = this;
- protected IAppSessionDataFactory<IAccSessionData> sessionDataFactory;
-
- protected AccSessionFactoryImpl() {
- }
-
- public AccSessionFactoryImpl(SessionFactory sessionFactory) {
- super();
-
- setSessionFactory((ISessionFactory) sessionFactory);
- }
-
- // ACC Factory Methods ------------------------------------------------------
-
- /**
- * @return the serverSessionListener
- */
- public ServerAccSessionListener getServerSessionListener() {
- if (this.serverSessionListener != null) {
- return serverSessionListener;
- }
- else {
- return this;
- }
- }
-
- /**
- * @param serverSessionListener
- * the serverSessionListener to set
- */
- public void setServerSessionListener(ServerAccSessionListener serverSessionListener) {
- this.serverSessionListener = serverSessionListener;
- }
-
- /**
- * @return the stateListener
- */
- public StateChangeListener<AppSession> getStateListener() {
- if (this.stateListener != null) {
- return stateListener;
- }
- else {
- return this;
- }
- }
-
- /**
- * @param stateListener
- * the stateListener to set
- */
- public void setStateListener(StateChangeListener<AppSession> stateListener) {
- this.stateListener = stateListener;
- }
-
- /**
- * @return the clientSessionListener
- */
- public ClientAccSessionListener getClientSessionListener() {
- if (this.clientSessionListener != null) {
- return clientSessionListener;
- }
- else {
- return this;
- }
- }
-
- /**
- * @param clientSessionListener
- * the clientSessionListener to set
- */
- public void setClientSessionListener(ClientAccSessionListener clientSessionListener) {
- this.clientSessionListener = clientSessionListener;
- }
-
- /**
- * @return the clientContextListener
- */
- public IClientAccActionContext getClientContextListener() {
- if (this.clientContextListener != null) {
- return clientContextListener;
- }
- else {
- return this;
- }
- }
-
- /**
- * @param clientContextListener
- * the clientContextListener to set
- */
- public void setClientContextListener(IClientAccActionContext clientContextListener) {
- this.clientContextListener = clientContextListener;
- }
-
- /**
- * @return the serverContextListener
- */
- public IServerAccActionContext getServerContextListener() {
- if (this.serverContextListener != null) {
- return serverContextListener;
- }
- else {
- return this;
- }
- }
-
- /**
- * @param serverContextListener
- * the serverContextListener to set
- */
- public void setServerContextListener(IServerAccActionContext serverContextListener) {
- this.serverContextListener = serverContextListener;
- }
-
- public void setMessageFactory(IAccMessageFactory messageFactory)
- {
- this.messageFactory = messageFactory;
- }
-
- public IAccMessageFactory getMessageFactory()
- {
- return this.messageFactory;
-
- }
- /**
- * @return the sessionFactory
- */
- public ISessionFactory getSessionFactory() {
- return sessionFactory;
- }
-
- /**
- * @param sessionFactory the sessionFactory to set
- */
- public void setSessionFactory(ISessionFactory sessionFactory) {
- this.sessionFactory = sessionFactory;
- if (this.iss == null) {
- this.iss = this.sessionFactory.getContainer().getAssemblerFacility().getComponentInstance(ISessionDatasource.class);
- this.sessionDataFactory = (IAppSessionDataFactory<IAccSessionData>) this.iss.getDataFactory(IAccSessionData.class);
- if (this.sessionDataFactory == null) {
- logger.debug("No factory for Accounting Application data, using default/local.");
- this.sessionDataFactory = new AccLocalSessionDataFactory();
- }
- }
- }
-
- /*
- * (non-Javadoc)
- *
- * @see org.jdiameter.common.api.app.acc.IAccSessionFactory#getApplicationId()
- */
- public ApplicationId getApplicationId() {
- return this.applicationId;
- }
-
- /*
- * (non-Javadoc)
- *
- * @see org.jdiameter.common.api.app.acc.IAccSessionFactory#setApplicationId( org.jdiameter.api.ApplicationId)
- */
- public void setApplicationId(ApplicationId id) {
- this.applicationId = id;
- }
-
- // App Session Factory ------------------------------------------------------
-
- @Override
- public AppSession getSession(String sessionId, Class<? extends AppSession> aClass) {
- if (sessionId == null) {
- throw new IllegalArgumentException("SessionId must not be null");
- }
- if(!this.iss.exists(sessionId)) {
- return null;
- }
- AppSession appSession = null;
- try {
- if (aClass == ClientAccSession.class) {
- IClientAccSessionData data = (IClientAccSessionData) this.sessionDataFactory.getAppSessionData(ClientAccSession.class, sessionId);
- ClientAccSessionImpl clientSession = new ClientAccSessionImpl(data, sessionFactory, getClientSessionListener(), getClientContextListener(), getStateListener());
-
- clientSession.getSessions().get(0).setRequestListener(clientSession);
- appSession = clientSession;
- }
- else if (aClass == ServerAccSession.class) {
- ServerAccSessionImpl serverSession = null;
- IServerAccSessionData data = (IServerAccSessionData) this.sessionDataFactory.getAppSessionData(ServerAccSession.class, sessionId);
-
- //here we use shorter con, since some data is already present.
- serverSession = new ServerAccSessionImpl(data,sessionFactory, getServerSessionListener(), getServerContextListener(), getStateListener());
- serverSession.getSessions().get(0).setRequestListener(serverSession);
- appSession = serverSession;
- }
- else {
- throw new IllegalArgumentException("Wrong session class: " + aClass + ". Supported[" + ClientAccSession.class + "," + ServerAccSession.class + "]");
- }
- }
- catch (Exception e) {
- logger.error("Failure to obtain new Accounting Session.", e);
- }
-
- return appSession;
- }
-
- public AppSession getNewSession(String sessionId, Class<? extends AppSession> aClass, ApplicationId applicationId, Object[] args) {
- try {
- if (aClass == ServerAccSession.class) {
- if (sessionId == null) {
- if (args != null && args.length > 0 && args[0] instanceof Request) {
- Request request = (Request) args[0];
- sessionId = request.getSessionId();
- }
- else {
- sessionId = this.sessionFactory.getSessionId();
- }
- }
- boolean stateless = true;
- if(args != null && args.length > 0) {
- for(Object o:args) {
- if(o instanceof Boolean) {
- stateless = (Boolean)o;
- }
- }
- }
-
- IServerAccSessionData data = (IServerAccSessionData) this.sessionDataFactory.getAppSessionData(ServerAccSession.class, sessionId);
- data.setApplicationId(applicationId);
- ServerAccSessionImpl session = new ServerAccSessionImpl(data, sessionFactory, getServerSessionListener(), getServerContextListener(), getStateListener(), stateless);
- iss.addSession(session);
- session.getSessions().get(0).setRequestListener(session);
- return session;
- }
- else if (aClass == ClientAccSession.class) {
- if (sessionId == null) {
- if (args != null && args.length > 0 && args[0] instanceof Request) {
- Request request = (Request) args[0];
- sessionId = request.getSessionId();
- }
- else {
- sessionId = this.sessionFactory.getSessionId();
- }
- }
- IClientAccSessionData data = (IClientAccSessionData) this.sessionDataFactory.getAppSessionData(ClientAccSession.class, sessionId);
- data.setApplicationId(applicationId);
- ClientAccSessionImpl session = new ClientAccSessionImpl(data, sessionFactory, getClientSessionListener(), getClientContextListener(), getStateListener());
-
- iss.addSession(session);
- session.getSessions().get(0).setRequestListener(session);
- return session;
- }
- }
- catch (Exception e) {
- logger.error("Failure to obtain new Accounting Session.", e);
- }
-
- return null;
- }
-
- // State Change Listener ----------------------------------------------------
-
- public void stateChanged(Enum oldState, Enum newState) {
- logger.info("Diameter ACC SessionFactory :: stateChanged :: oldState[{}], newState[{}]", oldState, newState);
- }
-
- /*
- * (non-Javadoc)
- *
- * @see org.jdiameter.api.app.StateChangeListener#stateChanged(java.lang.Object, java.lang.Enum, java.lang.Enum)
- */
- public void stateChanged(AppSession source, Enum oldState, Enum newState) {
- logger.info("Diameter ACC SessionFactory :: stateChanged :: source[{}], oldState[{}], newState[{}]", new Object[] { source, oldState, newState });
- }
-
- // ///////////////////
- // Event listeners //
- // ///////////////////
-
- public void doAccRequestEvent(ServerAccSession appSession, AccountRequest acr) throws InternalException, IllegalDiameterStateException, RouteException, OverloadException {
- logger.info("Diameter Base AccountingSessionFactory :: doAccRequestEvent :: appSession[" + appSession + "], Request[" + acr + "]");
- }
-
- public void doAccAnswerEvent(ClientAccSession appSession, AccountRequest acr, AccountAnswer aca) throws InternalException, IllegalDiameterStateException, RouteException, OverloadException {
- logger.info("doAccAnswerEvent :: appSession[" + appSession + "], Request[" + acr + "], Answer[" + aca + "]");
- }
-
- public void doOtherEvent(AppSession appSession, AppRequestEvent request, AppAnswerEvent answer) throws InternalException, IllegalDiameterStateException, RouteException, OverloadException {
- logger.info("Diameter Base AccountingSessionFactory :: doOtherEvent :: appSession[" + appSession + "], Request[" + request + "], Answer[" + answer + "]");
- }
-
- // Client context -----------------------------------------------------------
-
- /*
- * (non-Javadoc)
- *
- * @see org.jdiameter.common.api.app.acc.IClientAccActionContext#disconnectUserOrDev (org.jdiameter.api.Request)
- */
- public void disconnectUserOrDev(ClientAccSession appSession, Request sessionTermRequest) throws InternalException {
- logger.info("disconnectUserOrDev :: appSession[" + appSession + "], Request[" + sessionTermRequest + "]");
- }
-
- /*
- * (non-Javadoc)
- *
- * @see org.jdiameter.common.api.app.acc.IClientAccActionContext#failedSendRecord (org.jdiameter.api.Request)
- */
- public boolean failedSendRecord(ClientAccSession appSession, Request accRequest) throws InternalException {
- logger.info("failedSendRecord :: appSession[" + appSession + "], Request[" + accRequest + "]");
- return false;
- }
-
- /*
- * (non-Javadoc)
- *
- * @see org.jdiameter.common.api.app.acc.IClientAccActionContext# interimIntervalElapses(org.jdiameter.api.Request)
- */
- public void interimIntervalElapses(ClientAccSession appSession, Request interimRequest) throws InternalException {
- logger.info("interimIntervalElapses :: appSession[" + appSession + "], Request[" + interimRequest + "]");
- }
-
- // Server context -----------------------------------------------------------
-
- /*
- * (non-Javadoc)
- *
- * @seeorg.jdiameter.common.api.app.acc.IServerAccActionContext#sessionTimeoutElapses(org.jdiameter.api.acc.ServerAccSession)
- */
- public void sessionTimeoutElapses(ServerAccSession appSession) throws InternalException {
- logger.info("sessionTimeoutElapses :: appSession[" + appSession + "]");
-
- }
-
- /*
- * (non-Javadoc)
- *
- * @see org.jdiameter.common.api.app.acc.IServerAccActionContext#sessionTimerStarted(org.jdiameter.api.acc.ServerAccSession, java.util.concurrent.ScheduledFuture)
- */
- @SuppressWarnings("unchecked")
- public void sessionTimerStarted(ServerAccSession appSession, ScheduledFuture timer) throws InternalException {
- logger.info("sessionTimerStarted :: appSession[" + appSession + "]");
- }
-
- /*
- * (non-Javadoc)
- *
- * @see org.jdiameter.common.api.app.acc.IServerAccActionContext#srssionTimerCanceled(org.jdiameter.api.acc.ServerAccSession, java.util.concurrent.ScheduledFuture)
- */
- @SuppressWarnings("unchecked")
- public void sessionTimerCanceled(ServerAccSession appSession, ScheduledFuture timer) throws InternalException {
- logger.info("sessionTimerCanceled :: appSession[" + appSession + "]");
- }
-
- /*
- * (non-Javadoc)
- *
- * @see
- * org.jdiameter.common.api.app.acc.IAccMessageFactory#getAccMessageCommandCode
- * ()
- */
- @Override
- public int getAccMessageCommandCode() {
- // TODO Auto-generated method stub
- return AccountRequest.code;
- }
-
- /*
- * (non-Javadoc)
- *
- * @see
- * org.jdiameter.common.api.app.acc.IAccMessageFactory#createAccRequest(
- * org.jdiameter.api.Request)
- */
- @Override
- public AccountRequest createAccRequest(Request request) {
- return new AccountRequestImpl(request);
- }
-
- /*
- * (non-Javadoc)
- *
- * @see
- * org.jdiameter.common.api.app.acc.IAccMessageFactory#createAccAnswer(org
- * .jdiameter.api.Answer)
- */
- @Override
- public AccountAnswer createAccAnswer(Answer answer) {
- return new AccountAnswerImpl(answer);
- }
-
- }