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

/servers/diameter/core/jdiameter/impl/src/main/java/org/jdiameter/common/impl/app/sh/ShSessionFactoryImpl.java

http://mobicents.googlecode.com/
Java | 438 lines | 243 code | 47 blank | 148 comment | 40 complexity | bf38b2cf84b472e768cca43f3bf68bc8 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.sh;
  23. import org.jdiameter.api.Answer;
  24. import org.jdiameter.api.ApplicationId;
  25. import org.jdiameter.api.IllegalDiameterStateException;
  26. import org.jdiameter.api.InternalException;
  27. import org.jdiameter.api.NetworkReqListener;
  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.app.AppAnswerEvent;
  33. import org.jdiameter.api.app.AppRequestEvent;
  34. import org.jdiameter.api.app.AppSession;
  35. import org.jdiameter.api.app.StateChangeListener;
  36. import org.jdiameter.api.sh.ClientShSession;
  37. import org.jdiameter.api.sh.ClientShSessionListener;
  38. import org.jdiameter.api.sh.ServerShSession;
  39. import org.jdiameter.api.sh.ServerShSessionListener;
  40. import org.jdiameter.api.sh.events.ProfileUpdateAnswer;
  41. import org.jdiameter.api.sh.events.ProfileUpdateRequest;
  42. import org.jdiameter.api.sh.events.PushNotificationAnswer;
  43. import org.jdiameter.api.sh.events.PushNotificationRequest;
  44. import org.jdiameter.api.sh.events.SubscribeNotificationsAnswer;
  45. import org.jdiameter.api.sh.events.SubscribeNotificationsRequest;
  46. import org.jdiameter.api.sh.events.UserDataAnswer;
  47. import org.jdiameter.api.sh.events.UserDataRequest;
  48. import org.jdiameter.client.api.ISessionFactory;
  49. import org.jdiameter.client.impl.app.sh.IShClientSessionData;
  50. import org.jdiameter.client.impl.app.sh.ShClientSessionImpl;
  51. import org.jdiameter.common.api.app.IAppSessionDataFactory;
  52. import org.jdiameter.common.api.app.sh.IShMessageFactory;
  53. import org.jdiameter.common.api.app.sh.IShSessionData;
  54. import org.jdiameter.common.api.app.sh.IShSessionFactory;
  55. import org.jdiameter.common.api.data.ISessionDatasource;
  56. import org.jdiameter.server.impl.app.sh.IShServerSessionData;
  57. import org.jdiameter.server.impl.app.sh.ShServerSessionImpl;
  58. import org.slf4j.Logger;
  59. import org.slf4j.LoggerFactory;
  60. /**
  61. *
  62. * @author <a href="mailto:brainslog@gmail.com"> Alexandre Mendonca </a>
  63. * @author <a href="mailto:baranowb@gmail.com"> Bartosz Baranowski </a>
  64. */
  65. public class ShSessionFactoryImpl implements IShSessionFactory, StateChangeListener<AppSession>, ClientShSessionListener, ServerShSessionListener, IShMessageFactory {
  66. protected Logger logger = LoggerFactory.getLogger(ShSessionFactoryImpl.class);
  67. // Listeners provided by developer ----------------------------------------
  68. protected ClientShSessionListener clientShSessionListener;
  69. protected ServerShSessionListener serverShSessionListener;
  70. protected IShMessageFactory messageFactory;
  71. //not used.
  72. protected StateChangeListener<AppSession> stateChangeListener;
  73. // Our magic --------------------------------------------------------------
  74. protected ISessionFactory sessionFactory;
  75. protected ISessionDatasource sessionDataSource;
  76. protected IAppSessionDataFactory<IShSessionData> sessionDataFactory;
  77. protected long messageTimeout = 10000; // 10s default timeout
  78. protected static final long applicationId = 16777217;
  79. public ShSessionFactoryImpl(SessionFactory sessionFactory) {
  80. super();
  81. this.sessionFactory = (ISessionFactory) sessionFactory;
  82. this.sessionDataSource = this.sessionFactory.getContainer().getAssemblerFacility().getComponentInstance(ISessionDatasource.class);
  83. this.sessionDataFactory = (IAppSessionDataFactory<IShSessionData>) this.sessionDataSource.getDataFactory(IShSessionData.class);
  84. if(this.sessionDataFactory == null) {
  85. logger.debug("No factory for Sh Application data, using default/local.");
  86. this.sessionDataFactory = new ShLocalSessionDataFactory();
  87. }
  88. }
  89. /**
  90. * @return the clientShSessionListener
  91. */
  92. public ClientShSessionListener getClientShSessionListener() {
  93. if (this.clientShSessionListener == null) {
  94. return this;
  95. }
  96. else {
  97. return clientShSessionListener;
  98. }
  99. }
  100. /**
  101. * @param clientShSessionListener
  102. * the clientShSessionListener to set
  103. */
  104. public void setClientShSessionListener(ClientShSessionListener clientShSessionListener) {
  105. this.clientShSessionListener = clientShSessionListener;
  106. }
  107. /**
  108. * @return the serverShSessionListener
  109. */
  110. public ServerShSessionListener getServerShSessionListener() {
  111. if (this.serverShSessionListener == null) {
  112. return this;
  113. }
  114. else {
  115. return serverShSessionListener;
  116. }
  117. }
  118. /**
  119. * @param serverShSessionListener
  120. * the serverShSessionListener to set
  121. */
  122. public void setServerShSessionListener(ServerShSessionListener serverShSessionListener) {
  123. this.serverShSessionListener = serverShSessionListener;
  124. }
  125. /**
  126. * @return the messageFactory
  127. */
  128. public IShMessageFactory getMessageFactory() {
  129. if (this.messageFactory == null) {
  130. return this;
  131. }
  132. else {
  133. return messageFactory;
  134. }
  135. }
  136. /**
  137. * @param messageFactory
  138. * the messageFactory to set
  139. */
  140. public void setMessageFactory(IShMessageFactory messageFactory) {
  141. this.messageFactory = messageFactory;
  142. }
  143. /**
  144. * @return the stateChangeListener
  145. */
  146. public StateChangeListener<AppSession> getStateChangeListener() {
  147. return stateChangeListener;
  148. }
  149. /**
  150. * @param stateChangeListener
  151. * the stateChangeListener to set
  152. */
  153. public void setStateChangeListener(StateChangeListener<AppSession> stateChangeListener) {
  154. this.stateChangeListener = stateChangeListener;
  155. }
  156. // IAppSession ------------------------------------------------------------
  157. /*
  158. * (non-Javadoc)
  159. *
  160. * @see org.jdiameter.common.api.app.IAppSessionFactory#getNewSession(java.lang.String, java.lang.Class,
  161. * org.jdiameter.api.ApplicationId, java.lang.Object[])
  162. */
  163. public AppSession getNewSession(String sessionId, Class<? extends AppSession> aClass, ApplicationId applicationId, Object[] args) {
  164. try {
  165. // FIXME: add proper handling for SessionId
  166. if (aClass == ClientShSession.class) {
  167. ShClientSessionImpl clientSession = null;
  168. if (sessionId == null) {
  169. if (args != null && args.length > 0 && args[0] instanceof Request) {
  170. Request request = (Request) args[0];
  171. sessionId = request.getSessionId();
  172. }
  173. else {
  174. sessionId = this.sessionFactory.getSessionId();
  175. }
  176. }
  177. IShClientSessionData sessionData = (IShClientSessionData) this.sessionDataFactory.getAppSessionData(ClientShSession.class, sessionId);
  178. sessionData.setApplicationId(applicationId);
  179. clientSession = new ShClientSessionImpl(sessionData, this.getMessageFactory(), sessionFactory, this.getClientShSessionListener());
  180. sessionDataSource.addSession(clientSession);
  181. clientSession.getSessions().get(0).setRequestListener(clientSession);
  182. return clientSession;
  183. }
  184. else if (aClass == ServerShSession.class) {
  185. ShServerSessionImpl serverSession = null;
  186. if (sessionId == null) {
  187. if (args != null && args.length > 0 && args[0] instanceof Request) {
  188. Request request = (Request) args[0];
  189. sessionId = request.getSessionId();
  190. }
  191. else {
  192. sessionId = this.sessionFactory.getSessionId();
  193. }
  194. }
  195. IShServerSessionData sessionData = (IShServerSessionData) this.sessionDataFactory.getAppSessionData(ServerShSession.class, sessionId);
  196. sessionData.setApplicationId(applicationId);
  197. serverSession = new ShServerSessionImpl(sessionData, this.getMessageFactory(), sessionFactory, getServerShSessionListener());
  198. sessionDataSource.addSession(serverSession);
  199. serverSession.getSessions().get(0).setRequestListener(serverSession);
  200. return serverSession;
  201. }
  202. else {
  203. throw new IllegalArgumentException("Wrong session class: [" + aClass + "]. Supported[" + ClientShSession.class + "]");
  204. }
  205. }
  206. catch (IllegalArgumentException iae) {
  207. throw iae;
  208. }
  209. catch (Exception e) {
  210. logger.error("Failure to obtain new Sh Session.", e);
  211. }
  212. return null;
  213. }
  214. @Override
  215. public AppSession getSession(String sessionId, Class<? extends AppSession> aClass) {
  216. if (sessionId == null) {
  217. throw new IllegalArgumentException("Session-Id must not be null");
  218. }
  219. if(!this.sessionDataSource.exists(sessionId)) {
  220. return null;
  221. }
  222. AppSession appSession = null;
  223. try {
  224. if(aClass == ServerShSession.class) {
  225. IShServerSessionData sessionData = (IShServerSessionData) this.sessionDataFactory.getAppSessionData(ServerShSession.class, sessionId);
  226. appSession = new ShServerSessionImpl(sessionData, this.getMessageFactory(), sessionFactory, getServerShSessionListener());
  227. appSession.getSessions().get(0).setRequestListener((NetworkReqListener) appSession);
  228. }
  229. else if (aClass == ClientShSession.class) {
  230. IShClientSessionData sessionData = (IShClientSessionData) this.sessionDataFactory.getAppSessionData(ClientShSession.class, sessionId);
  231. appSession = new ShClientSessionImpl(sessionData, this.getMessageFactory(), sessionFactory, this.getClientShSessionListener());
  232. appSession.getSessions().get(0).setRequestListener((NetworkReqListener) appSession);
  233. }
  234. else {
  235. throw new IllegalArgumentException("Wrong session class: " + aClass + ". Supported[" + ServerShSession.class + "," + ClientShSession.class + "]");
  236. }
  237. }
  238. catch (Exception e) {
  239. logger.error("Failure to obtain new Sh Session.", e);
  240. }
  241. return appSession;
  242. }
  243. // Methods to handle default values for user listeners --------------------
  244. @SuppressWarnings("unchecked")
  245. public void stateChanged(Enum oldState, Enum newState) {
  246. logger.info("Diameter Sh SessionFactory :: stateChanged :: oldState[{}], newState[{}]", oldState, newState);
  247. }
  248. /*
  249. * (non-Javadoc)
  250. *
  251. * @see org.jdiameter.api.app.StateChangeListener#stateChanged(java.lang.Object, java.lang.Enum, java.lang.Enum)
  252. */
  253. @SuppressWarnings("unchecked")
  254. public void stateChanged(AppSession source, Enum oldState, Enum newState) {
  255. logger.info("Diameter Sh SessionFactory :: stateChanged :: source[{}], oldState[{}], newState[{}]", new Object[] { source, oldState, newState });
  256. }
  257. // Message Handlers -------------------------------------------------------
  258. /*
  259. * (non-Javadoc)
  260. *
  261. * @see org.jdiameter.api.sh.ClientShSessionListener#doOtherEvent(org.jdiameter.api.app.AppSession,
  262. * org.jdiameter.api.app.AppRequestEvent, org.jdiameter.api.app.AppAnswerEvent)
  263. */
  264. public void doOtherEvent(AppSession session, AppRequestEvent request, AppAnswerEvent answer)
  265. throws InternalException, IllegalDiameterStateException, RouteException, OverloadException {
  266. // TODO Auto-generated method stub
  267. }
  268. /*
  269. * (non-Javadoc)
  270. *
  271. * @see org.jdiameter.api.sh.ClientShSessionListener#doProfileUpdateAnswerEvent(org.jdiameter.api.sh.ClientShSession,
  272. * org.jdiameter.api.sh.events.ProfileUpdateRequest, org.jdiameter.api.sh.events.ProfileUpdateAnswer)
  273. */
  274. public void doProfileUpdateAnswerEvent(ClientShSession session, ProfileUpdateRequest request, ProfileUpdateAnswer answer)
  275. throws InternalException, IllegalDiameterStateException, RouteException, OverloadException {
  276. // TODO Auto-generated method stub
  277. }
  278. /*
  279. * (non-Javadoc)
  280. *
  281. * @see org.jdiameter.api.sh.ClientShSessionListener#doPushNotificationRequestEvent(
  282. * org.jdiameter.api.sh.ClientShSession, org.jdiameter.api.sh.events.PushNotificationRequest)
  283. */
  284. public void doPushNotificationRequestEvent(ClientShSession session, PushNotificationRequest request)
  285. throws InternalException, IllegalDiameterStateException, RouteException, OverloadException {
  286. // TODO Auto-generated method stub
  287. }
  288. /*
  289. * (non-Javadoc)
  290. *
  291. * @see org.jdiameter.api.sh.ClientShSessionListener#doSubscribeNotificationsAnswerEvent(org.jdiameter.api.sh.ClientShSession,
  292. * org.jdiameter.api.sh.events.SubscribeNotificationsRequest, org.jdiameter.api.sh.events.SubscribeNotificationsAnswer)
  293. */
  294. public void doSubscribeNotificationsAnswerEvent(ClientShSession session, SubscribeNotificationsRequest request,
  295. SubscribeNotificationsAnswer answer) throws InternalException, IllegalDiameterStateException, RouteException, OverloadException {
  296. // TODO Auto-generated method stub
  297. }
  298. /*
  299. * (non-Javadoc)
  300. *
  301. * @see
  302. * org.jdiameter.api.sh.ClientShSessionListener#doUserDataAnswerEvent(org
  303. * .jdiameter.api.sh.ClientShSession,
  304. * org.jdiameter.api.sh.events.UserDataRequest,
  305. * org.jdiameter.api.sh.events.UserDataAnswer)
  306. */
  307. public void doUserDataAnswerEvent(ClientShSession session, UserDataRequest request, UserDataAnswer answer)
  308. throws InternalException, IllegalDiameterStateException, RouteException, OverloadException {
  309. // TODO Auto-generated method stub
  310. }
  311. /*
  312. * (non-Javadoc)
  313. *
  314. * @see
  315. * org.jdiameter.api.sh.ServerShSessionListener#doProfileUpdateRequestEvent
  316. * (org.jdiameter.api.sh.ServerShSession,
  317. * org.jdiameter.api.sh.events.ProfileUpdateRequest)
  318. */
  319. public void doProfileUpdateRequestEvent(ServerShSession session, ProfileUpdateRequest request)
  320. throws InternalException, IllegalDiameterStateException, RouteException, OverloadException {
  321. // TODO Auto-generated method stub
  322. }
  323. /*
  324. * (non-Javadoc)
  325. *
  326. * @see
  327. * org.jdiameter.api.sh.ServerShSessionListener#doPushNotificationAnswerEvent
  328. * (org.jdiameter.api.sh.ServerShSession,
  329. * org.jdiameter.api.sh.events.PushNotificationRequest,
  330. * org.jdiameter.api.sh.events.PushNotificationAnswer)
  331. */
  332. public void doPushNotificationAnswerEvent(ServerShSession session, PushNotificationRequest request, PushNotificationAnswer answer)
  333. throws InternalException, IllegalDiameterStateException, RouteException, OverloadException {
  334. // TODO Auto-generated method stub
  335. }
  336. /*
  337. * (non-Javadoc)
  338. *
  339. * @seeorg.jdiameter.api.sh.ServerShSessionListener#
  340. * doSubscribeNotificationsRequestEvent
  341. * (org.jdiameter.api.sh.ServerShSession,
  342. * org.jdiameter.api.sh.events.SubscribeNotificationsRequest)
  343. */
  344. public void doSubscribeNotificationsRequestEvent(ServerShSession session, SubscribeNotificationsRequest request)
  345. throws InternalException, IllegalDiameterStateException, RouteException, OverloadException {
  346. // TODO Auto-generated method stub
  347. }
  348. /*
  349. * (non-Javadoc)
  350. *
  351. * @see
  352. * org.jdiameter.api.sh.ServerShSessionListener#doUserDataRequestEvent(org
  353. * .jdiameter.api.sh.ServerShSession,
  354. * org.jdiameter.api.sh.events.UserDataRequest)
  355. */
  356. public void doUserDataRequestEvent(ServerShSession session, UserDataRequest request)
  357. throws InternalException, IllegalDiameterStateException, RouteException, OverloadException {
  358. // TODO Auto-generated method stub
  359. }
  360. // Message Factory ----------------------------------------------------------
  361. public AppAnswerEvent createProfileUpdateAnswer(Answer answer) {
  362. return new ProfileUpdateAnswerImpl(answer);
  363. }
  364. public AppRequestEvent createProfileUpdateRequest(Request request) {
  365. return new ProfileUpdateRequestImpl(request);
  366. }
  367. public AppAnswerEvent createPushNotificationAnswer(Answer answer) {
  368. return new PushNotificationAnswerImpl(answer);
  369. }
  370. public AppRequestEvent createPushNotificationRequest(Request request) {
  371. return new PushNotificationRequestImpl(request);
  372. }
  373. public AppAnswerEvent createSubscribeNotificationsAnswer(Answer answer) {
  374. return new SubscribeNotificationsAnswerImpl(answer);
  375. }
  376. public AppRequestEvent createSubscribeNotificationsRequest(Request request) {
  377. return new SubscribeNotificationsRequestImpl(request);
  378. }
  379. public AppAnswerEvent createUserDataAnswer(Answer answer) {
  380. return new UserDataAnswerImpl(answer);
  381. }
  382. public AppRequestEvent createUserDataRequest(Request request) {
  383. return new UserDataRequestImpl(request);
  384. }
  385. public long getApplicationId() {
  386. return applicationId;
  387. }
  388. public long getMessageTimeout() {
  389. return messageTimeout;
  390. }
  391. }