KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jacorb > trading > impl > SourceAdapter


1
2 // Copyright (C) 1998-1999
3
// Object Oriented Concepts, Inc.
4

5 // **********************************************************************
6
//
7
// Copyright (c) 1997
8
// Mark Spruiell (mark@intellisoft.com)
9
//
10
// See the COPYING file for more information
11
//
12
// **********************************************************************
13

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 JavaDoc;
23 import org.jacorb.trading.constraint.*;
24 import org.jacorb.trading.util.*;
25
26 /**
27  * Provides property values to the expression evaluators
28  */

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 JavaDoc m_object;
36     private Property[] m_props;
37
38     private org.omg.CORBA.ORB JavaDoc orb;
39
40
41     protected SourceAdapter()
42     {
43     }
44
45
46     public SourceAdapter(org.omg.CORBA.Object JavaDoc object, Property[] props)
47     {
48     orb = ((org.omg.CORBA.portable.ObjectImpl JavaDoc)object)._orb();
49
50     m_object = object;
51     m_props = props;
52
53     m_propTable = new Hashtable();
54     // cache the Value objects we have produced
55
m_values = new Hashtable();
56     // cache the property values we have retrieved
57
m_propValues = new Hashtable();
58
59     // load the offer's properties into a hashtable for quicker lookup
60
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 JavaDoc 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     // we use the SourceAdapter object to obtain the values for the
82
// desired properties
83

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 JavaDoc[])result);
93     }
94     else if (desired_props.discriminator() == HowManyProps.some) {
95         String JavaDoc[] 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 JavaDoc[])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 JavaDoc property)
113     {
114     return m_propTable.containsKey(property);
115     }
116
117
118     public Value getValue(String JavaDoc property)
119     {
120     Value result = null;
121
122     // first see if we've already cached the value
123
java.lang.Object JavaDoc 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         // next try to get the property's value
131
// NOTE: this may cause dynamic property evaluation
132
Any val = getPropertyValue(property);
133
134         if (val == null)
135         return null;
136
137         // cache the property value
138
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 JavaDoc s = val.extract_string();
201             result = ValueFactory.createString(s);
202         }
203         break;
204         }
205         }
206         catch (org.omg.CORBA.TypeCodePackage.BadKind JavaDoc e) {
207         throw new RuntimeException JavaDoc();
208         }
209         catch (BAD_OPERATION e) {
210         throw new RuntimeException JavaDoc();
211         }
212
213         // cache the value object
214
m_values.put(property, result);
215     }
216     else {
217         // make sure we've got a Value (i.e. not a Value[])
218
if (v instanceof Value)
219         result = (Value)v;
220     }
221
222     return result;
223     }
224
225
226     public Value[] getSequenceValues(String JavaDoc property)
227     {
228     Value[] result = null;
229
230     // first see if we've already cached the values
231
java.lang.Object JavaDoc 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         // next try to get the property's value
240
// NOTE: this may cause dynamic property evaluation
241
Any val = getPropertyValue(property);
242
243         if (val == null)
244         return null;
245
246         // cache the property value
247
m_propValues.put(property, val);
248
249         try {
250         TypeCode tc = val.type();
251
252         // skip aliases
253
while (tc.kind() == TCKind.tk_alias)
254             tc = tc.content_type();
255
256         if (tc.kind() != TCKind.tk_sequence)
257             return null;
258
259         // get the type of the sequence data
260
tc = tc.content_type();
261         TCKind kind = tc.kind();
262
263         org.omg.DynamicAny.DynAny JavaDoc da = DynAnyFactoryHelper.narrow(orb.resolve_initial_references("DynAnyFactory")).create_dyn_any(val);
264         DynSequence JavaDoc ds = DynSequenceHelper.narrow(da);
265         int len = ds.get_length();
266         //**//
267
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 JavaDoc e) {
328         throw new RuntimeException JavaDoc();
329         }
330         //**//
331
catch (org.omg.DynamicAny.DynAnyPackage.TypeMismatch JavaDoc e) {
332         throw new RuntimeException JavaDoc();
333         }
334         catch (org.omg.DynamicAny.DynAnyPackage.InvalidValue JavaDoc e) {
335         throw new RuntimeException JavaDoc();
336         }
337         catch (org.omg.DynamicAny.DynAnyFactoryPackage.InconsistentTypeCode JavaDoc e) {
338         throw new RuntimeException JavaDoc();
339         }
340         catch (org.omg.CORBA.ORBPackage.InvalidName JavaDoc e) {
341         throw new RuntimeException JavaDoc();
342         }
343         //**//
344
catch (BAD_OPERATION e) {
345         throw new RuntimeException JavaDoc();
346         }
347
348         // add the result to the cache
349
m_values.put(property, result);
350     }
351     else {
352         // make sure we've got a Value[] (i.e. not a Value)
353
if (v instanceof Value[])
354         result = (Value[])v;
355     }
356
357     return result;
358     }
359
360
361     public Any getPropertyValue(String JavaDoc property)
362     {
363     // first see if we've already cached the value
364
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         // NOTE: this may cause dynamic property evaluation
372
result = PropUtil.getPropertyValue(p);
373
374         if (result != null)
375         // cache the property value
376
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