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

/servers/jain-slee/resources/mscontrol/ra/src/main/java/org/mobicents/slee/resource/mediacontrol/MsResourceAdaptor.java

http://mobicents.googlecode.com/
Java | 541 lines | 337 code | 76 blank | 128 comment | 35 complexity | fb8ad79336b59b3517df302ee0ba6cea 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 XXXX, Red Hat Middleware LLC, and individual contributors as indicated
  4. * by the @authors tag. All rights reserved.
  5. * See the copyright.txt in the distribution for a full listing
  6. * of individual contributors.
  7. * This copyrighted material is made available to anyone wishing to use,
  8. * modify, copy, or redistribute it subject to the terms and conditions
  9. * of the GNU General Public License, v. 2.0.
  10. * This program is distributed in the hope that it will be useful, but WITHOUT A
  11. * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
  12. * PARTICULAR PURPOSE. See the GNU General Public License for more details.
  13. * You should have received a copy of the GNU General Public License,
  14. * v. 2.0 along with this distribution; if not, write to the Free Software
  15. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
  16. * MA 02110-1301, USA.
  17. */
  18. package org.mobicents.slee.resource.mediacontrol;
  19. import java.io.Reader;
  20. import java.net.URI;
  21. import java.util.HashSet;
  22. import java.util.Map;
  23. import java.util.Properties;
  24. import java.util.Set;
  25. import java.util.concurrent.ConcurrentHashMap;
  26. import javax.media.mscontrol.Configuration;
  27. import javax.media.mscontrol.MediaConfig;
  28. import javax.media.mscontrol.MediaConfigException;
  29. import javax.media.mscontrol.MediaEvent;
  30. import javax.media.mscontrol.MediaObject;
  31. import javax.media.mscontrol.MediaSession;
  32. import javax.media.mscontrol.MsControlException;
  33. import javax.media.mscontrol.MsControlFactory;
  34. import javax.media.mscontrol.Parameters;
  35. import javax.media.mscontrol.resource.video.VideoLayout;
  36. import javax.media.mscontrol.spi.Driver;
  37. import javax.media.mscontrol.spi.DriverManager;
  38. import javax.slee.Address;
  39. import javax.slee.EventTypeID;
  40. import javax.slee.SLEEException;
  41. import javax.slee.UnrecognizedActivityException;
  42. import javax.slee.UnrecognizedEventException;
  43. import javax.slee.facilities.EventLookupFacility;
  44. import javax.slee.facilities.FacilityException;
  45. import javax.slee.facilities.Tracer;
  46. import javax.slee.resource.ActivityAlreadyExistsException;
  47. import javax.slee.resource.ActivityFlags;
  48. import javax.slee.resource.ActivityHandle;
  49. import javax.slee.resource.ActivityIsEndingException;
  50. import javax.slee.resource.ConfigProperties;
  51. import javax.slee.resource.ConfigProperties.Property;
  52. import javax.slee.resource.FailureReason;
  53. import javax.slee.resource.FireEventException;
  54. import javax.slee.resource.FireableEventType;
  55. import javax.slee.resource.IllegalEventException;
  56. import javax.slee.resource.InvalidConfigurationException;
  57. import javax.slee.resource.Marshaler;
  58. import javax.slee.resource.ReceivableService;
  59. import javax.slee.resource.ResourceAdaptor;
  60. import javax.slee.resource.ResourceAdaptorContext;
  61. import javax.slee.resource.SleeEndpoint;
  62. import javax.slee.resource.StartActivityException;
  63. import javax.slee.resource.UnrecognizedActivityHandleException;
  64. import org.mobicents.slee.resource.mediacontrol.wrapper.MediaSessionWrapper;
  65. import org.mobicents.javax.media.mscontrol.spi.DriverImpl;
  66. /**
  67. *
  68. * @author baranowb
  69. */
  70. public class MsResourceAdaptor implements ResourceAdaptor {
  71. /**
  72. * RA config property which holds driver name.
  73. */
  74. public static final String DRIVER = "driver.name";
  75. //flags for events.
  76. private static final int ACTIVITY_FLAGS = ActivityFlags.setRequestEndedCallback(ActivityFlags.REQUEST_ACTIVITY_UNREFERENCED_CALLBACK);
  77. // Media control factory
  78. private MsControlFactoryWrapper mscFactory;
  79. private Driver mscDriver;
  80. private ResourceAdaptorContext context;
  81. private SleeEndpoint sleeEndpoint;
  82. private EventLookupFacility eventLookupFacility;
  83. private Tracer tracer;
  84. // Driver configuration
  85. private Properties config = new Properties();
  86. private String driverName;
  87. private Map<MsActivityHandle, MsActivity> activities = new ConcurrentHashMap<MsActivityHandle, MsActivity>();
  88. /**
  89. *
  90. */
  91. public MsResourceAdaptor() {
  92. super();
  93. this.mscFactory = new MsControlFactoryWrapper(this);
  94. }
  95. public void setResourceAdaptorContext(ResourceAdaptorContext context) {
  96. this.context = context;
  97. this.tracer = this.getTracer(this);
  98. this.sleeEndpoint = context.getSleeEndpoint();
  99. this.eventLookupFacility = context.getEventLookupFacility();
  100. }
  101. public void unsetResourceAdaptorContext() {
  102. }
  103. public void raConfigure(ConfigProperties cfg) {
  104. for(Property p:cfg.getProperties())
  105. {
  106. if(p.getName().equals(DRIVER))
  107. {
  108. this.driverName = (String)p.getValue(); //it must be string
  109. }else
  110. {
  111. //add to props.
  112. this.config.put(p.getName(), p.getValue());
  113. }
  114. }
  115. }
  116. public void raUnconfigure() {
  117. this.config.clear();
  118. this.driverName = null;
  119. }
  120. public void raActive() {
  121. try {
  122. this.mscDriver = DriverManager.getDriver(this.driverName);
  123. this.tracer.info("Created MSC Driver: " + mscDriver + ", from name: " + driverName);
  124. this.mscFactory.setFactory(this.mscDriver.getFactory(this.config));
  125. this.mscFactory.setActive(true);
  126. this.tracer.info("Successfully started MSC RA Entity:" + this.context.getEntityName());
  127. } catch (Exception e) {
  128. tracer.severe("Can not activate driver[" + driverName + "]", e);
  129. }
  130. }
  131. public void raStopping() {
  132. //set it before becoming inactive?
  133. this.mscFactory.setActive(false);
  134. Set<MsActivity> acs = new HashSet<MsActivity>(this.activities.values());
  135. for(MsActivity a:acs)
  136. {
  137. //release only on media session, rest will be released as children
  138. if(a instanceof MediaSession)
  139. a.release();
  140. }
  141. }
  142. public void raInactive() {
  143. if(this.activities.size()>0)
  144. {
  145. this.tracer.severe("Some activities still remain! "+this.activities);
  146. }
  147. this.activities.clear();
  148. //HACK, MSC does not expose any method to shutdown resources once driver is not used.
  149. //mobicents impl does.
  150. if(this.mscDriver instanceof DriverImpl)
  151. {
  152. DriverImpl driverImpl = (DriverImpl)this.mscDriver;
  153. driverImpl.shutdown();
  154. }
  155. this.mscDriver = null;
  156. this.mscFactory.setFactory(null);
  157. }
  158. public void raVerifyConfiguration(ConfigProperties cfg) throws InvalidConfigurationException {
  159. Property driverProperty = cfg.getProperty(DRIVER);
  160. if(driverProperty == null || driverProperty.getValue() == null || !driverProperty.getType().equals("java.lang.String"))
  161. {
  162. throw new InvalidConfigurationException("No driver property specified!");
  163. }
  164. String driverName = null;
  165. Properties config = new Properties();
  166. for(Property p:cfg.getProperties())
  167. {
  168. if(p.getName().equals(DRIVER))
  169. {
  170. driverName = (String)p.getValue(); //it must be string
  171. }else
  172. {
  173. //add to props.
  174. config.put(p.getName(), p.getValue());
  175. }
  176. }
  177. Driver d = DriverManager.getDriver(driverName);
  178. if(d== null)
  179. {
  180. throw new InvalidConfigurationException("Failed to create driver for: "+driverName);
  181. }
  182. //postpone interaction with Driver, since we dont want anything to be started.
  183. // try {
  184. // if(d.getFactory(config) == null)
  185. // {
  186. // throw new InvalidConfigurationException("Failed to create MscFactory for: "+config);
  187. // }
  188. // } catch (MsControlException e) {
  189. // throw new InvalidConfigurationException("Failed to create MscFactory for: "+config,e);
  190. // }
  191. }
  192. public void raConfigurationUpdate(ConfigProperties config) {
  193. throw new UnsupportedOperationException();
  194. }
  195. public Object getResourceAdaptorInterface(String name) {
  196. return mscFactory;
  197. }
  198. public Marshaler getMarshaler() {
  199. return null;
  200. }
  201. public void serviceActive(ReceivableService service) {
  202. }
  203. public void serviceStopping(ReceivableService service) {
  204. }
  205. public void serviceInactive(ReceivableService service) {
  206. }
  207. public void queryLiveness(ActivityHandle handle) {
  208. }
  209. public Object getActivity(ActivityHandle handle) {
  210. if (handle instanceof MsActivityHandle) {
  211. return activities.get(handle);
  212. } else {
  213. return null;
  214. }
  215. }
  216. public ActivityHandle getActivityHandle(Object activity) {
  217. if (activity instanceof MsActivity) {
  218. MsActivity mcActivity = (MsActivity) activity;
  219. return mcActivity.getActivityHandle();
  220. }
  221. return null;
  222. }
  223. public void administrativeRemove(ActivityHandle handle) {
  224. }
  225. public void eventProcessingSuccessful(ActivityHandle handle, FireableEventType evtTyppe, Object arg2, Address arg3, ReceivableService arg4, int arg5) {
  226. }
  227. public void eventProcessingFailed(ActivityHandle arg0, FireableEventType arg1, Object arg2, Address arg3, ReceivableService arg4, int arg5,
  228. FailureReason arg6) {
  229. }
  230. public void eventUnreferenced(ActivityHandle arg0, FireableEventType arg1, Object arg2, Address arg3, ReceivableService arg4, int arg5) {
  231. }
  232. public void activityEnded(ActivityHandle handle) {
  233. if (handle instanceof MsActivityHandle) {
  234. this.activities.remove(handle);
  235. }
  236. }
  237. public void activityUnreferenced(ActivityHandle handle) {
  238. }
  239. public void fireEvent(String eventName, ActivityHandle activityHandle, MediaEvent event) {
  240. tracer.info("Fire on: "+activityHandle+", event: " + eventName);
  241. FireableEventType eventID = null;
  242. try {
  243. EventTypeID eventTypeId = new EventTypeID(eventName, "org.mobicents", "1.0");
  244. eventID = eventLookupFacility.getFireableEventType(eventTypeId);
  245. } catch (FacilityException fe) {
  246. if (tracer.isSevereEnabled()) {
  247. tracer.severe("Caught a FacilityException: ");
  248. }
  249. fe.printStackTrace();
  250. throw new RuntimeException("JccResourceAdaptor.firEvent(): FacilityException caught. ", fe);
  251. } catch (UnrecognizedEventException ue) {
  252. if (tracer.isSevereEnabled()) {
  253. tracer.severe("Caught an UnrecognizedEventException: ");
  254. }
  255. ue.printStackTrace();
  256. throw new RuntimeException("JccResourceAdaptor.firEvent(): UnrecognizedEventException caught.", ue);
  257. }
  258. if (eventID == null) {
  259. if (tracer.isWarningEnabled()) {
  260. tracer.warning("Unknown event type: " + eventName);
  261. }
  262. return;
  263. }
  264. try {
  265. sleeEndpoint.fireEvent(activityHandle, eventID, event, null, null);
  266. if (tracer.isFineEnabled()) {
  267. tracer.fine("Fire on: "+activityHandle+", event: " + eventName);
  268. }
  269. } catch (IllegalStateException ise) {
  270. if (tracer.isSevereEnabled()) {
  271. tracer.severe("Caught an IllegalStateException: ");
  272. }
  273. ise.printStackTrace();
  274. } catch (ActivityIsEndingException aiee) {
  275. if (tracer.isSevereEnabled()) {
  276. tracer.severe("Caught an ActivityIsEndingException: ");
  277. }
  278. aiee.printStackTrace();
  279. } catch (UnrecognizedActivityException uaee) {
  280. if (tracer.isSevereEnabled()) {
  281. tracer.severe("Caught an UnrecognizedActivityException: ");
  282. }
  283. uaee.printStackTrace();
  284. } catch (UnrecognizedActivityHandleException e) {
  285. // TODO Auto-generated catch block
  286. e.printStackTrace();
  287. } catch (IllegalEventException e) {
  288. // TODO Auto-generated catch block
  289. e.printStackTrace();
  290. } catch (NullPointerException e) {
  291. // TODO Auto-generated catch block
  292. e.printStackTrace();
  293. } catch (SLEEException e) {
  294. // TODO Auto-generated catch block
  295. e.printStackTrace();
  296. } catch (FireEventException e) {
  297. // TODO Auto-generated catch block
  298. e.printStackTrace();
  299. }
  300. }
  301. /**
  302. * @param activityHandle
  303. */
  304. public void endActivity(MsActivityHandle activityHandle) {
  305. this.sleeEndpoint.endActivity(activityHandle);
  306. }
  307. /**
  308. * @param wrapper
  309. * @throws StartActivityException
  310. * @throws SLEEException
  311. * @throws IllegalStateException
  312. * @throws NullPointerException
  313. * @throws ActivityAlreadyExistsException
  314. */
  315. public void startActivity(MsActivity wrapper) throws ActivityAlreadyExistsException, NullPointerException, IllegalStateException, SLEEException,
  316. StartActivityException {
  317. MsActivityHandle handle = wrapper.getActivityHandle();
  318. sleeEndpoint.startActivitySuspended(handle, wrapper, ACTIVITY_FLAGS);
  319. this.activities.put(handle, wrapper);
  320. }
  321. public Tracer getTracer(Object o)
  322. {
  323. //hmm just to have single logging framework working...
  324. String name = getClass().getName();
  325. return this.context.getTracer(name);
  326. }
  327. //seems like it has to be in RA class?
  328. /**
  329. * This wrapper is actually a provider of RA.
  330. *
  331. * @author baranowb
  332. *
  333. */
  334. public class MsControlFactoryWrapper implements MsControlFactory {
  335. protected MsControlFactory wrappedFactory;
  336. protected MsResourceAdaptor ra; //required to pass into other wrappers
  337. protected boolean active = false;
  338. /**
  339. * @param wrappedFactory
  340. * @param ra
  341. */
  342. public MsControlFactoryWrapper(MsResourceAdaptor ra) {
  343. super();
  344. this.ra = ra;
  345. }
  346. /*
  347. * (non-Javadoc)
  348. *
  349. * @see javax.media.mscontrol.MsControlFactory#createMediaSession()
  350. */
  351. public MediaSession createMediaSession() throws MsControlException {
  352. if(!active){
  353. throw new IllegalStateException("Factory is not ready!");
  354. }
  355. MediaSessionWrapper msw = new MediaSessionWrapper(this.wrappedFactory.createMediaSession(), this.ra);
  356. try {
  357. this.ra.startActivity(msw);
  358. } catch (Exception e) {
  359. throw new MsControlException("Failed to create MsControl resource.", e);
  360. }
  361. return msw;
  362. }
  363. /*
  364. * (non-Javadoc)
  365. *
  366. * @see javax.media.mscontrol.MsControlFactory#createParameters()
  367. */
  368. public Parameters createParameters() {
  369. if(!active){
  370. throw new IllegalStateException("Factory is not ready!");
  371. }
  372. return this.wrappedFactory.createParameters();
  373. }
  374. /*
  375. * (non-Javadoc)
  376. *
  377. * @see
  378. * javax.media.mscontrol.MsControlFactory#createVideoLayout(java.lang.String
  379. * , java.io.Reader)
  380. */
  381. public VideoLayout createVideoLayout(String mimeType, Reader xmlDef) throws MediaConfigException {
  382. if(!active){
  383. throw new IllegalStateException("Factory is not ready!");
  384. }
  385. return this.wrappedFactory.createVideoLayout(mimeType, xmlDef);
  386. }
  387. /*
  388. * (non-Javadoc)
  389. *
  390. * @see
  391. * javax.media.mscontrol.MsControlFactory#getMediaConfig(javax.media.mscontrol
  392. * .Configuration)
  393. */
  394. public MediaConfig getMediaConfig(Configuration<?> configuration) throws MediaConfigException {
  395. if(!active){
  396. throw new IllegalStateException("Factory is not ready!");
  397. }
  398. return this.wrappedFactory.getMediaConfig(configuration);
  399. }
  400. /*
  401. * (non-Javadoc)
  402. *
  403. * @see
  404. * javax.media.mscontrol.MsControlFactory#getMediaConfig(java.io.Reader)
  405. */
  406. public MediaConfig getMediaConfig(Reader xmlDef) throws MediaConfigException {
  407. if(!active){
  408. throw new IllegalStateException("Factory is not ready!");
  409. }
  410. return this.wrappedFactory.getMediaConfig(xmlDef);
  411. }
  412. /*
  413. * (non-Javadoc)
  414. *
  415. * @see javax.media.mscontrol.MsControlFactory#getMediaObject(java.net.URI)
  416. */
  417. public MediaObject getMediaObject(URI arg0) {
  418. // TODO
  419. throw new UnsupportedOperationException();
  420. }
  421. /*
  422. * (non-Javadoc)
  423. *
  424. * @see
  425. * javax.media.mscontrol.MsControlFactory#getPresetLayout(java.lang.String)
  426. */
  427. public VideoLayout getPresetLayout(String type) throws MediaConfigException {
  428. if(!active){
  429. throw new IllegalStateException("Factory is not ready!");
  430. }
  431. return this.wrappedFactory.getPresetLayout(type);
  432. }
  433. /*
  434. * (non-Javadoc)
  435. *
  436. * @see javax.media.mscontrol.MsControlFactory#getPresetLayouts(int)
  437. */
  438. public VideoLayout[] getPresetLayouts(int numberOfLiveRegions) throws MediaConfigException {
  439. if(!active){
  440. throw new IllegalStateException("Factory is not ready!");
  441. }
  442. return this.wrappedFactory.getPresetLayouts(numberOfLiveRegions);
  443. }
  444. /*
  445. * (non-Javadoc)
  446. *
  447. * @see javax.media.mscontrol.MsControlFactory#getProperties()
  448. */
  449. public Properties getProperties() {
  450. if(!active){
  451. throw new IllegalStateException("Factory is not ready!");
  452. }
  453. return this.wrappedFactory.getProperties();
  454. }
  455. /**
  456. * @param factory
  457. */
  458. public void setFactory(MsControlFactory factory) {
  459. this.wrappedFactory = factory;
  460. }
  461. public void setActive(boolean active) {
  462. this.active = active;
  463. }
  464. }
  465. }