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

/servers/diameter/core/jdiameter/impl/src/main/java/org/jdiameter/common/impl/app/cca/CCASessionFactoryImpl.java

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