KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > media > engine > MediaXmlLoader


1 /*
2  * JBoss, the OpenSource J2EE webOS
3  *
4  * Distributable under LGPL license.
5  * See terms of license at gnu.org.
6  */

7
8 package org.jboss.media.engine;
9
10 import java.net.URL JavaDoc;
11 import java.util.Iterator JavaDoc;
12 import java.util.Vector JavaDoc;
13
14 import javax.management.Attribute JavaDoc;
15 import javax.management.AttributeNotFoundException JavaDoc;
16 import javax.management.InstanceNotFoundException JavaDoc;
17 import javax.management.InvalidAttributeValueException JavaDoc;
18 import javax.management.MBeanException JavaDoc;
19 import javax.management.MBeanServer JavaDoc;
20 import javax.management.MalformedObjectNameException JavaDoc;
21 import javax.management.ObjectName JavaDoc;
22 import javax.management.ReflectionException JavaDoc;
23
24 import org.jboss.deployment.DeploymentException;
25 import org.jboss.logging.Logger;
26 import org.jboss.media.emb.CaptureMetaData;
27 import org.jboss.mx.util.MBeanProxy;
28 import org.jboss.mx.util.MBeanProxyCreationException;
29 import org.w3c.dom.DOMException JavaDoc;
30 import org.w3c.dom.Document JavaDoc;
31 import org.w3c.dom.Element JavaDoc;
32 import org.w3c.dom.Node JavaDoc;
33 import org.w3c.dom.NodeList JavaDoc;
34
35 /**
36  * @version <tt>$Revision: 1.3 $</tt>
37  * @author <a HREF="mailto:spyridon_samothrakis@yahoo.com">Spyridon Samothrakis</a>
38  */

39 public class MediaXmlLoader
40 {
41    public static final String JavaDoc PUBLISHER_FACTORY_TAG = "publisher-factory";
42
43    public static final String JavaDoc PUBLISHER_TAG = "publisher";
44    public static final String JavaDoc PUBLISHER_CLASS = "class";
45    public static final String JavaDoc PUBLISHER_HOST = "host";
46    public static final String JavaDoc PUBLISHER_PORT = "port";
47    public static final String JavaDoc PUBLISHER_CONTEXT = "context";
48    public static final String JavaDoc PUBLISHER_START = "true";
49
50    public static final String JavaDoc TRANCODER_TAG = "transcoder-name";
51    public static final String JavaDoc PLUGIN_REGISTRY_TAG = "plugin-registry-name";
52
53    public static final String JavaDoc PLUGIN_GRAPH_TAG = "plugin-graph";
54    public static final String JavaDoc PLUGIN_TAG = "plugin";
55    public static final String JavaDoc PLUGIN_NAME_ATT = "name";
56
57    public static final String JavaDoc CAPTURE_DEVICE_TAG = "capture";
58    public static final String JavaDoc CAPTURE_DEVICE_NAME_ATT = "device";
59    public static final String JavaDoc CAPTURA_DEVICE_TYPE_ATT = "type";
60
61    public static final String JavaDoc SOURCE = "source";
62    public static final String JavaDoc FILE_NAME = "file-name";
63
64    private static Logger log = Logger.getLogger(MediaXmlLoader.class);
65
66    public static boolean deployXML(Document JavaDoc doc, URL JavaDoc url, MBeanServer JavaDoc server)
67       throws
68          MalformedObjectNameException JavaDoc,
69          DOMException JavaDoc,
70          DeploymentException,
71          MediaPluginGraphException
72    {
73
74       ObjectName JavaDoc m_factory;
75
76       ObjectName JavaDoc m_name;
77       MediaPluginRegistryMBean reg = null;
78
79       m_factory = null;
80
81       String JavaDoc publisher = "";
82       String JavaDoc publisher_class = "";
83       String JavaDoc publisher_host = "";
84       String JavaDoc publisher_port = "";
85       String JavaDoc publisher_context = "";
86
87       ObjectName JavaDoc transcoder = null;
88
89       ObjectName JavaDoc pluginRegistry = null;
90       MediaPluginGraph pluginGraph = null;
91
92       CaptureMetaData captureMetadata = null;
93
94       String JavaDoc source = null;
95
96       int pluginNum = 0;
97
98       Vector JavaDoc extraAttributes = new Vector JavaDoc();
99       boolean shouldStart = false;
100
101       NodeList JavaDoc children = doc.getDocumentElement().getChildNodes();
102       for (int i = 0; i < children.getLength(); i++)
103       {
104          if (children.item(i).getNodeType() == Node.ELEMENT_NODE)
105          {
106             Element JavaDoc element = (Element JavaDoc) children.item(i);
107             String JavaDoc tag = element.getTagName();
108
109             if (tag.equals(PUBLISHER_FACTORY_TAG))
110             {
111                m_factory =
112                   new ObjectName JavaDoc(
113                      element.getChildNodes().item(0).getNodeValue());
114
115             }
116
117             else if (tag.equals(PUBLISHER_TAG))
118             {
119                publisher_class = element.getAttribute(PUBLISHER_CLASS);
120                publisher_host = element.getAttribute(PUBLISHER_HOST);
121                publisher_port = element.getAttribute(PUBLISHER_PORT);
122                publisher_context = element.getAttribute(PUBLISHER_CONTEXT);
123
124                NodeList JavaDoc attributes = element.getChildNodes();
125
126                // seek the extra attributes and set them
127
for (int k = 0; k < attributes.getLength(); k++)
128                {
129                   if (attributes.item(k).getNodeType() == Node.ELEMENT_NODE)
130                   {
131                      Element JavaDoc attribute = (Element JavaDoc) attributes.item(k);
132                      String JavaDoc name = attribute.getAttribute("name");
133                      String JavaDoc value =
134                         attribute.getChildNodes().item(0).getNodeValue();
135                      // soooooo lame
136
Attribute JavaDoc at = null;
137                      try
138                      {
139
140                         at = new Attribute JavaDoc(name, Integer.decode(value));
141                      }
142                      catch (NumberFormatException JavaDoc e)
143                      {
144                         at = new Attribute JavaDoc(name, value);
145                      }
146
147                      //gotcha!!!
148
log.info(name + "=" + value);
149                      extraAttributes.add(at);
150                   }
151
152                }
153
154                if (element.getAttribute("start").equals("true"))
155                {
156                   shouldStart = true;
157                }
158
159             }
160
161             else if (tag.equals(TRANCODER_TAG))
162             {
163                // transcoder = new ObjectName(element.getChildNodes().item(0).getNodeValue());
164
}
165
166             else if (tag.equals(PLUGIN_REGISTRY_TAG))
167             {
168                pluginRegistry =
169                   new ObjectName JavaDoc(
170                      element.getChildNodes().item(0).getNodeValue());
171
172                try
173                {
174                   reg =
175                      (MediaPluginRegistryMBean) MBeanProxy.create(
176                         MediaPluginRegistry.class,
177                         MediaPluginRegistryMBean.class,
178                         pluginRegistry,
179                         server);
180                }
181                catch (MBeanProxyCreationException e)
182                {
183                   // TODO: Added just to compile!
184
log.error("MBeanProxyCreationException", e);
185                }
186             }
187
188             else if (tag.equals(PLUGIN_GRAPH_TAG))
189             {
190                pluginGraph = new MediaPluginGraph();
191                NodeList JavaDoc plugins = element.getChildNodes();
192
193                for (int k = 0; k < plugins.getLength(); k++)
194                {
195                   if (plugins.item(k).getNodeType() == Node.ELEMENT_NODE)
196                   {
197                      Element JavaDoc pluginElement = (Element JavaDoc) plugins.item(k);
198                      String JavaDoc plugin = pluginElement.getTagName();
199
200                      // if (plugin.equals(PLUGIN_TAG))
201
//{
202

203                      String JavaDoc pluginName =
204                         pluginElement.getAttribute(PLUGIN_NAME_ATT);
205                      log.info("plugin " + pluginNum + "is " + pluginName);
206                      pluginGraph.addPlugin(
207                         reg.getPlugin(pluginName),
208                         pluginNum);
209                      pluginNum++;
210
211                      //}
212
}
213                }
214             }
215
216             else if (tag.equals(CAPTURE_DEVICE_TAG))
217             {
218                //captureMetadata.setDeviceName();
219
}
220
221             else if (tag.equals(SOURCE))
222             {
223                source = element.getAttribute(FILE_NAME);
224             }
225
226             else
227             {
228                log.warn("Unknown EMB tag: " + tag);
229             }
230          }
231       }
232
233       log.info(m_factory);
234       log.info(publisher);
235       log.info(publisher_class);
236       log.info(publisher_host);
237       log.info(publisher_port);
238       log.info(publisher_context);
239
240       log.info(transcoder);
241
242       log.info(pluginRegistry);
243       log.info(pluginGraph);
244
245       log.info(captureMetadata);
246
247       log.info(source);
248
249       MediaPublisherFactoryMBean pub = null;
250
251       try
252       {
253          // get the media publisher
254
pub =
255             (MediaPublisherFactoryMBean) MBeanProxy.create(
256                MediaPublisherFactory.class,
257                MediaPublisherFactoryMBean.class,
258                m_factory,
259                server);
260       }
261       catch (MBeanProxyCreationException e)
262       {
263          // TODO: Added just to compile!
264
log.error("MBeanProxyCreationException", e);
265       }
266
267       ObjectName JavaDoc result =
268          pub.createPublisher(
269             publisher_class,
270             publisher_context,
271             publisher_host,
272             Integer.parseInt(publisher_port),
273             publisher,
274             source);
275
276       for (Iterator JavaDoc iter = extraAttributes.iterator(); iter.hasNext();)
277       {
278          Attribute JavaDoc at = (Attribute JavaDoc) iter.next();
279
280          try
281          {
282             server.setAttribute(result, at);
283          }
284          catch (InstanceNotFoundException JavaDoc e)
285          {
286             log.error(e);
287          }
288          catch (AttributeNotFoundException JavaDoc e)
289          {
290             log.error(e);
291          }
292          catch (InvalidAttributeValueException JavaDoc e)
293          {
294             log.error(e);
295          }
296          catch (MBeanException JavaDoc e)
297          {
298             log.error(e);
299          }
300          catch (ReflectionException JavaDoc e)
301          {
302             log.error(e);
303          }
304
305       }
306
307       MediaPublisherMBean pubObj = null;
308
309       try
310       {
311          pubObj =
312             (MediaPublisherMBean) MBeanProxy.create(
313                MediaPublisher.class,
314                MediaPublisherMBean.class,
315                result,
316                server);
317       }
318       catch (MBeanProxyCreationException e)
319       {
320          // TODO: Added just to compile!
321
log.error("MBeanProxyCreationException", e);
322       }
323
324       pubObj.addPluginGraph(pluginGraph);
325
326       if (shouldStart)
327       {
328          try
329          {
330             pubObj.publish();
331          }
332          catch (Exception JavaDoc e)
333          {
334             log.error("Could not start publisher", e);
335          }
336       }
337
338       return true;
339    }
340
341    /**
342     * @param document
343     * @param url
344     * @param server
345     */

346    public static void undeploy(Document JavaDoc doc, URL JavaDoc url, MBeanServer JavaDoc server)
347       throws MalformedObjectNameException JavaDoc, NullPointerException JavaDoc, DOMException JavaDoc
348    {
349       String JavaDoc publisher_class = "";
350       String JavaDoc publisher = "";
351       String JavaDoc publisher_context = "";
352
353       ObjectName JavaDoc m_factory = null;
354
355       NodeList JavaDoc children = doc.getDocumentElement().getChildNodes();
356       for (int i = 0; i < children.getLength(); i++)
357       {
358          if (children.item(i).getNodeType() == Node.ELEMENT_NODE)
359          {
360             Element JavaDoc element = (Element JavaDoc) children.item(i);
361             String JavaDoc tag = element.getTagName();
362
363             if (tag.equals(PUBLISHER_FACTORY_TAG))
364             {
365                m_factory =
366                   new ObjectName JavaDoc(
367                      element.getChildNodes().item(0).getNodeValue());
368
369             }
370
371             else if (tag.equals(PUBLISHER_TAG))
372             {
373                publisher = element.getChildNodes().item(0).getNodeValue();
374                publisher_class = element.getAttribute(PUBLISHER_CLASS);
375                publisher_context = element.getAttribute(PUBLISHER_CONTEXT);
376             }
377          }
378       }
379
380       MediaPublisherFactoryMBean pub = null;
381
382       try
383       {
384          // get the media publisher
385
pub =
386             (MediaPublisherFactoryMBean) MBeanProxy.create(
387                MediaPublisherFactory.class,
388                MediaPublisherFactoryMBean.class,
389                m_factory,
390                server);
391       }
392       catch (MBeanProxyCreationException e)
393       {
394          // TODO: Added just to compile!
395
log.error("MBeanProxyCreationException", e);
396       }
397
398       ObjectName JavaDoc m_name = pub.getObjectName(publisher_class, publisher_context);
399       pub.destroyPublisher(m_name);
400
401    }
402 }
403
Popular Tags