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

/servers/diameter/core/jdiameter/impl/src/main/java/org/jdiameter/common/impl/app/acc/AccSessionFactoryImpl.java

http://mobicents.googlecode.com/
Java | 466 lines | 266 code | 56 blank | 144 comment | 52 complexity | 942673ccf22e428c4d9bccc769ebfb0f 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 2006, 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.acc;
  23. import java.util.concurrent.ScheduledFuture;
  24. import org.jdiameter.api.Answer;
  25. import org.jdiameter.api.ApplicationId;
  26. import org.jdiameter.api.IllegalDiameterStateException;
  27. import org.jdiameter.api.InternalException;
  28. import org.jdiameter.api.OverloadException;
  29. import org.jdiameter.api.Request;
  30. import org.jdiameter.api.RouteException;
  31. import org.jdiameter.api.SessionFactory;
  32. import org.jdiameter.api.acc.ClientAccSession;
  33. import org.jdiameter.api.acc.ClientAccSessionListener;
  34. import org.jdiameter.api.acc.ServerAccSession;
  35. import org.jdiameter.api.acc.ServerAccSessionListener;
  36. import org.jdiameter.api.acc.events.AccountAnswer;
  37. import org.jdiameter.api.acc.events.AccountRequest;
  38. import org.jdiameter.api.app.AppAnswerEvent;
  39. import org.jdiameter.api.app.AppRequestEvent;
  40. import org.jdiameter.api.app.AppSession;
  41. import org.jdiameter.api.app.StateChangeListener;
  42. import org.jdiameter.client.api.ISessionFactory;
  43. import org.jdiameter.client.impl.app.acc.ClientAccSessionImpl;
  44. import org.jdiameter.client.impl.app.acc.IClientAccSessionData;
  45. import org.jdiameter.common.api.app.IAppSessionDataFactory;
  46. import org.jdiameter.common.api.app.acc.IAccMessageFactory;
  47. import org.jdiameter.common.api.app.acc.IAccSessionData;
  48. import org.jdiameter.common.api.app.acc.IAccSessionFactory;
  49. import org.jdiameter.common.api.app.acc.IClientAccActionContext;
  50. import org.jdiameter.common.api.app.acc.IServerAccActionContext;
  51. import org.jdiameter.common.api.data.ISessionDatasource;
  52. import org.jdiameter.server.impl.app.acc.IServerAccSessionData;
  53. import org.jdiameter.server.impl.app.acc.ServerAccSessionImpl;
  54. import org.slf4j.Logger;
  55. import org.slf4j.LoggerFactory;
  56. /**
  57. * Default Diameter Account Session Factory implementation
  58. *
  59. * @author <a href="mailto:baranowb@gmail.com"> Bartosz Baranowski </a>
  60. * @author <a href="mailto:brainslog@gmail.com"> Alexandre Mendonca </a>
  61. */
  62. public class AccSessionFactoryImpl implements IAccSessionFactory,IAccMessageFactory, ServerAccSessionListener, ClientAccSessionListener, IClientAccActionContext, IServerAccActionContext, StateChangeListener<AppSession> {
  63. protected Logger logger = LoggerFactory.getLogger(AccSessionFactoryImpl.class);
  64. protected ServerAccSessionListener serverSessionListener;
  65. protected StateChangeListener<AppSession> stateListener;
  66. protected ClientAccSessionListener clientSessionListener;
  67. protected IClientAccActionContext clientContextListener;
  68. protected IServerAccActionContext serverContextListener;
  69. protected ISessionDatasource iss;
  70. protected ISessionFactory sessionFactory = null;
  71. protected ApplicationId applicationId;
  72. protected IAccMessageFactory messageFactory = this;
  73. protected IAppSessionDataFactory<IAccSessionData> sessionDataFactory;
  74. protected AccSessionFactoryImpl() {
  75. }
  76. public AccSessionFactoryImpl(SessionFactory sessionFactory) {
  77. super();
  78. setSessionFactory((ISessionFactory) sessionFactory);
  79. }
  80. // ACC Factory Methods ------------------------------------------------------
  81. /**
  82. * @return the serverSessionListener
  83. */
  84. public ServerAccSessionListener getServerSessionListener() {
  85. if (this.serverSessionListener != null) {
  86. return serverSessionListener;
  87. }
  88. else {
  89. return this;
  90. }
  91. }
  92. /**
  93. * @param serverSessionListener
  94. * the serverSessionListener to set
  95. */
  96. public void setServerSessionListener(ServerAccSessionListener serverSessionListener) {
  97. this.serverSessionListener = serverSessionListener;
  98. }
  99. /**
  100. * @return the stateListener
  101. */
  102. public StateChangeListener<AppSession> getStateListener() {
  103. if (this.stateListener != null) {
  104. return stateListener;
  105. }
  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 ClientAccSessionListener getClientSessionListener() {
  121. if (this.clientSessionListener != null) {
  122. return clientSessionListener;
  123. }
  124. else {
  125. return this;
  126. }
  127. }
  128. /**
  129. * @param clientSessionListener
  130. * the clientSessionListener to set
  131. */
  132. public void setClientSessionListener(ClientAccSessionListener clientSessionListener) {
  133. this.clientSessionListener = clientSessionListener;
  134. }
  135. /**
  136. * @return the clientContextListener
  137. */
  138. public IClientAccActionContext getClientContextListener() {
  139. if (this.clientContextListener != null) {
  140. return clientContextListener;
  141. }
  142. else {
  143. return this;
  144. }
  145. }
  146. /**
  147. * @param clientContextListener
  148. * the clientContextListener to set
  149. */
  150. public void setClientContextListener(IClientAccActionContext clientContextListener) {
  151. this.clientContextListener = clientContextListener;
  152. }
  153. /**
  154. * @return the serverContextListener
  155. */
  156. public IServerAccActionContext getServerContextListener() {
  157. if (this.serverContextListener != null) {
  158. return serverContextListener;
  159. }
  160. else {
  161. return this;
  162. }
  163. }
  164. /**
  165. * @param serverContextListener
  166. * the serverContextListener to set
  167. */
  168. public void setServerContextListener(IServerAccActionContext serverContextListener) {
  169. this.serverContextListener = serverContextListener;
  170. }
  171. public void setMessageFactory(IAccMessageFactory messageFactory)
  172. {
  173. this.messageFactory = messageFactory;
  174. }
  175. public IAccMessageFactory getMessageFactory()
  176. {
  177. return this.messageFactory;
  178. }
  179. /**
  180. * @return the sessionFactory
  181. */
  182. public ISessionFactory getSessionFactory() {
  183. return sessionFactory;
  184. }
  185. /**
  186. * @param sessionFactory the sessionFactory to set
  187. */
  188. public void setSessionFactory(ISessionFactory sessionFactory) {
  189. this.sessionFactory = sessionFactory;
  190. if (this.iss == null) {
  191. this.iss = this.sessionFactory.getContainer().getAssemblerFacility().getComponentInstance(ISessionDatasource.class);
  192. this.sessionDataFactory = (IAppSessionDataFactory<IAccSessionData>) this.iss.getDataFactory(IAccSessionData.class);
  193. if (this.sessionDataFactory == null) {
  194. logger.debug("No factory for Accounting Application data, using default/local.");
  195. this.sessionDataFactory = new AccLocalSessionDataFactory();
  196. }
  197. }
  198. }
  199. /*
  200. * (non-Javadoc)
  201. *
  202. * @see org.jdiameter.common.api.app.acc.IAccSessionFactory#getApplicationId()
  203. */
  204. public ApplicationId getApplicationId() {
  205. return this.applicationId;
  206. }
  207. /*
  208. * (non-Javadoc)
  209. *
  210. * @see org.jdiameter.common.api.app.acc.IAccSessionFactory#setApplicationId( org.jdiameter.api.ApplicationId)
  211. */
  212. public void setApplicationId(ApplicationId id) {
  213. this.applicationId = id;
  214. }
  215. // App Session Factory ------------------------------------------------------
  216. @Override
  217. public AppSession getSession(String sessionId, Class<? extends AppSession> aClass) {
  218. if (sessionId == null) {
  219. throw new IllegalArgumentException("SessionId must not be null");
  220. }
  221. if(!this.iss.exists(sessionId)) {
  222. return null;
  223. }
  224. AppSession appSession = null;
  225. try {
  226. if (aClass == ClientAccSession.class) {
  227. IClientAccSessionData data = (IClientAccSessionData) this.sessionDataFactory.getAppSessionData(ClientAccSession.class, sessionId);
  228. ClientAccSessionImpl clientSession = new ClientAccSessionImpl(data, sessionFactory, getClientSessionListener(), getClientContextListener(), getStateListener());
  229. clientSession.getSessions().get(0).setRequestListener(clientSession);
  230. appSession = clientSession;
  231. }
  232. else if (aClass == ServerAccSession.class) {
  233. ServerAccSessionImpl serverSession = null;
  234. IServerAccSessionData data = (IServerAccSessionData) this.sessionDataFactory.getAppSessionData(ServerAccSession.class, sessionId);
  235. //here we use shorter con, since some data is already present.
  236. serverSession = new ServerAccSessionImpl(data,sessionFactory, getServerSessionListener(), getServerContextListener(), getStateListener());
  237. serverSession.getSessions().get(0).setRequestListener(serverSession);
  238. appSession = serverSession;
  239. }
  240. else {
  241. throw new IllegalArgumentException("Wrong session class: " + aClass + ". Supported[" + ClientAccSession.class + "," + ServerAccSession.class + "]");
  242. }
  243. }
  244. catch (Exception e) {
  245. logger.error("Failure to obtain new Accounting Session.", e);
  246. }
  247. return appSession;
  248. }
  249. public AppSession getNewSession(String sessionId, Class<? extends AppSession> aClass, ApplicationId applicationId, Object[] args) {
  250. try {
  251. if (aClass == ServerAccSession.class) {
  252. if (sessionId == null) {
  253. if (args != null && args.length > 0 && args[0] instanceof Request) {
  254. Request request = (Request) args[0];
  255. sessionId = request.getSessionId();
  256. }
  257. else {
  258. sessionId = this.sessionFactory.getSessionId();
  259. }
  260. }
  261. boolean stateless = true;
  262. if(args != null && args.length > 0) {
  263. for(Object o:args) {
  264. if(o instanceof Boolean) {
  265. stateless = (Boolean)o;
  266. }
  267. }
  268. }
  269. IServerAccSessionData data = (IServerAccSessionData) this.sessionDataFactory.getAppSessionData(ServerAccSession.class, sessionId);
  270. data.setApplicationId(applicationId);
  271. ServerAccSessionImpl session = new ServerAccSessionImpl(data, sessionFactory, getServerSessionListener(), getServerContextListener(), getStateListener(), stateless);
  272. iss.addSession(session);
  273. session.getSessions().get(0).setRequestListener(session);
  274. return session;
  275. }
  276. else if (aClass == ClientAccSession.class) {
  277. if (sessionId == null) {
  278. if (args != null && args.length > 0 && args[0] instanceof Request) {
  279. Request request = (Request) args[0];
  280. sessionId = request.getSessionId();
  281. }
  282. else {
  283. sessionId = this.sessionFactory.getSessionId();
  284. }
  285. }
  286. IClientAccSessionData data = (IClientAccSessionData) this.sessionDataFactory.getAppSessionData(ClientAccSession.class, sessionId);
  287. data.setApplicationId(applicationId);
  288. ClientAccSessionImpl session = new ClientAccSessionImpl(data, sessionFactory, getClientSessionListener(), getClientContextListener(), getStateListener());
  289. iss.addSession(session);
  290. session.getSessions().get(0).setRequestListener(session);
  291. return session;
  292. }
  293. }
  294. catch (Exception e) {
  295. logger.error("Failure to obtain new Accounting Session.", e);
  296. }
  297. return null;
  298. }
  299. // State Change Listener ----------------------------------------------------
  300. public void stateChanged(Enum oldState, Enum newState) {
  301. logger.info("Diameter ACC SessionFactory :: stateChanged :: oldState[{}], newState[{}]", oldState, newState);
  302. }
  303. /*
  304. * (non-Javadoc)
  305. *
  306. * @see org.jdiameter.api.app.StateChangeListener#stateChanged(java.lang.Object, java.lang.Enum, java.lang.Enum)
  307. */
  308. public void stateChanged(AppSession source, Enum oldState, Enum newState) {
  309. logger.info("Diameter ACC SessionFactory :: stateChanged :: source[{}], oldState[{}], newState[{}]", new Object[] { source, oldState, newState });
  310. }
  311. // ///////////////////
  312. // Event listeners //
  313. // ///////////////////
  314. public void doAccRequestEvent(ServerAccSession appSession, AccountRequest acr) throws InternalException, IllegalDiameterStateException, RouteException, OverloadException {
  315. logger.info("Diameter Base AccountingSessionFactory :: doAccRequestEvent :: appSession[" + appSession + "], Request[" + acr + "]");
  316. }
  317. public void doAccAnswerEvent(ClientAccSession appSession, AccountRequest acr, AccountAnswer aca) throws InternalException, IllegalDiameterStateException, RouteException, OverloadException {
  318. logger.info("doAccAnswerEvent :: appSession[" + appSession + "], Request[" + acr + "], Answer[" + aca + "]");
  319. }
  320. public void doOtherEvent(AppSession appSession, AppRequestEvent request, AppAnswerEvent answer) throws InternalException, IllegalDiameterStateException, RouteException, OverloadException {
  321. logger.info("Diameter Base AccountingSessionFactory :: doOtherEvent :: appSession[" + appSession + "], Request[" + request + "], Answer[" + answer + "]");
  322. }
  323. // Client context -----------------------------------------------------------
  324. /*
  325. * (non-Javadoc)
  326. *
  327. * @see org.jdiameter.common.api.app.acc.IClientAccActionContext#disconnectUserOrDev (org.jdiameter.api.Request)
  328. */
  329. public void disconnectUserOrDev(ClientAccSession appSession, Request sessionTermRequest) throws InternalException {
  330. logger.info("disconnectUserOrDev :: appSession[" + appSession + "], Request[" + sessionTermRequest + "]");
  331. }
  332. /*
  333. * (non-Javadoc)
  334. *
  335. * @see org.jdiameter.common.api.app.acc.IClientAccActionContext#failedSendRecord (org.jdiameter.api.Request)
  336. */
  337. public boolean failedSendRecord(ClientAccSession appSession, Request accRequest) throws InternalException {
  338. logger.info("failedSendRecord :: appSession[" + appSession + "], Request[" + accRequest + "]");
  339. return false;
  340. }
  341. /*
  342. * (non-Javadoc)
  343. *
  344. * @see org.jdiameter.common.api.app.acc.IClientAccActionContext# interimIntervalElapses(org.jdiameter.api.Request)
  345. */
  346. public void interimIntervalElapses(ClientAccSession appSession, Request interimRequest) throws InternalException {
  347. logger.info("interimIntervalElapses :: appSession[" + appSession + "], Request[" + interimRequest + "]");
  348. }
  349. // Server context -----------------------------------------------------------
  350. /*
  351. * (non-Javadoc)
  352. *
  353. * @seeorg.jdiameter.common.api.app.acc.IServerAccActionContext#sessionTimeoutElapses(org.jdiameter.api.acc.ServerAccSession)
  354. */
  355. public void sessionTimeoutElapses(ServerAccSession appSession) throws InternalException {
  356. logger.info("sessionTimeoutElapses :: appSession[" + appSession + "]");
  357. }
  358. /*
  359. * (non-Javadoc)
  360. *
  361. * @see org.jdiameter.common.api.app.acc.IServerAccActionContext#sessionTimerStarted(org.jdiameter.api.acc.ServerAccSession, java.util.concurrent.ScheduledFuture)
  362. */
  363. @SuppressWarnings("unchecked")
  364. public void sessionTimerStarted(ServerAccSession appSession, ScheduledFuture timer) throws InternalException {
  365. logger.info("sessionTimerStarted :: appSession[" + appSession + "]");
  366. }
  367. /*
  368. * (non-Javadoc)
  369. *
  370. * @see org.jdiameter.common.api.app.acc.IServerAccActionContext#srssionTimerCanceled(org.jdiameter.api.acc.ServerAccSession, java.util.concurrent.ScheduledFuture)
  371. */
  372. @SuppressWarnings("unchecked")
  373. public void sessionTimerCanceled(ServerAccSession appSession, ScheduledFuture timer) throws InternalException {
  374. logger.info("sessionTimerCanceled :: appSession[" + appSession + "]");
  375. }
  376. /*
  377. * (non-Javadoc)
  378. *
  379. * @see
  380. * org.jdiameter.common.api.app.acc.IAccMessageFactory#getAccMessageCommandCode
  381. * ()
  382. */
  383. @Override
  384. public int getAccMessageCommandCode() {
  385. // TODO Auto-generated method stub
  386. return AccountRequest.code;
  387. }
  388. /*
  389. * (non-Javadoc)
  390. *
  391. * @see
  392. * org.jdiameter.common.api.app.acc.IAccMessageFactory#createAccRequest(
  393. * org.jdiameter.api.Request)
  394. */
  395. @Override
  396. public AccountRequest createAccRequest(Request request) {
  397. return new AccountRequestImpl(request);
  398. }
  399. /*
  400. * (non-Javadoc)
  401. *
  402. * @see
  403. * org.jdiameter.common.api.app.acc.IAccMessageFactory#createAccAnswer(org
  404. * .jdiameter.api.Answer)
  405. */
  406. @Override
  407. public AccountAnswer createAccAnswer(Answer answer) {
  408. return new AccountAnswerImpl(answer);
  409. }
  410. }