KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jacorb > orb > TaggedComponentList


1
2 package org.jacorb.orb;
3
4 import java.lang.reflect.*;
5 import java.util.*;
6
7 import org.omg.IOP.*;
8
9 /**
10  * Represents a list of tagged components from an IOR, along with some
11  * generic methods to find and access individual components.
12  * <p>
13  * @author Andre Spiegel
14  * @version $Id: TaggedComponentList.java,v 1.7 2003/05/23 13:36:16 andre.spiegel Exp $
15  */

16 public class TaggedComponentList implements Cloneable JavaDoc
17 {
18     private TaggedComponent[] components = null;
19     
20     /**
21      * Constructs a TaggedComponentList object from a CDR representation
22      * of an array of tagged components.
23      */

24     public TaggedComponentList (org.omg.CORBA.portable.InputStream JavaDoc in)
25     {
26         components = TaggedComponentSeqHelper.read (in);
27     }
28     
29     /**
30      * Constructs a TaggedComponentList from a CDR encapsulation of
31      * an array of tagged components.
32      */

33     public TaggedComponentList (byte[] data)
34     {
35         CDRInputStream in = new CDRInputStream (null, data);
36         in.openEncapsulatedArray();
37         components = TaggedComponentSeqHelper.read (in);
38     }
39     
40     /**
41      * Constructs a new, empty TaggedComponentList.
42      */

43     public TaggedComponentList()
44     {
45         components = new TaggedComponent[0];
46     }
47     
48     public int size()
49     {
50         return components.length;
51     }
52     
53     public boolean isEmpty()
54     {
55         return components.length == 0;
56     }
57     
58     public TaggedComponent get (int index)
59     {
60         return components[index];
61     }
62     
63     public Object JavaDoc clone() throws CloneNotSupportedException JavaDoc
64     {
65         TaggedComponentList result = (TaggedComponentList)super.clone();
66         result.components = new TaggedComponent[this.components.length];
67         for (int i=0; i<this.components.length; i++)
68         {
69             result.components[i] = new TaggedComponent
70             (
71                 this.components[i].tag,
72                 new byte [this.components[i].component_data.length]
73             );
74             System.arraycopy (this.components[i].component_data, 0,
75                               result.components[i].component_data, 0,
76                               this.components[i].component_data.length);
77         }
78         return result;
79     }
80     
81     public TaggedComponent[] asArray()
82     {
83         return components;
84     }
85     
86     /**
87      * Adds a tagged component to this list. The component's data
88      * is created by marshaling the given data Object using the
89      * write() method of the given helper class.
90      */

91     public void addComponent (int tag, Object JavaDoc data, Class JavaDoc helper)
92     {
93         try
94         {
95             Method writeMethod = helper.getMethod
96             (
97                 "write",
98                 new Class JavaDoc[]
99                 {
100                     org.omg.CORBA.portable.OutputStream JavaDoc.class,
101                     data.getClass()
102                 }
103             );
104             CDROutputStream out = new CDROutputStream();
105             out.beginEncapsulatedArray();
106             writeMethod.invoke
107             (
108                 null,
109                 new Object JavaDoc[]{ out, data }
110             );
111             addComponent (tag, out.getBufferCopy());
112         }
113         catch (NoSuchMethodException JavaDoc ex)
114         {
115             throw new RuntimeException JavaDoc ("Helper " + helper.getName()
116                                         + " has no appropriate write() method.");
117         }
118         catch (IllegalAccessException JavaDoc ex)
119         {
120             throw new RuntimeException JavaDoc ("Cannot access write() method of helper "
121                                         + helper.getName());
122         }
123         catch (InvocationTargetException ex)
124         {
125             throw new RuntimeException JavaDoc ("Exception while marshaling component data: "
126                                         + ex.getTargetException());
127         }
128     }
129     
130     /**
131      * Adds a tagged component to this list.
132      */

133     public void addComponent (int tag, byte[] data)
134     {
135         addComponent (new TaggedComponent (tag, data));
136     }
137
138     /**
139      * Adds a tagged component to this list.
140      */

141     public void addComponent (TaggedComponent component)
142     {
143         TaggedComponent[] newComponents =
144             new TaggedComponent [components.length + 1];
145         System.arraycopy (components, 0, newComponents, 0, components.length);
146         newComponents [components.length] = component;
147         components = newComponents;
148     }
149     
150     /**
151      * Adds an entire TaggedComponentList to this list.
152      */

153     public void addAll (TaggedComponentList other)
154     {
155         TaggedComponent[] newComponents =
156             new TaggedComponent [components.length + other.components.length];
157         System.arraycopy (components, 0, newComponents, 0, components.length);
158         System.arraycopy (other.components, 0, newComponents, components.length,
159                           other.components.length);
160         components = newComponents;
161     }
162     
163     /**
164      * Searches for a component with the given tag in this component list.
165      * If one is found, this method reads the corresponding data with the given
166      * helper class, and returns the resulting object, otherwise returns
167      * null.
168      */

169     public Object JavaDoc getComponent (int tag, Class JavaDoc helper)
170     {
171         for (int i=0; i < components.length; i++)
172         {
173             if (components[i].tag == tag)
174             {
175                 return getComponentData (components[i].component_data, helper);
176             }
177         }
178         return null;
179     }
180
181     /**
182      * Returns the first component with the given tag, which is assumed
183      * to be a CDR string. If no component with the given tag exists,
184      * returns null.
185      */

186     public String JavaDoc getStringComponent (int tag)
187     {
188         for (int i=0; i < components.length; i++)
189         {
190             if (components[i].tag == tag)
191             {
192                 CDRInputStream in =
193                     new CDRInputStream (null,
194                                         components[i].component_data);
195                 in.openEncapsulatedArray();
196                 return in.read_string();
197             }
198         }
199         return null;
200     }
201     
202     /**
203      * Returns a List of all components with the given tag from this
204      * TaggedComponentList. Each individual component is read with
205      * the given helper class. If no components with the given tag
206      * can be found, an empty list is returned.
207      */

208     public List getComponents (int tag, Class JavaDoc helper)
209     {
210         List result = new ArrayList();
211         for (int i=0; i < components.length; i++)
212         {
213             if (components[i].tag == tag)
214             {
215                 result.add (getComponentData (components[i].component_data,
216                                               helper));
217             }
218         }
219         return result;
220     }
221     
222     /**
223      * Uses the given helper class to read a CDR-encapsulated component_data
224      * field from the given byte array, data.
225      */

226     private Object JavaDoc getComponentData (byte[] data, Class JavaDoc helper)
227     {
228         try
229         {
230             Method readMethod =
231                 helper.getMethod ("read",
232                                   new Class JavaDoc[] { org.omg.CORBA.portable.InputStream JavaDoc.class });
233             CDRInputStream in = new CDRInputStream (null, data);
234             in.openEncapsulatedArray();
235             return readMethod.invoke (null, new Object JavaDoc[] { in });
236         }
237         catch (NoSuchMethodException JavaDoc ex)
238         {
239             throw new RuntimeException JavaDoc ("Helper " + helper.getName()
240                                         + " has no appropriate read() method.");
241         }
242         catch (IllegalAccessException JavaDoc ex)
243         {
244             throw new RuntimeException JavaDoc ("Cannot access read() method of helper "
245                                         + helper.getName());
246         }
247         catch (InvocationTargetException ex)
248         {
249             throw new RuntimeException JavaDoc ("Exception while reading component data: "
250                                         + ex.getTargetException());
251         }
252     }
253
254 }
255
Popular Tags