1 2 5 14 package org.jacorb.trading.impl; 15 16 import java.util.*; 17 import org.omg.CORBA.*; 18 import org.omg.CosTrading.*; 19 import org.omg.CosTrading.LookupPackage.*; 20 import org.omg.CosTrading.RegisterPackage.*; 21 import org.omg.DynamicAny.*; 22 import org.omg.DynamicAny.DynSequence ; 23 import org.jacorb.trading.constraint.*; 24 import org.jacorb.trading.util.*; 25 26 29 30 public class SourceAdapter implements PropertySource 31 { 32 private Hashtable m_propTable; 33 private Hashtable m_values; 34 private Hashtable m_propValues; 35 private org.omg.CORBA.Object m_object; 36 private Property[] m_props; 37 38 private org.omg.CORBA.ORB orb; 39 40 41 protected SourceAdapter() 42 { 43 } 44 45 46 public SourceAdapter(org.omg.CORBA.Object object, Property[] props) 47 { 48 orb = ((org.omg.CORBA.portable.ObjectImpl )object)._orb(); 49 50 m_object = object; 51 m_props = props; 52 53 m_propTable = new Hashtable(); 54 m_values = new Hashtable(); 56 m_propValues = new Hashtable(); 58 59 for (int i = 0; i < props.length; i++) 61 m_propTable.put(props[i].name, props[i]); 62 } 63 64 65 public org.omg.CORBA.Object getObject() 66 { 67 return m_object; 68 } 69 70 71 public Property[] getProperties() 72 { 73 return m_props; 74 } 75 76 77 public Property[] getProperties(SpecifiedProps desired_props) 78 { 79 Property[] result = null; 80 81 84 if (desired_props.discriminator() == HowManyProps.all) { 85 Vector vec = new Vector(); 86 for (int i = 0; i < m_props.length; i++) { 87 Any value = getPropertyValue(m_props[i].name); 88 if (value != null) 89 vec.addElement(new Property(m_props[i].name, value)); 90 } 91 result = new Property[vec.size()]; 92 vec.copyInto((java.lang.Object [])result); 93 } 94 else if (desired_props.discriminator() == HowManyProps.some) { 95 String [] names = desired_props.prop_names(); 96 Vector vec = new Vector(); 97 for (int i = 0; i < names.length; i++) { 98 Any value = getPropertyValue(names[i]); 99 if (value != null) 100 vec.addElement(new Property(names[i], value)); 101 } 102 result = new Property[vec.size()]; 103 vec.copyInto((java.lang.Object [])result); 104 } 105 else if (desired_props.discriminator() == HowManyProps.none) 106 result = new Property[0]; 107 108 return result; 109 } 110 111 112 public boolean exists(String property) 113 { 114 return m_propTable.containsKey(property); 115 } 116 117 118 public Value getValue(String property) 119 { 120 Value result = null; 121 122 java.lang.Object v = m_values.get(property); 124 125 if (v == null) { 126 Property p = (Property)m_propTable.get(property); 127 if (p == null) 128 return null; 129 130 Any val = getPropertyValue(property); 133 134 if (val == null) 135 return null; 136 137 m_propValues.put(property, val); 139 140 try { 141 TypeCode tc = val.type(); 142 143 while (tc.kind() == TCKind.tk_alias) 144 tc = tc.content_type(); 145 146 TCKind kind = tc.kind(); 147 if (kind == TCKind.tk_sequence) 148 return null; 149 150 switch (kind.value()) { 151 case TCKind._tk_short: { 152 int s = val.extract_short(); 153 result = ValueFactory.createShort(s); 154 } 155 break; 156 157 case TCKind._tk_long: { 158 int l = val.extract_long(); 159 result = ValueFactory.createLong(l); 160 } 161 break; 162 163 case TCKind._tk_ushort: { 164 int i = val.extract_short(); 165 result = ValueFactory.createUShort(i); 166 } 167 break; 168 169 case TCKind._tk_ulong: { 170 long l = val.extract_ulong(); 171 result = ValueFactory.createULong(l); 172 } 173 break; 174 175 case TCKind._tk_float: { 176 float f = val.extract_float(); 177 result = ValueFactory.createFloat(f); 178 } 179 break; 180 181 case TCKind._tk_double: { 182 double d = val.extract_double(); 183 result = ValueFactory.createDouble(d); 184 } 185 break; 186 187 case TCKind._tk_boolean: { 188 boolean b = val.extract_boolean(); 189 result = ValueFactory.createBoolean(b); 190 } 191 break; 192 193 case TCKind._tk_char: { 194 char c = val.extract_char(); 195 result = ValueFactory.createChar(c); 196 } 197 break; 198 199 case TCKind._tk_string: { 200 String s = val.extract_string(); 201 result = ValueFactory.createString(s); 202 } 203 break; 204 } 205 } 206 catch (org.omg.CORBA.TypeCodePackage.BadKind e) { 207 throw new RuntimeException (); 208 } 209 catch (BAD_OPERATION e) { 210 throw new RuntimeException (); 211 } 212 213 m_values.put(property, result); 215 } 216 else { 217 if (v instanceof Value) 219 result = (Value)v; 220 } 221 222 return result; 223 } 224 225 226 public Value[] getSequenceValues(String property) 227 { 228 Value[] result = null; 229 230 java.lang.Object v = m_values.get(property); 232 233 if (v == null) { 234 235 Property p = (Property)m_propTable.get(property); 236 if (p == null) 237 return null; 238 239 Any val = getPropertyValue(property); 242 243 if (val == null) 244 return null; 245 246 m_propValues.put(property, val); 248 249 try { 250 TypeCode tc = val.type(); 251 252 while (tc.kind() == TCKind.tk_alias) 254 tc = tc.content_type(); 255 256 if (tc.kind() != TCKind.tk_sequence) 257 return null; 258 259 tc = tc.content_type(); 261 TCKind kind = tc.kind(); 262 263 org.omg.DynamicAny.DynAny da = DynAnyFactoryHelper.narrow(orb.resolve_initial_references("DynAnyFactory")).create_dyn_any(val); 264 DynSequence ds = DynSequenceHelper.narrow(da); 265 int len = ds.get_length(); 266 result = new Value[len]; 268 269 switch (kind.value()) { 270 case TCKind._tk_short: { 271 for (int i = 0; i < len; i++, ds.next()) 272 result[i] = ValueFactory.createShort(ds.get_short()); 273 } 274 break; 275 276 case TCKind._tk_long: { 277 for (int i = 0; i < len; i++, ds.next()) 278 result[i] = ValueFactory.createLong(ds.get_long()); 279 } 280 break; 281 282 case TCKind._tk_ushort: { 283 for (int i = 0; i < len; i++, ds.next()) 284 result[i] = ValueFactory.createUShort(ds.get_ushort()); 285 } 286 break; 287 288 case TCKind._tk_ulong: { 289 for (int i = 0; i < len; i++, ds.next()) 290 result[i] = ValueFactory.createULong(ds.get_ulong()); 291 } 292 break; 293 294 case TCKind._tk_float: { 295 for (int i = 0; i < len; i++, ds.next()) 296 result[i] = ValueFactory.createFloat(ds.get_float()); 297 } 298 break; 299 300 case TCKind._tk_double: { 301 for (int i = 0; i < len; i++, ds.next()) 302 result[i] = ValueFactory.createDouble(ds.get_double()); 303 } 304 break; 305 306 case TCKind._tk_boolean: { 307 for (int i = 0; i < len; i++, ds.next()) 308 result[i] = ValueFactory.createBoolean(ds.get_boolean()); 309 } 310 break; 311 312 case TCKind._tk_char: { 313 for (int i = 0; i < len; i++, ds.next()) 314 result[i] = ValueFactory.createChar(ds.get_char()); 315 } 316 break; 317 318 case TCKind._tk_string: { 319 for (int i = 0; i < len; i++, ds.next()) 320 result[i] = ValueFactory.createString(ds.get_string()); 321 } 322 break; 323 } 324 325 da.destroy(); 326 } 327 catch (org.omg.CORBA.TypeCodePackage.BadKind e) { 328 throw new RuntimeException (); 329 } 330 catch (org.omg.DynamicAny.DynAnyPackage.TypeMismatch e) { 332 throw new RuntimeException (); 333 } 334 catch (org.omg.DynamicAny.DynAnyPackage.InvalidValue e) { 335 throw new RuntimeException (); 336 } 337 catch (org.omg.DynamicAny.DynAnyFactoryPackage.InconsistentTypeCode e) { 338 throw new RuntimeException (); 339 } 340 catch (org.omg.CORBA.ORBPackage.InvalidName e) { 341 throw new RuntimeException (); 342 } 343 catch (BAD_OPERATION e) { 345 throw new RuntimeException (); 346 } 347 348 m_values.put(property, result); 350 } 351 else { 352 if (v instanceof Value[]) 354 result = (Value[])v; 355 } 356 357 return result; 358 } 359 360 361 public Any getPropertyValue(String property) 362 { 363 Any result = (Any)m_propValues.get(property); 365 366 if (result == null) { 367 Property p = (Property)m_propTable.get(property); 368 if (p == null) 369 return null; 370 371 result = PropUtil.getPropertyValue(p); 373 374 if (result != null) 375 m_propValues.put(property, result); 377 } 378 379 return result; 380 } 381 } 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 | Popular Tags |