PageRenderTime 51ms CodeModel.GetById 20ms RepoModel.GetById 1ms app.codeStats 0ms

/servers/diameter/core/jdiameter/impl/src/main/java/org/jdiameter/common/impl/app/rf/RfSessionFactoryImpl.java

http://mobicents.googlecode.com/
Java | 448 lines | 245 code | 50 blank | 153 comment | 46 complexity | cc4d4b21ae2dddce1ce6b9c04bedb8d5 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
  1. /*
  2. * JBoss, Home of Professional Open Source
  3. * Copyright 2008, Red Hat, Inc. and individual contributors
  4. * by the @authors tag. See the copyright.txt in the distribution for a
  5. * full listing of individual contributors.
  6. *
  7. * This is free software; you can redistribute it and/or modify it
  8. * under the terms of the GNU Lesser General Public License as
  9. * published by the Free Software Foundation; either version 2.1 of
  10. * the License, or (at your option) any later version.
  11. *
  12. * This software is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  15. * Lesser General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU Lesser General Public
  18. * License along with this software; if not, write to the Free
  19. * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
  20. * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
  21. */
  22. package org.jdiameter.common.impl.app.rf;
  23. import java.util.concurrent.ScheduledFuture;
  24. import org.jdiameter.api.ApplicationId;
  25. import org.jdiameter.api.IllegalDiameterStateException;
  26. import org.jdiameter.api.InternalException;
  27. import org.jdiameter.api.OverloadException;
  28. import org.jdiameter.api.Request;
  29. import org.jdiameter.api.RouteException;
  30. import org.jdiameter.api.SessionFactory;
  31. import org.jdiameter.api.app.AppAnswerEvent;
  32. import org.jdiameter.api.app.AppRequestEvent;
  33. import org.jdiameter.api.app.AppSession;
  34. import org.jdiameter.api.app.StateChangeListener;
  35. import org.jdiameter.api.rf.ClientRfSession;
  36. import org.jdiameter.api.rf.ClientRfSessionListener;
  37. import org.jdiameter.api.rf.ServerRfSession;
  38. import org.jdiameter.api.rf.ServerRfSessionListener;
  39. import org.jdiameter.api.rf.events.RfAccountingAnswer;
  40. import org.jdiameter.api.rf.events.RfAccountingRequest;
  41. import org.jdiameter.client.api.ISessionFactory;
  42. import org.jdiameter.client.impl.app.rf.ClientRfSessionImpl;
  43. import org.jdiameter.client.impl.app.rf.IClientRfSessionData;
  44. import org.jdiameter.common.api.app.IAppSessionDataFactory;
  45. import org.jdiameter.common.api.app.rf.IClientRfActionContext;
  46. import org.jdiameter.common.api.app.rf.IRfSessionData;
  47. import org.jdiameter.common.api.app.rf.IRfSessionFactory;
  48. import org.jdiameter.common.api.app.rf.IServerRfActionContext;
  49. import org.jdiameter.common.api.data.ISessionDatasource;
  50. import org.jdiameter.server.impl.app.rf.IServerRfSessionData;
  51. import org.jdiameter.server.impl.app.rf.ServerRfSessionImpl;
  52. import org.slf4j.Logger;
  53. import org.slf4j.LoggerFactory;
  54. /**
  55. * Default Diameter Rf Session Factory implementation
  56. *
  57. * @author <a href="mailto:baranowb@gmail.com"> Bartosz Baranowski </a>
  58. * @author <a href="mailto:brainslog@gmail.com"> Alexandre Mendonca </a>
  59. */
  60. public class RfSessionFactoryImpl implements IRfSessionFactory, ServerRfSessionListener, ClientRfSessionListener, IClientRfActionContext, IServerRfActionContext,
  61. StateChangeListener<AppSession> {
  62. protected Logger logger = LoggerFactory.getLogger(RfSessionFactoryImpl.class);
  63. protected ServerRfSessionListener serverSessionListener;
  64. protected StateChangeListener<AppSession> stateListener;
  65. protected ClientRfSessionListener clientSessionListener;
  66. protected IClientRfActionContext clientContextListener;
  67. protected IServerRfActionContext serverContextListener;
  68. protected ISessionDatasource iss;
  69. protected ISessionFactory sessionFactory = null;
  70. protected long messageTimeout = 5000;
  71. protected ApplicationId applicationId;
  72. protected IAppSessionDataFactory<IRfSessionData> sessionDataFactory;
  73. protected RfSessionFactoryImpl() {
  74. }
  75. public RfSessionFactoryImpl(SessionFactory sessionFactory) {
  76. super();
  77. this.sessionFactory = (ISessionFactory) sessionFactory;
  78. this.iss = this.sessionFactory.getContainer().getAssemblerFacility().getComponentInstance(ISessionDatasource.class);
  79. this.sessionDataFactory = (IAppSessionDataFactory<IRfSessionData>) this.iss.getDataFactory(IRfSessionData.class);
  80. }
  81. // ACC Factory Methods
  82. // ------------------------------------------------------
  83. /**
  84. * @return the serverSessionListener
  85. */
  86. public ServerRfSessionListener getServerSessionListener() {
  87. if (this.serverSessionListener != null) {
  88. return serverSessionListener;
  89. } else {
  90. return this;
  91. }
  92. }
  93. /**
  94. * @param serverSessionListener
  95. * the serverSessionListener to set
  96. */
  97. public void setServerSessionListener(ServerRfSessionListener serverSessionListener) {
  98. this.serverSessionListener = serverSessionListener;
  99. }
  100. /**
  101. * @return the stateListener
  102. */
  103. public StateChangeListener<AppSession> getStateListener() {
  104. if (this.stateListener != null) {
  105. return stateListener;
  106. } else {
  107. return this;
  108. }
  109. }
  110. /**
  111. * @param stateListener
  112. * the stateListener to set
  113. */
  114. public void setStateListener(StateChangeListener<AppSession> stateListener) {
  115. this.stateListener = stateListener;
  116. }
  117. /**
  118. * @return the clientSessionListener
  119. */
  120. public ClientRfSessionListener getClientSessionListener() {
  121. if (this.clientSessionListener != null) {
  122. return clientSessionListener;
  123. } else {
  124. return this;
  125. }
  126. }
  127. /**
  128. * @param clientSessionListener
  129. * the clientSessionListener to set
  130. */
  131. public void setClientSessionListener(ClientRfSessionListener clientSessionListener) {
  132. this.clientSessionListener = clientSessionListener;
  133. }
  134. /**
  135. * @return the clientContextListener
  136. */
  137. public IClientRfActionContext getClientContextListener() {
  138. if (this.clientContextListener != null) {
  139. return clientContextListener;
  140. } else {
  141. return this;
  142. }
  143. }
  144. /**
  145. * @param clientContextListener
  146. * the clientContextListener to set
  147. */
  148. public void setClientContextListener(IClientRfActionContext clientContextListener) {
  149. this.clientContextListener = clientContextListener;
  150. }
  151. /**
  152. * @return the serverContextListener
  153. */
  154. public IServerRfActionContext getServerContextListener() {
  155. if (this.serverContextListener != null) {
  156. return serverContextListener;
  157. } else {
  158. return this;
  159. }
  160. }
  161. /**
  162. * @param serverContextListener
  163. * the serverContextListener to set
  164. */
  165. public void setServerContextListener(IServerRfActionContext serverContextListener) {
  166. this.serverContextListener = serverContextListener;
  167. }
  168. /**
  169. * @return the messageTimeout
  170. */
  171. public long getMessageTimeout() {
  172. return messageTimeout;
  173. }
  174. /**
  175. * @param messageTimeout
  176. * the messageTimeout to set
  177. */
  178. public void setMessageTimeout(long messageTimeout) {
  179. this.messageTimeout = messageTimeout;
  180. }
  181. /**
  182. * @return the sessionFactory
  183. */
  184. public ISessionFactory getSessionFactory() {
  185. return sessionFactory;
  186. }
  187. /**
  188. * @param sessionFactory
  189. * the sessionFactory to set
  190. */
  191. public void setSessionFactory(ISessionFactory sessionFactory) {
  192. this.sessionFactory = sessionFactory;
  193. if (this.iss == null) {
  194. this.iss = this.sessionFactory.getContainer().getAssemblerFacility().getComponentInstance(ISessionDatasource.class);
  195. }
  196. }
  197. /*
  198. * (non-Javadoc)
  199. *
  200. * @see
  201. * org.jdiameter.common.api.app.acc.IAccSessionFactory#getApplicationId()
  202. */
  203. public ApplicationId getApplicationId() {
  204. return this.applicationId;
  205. }
  206. /*
  207. * (non-Javadoc)
  208. *
  209. * @see
  210. * org.jdiameter.common.api.app.acc.IAccSessionFactory#setApplicationId(
  211. * org.jdiameter.api.ApplicationId)
  212. */
  213. public void setApplicationId(ApplicationId id) {
  214. this.applicationId = id;
  215. }
  216. // App Session Factory
  217. // ------------------------------------------------------
  218. @Override
  219. public AppSession getSession(String sessionId, Class<? extends AppSession> aClass) {
  220. if (sessionId == null) {
  221. throw new IllegalArgumentException("SessionId must not be null");
  222. }
  223. if (!this.iss.exists(sessionId)) {
  224. return null;
  225. }
  226. AppSession appSession = null;
  227. try {
  228. if (aClass == ServerRfSession.class) {
  229. IServerRfSessionData sessionData = (IServerRfSessionData) this.sessionDataFactory.getAppSessionData(ServerRfSession.class, sessionId);
  230. // FIXME: determine how to get boolean flag!
  231. ServerRfSessionImpl session = new ServerRfSessionImpl(sessionData, sessionFactory, getServerSessionListener(), getServerContextListener(),
  232. getStateListener(), messageTimeout, true);
  233. session.getSessions().get(0).setRequestListener(session);
  234. appSession = session;
  235. }
  236. else if (aClass == ClientRfSession.class) {
  237. IClientRfSessionData sessionData = (IClientRfSessionData) this.sessionDataFactory.getAppSessionData(ClientRfSession.class, sessionId);
  238. ClientRfSessionImpl session = new ClientRfSessionImpl(sessionData, sessionFactory, getClientSessionListener(), getClientContextListener(), getStateListener(),
  239. this.getApplicationId());
  240. session.getSessions().get(0).setRequestListener(session);
  241. appSession = session;
  242. }
  243. else {
  244. throw new IllegalArgumentException("Wrong session class: " + aClass + ". Supported[" + ClientRfSession.class + "," + ServerRfSession.class + "]");
  245. }
  246. }
  247. catch (Exception e) {
  248. logger.error("Failure to obtain new Rf Session.", e);
  249. }
  250. return appSession;
  251. }
  252. public AppSession getNewSession(String sessionId, Class<? extends AppSession> aClass, ApplicationId applicationId, Object[] args) {
  253. try {
  254. if (aClass == ServerRfSession.class) {
  255. if (sessionId == null) {
  256. if (args != null && args.length > 0 && args[0] instanceof Request) {
  257. Request request = (Request) args[0];
  258. sessionId = request.getSessionId();
  259. }
  260. else {
  261. sessionId = this.sessionFactory.getSessionId();
  262. }
  263. }
  264. IServerRfSessionData sessionData = (IServerRfSessionData) this.sessionDataFactory.getAppSessionData(ServerRfSession.class, sessionId);
  265. sessionData.setApplicationId(applicationId);
  266. // FIXME: determine how to get boolean flag!
  267. ServerRfSessionImpl session = new ServerRfSessionImpl(sessionData, sessionFactory, getServerSessionListener(), getServerContextListener(),
  268. getStateListener(), messageTimeout, true);
  269. iss.addSession(session);
  270. session.getSessions().get(0).setRequestListener(session);
  271. return session;
  272. }
  273. else if (aClass == ClientRfSession.class) {
  274. if (sessionId == null) {
  275. if (args != null && args.length > 0 && args[0] instanceof Request) {
  276. Request request = (Request) args[0];
  277. sessionId = request.getSessionId();
  278. }
  279. else {
  280. sessionId = this.sessionFactory.getSessionId();
  281. }
  282. }
  283. IClientRfSessionData sessionData = (IClientRfSessionData) this.sessionDataFactory.getAppSessionData(ClientRfSession.class, sessionId);
  284. sessionData.setApplicationId(applicationId);
  285. ClientRfSessionImpl session = new ClientRfSessionImpl(sessionData, sessionFactory, getClientSessionListener(), getClientContextListener(), getStateListener(),
  286. this.getApplicationId());
  287. iss.addSession(session);
  288. session.getSessions().get(0).setRequestListener(session);
  289. return session;
  290. }
  291. else {
  292. throw new IllegalArgumentException("Wrong session class: " + aClass + ". Supported[" + ClientRfSession.class + "," + ServerRfSession.class + "]");
  293. }
  294. }
  295. catch (Exception e) {
  296. logger.error("Failure to obtain new Rf Session.", e);
  297. }
  298. return null;
  299. }
  300. // State Change Listener
  301. // ----------------------------------------------------
  302. @SuppressWarnings("unchecked")
  303. public void stateChanged(Enum oldState, Enum newState) {
  304. logger.info("Diameter ACC SessionFactory :: stateChanged :: oldState[{}], newState[{}]", oldState, newState);
  305. }
  306. /*
  307. * (non-Javadoc)
  308. *
  309. * @see
  310. * org.jdiameter.api.app.StateChangeListener#stateChanged(java.lang.Object,
  311. * java.lang.Enum, java.lang.Enum)
  312. */
  313. @SuppressWarnings("unchecked")
  314. public void stateChanged(AppSession source, Enum oldState, Enum newState) {
  315. logger.info("Diameter Rf SessionFactory :: stateChanged :: source[{}], oldState[{}], newState[{}]", new Object[] { source, oldState, newState });
  316. }
  317. // ///////////////////
  318. // Event listeners //
  319. // ///////////////////
  320. public void doRfAccountingRequestEvent(ServerRfSession appSession, RfAccountingRequest acr) throws InternalException, IllegalDiameterStateException, RouteException, OverloadException {
  321. logger.info("Diameter Base RfSessionFactory :: doAccRequestEvent :: appSession[" + appSession + "], Request[" + acr + "]");
  322. }
  323. public void doRfAccountingAnswerEvent(ClientRfSession appSession, RfAccountingRequest acr, RfAccountingAnswer aca) throws InternalException, IllegalDiameterStateException, RouteException,
  324. OverloadException {
  325. logger.info("doRfAnswerEvent :: appSession[" + appSession + "], Request[" + acr + "], Answer[" + aca + "]");
  326. }
  327. public void doOtherEvent(AppSession appSession, AppRequestEvent request, AppAnswerEvent answer) throws InternalException, IllegalDiameterStateException, RouteException,
  328. OverloadException {
  329. logger.info("Diameter Base RfountingSessionFactory :: doOtherEvent :: appSession[" + appSession + "], Request[" + request + "], Answer[" + answer + "]");
  330. }
  331. // Client context
  332. // -----------------------------------------------------------
  333. /*
  334. * (non-Javadoc)
  335. *
  336. * @see
  337. * org.jdiameter.common.api.app.Rf.IClientRfActionContext#disconnectUserOrDev
  338. * (org.jdiameter.api.Request)
  339. */
  340. public void disconnectUserOrDev(ClientRfSession appSession, Request sessionTermRequest) throws InternalException {
  341. logger.info("disconnectUserOrDev :: appSession[" + appSession + "], Request[" + sessionTermRequest + "]");
  342. }
  343. /*
  344. * (non-Javadoc)
  345. *
  346. * @see
  347. * org.jdiameter.common.api.app.Rf.IClientRfActionContext#failedSendRecord
  348. * (org.jdiameter.api.Request)
  349. */
  350. public boolean failedSendRecord(ClientRfSession appSession, Request rfRequest) throws InternalException {
  351. logger.info("failedSendRecord :: appSession[" + appSession + "], Request[" + rfRequest + "]");
  352. return false;
  353. }
  354. /*
  355. * (non-Javadoc)
  356. *
  357. * @see org.jdiameter.common.api.app.acc.IClientAccActionContext#
  358. * interimIntervalElapses(org.jdiameter.api.Request)
  359. */
  360. public void interimIntervalElapses(ClientRfSession appSession, Request interimRequest) throws InternalException {
  361. logger.info("interimIntervalElapses :: appSession[" + appSession + "], Request[" + interimRequest + "]");
  362. }
  363. // Server context
  364. // -----------------------------------------------------------
  365. /*
  366. * (non-Javadoc)
  367. *
  368. * @seeorg.jdiameter.common.api.app.Rf.IServerRfActionContext#
  369. * sessionTimeoutElapses(org.jdiameter.api.Rf.ServerRfSession)
  370. */
  371. public void sessionTimeoutElapses(ServerRfSession appSession) throws InternalException {
  372. logger.info("sessionTimeoutElapses :: appSession[" + appSession + "]");
  373. }
  374. /*
  375. * (non-Javadoc)
  376. *
  377. * @see
  378. * org.jdiameter.common.api.app.Rf.IServerRfActionContext#sessionTimerStarted
  379. * (org.jdiameter.api.Rf.ServerRfSession,
  380. * java.util.concurrent.ScheduledFuture)
  381. */
  382. @SuppressWarnings("unchecked")
  383. public void sessionTimerStarted(ServerRfSession appSession, ScheduledFuture timer) throws InternalException {
  384. logger.info("sessionTimerStarted :: appSession[" + appSession + "]");
  385. }
  386. /*
  387. * (non-Javadoc)
  388. *
  389. * @see
  390. * org.jdiameter.common.api.app.Rf.IServerRfActionContext#srssionTimerCanceled
  391. * (org.jdiameter.api.Rf.ServerRfSession,
  392. * java.util.concurrent.ScheduledFuture)
  393. */
  394. @SuppressWarnings("unchecked")
  395. public void sessionTimerCanceled(ServerRfSession appSession, ScheduledFuture timer) throws InternalException {
  396. logger.info("sessionTimerCanceled :: appSession[" + appSession + "]");
  397. }
  398. }