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

/servers/diameter/core/jdiameter/impl/src/main/java/org/jdiameter/common/impl/app/gx/GxSessionFactoryImpl.java

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