PageRenderTime 51ms CodeModel.GetById 20ms RepoModel.GetById 0ms app.codeStats 0ms

/servers/jain-slee/resources/jcc/ra/src/main/java/org/mobicents/slee/resource/jcc/ra/JccResourceAdaptor.java

http://mobicents.googlecode.com/
Java | 873 lines | 522 code | 135 blank | 216 comment | 56 complexity | 5dd83c069b4e2c122a3ea172785aa0f3 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 2011, 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. /*
  23. * File Name : JccResourceAdaptor.java
  24. *
  25. * The Java Call Control RA
  26. *
  27. * The source code contained in this file is in in the public domain.
  28. * It can be used in any project or product without prior permission,
  29. * license or royalty payments. There is NO WARRANTY OF ANY KIND,
  30. * EXPRESS, IMPLIED OR STATUTORY, INCLUDING, WITHOUT LIMITATION,
  31. * THE IMPLIED WARRANTY OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE,
  32. * AND DATA ACCURACY. We do not warrant or make any representations
  33. * regarding the use of the software or the results thereof, including
  34. * but not limited to the correctness, accuracy, reliability or
  35. * usefulness of the software.
  36. */
  37. package org.mobicents.slee.resource.jcc.ra;
  38. import EDU.oswego.cs.dl.util.concurrent.ConcurrentReaderHashMap;
  39. import java.io.Serializable;
  40. import java.util.Enumeration;
  41. import java.util.Properties;
  42. import javax.slee.Address;
  43. import javax.slee.AddressPlan;
  44. import javax.slee.EventTypeID;
  45. import javax.slee.SLEEException;
  46. import javax.slee.UnrecognizedActivityException;
  47. import javax.slee.UnrecognizedEventException;
  48. import javax.slee.facilities.FacilityException;
  49. import javax.slee.facilities.Tracer;
  50. import javax.slee.resource.ActivityIsEndingException;
  51. import javax.slee.resource.ResourceAdaptor;
  52. import javax.slee.resource.SleeEndpoint;
  53. import javax.slee.facilities.EventLookupFacility;
  54. import javax.slee.resource.ActivityAlreadyExistsException;
  55. import javax.slee.resource.ActivityFlags;
  56. import javax.slee.resource.ActivityHandle;
  57. import javax.slee.resource.ConfigProperties;
  58. import javax.slee.resource.EventFlags;
  59. import javax.slee.resource.FailureReason;
  60. import javax.slee.resource.FireEventException;
  61. import javax.slee.resource.FireableEventType;
  62. import javax.slee.resource.IllegalEventException;
  63. import javax.slee.resource.InvalidConfigurationException;
  64. import javax.slee.resource.Marshaler;
  65. import javax.slee.resource.ReceivableService;
  66. import javax.slee.resource.ResourceAdaptorContext;
  67. import javax.slee.resource.StartActivityException;
  68. import javax.slee.resource.UnrecognizedActivityHandleException;
  69. import javax.csapi.cc.jcc.JccPeerFactory;
  70. import javax.csapi.cc.jcc.JccPeer;
  71. import javax.csapi.cc.jcc.JccConnectionListener;
  72. import javax.csapi.cc.jcc.JccEvent;
  73. import javax.csapi.cc.jcc.JccCall;
  74. import javax.csapi.cc.jcc.JccConnection;
  75. import javax.csapi.cc.jcc.JccCallEvent;
  76. import javax.csapi.cc.jcc.JccConnectionEvent;
  77. import javax.csapi.cc.jcc.MethodNotSupportedException;
  78. import javax.csapi.cc.jcc.ResourceUnavailableException;
  79. import org.mobicents.slee.resource.jcc.local.JccProviderLocal;
  80. import org.mobicents.slee.resource.jcc.local.JccCallLocal;
  81. import org.mobicents.slee.resource.jcc.local.JccConnectionLocal;
  82. import org.mobicents.slee.resource.jcc.local.JccCallEventLocal;
  83. import org.mobicents.slee.resource.jcc.local.JccConnectionEventLocal;
  84. import org.mobicents.slee.resource.jcc.ratype.JccActivityContextInterfaceFactory;
  85. /**
  86. *
  87. * @author Oleg Kulikov
  88. * @author Pavel Mitrenko
  89. * @author baranowb
  90. */
  91. public class JccResourceAdaptor implements ResourceAdaptor, Serializable, JccConnectionListener {
  92. private static final String _CONFIG_OPT_NAME_JCCPEER = "jccPeer";
  93. private static final String _CONFIG_OPT_NAME_CONF = "configName";
  94. private JccProviderLocal provider = new JccProviderLocal();
  95. private Address address = new Address(AddressPlan.IP, "127.0.0.1");
  96. private Tracer logger = null;
  97. private ConcurrentReaderHashMap activities = new ConcurrentReaderHashMap();
  98. private ConcurrentReaderHashMap handlers = new ConcurrentReaderHashMap();
  99. private String peerName = null;
  100. private String configName;
  101. private JccActivityContextInterfaceFactory activityContextInterfaceFactory;
  102. private boolean stopped = false;
  103. private Thread monitor;
  104. private String xmlJCCConfiguration = "";
  105. /**
  106. *
  107. */
  108. private ResourceAdaptorContext raContext;
  109. private SleeEndpoint sleeEndpoint;
  110. private EventLookupFacility eventLookupFacility;
  111. /**
  112. * for all events we are interested in knowing when the event failed to be
  113. * processed
  114. */
  115. public static final int DEFAULT_EVENT_FLAGS = EventFlags.REQUEST_PROCESSING_FAILED_CALLBACK;
  116. private static final int ACTIVITY_FLAGS = ActivityFlags.REQUEST_ENDED_CALLBACK;// .NO_FLAGS;
  117. /** Creates a new instance of JccResourceAdaptor */
  118. public JccResourceAdaptor() {
  119. }
  120. // //////////////
  121. // Properties //
  122. // //////////////
  123. public String getJccPeer() {
  124. return peerName;
  125. }
  126. public void setJccPeer(String jccPeer) {
  127. this.peerName = jccPeer;
  128. }
  129. public String getConfigName() {
  130. return configName;
  131. }
  132. public void setConfigName(String configName) {
  133. this.configName = configName;
  134. }
  135. // ///////////////////////
  136. // Life Cycle Methods //
  137. // ///////////////////////
  138. /*
  139. * (non-Javadoc)
  140. *
  141. * @see
  142. * javax.slee.resource.ResourceAdaptor#administrativeRemove(javax.slee.resource
  143. * .ActivityHandle)
  144. */
  145. public void administrativeRemove(ActivityHandle arg0) {
  146. // TODO Auto-generated method stub
  147. }
  148. /*
  149. * (non-Javadoc)
  150. *
  151. * @see
  152. * javax.slee.resource.ResourceAdaptor#eventProcessingFailed(javax.slee.
  153. * resource.ActivityHandle, javax.slee.resource.FireableEventType,
  154. * java.lang.Object, javax.slee.Address,
  155. * javax.slee.resource.ReceivableService, int,
  156. * javax.slee.resource.FailureReason)
  157. */
  158. public void eventProcessingFailed(ActivityHandle arg0, FireableEventType arg1, Object arg2, Address arg3, ReceivableService arg4,
  159. int arg5, FailureReason arg6) {
  160. // TODO Auto-generated method stub
  161. }
  162. /*
  163. * (non-Javadoc)
  164. *
  165. * @see
  166. * javax.slee.resource.ResourceAdaptor#eventProcessingSuccessful(javax.slee
  167. * .resource.ActivityHandle, javax.slee.resource.FireableEventType,
  168. * java.lang.Object, javax.slee.Address,
  169. * javax.slee.resource.ReceivableService, int)
  170. */
  171. public void eventProcessingSuccessful(ActivityHandle arg0, FireableEventType arg1, Object arg2, Address arg3, ReceivableService arg4,
  172. int arg5) {
  173. // TODO Auto-generated method stub
  174. }
  175. /*
  176. * (non-Javadoc)
  177. *
  178. * @see
  179. * javax.slee.resource.ResourceAdaptor#eventUnreferenced(javax.slee.resource
  180. * .ActivityHandle, javax.slee.resource.FireableEventType, java.lang.Object,
  181. * javax.slee.Address, javax.slee.resource.ReceivableService, int)
  182. */
  183. public void eventUnreferenced(ActivityHandle arg0, FireableEventType arg1, Object arg2, Address arg3, ReceivableService arg4, int arg5) {
  184. // TODO Auto-generated method stub
  185. }
  186. /*
  187. * (non-Javadoc)
  188. *
  189. * @see
  190. * javax.slee.resource.ResourceAdaptor#getResourceAdaptorInterface(java.
  191. * lang.String)
  192. */
  193. public Object getResourceAdaptorInterface(String arg0) {
  194. return this.provider;
  195. }
  196. /*
  197. * (non-Javadoc)
  198. *
  199. * @seejavax.slee.resource.ResourceAdaptor#raConfigure(javax.slee.resource.
  200. * ConfigProperties)
  201. */
  202. public void raConfigure(ConfigProperties cps) {
  203. try {
  204. if(logger.isInfoEnabled())
  205. {
  206. logger.info("Configuring JCCRA: "+this.raContext.getEntityName());
  207. }
  208. // FIXME: get props!
  209. Properties properties = new Properties();
  210. properties.load(getClass().getResourceAsStream("/" + configName));
  211. logger.info("Loaded properties: " + properties);
  212. xmlJCCConfiguration = "<jcc-inap>";
  213. Enumeration keys = properties.keys();
  214. while (keys.hasMoreElements()) {
  215. String key = (String) keys.nextElement();
  216. xmlJCCConfiguration += ";" + key + "=" + properties.getProperty(key);
  217. }
  218. //FIXME: make this work
  219. // JccPeer peer = JccPeerFactory.getJccPeer(peerName);
  220. // JccProvider testProvider = peer.getProvider(xmlConvofiguration);
  221. // testProvider.shutdown();
  222. } catch (UnsatisfiedLinkError ex) {
  223. logger.warning("JCC Resource adaptor is not attached to baord driver", ex);
  224. } catch (Exception e) {
  225. if (logger.isSevereEnabled())
  226. logger.severe("Can not start Jcc Provider: ", e);
  227. }
  228. }
  229. /*
  230. * (non-Javadoc)
  231. *
  232. * @see
  233. * javax.slee.resource.ResourceAdaptor#raVerifyConfiguration(javax.slee.
  234. * resource.ConfigProperties)
  235. */
  236. public void raVerifyConfiguration(ConfigProperties cps) throws InvalidConfigurationException {
  237. try {
  238. if(logger.isInfoEnabled())
  239. {
  240. logger.info("Verifyin configuring JCCRA: "+this.raContext.getEntityName());
  241. }
  242. this.configName = (String) cps.getProperty(_CONFIG_OPT_NAME_CONF).getValue();
  243. this.peerName = (String) cps.getProperty(_CONFIG_OPT_NAME_JCCPEER).getValue();
  244. // FIXME: is there any better way ?
  245. if (this.configName == null) {
  246. throw new InvalidConfigurationException("No name set for configuration file.");
  247. }
  248. if (this.peerName == null) {
  249. throw new InvalidConfigurationException("No name set jcc peer.");
  250. }
  251. if (null == getClass().getResource("/" + configName)) {
  252. throw new InvalidConfigurationException("Configuration file: " + configName + ", can not be located.");
  253. }
  254. } catch (InvalidConfigurationException e) {
  255. throw e;
  256. } catch (Exception e) {
  257. throw new InvalidConfigurationException("Failed to test configuration options!", e);
  258. }
  259. logger.info("JCC Provider started successfuly");
  260. logger.info("Running monitoring thread");
  261. }
  262. /*
  263. * (non-Javadoc)
  264. *
  265. * @see javax.slee.resource.ResourceAdaptor#raActive()
  266. */
  267. public void raActive() {
  268. try {
  269. JccPeer peer = JccPeerFactory.getJccPeer(peerName);
  270. provider.setProvider(peer.getProvider(xmlJCCConfiguration));
  271. provider.addConnectionListener(this, null);
  272. provider.addCallListener(this);
  273. logger.info("JCC Provider started successfuly");
  274. monitor = new Thread(new ActivityMonitor());
  275. monitor.start();
  276. logger.info("Running monitoring thread");
  277. } catch (ClassNotFoundException e) {
  278. // TODO Auto-generated catch block
  279. e.printStackTrace();
  280. } catch (ResourceUnavailableException e) {
  281. // TODO Auto-generated catch block
  282. e.printStackTrace();
  283. } catch (MethodNotSupportedException e) {
  284. // TODO Auto-generated catch block
  285. e.printStackTrace();
  286. }
  287. }
  288. /*
  289. * (non-Javadoc)
  290. *
  291. * @see
  292. * javax.slee.resource.ResourceAdaptor#raConfigurationUpdate(javax.slee.
  293. * resource.ConfigProperties)
  294. */
  295. public void raConfigurationUpdate(ConfigProperties arg0) {
  296. // not supported?
  297. }
  298. /*
  299. * (non-Javadoc)
  300. *
  301. * @see javax.slee.resource.ResourceAdaptor#raStopping()
  302. */
  303. public void raStopping() {
  304. try{
  305. //weird this causes exception?
  306. provider.shutdown();
  307. provider = null;
  308. }catch(Exception e)
  309. {
  310. }
  311. for (Object o : this.handlers.values()) {
  312. ActivityHandle ah = (ActivityHandle) o;
  313. try {
  314. sleeEndpoint.endActivity(ah);
  315. } catch (UnrecognizedActivityException uae) {
  316. if (logger.isSevereEnabled())
  317. logger.severe("Caught an UnrecognizedActivityException: ");
  318. uae.printStackTrace();
  319. }
  320. }
  321. }
  322. /*
  323. * (non-Javadoc)
  324. *
  325. * @see javax.slee.resource.ResourceAdaptor#raInactive()
  326. */
  327. public void raInactive() {
  328. stopped = true;
  329. monitor.interrupt();
  330. monitor = null;
  331. }
  332. /*
  333. * (non-Javadoc)
  334. *
  335. * @see javax.slee.resource.ResourceAdaptor#raUnconfigure()
  336. */
  337. public void raUnconfigure() {
  338. this.xmlJCCConfiguration = null;
  339. this.configName = null;
  340. this.peerName = null;
  341. }
  342. /*
  343. * (non-Javadoc)
  344. *
  345. * @see
  346. * javax.slee.resource.ResourceAdaptor#serviceActive(javax.slee.resource
  347. * .ReceivableService)
  348. */
  349. public void serviceActive(ReceivableService arg0) {
  350. // TODO Auto-generated method stub
  351. }
  352. /*
  353. * (non-Javadoc)
  354. *
  355. * @see
  356. * javax.slee.resource.ResourceAdaptor#serviceInactive(javax.slee.resource
  357. * .ReceivableService)
  358. */
  359. public void serviceInactive(ReceivableService arg0) {
  360. // TODO Auto-generated method stub
  361. }
  362. /*
  363. * (non-Javadoc)
  364. *
  365. * @see
  366. * javax.slee.resource.ResourceAdaptor#serviceStopping(javax.slee.resource
  367. * .ReceivableService)
  368. */
  369. public void serviceStopping(ReceivableService arg0) {
  370. // TODO Auto-generated method stub
  371. }
  372. /*
  373. * (non-Javadoc)
  374. *
  375. * @see
  376. * javax.slee.resource.ResourceAdaptor#setResourceAdaptorContext(javax.slee
  377. * .resource.ResourceAdaptorContext)
  378. */
  379. public void setResourceAdaptorContext(ResourceAdaptorContext raContext) {
  380. this.raContext = raContext;
  381. this.logger = raContext.getTracer("JccResourceAdaptor");
  382. this.sleeEndpoint = raContext.getSleeEndpoint();
  383. this.eventLookupFacility = raContext.getEventLookupFacility();
  384. }
  385. /*
  386. * (non-Javadoc)
  387. *
  388. * @see javax.slee.resource.ResourceAdaptor#unsetResourceAdaptorContext()
  389. */
  390. public void unsetResourceAdaptorContext() {
  391. this.raContext = null;
  392. this.sleeEndpoint = null;
  393. this.eventLookupFacility = null;
  394. }
  395. public void activityEnded(ActivityHandle handle) {
  396. String ID = ((JccConnectionActivityHandle) handle).getID();
  397. if(logger.isInfoEnabled())
  398. {
  399. logger.info("Activity ended id: "+ID+", handle: "+handle);
  400. }
  401. //sleeEndpoint.endActivity(handle);
  402. Object activity = activities.remove(ID);
  403. if(logger.isInfoEnabled())
  404. {
  405. logger.info("Activity ended activity: "+activity);
  406. }
  407. if (logger.isFineEnabled())
  408. logger.fine("Removed activity: " + activity + " under key: " + ID);
  409. JccConnectionActivityHandle h = (JccConnectionActivityHandle) handlers.remove(activity.toString());
  410. if(logger.isInfoEnabled())
  411. {
  412. logger.info("Activity ended removed handler: "+h);
  413. }
  414. if (logger.isFineEnabled())
  415. logger.fine("Removed handle: " + h.getID() + " under key: " + activity.toString());
  416. }
  417. public void activityUnreferenced(ActivityHandle arg0) {
  418. // TODO Auto-generated method stub
  419. }
  420. public Object getActivity(ActivityHandle handle) {
  421. String ID = ((JccConnectionActivityHandle) handle).getID();
  422. return activities.get(ID);
  423. }
  424. public ActivityHandle getActivityHandle(Object obj) {
  425. return (ActivityHandle) handlers.get(obj.toString());
  426. }
  427. public Marshaler getMarshaler() {
  428. return null;
  429. }
  430. public void queryLiveness(ActivityHandle activityHandle) {
  431. }
  432. // ////////////////////////
  433. // JCC Listener methods //
  434. // ////////////////////////
  435. // JccConnectionEventListener
  436. public void callActive(JccCallEvent event) {
  437. if (logger.isFineEnabled())
  438. logger.fine("Receive JccCallEvent.CALL_ACTIVE event");
  439. JccCallEvent evt = wrapJccCallEvent(event);
  440. JccCall call = evt.getCall();
  441. ActivityHandle handle = getActivityHandle(call);
  442. fireEvent("javax.csapi.cc.jcc.JccCallEvent.CALL_ACTIVE", handle, evt);
  443. }
  444. public void callCreated(JccCallEvent event) {
  445. if (logger.isFineEnabled())
  446. logger.fine("Receive JccCallEvent.CALL_CREATED event");
  447. JccCallEvent evt = wrapJccCallEvent(event);
  448. JccCall call = evt.getCall();
  449. // create new activity handle
  450. JccCallActivityHandle handle = new JccCallActivityHandle(call);
  451. try {
  452. if(logger.isInfoEnabled())
  453. {
  454. logger.info("Starting activity: "+call+", handle: "+handle);
  455. }
  456. this.sleeEndpoint.startActivity(handle, call,ACTIVITY_FLAGS);
  457. //FIXME: add this
  458. } catch (ActivityAlreadyExistsException e) {
  459. // TODO Auto-generated catch block
  460. e.printStackTrace();
  461. } catch (NullPointerException e) {
  462. // TODO Auto-generated catch block
  463. e.printStackTrace();
  464. } catch (IllegalStateException e) {
  465. // TODO Auto-generated catch block
  466. e.printStackTrace();
  467. } catch (SLEEException e) {
  468. // TODO Auto-generated catch block
  469. e.printStackTrace();
  470. } catch (StartActivityException e) {
  471. // TODO Auto-generated catch block
  472. e.printStackTrace();
  473. }
  474. //FIXME: Oleg is this the way it should be ?
  475. activities.put(call, handle);
  476. fireEvent("javax.csapi.cc.jcc.JccCallEvent.CALL_CREATED", handle, evt);
  477. }
  478. public void callEventTransmissionEnded(JccCallEvent event) {
  479. if (logger.isFineEnabled())
  480. logger.fine("Receive JccCallEvent.CALL_EVENT_TRANSMISSION_ENDED event");
  481. JccCallEvent evt = wrapJccCallEvent(event);
  482. JccCall call = evt.getCall();
  483. ActivityHandle handle = getActivityHandle(call);
  484. fireEvent("javax.csapi.cc.jcc.JccCallEvent.CALL_EVENT_TRANSMISSION_ENDED", handle, evt);
  485. try {
  486. if(logger.isInfoEnabled())
  487. {
  488. logger.info("callEventTransmissionEnded : ending activity "+handle);
  489. }
  490. sleeEndpoint.endActivity(handle);
  491. } catch (UnrecognizedActivityException uae) {
  492. if (logger.isSevereEnabled())
  493. logger.severe("Caught an UnrecognizedActivityException: ");
  494. uae.printStackTrace();
  495. }
  496. }
  497. public void callInvalid(JccCallEvent event) {
  498. if (logger.isFineEnabled())
  499. logger.fine("Receive JccCallEvent.CALL_INVALID event");
  500. JccCallEvent evt = wrapJccCallEvent(event);
  501. JccCall call = evt.getCall();
  502. ActivityHandle handle = getActivityHandle(call);
  503. fireEvent("javax.csapi.cc.jcc.JccCallEvent.CALL_INVALID", handle, evt);
  504. try {
  505. if(logger.isInfoEnabled())
  506. {
  507. logger.info("callInvalid : ending activity "+handle);
  508. }
  509. sleeEndpoint.endActivity(handle);
  510. } catch (UnrecognizedActivityException uae) {
  511. if (logger.isSevereEnabled())
  512. logger.severe("Caught an UnrecognizedActivityException: ");
  513. uae.printStackTrace();
  514. }
  515. }
  516. public void callSuperviseEnd(JccCallEvent event) {
  517. if (logger.isFineEnabled())
  518. logger.fine("Receive JccCallEvent.CALL_SUPERVISE_END event");
  519. JccCallEvent evt = wrapJccCallEvent(event);
  520. JccCall call = evt.getCall();
  521. ActivityHandle handle = getActivityHandle(call);
  522. fireEvent("javax.csapi.cc.jcc.JccCallEvent.CALL_CSUPERVISE_END", handle, evt);
  523. }
  524. public void callSuperviseStart(JccCallEvent event) {
  525. if (logger.isFineEnabled())
  526. logger.fine("Receive JccCallEvent.CALL_SUPERVISE_START event");
  527. JccCallEvent evt = wrapJccCallEvent(event);
  528. JccCall call = evt.getCall();
  529. ActivityHandle handle = getActivityHandle(call);
  530. fireEvent("javax.csapi.cc.jcc.JccCallEvent.CALL_CSUPERVISE_START", handle, evt);
  531. }
  532. public void connectionAddressAnalyze(JccConnectionEvent event) {
  533. if (logger.isFineEnabled())
  534. logger.fine("Receive JccConnection.CONNECTION_ADDRESS_ANALYZE event");
  535. JccConnectionEvent evt = wrapJccConnectionEvent(event);
  536. JccConnection connection = evt.getConnection();
  537. ActivityHandle handle = getActivityHandle(connection);
  538. fireEvent("javax.csapi.cc.jcc.JccConnectionEvent.CONNECTION_ADDRESS_ANALYZE", handle, evt);
  539. }
  540. public void connectionAddressCollect(JccConnectionEvent event) {
  541. if (logger.isFineEnabled())
  542. logger.fine("Receive JccConnection.CONNECTION_ADDRESS_COLLECT event");
  543. JccConnectionEvent evt = wrapJccConnectionEvent(event);
  544. JccConnection connection = evt.getConnection();
  545. ActivityHandle handle = getActivityHandle(connection);
  546. fireEvent("javax.csapi.cc.jcc.JccConnectionEvent.CONNECTION_ADDRESS_COLLECT", handle, evt);
  547. }
  548. public void connectionAlerting(JccConnectionEvent event) {
  549. if (logger.isFineEnabled())
  550. logger.fine("Receive JccConnection.CONNECTION_ALERTING event");
  551. JccConnectionEvent evt = wrapJccConnectionEvent(event);
  552. JccConnection connection = evt.getConnection();
  553. ActivityHandle handle = getActivityHandle(connection);
  554. fireEvent("javax.csapi.cc.jcc.JccConnectionEvent.CONNECTION_ALERTING", handle, evt);
  555. }
  556. public void connectionAuthorizeCallAttempt(JccConnectionEvent event) {
  557. if (logger.isFineEnabled())
  558. logger.fine("Receive JccConnection.CONNECTION_AUTHORIZE_CALL_ATTEMPT event");
  559. JccConnectionEvent evt = wrapJccConnectionEvent(event);
  560. JccConnection connection = evt.getConnection();
  561. ActivityHandle handle = getActivityHandle(connection);
  562. fireEvent("javax.csapi.cc.jcc.JccConnectionEvent.CONNECTION_AUTHORIZE_CALL_ATTEMPT", handle, evt);
  563. }
  564. public void connectionCallDelivery(JccConnectionEvent event) {
  565. if (logger.isFineEnabled())
  566. logger.fine("Receive JccConnection.CONNECTION_CALL_DELIVERY event");
  567. JccConnectionEvent evt = wrapJccConnectionEvent(event);
  568. JccConnection connection = evt.getConnection();
  569. ActivityHandle handle = getActivityHandle(connection);
  570. fireEvent("javax.csapi.cc.jcc.JccConnectionEvent.CONNECTION_CALL_DELIVERY", handle, evt);
  571. }
  572. public void connectionConnected(JccConnectionEvent event) {
  573. if (logger.isFineEnabled())
  574. logger.fine("Receive JccConnection.CONNECTION_CONNECTED event");
  575. JccConnectionEvent evt = wrapJccConnectionEvent(event);
  576. JccConnection connection = evt.getConnection();
  577. ActivityHandle handle = getActivityHandle(connection);
  578. fireEvent("javax.csapi.cc.jcc.JccConnectionEvent.CONNECTION_CONNECTED", handle, evt);
  579. }
  580. public void connectionCreated(JccConnectionEvent event) {
  581. if (logger.isFineEnabled())
  582. logger.fine("Receive JccConnection.CONNECTION_CREATED event");
  583. JccConnectionEvent evt = wrapJccConnectionEvent(event);
  584. JccConnection connection = evt.getConnection();
  585. // create new activity handle
  586. JccConnectionActivityHandle handle = new JccConnectionActivityHandle(connection);
  587. try {
  588. if(logger.isInfoEnabled())
  589. {
  590. logger.info("Starting activity: "+connection+", handle: "+handle);
  591. }
  592. this.sleeEndpoint.startActivity(handle, connection,ACTIVITY_FLAGS);
  593. //FIXME: add this
  594. } catch (ActivityAlreadyExistsException e) {
  595. // TODO Auto-generated catch block
  596. e.printStackTrace();
  597. } catch (NullPointerException e) {
  598. // TODO Auto-generated catch block
  599. e.printStackTrace();
  600. } catch (IllegalStateException e) {
  601. // TODO Auto-generated catch block
  602. e.printStackTrace();
  603. } catch (SLEEException e) {
  604. // TODO Auto-generated catch block
  605. e.printStackTrace();
  606. } catch (StartActivityException e) {
  607. // TODO Auto-generated catch block
  608. e.printStackTrace();
  609. }
  610. handlers.put(connection.toString(), handle);
  611. if (logger.isFineEnabled())
  612. logger.fine("onConnectionCreate():put handle: " + handle + " under key: " + connection.toString());
  613. activities.put(handle.getID(), connection);
  614. if (logger.isFineEnabled())
  615. logger.fine("onConnectionCreate():put connection " + connection + " under key: " + handle.getID());
  616. fireEvent("javax.csapi.cc.jcc.JccConnectionEvent.CONNECTION_CREATED", handle, evt);
  617. }
  618. public void connectionDisconnected(JccConnectionEvent event) {
  619. if (logger.isFineEnabled())
  620. logger.fine("Receive JccConnection.CONNECTION_DISCONNECTED event");
  621. JccConnectionEvent evt = wrapJccConnectionEvent(event);
  622. JccConnection connection = evt.getConnection();
  623. ActivityHandle handle = getActivityHandle(connection.toString());
  624. fireEvent("javax.csapi.cc.jcc.JccConnectionEvent.CONNECTION_DISCONNECTED", handle, evt);
  625. try {
  626. if(logger.isInfoEnabled())
  627. {
  628. logger.info("connectionDisconnected : ending activity "+handle+", activity: "+connection);
  629. }
  630. sleeEndpoint.endActivity(handle);
  631. } catch (UnrecognizedActivityException uae) {
  632. if (logger.isSevereEnabled())
  633. logger.severe("Caught an UnrecognizedActivityException: ");
  634. uae.printStackTrace();
  635. }
  636. }
  637. public void connectionFailed(JccConnectionEvent event) {
  638. if (logger.isFineEnabled())
  639. logger.fine("Receive JccConnection.CONNECTION_FAILED event");
  640. JccConnectionEvent evt = wrapJccConnectionEvent(event);
  641. JccConnection connection = evt.getConnection();
  642. if (logger.isFineEnabled())
  643. logger.fine("onfailed(): connectionID: " + connection.toString());
  644. ActivityHandle handle = getActivityHandle(connection);
  645. if(logger.isInfoEnabled())
  646. {
  647. logger.info("connectionFailed : ending activity "+handle+", activity: "+connection);
  648. }
  649. fireEvent("javax.csapi.cc.jcc.JccConnectionEvent.CONNECTION_FAILED", handle, evt);
  650. //called in {@link #connectionDisconnected(JccConnectionEvent)}
  651. //sleeEndpoint.endActivity(handle);
  652. if (logger.isFineEnabled())
  653. logger.fine("onfailed(): handle=" + handle);
  654. }
  655. public void connectionMidCall(JccConnectionEvent event) {
  656. if (logger.isFineEnabled())
  657. logger.fine("Receive JccConnection.CONNECTION_MID_CALLevent");
  658. JccConnectionEvent evt = wrapJccConnectionEvent(event);
  659. JccConnection connection = evt.getConnection();
  660. ActivityHandle handle = getActivityHandle(connection);
  661. fireEvent("javax.csapi.cc.jcc.JccConnectionEvent.CONNECTION_MID_CALL", handle, evt);
  662. }
  663. private JccConnectionEventLocal wrapJccConnectionEvent(JccConnectionEvent event) {
  664. JccCallLocal call = new JccCallLocal(event.getCall(), provider);
  665. JccConnectionLocal connection = new JccConnectionLocal(event.getConnection(), provider, call);
  666. return new JccConnectionEventLocal(connection, event.getID(), event.getCause(), event.getSource(), call);
  667. }
  668. private JccCallEventLocal wrapJccCallEvent(JccCallEvent event) {
  669. JccCallLocal call = new JccCallLocal(event.getCall(), provider);
  670. return new JccCallEventLocal(event.getID(), event.getCause(), event.getSource(), call);
  671. }
  672. private void fireEvent(String eventName, ActivityHandle activityHandle, JccEvent event) {
  673. FireableEventType eventID = null;
  674. try {
  675. EventTypeID eventTypeId = new EventTypeID(eventName, "javax.csapi.cc.jcc", "1.1");
  676. eventID = eventLookupFacility.getFireableEventType(eventTypeId);
  677. } catch (FacilityException fe) {
  678. if (logger.isSevereEnabled())
  679. logger.severe("Caught a FacilityException: ");
  680. fe.printStackTrace();
  681. throw new RuntimeException("JccResourceAdaptor.firEvent(): FacilityException caught. ", fe);
  682. } catch (UnrecognizedEventException ue) {
  683. if (logger.isSevereEnabled())
  684. logger.severe("Caught an UnrecognizedEventException: ");
  685. ue.printStackTrace();
  686. throw new RuntimeException("JccResourceAdaptor.firEvent(): UnrecognizedEventException caught.", ue);
  687. }
  688. if (eventID == null) {
  689. if (logger.isWarningEnabled())
  690. logger.warning("Unknown event type: " + eventName);
  691. return;
  692. }
  693. try {
  694. sleeEndpoint.fireEvent(activityHandle, eventID, event, address, null);
  695. if (logger.isFineEnabled())
  696. logger.fine("Fire event: " + eventName);
  697. } catch (IllegalStateException ise) {
  698. if (logger.isSevereEnabled())
  699. logger.severe("Caught an IllegalStateException: ");
  700. ise.printStackTrace();
  701. } catch (ActivityIsEndingException aiee) {
  702. if (logger.isSevereEnabled())
  703. logger.severe("Caught an ActivityIsEndingException: ");
  704. aiee.printStackTrace();
  705. } catch (UnrecognizedActivityException uaee) {
  706. if (logger.isSevereEnabled())
  707. logger.severe("Caught an UnrecognizedActivityException: ");
  708. uaee.printStackTrace();
  709. } catch (UnrecognizedActivityHandleException e) {
  710. // TODO Auto-generated catch block
  711. e.printStackTrace();
  712. } catch (IllegalEventException e) {
  713. // TODO Auto-generated catch block
  714. e.printStackTrace();
  715. } catch (NullPointerException e) {
  716. // TODO Auto-generated catch block
  717. e.printStackTrace();
  718. } catch (SLEEException e) {
  719. // TODO Auto-generated catch block
  720. e.printStackTrace();
  721. } catch (FireEventException e) {
  722. // TODO Auto-generated catch block
  723. e.printStackTrace();
  724. }
  725. }
  726. private class ActivityMonitor implements Runnable {
  727. public void run() {
  728. while (!stopped) {
  729. try {
  730. Thread.currentThread().sleep(60000);
  731. logger.info("activities=" + activities.size() + ", handlers=" + handlers.size());
  732. } catch (InterruptedException e) {
  733. stopped = true;
  734. }
  735. }
  736. }
  737. }
  738. }