1 2 package org.jacorb.orb; 3 4 import java.lang.reflect.*; 5 import java.util.*; 6 7 import org.omg.IOP.*; 8 9 16 public class TaggedComponentList implements Cloneable 17 { 18 private TaggedComponent[] components = null; 19 20 24 public TaggedComponentList (org.omg.CORBA.portable.InputStream in) 25 { 26 components = TaggedComponentSeqHelper.read (in); 27 } 28 29 33 public TaggedComponentList (byte[] data) 34 { 35 CDRInputStream in = new CDRInputStream (null, data); 36 in.openEncapsulatedArray(); 37 components = TaggedComponentSeqHelper.read (in); 38 } 39 40 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 clone() throws CloneNotSupportedException 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 91 public void addComponent (int tag, Object data, Class helper) 92 { 93 try 94 { 95 Method writeMethod = helper.getMethod 96 ( 97 "write", 98 new Class [] 99 { 100 org.omg.CORBA.portable.OutputStream .class, 101 data.getClass() 102 } 103 ); 104 CDROutputStream out = new CDROutputStream(); 105 out.beginEncapsulatedArray(); 106 writeMethod.invoke 107 ( 108 null, 109 new Object []{ out, data } 110 ); 111 addComponent (tag, out.getBufferCopy()); 112 } 113 catch (NoSuchMethodException ex) 114 { 115 throw new RuntimeException ("Helper " + helper.getName() 116 + " has no appropriate write() method."); 117 } 118 catch (IllegalAccessException ex) 119 { 120 throw new RuntimeException ("Cannot access write() method of helper " 121 + helper.getName()); 122 } 123 catch (InvocationTargetException ex) 124 { 125 throw new RuntimeException ("Exception while marshaling component data: " 126 + ex.getTargetException()); 127 } 128 } 129 130 133 public void addComponent (int tag, byte[] data) 134 { 135 addComponent (new TaggedComponent (tag, data)); 136 } 137 138 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 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 169 public Object getComponent (int tag, Class 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 186 public String 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 208 public List getComponents (int tag, Class 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 226 private Object getComponentData (byte[] data, Class helper) 227 { 228 try 229 { 230 Method readMethod = 231 helper.getMethod ("read", 232 new Class [] { org.omg.CORBA.portable.InputStream .class }); 233 CDRInputStream in = new CDRInputStream (null, data); 234 in.openEncapsulatedArray(); 235 return readMethod.invoke (null, new Object [] { in }); 236 } 237 catch (NoSuchMethodException ex) 238 { 239 throw new RuntimeException ("Helper " + helper.getName() 240 + " has no appropriate read() method."); 241 } 242 catch (IllegalAccessException ex) 243 { 244 throw new RuntimeException ("Cannot access read() method of helper " 245 + helper.getName()); 246 } 247 catch (InvocationTargetException ex) 248 { 249 throw new RuntimeException ("Exception while reading component data: " 250 + ex.getTargetException()); 251 } 252 } 253 254 } 255 | Popular Tags |