PageRenderTime 27ms CodeModel.GetById 0ms RepoModel.GetById 0ms app.codeStats 0ms

/servers/diameter/core/jdiameter/impl/src/main/java/org/jdiameter/common/impl/app/ro/RoSessionFactoryImpl.java

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