KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > media > registry > ReflectionMediaFormatRegistry


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.registry;
9
10 import java.util.Iterator JavaDoc;
11 import java.util.Map JavaDoc;
12
13 import javax.emb.FormatAlreadyBoundException;
14 import javax.emb.MediaFormat;
15
16 /**
17  * A reflection based implementation of a <code>MediaFormat</code> registry.
18  *
19  * <p>It allows to declare <code>MediaFormat</code> instances as class names.
20  *
21  * @version <tt>$Revision: 1.1 $</tt>
22  * @author <a HREF="mailto:ricardoarguello@users.sourceforge.net">Ricardo ArgŁello</a>
23  */

24 public class ReflectionMediaFormatRegistry extends SimpleMediaFormatRegistry
25 {
26    public ReflectionMediaFormatRegistry()
27    {
28       super();
29    }
30
31    public ReflectionMediaFormatRegistry(Map JavaDoc initialEntries)
32    {
33       this();
34
35       Iterator JavaDoc it = initialEntries.keySet().iterator();
36
37       while (it.hasNext())
38       {
39          String JavaDoc fileExtension = (String JavaDoc) it.next();
40
41          String JavaDoc mediaFormatClassName =
42             (String JavaDoc) initialEntries.get(fileExtension);
43
44          try
45          {
46             this.bind(fileExtension, mediaFormatClassName);
47          }
48          catch (FormatAlreadyBoundException ignore)
49          {
50          }
51       }
52
53    }
54
55    public void bind(String JavaDoc fileExtension, String JavaDoc mediaFormatClassName)
56       throws FormatAlreadyBoundException
57    {
58       try
59       {
60          Class JavaDoc mediaFormatClass = Class.forName(mediaFormatClassName);
61
62          // FIXME: This won't work, since IIO MediaFormats have constructors
63
// with an ImageReader as parameter!
64
MediaFormat mediaFormat = (MediaFormat) mediaFormatClass.newInstance();
65
66          super.bind(fileExtension, mediaFormat);
67       }
68       catch (ClassNotFoundException JavaDoc e)
69       {
70          throw new IllegalArgumentException JavaDoc(e.getMessage());
71       }
72       catch (InstantiationException JavaDoc e)
73       {
74          throw new IllegalArgumentException JavaDoc(e.getMessage());
75       }
76       catch (IllegalAccessException JavaDoc e)
77       {
78          throw new IllegalArgumentException JavaDoc(e.getMessage());
79       }
80    }
81
82    public void rebind(String JavaDoc fileExtension, String JavaDoc mediaFormatClassName)
83    {
84       try
85       {
86          Class JavaDoc mediaFormatClass = Class.forName(mediaFormatClassName);
87          MediaFormat mediaFormat = (MediaFormat) mediaFormatClass.newInstance();
88          super.rebind(fileExtension, mediaFormat);
89       }
90       catch (ClassNotFoundException JavaDoc e)
91       {
92          throw new IllegalArgumentException JavaDoc(e.getMessage());
93       }
94       catch (InstantiationException JavaDoc e)
95       {
96          throw new IllegalArgumentException JavaDoc(e.getMessage());
97       }
98       catch (IllegalAccessException JavaDoc e)
99       {
100          throw new IllegalArgumentException JavaDoc(e.getMessage());
101       }
102    }
103 }
104
Popular Tags