/servers/media/core/server-impl/src/main/java/org/mobicents/media/server/resource/ChannelFactory.java

http://mobicents.googlecode.com/ · Java · 219 lines · 117 code · 23 blank · 79 comment · 15 complexity · 3fb01cb7d4c7aba9bdc66890e647726c MD5 · raw file

  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.media.server.resource;
  19. import java.util.ArrayList;
  20. import java.util.Collection;
  21. import java.util.HashMap;
  22. import java.util.List;
  23. import org.apache.log4j.Logger;
  24. import org.mobicents.media.Component;
  25. import org.mobicents.media.ComponentFactory;
  26. import org.mobicents.media.Inlet;
  27. import org.mobicents.media.MediaSink;
  28. import org.mobicents.media.MediaSource;
  29. import org.mobicents.media.Outlet;
  30. import org.mobicents.media.server.spi.Endpoint;
  31. import org.mobicents.media.server.spi.MediaType;
  32. import org.mobicents.media.server.spi.MultimediaSink;
  33. import org.mobicents.media.server.spi.MultimediaSource;
  34. import org.mobicents.media.server.spi.ResourceUnavailableException;
  35. /**
  36. * Factory class for creating channels.
  37. *
  38. * @author kulikov
  39. */
  40. public class ChannelFactory {
  41. private List<PipeFactory> pipes;
  42. private List<ComponentFactory> factories;
  43. private MediaType mediaType;
  44. private volatile boolean started = false;
  45. private static final Logger logger = Logger.getLogger(ChannelFactory.class);
  46. /**
  47. * Returns new channel.
  48. *
  49. * if there is unused channels in the cache the existing channels will be
  50. * returned and new instance other wise.
  51. *
  52. * @return
  53. * @throws org.mobicents.media.server.resource.UnknownComponentException
  54. */
  55. public Channel newInstance(Endpoint endpoint, MediaType media) throws ResourceUnavailableException{
  56. if (!started) {
  57. throw new IllegalStateException("Factory is not started");
  58. }
  59. return createNewChannel(endpoint,media);
  60. }
  61. /**
  62. * Constructs new channel instance.
  63. * @param media
  64. *
  65. * @return channel instance.
  66. * @throws org.mobicents.media.server.resource.UnknownComponentException
  67. */
  68. private Channel createNewChannel(Endpoint endpoint, MediaType media) throws ResourceUnavailableException {
  69. //creating components
  70. HashMap<String, MediaSource> sources = new HashMap<String, MediaSource>();
  71. HashMap<String, MediaSink> sinks = new HashMap<String, MediaSink>();
  72. HashMap<String, Inlet> inlets = new HashMap<String, Inlet>();
  73. HashMap<String, Outlet> outlets = new HashMap<String, Outlet>();
  74. for (ComponentFactory factory: factories) {
  75. Component component = factory.newInstance(endpoint);
  76. if(component instanceof MultimediaSource)
  77. {
  78. MultimediaSource ms = (MultimediaSource) component;
  79. Collection<MediaType> supportedTypes=ms.getMediaTypes();
  80. if(supportedTypes.contains(media))
  81. {
  82. component = ms.getMediaSource(media);
  83. }else
  84. {
  85. //?
  86. continue;
  87. }
  88. }
  89. if (component instanceof MultimediaSink) {
  90. MultimediaSink ms = (MultimediaSink) component;
  91. Collection<MediaType> supportedTypes=ms.getMediaTypes();
  92. if(supportedTypes.contains(media))
  93. {
  94. component = ms.getMediaSink(media);
  95. }else
  96. {
  97. //?
  98. continue;
  99. }
  100. }
  101. if (component instanceof MediaSink) {
  102. sinks.put(component.getName(), (MediaSink)component);
  103. }
  104. if (component instanceof MediaSource) {
  105. sources.put(component.getName(),(MediaSource)component);
  106. }
  107. if (component instanceof Inlet) {
  108. sinks.put(component.getName(), ((Inlet)component).getInput());
  109. inlets.put(component.getName(), (Inlet)component);
  110. }
  111. if (component instanceof Outlet) {
  112. sources.put(component.getName(), ((Outlet)component).getOutput());
  113. outlets.put(component.getName(), (Outlet)component);
  114. }
  115. }
  116. Channel channel = new Channel(sources, sinks, inlets, outlets);
  117. //creating pipes
  118. for (PipeFactory pipeFactory : pipes) {
  119. try {
  120. pipeFactory.openPipe(channel);
  121. } catch (UnknownComponentException e) {
  122. throw new ResourceUnavailableException(e);
  123. }
  124. }
  125. return channel;
  126. }
  127. /**
  128. * Modify pipe list.
  129. *
  130. * @param pipes the list of pipes beans defining media flow path
  131. */
  132. public void setPipes(List<PipeFactory> pipes) {
  133. this.pipes = pipes;
  134. }
  135. /**
  136. * Gets the existing list of pipes.
  137. *
  138. * @return the list of pipes.
  139. */
  140. public List getPipes() {
  141. return this.pipes;
  142. }
  143. /**
  144. * Gets the list of components which will be placed into the new channel.
  145. *
  146. * @return the list of media component.
  147. */
  148. public List getComponents() {
  149. return factories;
  150. }
  151. /**
  152. * Sets the list of components which will be placed into the new channel.
  153. *
  154. * @return the list of media component.
  155. */
  156. public void setComponents(List components) {
  157. this.factories = components;
  158. }
  159. /**
  160. * Gets the media type of the channel generated by this factory.
  161. *
  162. * @return the media type identifier
  163. */
  164. public MediaType getMediaType() {
  165. return mediaType;
  166. }
  167. /**
  168. * Assigns media type which will be used by this factory for generation channels
  169. *
  170. * @param mediaType media type identifier.
  171. */
  172. public void setMediaType(MediaType mediaType) {
  173. this.mediaType = mediaType;
  174. }
  175. /**
  176. * Starts this factory.
  177. *
  178. */
  179. public void start() throws Exception {
  180. started = true;
  181. if (factories == null) {
  182. factories = new ArrayList();
  183. }
  184. if (pipes == null) {
  185. pipes = new ArrayList();
  186. }
  187. }
  188. /**
  189. * Stop this factory.
  190. */
  191. public void stop() {
  192. started = false;
  193. pipes.clear();
  194. factories.clear();
  195. }
  196. }