PageRenderTime 26ms CodeModel.GetById 16ms RepoModel.GetById 0ms app.codeStats 0ms

/servers/diameter/core/jdiameter/impl/src/main/java/org/jdiameter/common/impl/app/rx/RxSessionFactoryImpl.java

http://mobicents.googlecode.com/
Java | 465 lines | 266 code | 81 blank | 118 comment | 37 complexity | f1109725e7d541c4b3c36c282921340a 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 2011, 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.rx;
  23. import java.util.concurrent.ScheduledFuture;
  24. import org.jdiameter.api.Answer;
  25. import org.jdiameter.api.ApplicationId;
  26. import org.jdiameter.api.InternalException;
  27. import org.jdiameter.api.Message;
  28. import org.jdiameter.api.Request;
  29. import org.jdiameter.api.SessionFactory;
  30. import org.jdiameter.api.app.AppAnswerEvent;
  31. import org.jdiameter.api.app.AppRequestEvent;
  32. import org.jdiameter.api.app.AppSession;
  33. import org.jdiameter.api.app.StateChangeListener;
  34. //import org.jdiameter.api.auth.events.ReAuthAnswer;
  35. //import org.jdiameter.api.auth.events.ReAuthRequest;
  36. import org.jdiameter.api.rx.events.RxReAuthAnswer;
  37. import org.jdiameter.api.rx.events.RxReAuthRequest;
  38. //import org.jdiameter.api.auth.events.AbortSessionAnswer;
  39. //import org.jdiameter.api.auth.events.AbortSessionRequest;
  40. import org.jdiameter.api.rx.events.RxAbortSessionAnswer;
  41. import org.jdiameter.api.rx.events.RxAbortSessionRequest;
  42. //import org.jdiameter.api.auth.events.SessionTermAnswer;
  43. //import org.jdiameter.api.auth.events.SessionTermRequest;
  44. import org.jdiameter.api.rx.events.RxSessionTermAnswer;
  45. import org.jdiameter.api.rx.events.RxSessionTermRequest;
  46. import org.jdiameter.api.rx.ClientRxSession;
  47. import org.jdiameter.api.rx.ClientRxSessionListener;
  48. import org.jdiameter.api.rx.ServerRxSession;
  49. import org.jdiameter.api.rx.ServerRxSessionListener;
  50. import org.jdiameter.api.rx.events.RxAAAnswer;
  51. import org.jdiameter.api.rx.events.RxAARequest;
  52. import org.jdiameter.client.api.ISessionFactory;
  53. import org.jdiameter.client.impl.app.rx.ClientRxSessionImpl;
  54. import org.jdiameter.client.impl.app.rx.IClientRxSessionData;
  55. import org.jdiameter.common.api.app.IAppSessionDataFactory;
  56. import org.jdiameter.common.api.app.rx.IRxSessionData;
  57. import org.jdiameter.common.api.app.rx.IClientRxSessionContext;
  58. import org.jdiameter.common.api.app.rx.IRxMessageFactory;
  59. import org.jdiameter.common.api.app.rx.IRxSessionFactory;
  60. import org.jdiameter.common.api.app.rx.IServerRxSessionContext;
  61. import org.jdiameter.common.api.data.ISessionDatasource;
  62. //import org.jdiameter.common.impl.app.auth.ReAuthAnswerImpl;
  63. //import org.jdiameter.common.impl.app.auth.ReAuthRequestImpl;
  64. //import org.jdiameter.common.impl.app.auth.AbortSessionAnswerImpl;
  65. //import org.jdiameter.common.impl.app.auth.AbortSessionRequestImpl;
  66. //import org.jdiameter.common.impl.app.auth.SessionTermAnswerImpl;
  67. //import org.jdiameter.common.impl.app.auth.SessionTermRequestImpl;
  68. import org.jdiameter.server.impl.app.rx.IServerRxSessionData;
  69. import org.jdiameter.server.impl.app.rx.ServerRxSessionImpl;
  70. import org.slf4j.Logger;
  71. import org.slf4j.LoggerFactory;
  72. /**
  73. * Default Diameter Rx Session Factory implementation.
  74. *
  75. * @author <a href="mailto:richard.good@smilecoms.com"> Richard Good </a>
  76. * @author <a href="mailto:brainslog@gmail.com"> Alexandre Mendonca </a>
  77. * @author <a href="mailto:baranowb@gmail.com"> Bartosz Baranowski </a>
  78. */
  79. public class RxSessionFactoryImpl implements IRxSessionFactory, ClientRxSessionListener, ServerRxSessionListener, StateChangeListener<AppSession>,
  80. IRxMessageFactory, IServerRxSessionContext, IClientRxSessionContext {
  81. // Message timeout value (in milliseconds)
  82. protected int defaultDirectDebitingFailureHandling = 0;
  83. protected int defaultAAFailureHandling = 0;
  84. // its seconds
  85. protected long defaultValidityTime = 60;
  86. protected long defaultTxTimerValue = 30;
  87. // local not replicated listeners:
  88. protected ClientRxSessionListener clientSessionListener;
  89. protected ServerRxSessionListener serverSessionListener;
  90. protected StateChangeListener<AppSession> stateListener;
  91. protected IServerRxSessionContext serverContextListener;
  92. protected IClientRxSessionContext clientContextListener;
  93. protected IRxMessageFactory messageFactory;
  94. protected Logger logger = LoggerFactory.getLogger(RxSessionFactoryImpl.class);
  95. protected ISessionDatasource iss;
  96. protected ISessionFactory sessionFactory = null;
  97. protected IAppSessionDataFactory<IRxSessionData> sessionDataFactory;
  98. public RxSessionFactoryImpl(SessionFactory sessionFactory) {
  99. super();
  100. this.sessionFactory = (ISessionFactory) sessionFactory;
  101. this.iss = this.sessionFactory.getContainer().getAssemblerFacility().getComponentInstance(ISessionDatasource.class);
  102. this.sessionDataFactory = (IAppSessionDataFactory<IRxSessionData>) this.iss.getDataFactory(IRxSessionData.class);
  103. if(this.sessionDataFactory == null) {
  104. logger.debug("Initialized Rx SessionDataFactory is null");
  105. }
  106. }
  107. public RxSessionFactoryImpl(SessionFactory sessionFactory, int defaultDirectDebitingFailureHandling, int defaultAAFailureHandling,
  108. long defaultValidityTime, long defaultTxTimerValue) {
  109. this(sessionFactory);
  110. this.defaultDirectDebitingFailureHandling = defaultDirectDebitingFailureHandling;
  111. this.defaultAAFailureHandling = defaultAAFailureHandling;
  112. this.defaultValidityTime = defaultValidityTime;
  113. this.defaultTxTimerValue = defaultTxTimerValue;
  114. }
  115. /**
  116. * @return the clientSessionListener
  117. */
  118. public ClientRxSessionListener getClientSessionListener() {
  119. return clientSessionListener != null ? clientSessionListener : this;
  120. }
  121. /**
  122. * @param clientSessionListener
  123. * the clientSessionListener to set
  124. */
  125. public void setClientSessionListener(final ClientRxSessionListener clientSessionListener) {
  126. this.clientSessionListener = clientSessionListener;
  127. }
  128. /**
  129. * @return the serverSessionListener
  130. */
  131. public ServerRxSessionListener getServerSessionListener() {
  132. return serverSessionListener != null ? serverSessionListener : this;
  133. }
  134. /**
  135. * @param serverSessionListener
  136. * the serverSessionListener to set
  137. */
  138. public void setServerSessionListener(ServerRxSessionListener serverSessionListener) {
  139. this.serverSessionListener = serverSessionListener;
  140. }
  141. /**
  142. * @return the serverContextListener
  143. */
  144. public IServerRxSessionContext getServerContextListener() {
  145. return serverContextListener != null ? serverContextListener : this;
  146. }
  147. /**
  148. * @param serverContextListener
  149. * the serverContextListener to set
  150. */
  151. public void setServerContextListener(IServerRxSessionContext serverContextListener) {
  152. this.serverContextListener = serverContextListener;
  153. }
  154. /**
  155. * @return the clientContextListener
  156. */
  157. public IClientRxSessionContext getClientContextListener() {
  158. return clientContextListener != null ? clientContextListener : this;
  159. }
  160. /**
  161. * @return the messageFactory
  162. */
  163. public IRxMessageFactory getMessageFactory() {
  164. return messageFactory != null ? messageFactory : this;
  165. }
  166. /**
  167. * @param messageFactory
  168. * the messageFactory to set
  169. */
  170. public void setMessageFactory(final IRxMessageFactory messageFactory) {
  171. this.messageFactory = messageFactory;
  172. }
  173. /**
  174. * @param clientContextListener
  175. * the clientContextListener to set
  176. */
  177. public void setClientContextListener(IClientRxSessionContext clientContextListener) {
  178. this.clientContextListener = clientContextListener;
  179. }
  180. /**
  181. * @return the sessionFactory
  182. */
  183. public SessionFactory getSessionFactory() {
  184. return sessionFactory;
  185. }
  186. /**
  187. * @param sessionFactory
  188. * the sessionFactory to set
  189. */
  190. public void setSessionFactory(SessionFactory sessionFactory) {
  191. this.sessionFactory = (ISessionFactory) sessionFactory;
  192. }
  193. /**
  194. * @return the stateListener
  195. */
  196. public StateChangeListener<AppSession> getStateListener() {
  197. return stateListener != null ? stateListener : this;
  198. }
  199. /**
  200. * @param stateListener
  201. * the stateListener to set
  202. */
  203. public void setStateListener(StateChangeListener<AppSession> stateListener) {
  204. this.stateListener = stateListener;
  205. }
  206. @Override
  207. public AppSession getSession(String sessionId, Class<? extends AppSession> aClass) {
  208. if (sessionId == null) {
  209. throw new IllegalArgumentException("SessionId must not be null");
  210. }
  211. if(!this.iss.exists(sessionId)) {
  212. return null;
  213. }
  214. AppSession appSession = null;
  215. try {
  216. if (aClass == ClientRxSession.class) {
  217. IClientRxSessionData sessionData = (IClientRxSessionData) this.sessionDataFactory.getAppSessionData(ClientRxSession.class, sessionId);
  218. ClientRxSessionImpl clientSession = new ClientRxSessionImpl(sessionData, this.getMessageFactory(), sessionFactory, this.getClientSessionListener(), this.getClientContextListener(), this.getStateListener());
  219. clientSession.getSessions().get(0).setRequestListener(clientSession);
  220. appSession = clientSession;
  221. }
  222. else if (aClass == ServerRxSession.class) {
  223. IServerRxSessionData sessionData = (IServerRxSessionData) this.sessionDataFactory.getAppSessionData(ServerRxSession.class, sessionId);
  224. ServerRxSessionImpl serverSession = new ServerRxSessionImpl(sessionData, this.getMessageFactory(), sessionFactory, this.getServerSessionListener(), this.getServerContextListener(), this.getStateListener());
  225. serverSession.getSessions().get(0).setRequestListener(serverSession);
  226. appSession = serverSession;
  227. }
  228. else {
  229. throw new IllegalArgumentException("Wrong session class: " + aClass + ". Supported[" + ClientRxSession.class + "," + ServerRxSession.class + "]");
  230. }
  231. }
  232. catch (Exception e) {
  233. logger.error("Failure to obtain new Rx Session.", e);
  234. }
  235. return appSession;
  236. }
  237. public AppSession getNewSession(String sessionId, Class<? extends AppSession> aClass, ApplicationId applicationId, Object[] args) {
  238. AppSession appSession = null;
  239. try {
  240. // FIXME:
  241. if (aClass == ClientRxSession.class) {
  242. if (sessionId == null) {
  243. if (args != null && args.length > 0 && args[0] instanceof Request) {
  244. Request request = (Request) args[0];
  245. sessionId = request.getSessionId();
  246. }
  247. else {
  248. sessionId = this.sessionFactory.getSessionId();
  249. }
  250. }
  251. IClientRxSessionData sessionData = (IClientRxSessionData) this.sessionDataFactory.getAppSessionData(ClientRxSession.class, sessionId);
  252. ClientRxSessionImpl clientSession = new ClientRxSessionImpl(sessionData, this.getMessageFactory(), sessionFactory, this.getClientSessionListener(), this.getClientContextListener(), this.getStateListener());
  253. // this goes first!
  254. iss.addSession(clientSession);
  255. clientSession.getSessions().get(0).setRequestListener(clientSession);
  256. appSession = clientSession;
  257. }
  258. else if (aClass == ServerRxSession.class) {
  259. if (sessionId == null) {
  260. if (args != null && args.length > 0 && args[0] instanceof Request) {
  261. Request request = (Request) args[0];
  262. sessionId = request.getSessionId();
  263. }
  264. else {
  265. sessionId = this.sessionFactory.getSessionId();
  266. }
  267. }
  268. IServerRxSessionData sessionData = (IServerRxSessionData) this.sessionDataFactory.getAppSessionData(ServerRxSession.class, sessionId);
  269. ServerRxSessionImpl serverSession = new ServerRxSessionImpl(sessionData, this.getMessageFactory(), sessionFactory, this.getServerSessionListener(), this.getServerContextListener(), this.getStateListener());
  270. iss.addSession(serverSession);
  271. serverSession.getSessions().get(0).setRequestListener(serverSession);
  272. appSession = serverSession;
  273. }
  274. else {
  275. throw new IllegalArgumentException("Wrong session class: " + aClass + ". Supported[" + ClientRxSession.class + "," + ServerRxSession.class + "]");
  276. }
  277. }
  278. catch (Exception e) {
  279. logger.error("Failure to obtain new Rx Session.", e);
  280. }
  281. return appSession;
  282. }
  283. // Default implementation of methods so there are no exception!
  284. // Message Handlers --------------------------------------------------------
  285. public void doAARequest(ServerRxSession session, RxAARequest request) throws InternalException {
  286. }
  287. public void doAAAnswer(ClientRxSession session, RxAARequest request, RxAAAnswer answer) throws InternalException {
  288. }
  289. public void doReAuthRequest(ClientRxSession session, RxReAuthRequest request) throws InternalException {
  290. }
  291. public void doReAuthAnswer(ServerRxSession session, RxReAuthRequest request, RxReAuthAnswer answer) throws InternalException {
  292. }
  293. public void doAbortSessionRequest(ClientRxSession session, RxAbortSessionRequest request) throws InternalException {
  294. }
  295. public void doAbortSessionAnswer(ServerRxSession session, RxAbortSessionRequest request, RxAbortSessionAnswer answer) throws InternalException {
  296. }
  297. public void doSessionTermRequest(ServerRxSession session, RxSessionTermRequest request) throws InternalException {
  298. }
  299. public void doSessionTermAnswer(ClientRxSession session, RxSessionTermRequest request, RxSessionTermAnswer answer) throws InternalException {
  300. }
  301. public void doOtherEvent(AppSession session, AppRequestEvent request, AppAnswerEvent answer) throws InternalException {
  302. }
  303. // Message Factory Methods -------------------------------------------------
  304. public RxAAAnswer createAAAnswer(Answer answer) {
  305. return new RxAAAnswerImpl(answer);
  306. }
  307. public RxAARequest createAARequest(Request req) {
  308. return new RxAARequestImpl(req);
  309. }
  310. public RxReAuthAnswer createReAuthAnswer(Answer answer) {
  311. return new RxReAuthAnswerImpl(answer);
  312. }
  313. public RxReAuthRequest createReAuthRequest(Request req) {
  314. return new RxReAuthRequestImpl(req);
  315. }
  316. public RxSessionTermAnswer createSessionTermAnswer(Answer answer) {
  317. return new RxSessionTermAnswerImpl(answer);
  318. }
  319. public RxSessionTermRequest createSessionTermRequest(Request req) {
  320. return new RxSessionTermRequestImpl(req);
  321. }
  322. public RxAbortSessionAnswer createAbortSessionAnswer(Answer answer) {
  323. return new RxAbortSessionAnswerImpl(answer);
  324. }
  325. public RxAbortSessionRequest createAbortSessionRequest(Request req) {
  326. return new RxAbortSessionRequestImpl(req);
  327. }
  328. // Context Methods ----------------------------------------------------------
  329. public void stateChanged(Enum oldState, Enum newState) {
  330. logger.info("Diameter Rx SessionFactory :: stateChanged :: oldState[{}], newState[{}]", oldState, newState);
  331. }
  332. /*
  333. * (non-Javadoc)
  334. *
  335. * @see org.jdiameter.api.app.StateChangeListener#stateChanged(java.lang.Object, java.lang.Enum, java.lang.Enum)
  336. */
  337. public void stateChanged(AppSession source, Enum oldState, Enum newState) {
  338. logger.info("Diameter Rx SessionFactory :: stateChanged :: source[{}], oldState[{}], newState[{}]", new Object[]{source, oldState, newState});
  339. }
  340. // FIXME: add ctx methods proxy calls!
  341. public void sessionSupervisionTimerExpired(ServerRxSession session) {
  342. // this.resourceAdaptor.sessionDestroyed(session.getSessions().get(0).getSessionId(), session);
  343. session.release();
  344. }
  345. public void sessionSupervisionTimerReStarted(ServerRxSession session, ScheduledFuture future) {
  346. // TODO Complete this method.
  347. }
  348. public void sessionSupervisionTimerStarted(ServerRxSession session, ScheduledFuture future) {
  349. // TODO Complete this method.
  350. }
  351. public void sessionSupervisionTimerStopped(ServerRxSession session, ScheduledFuture future) {
  352. // TODO Complete this method.
  353. }
  354. public void timeoutExpired(Request request) {
  355. // FIXME What should we do when there's a timeout?
  356. }
  357. public void denyAccessOnDeliverFailure(ClientRxSession clientRxSessionImpl, Message request) {
  358. // TODO Complete this method.
  359. }
  360. public void denyAccessOnFailureMessage(ClientRxSession clientRxSessionImpl) {
  361. // TODO Complete this method.
  362. }
  363. public void denyAccessOnTxExpire(ClientRxSession clientRxSessionImpl) {
  364. // this.resourceAdaptor.sessionDestroyed(clientRxSessionImpl.getSessions().get(0).getSessionId(),
  365. // clientRxSessionImpl);
  366. clientRxSessionImpl.release();
  367. }
  368. public void grantAccessOnDeliverFailure(ClientRxSession clientRxSessionImpl, Message request) {
  369. // TODO Auto-generated method stub
  370. }
  371. public void grantAccessOnFailureMessage(ClientRxSession clientRxSessionImpl) {
  372. // TODO Auto-generated method stub
  373. }
  374. public void grantAccessOnTxExpire(ClientRxSession clientRxSessionImpl) {
  375. // TODO Auto-generated method stub
  376. }
  377. public void indicateServiceError(ClientRxSession clientRxSessionImpl) {
  378. // TODO Auto-generated method stub
  379. }
  380. public long[] getApplicationIds() {
  381. // FIXME: What should we do here?
  382. return new long[]{16777236};
  383. }
  384. }