summaryrefslogtreecommitdiff
path: root/TAO/orbsvcs/tests/AVStreams/server_discovery/Property_Evaluator.java
blob: 4689c9443b4fad989f53f63da019e852a1ff715f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
/* -*- C++ -*- */

// ========================================================================
// $Id$
// 
// = FILENAME
//    Property_Evaluator.java
// 
// = AUTHOR 
//    Seth Widoff <sbw1@cs.wustl.edu> 
//   
// ========================================================================


import org.omg.CORBA.*;
import CosTrading.*;
import CosTradingDynamic.*;
import TAO_Sequences.*;
import TAO_VR.*;

/**
  * This is a Java port of the TAO_Property_Evaluator class in the TAO
  * trading service implementation.
  */

public class Property_Evaluator
{
  private Property[] props_;
  private boolean supports_dp_ = true;
  
  /**
    * Construct an instance of TAO_Property_Evaluator that operates on
    * a PropertySeq, <properties>.
    */
  Property_Evaluator (Property[] properties)
    {
      this.props_ = properties;
    }

  /**
    * Construct an instance of TAO_Property_Evaluator that operates on
    * an <offer>.
    */
  public Property_Evaluator (Offer offer)
    {
      this.props_ = offer.properties;
    }

  /**
    * Disable or enable the evaluation of dynamic properties.
    */
  public void allow_dynamic_properties (boolean toggle)
    {
      this.supports_dp_ = toggle;
    }

  /**
    * Returns true if the property at index <index> is dynamic. Returns a
    * 0 when the index is out of bounds.    
    */
  public boolean is_dynamic_property (int index)
    {
      boolean return_value = false;
      int num_properties = this.props_.length;
      
      if (index >= 0 && index < num_properties)
	return_value = Property_Evaluator.is_dynamic_property (this.props_[index]);
      
      return return_value;
    }	 

  public static boolean is_dynamic_property (Property prop)
    {
      boolean return_value = false;

      Any value = prop.value;
      TypeCode type = value.type ();

      // Compare the type of the Any contents with the typecode
      // for a dynamic property structure. 
      if (type.equal (DynamicPropHelper.type ()))
	return_value = true;

      return return_value;
    }
  
  /**
    * Returns value of the property whose index is <index>. If the
    * property at that index is dynamic and the trader supports dynamic 
    * properties, then the property_value method will obtain the value
    * of the dynamic property using the evalDP method on the
    * CosTradingDynamic::DynamicPropEval interface, passing on a
    * CosTradingDynamic::DPEvalFailure exception on failure. If the
    * property index is undefined, the method returns a null pointer.
    */
  public Any property_value (int index, ORB orb)
    throws DPEvalFailure
    {
      Any return_value = null;
      int num_properties = this.props_.length;
      
      if (index >= 0 && index < num_properties)
	{
	  return_value =
	    Property_Evaluator.property_value (this.props_[index], orb);
	}

      return return_value;
    }

  public static Any property_value (Property prop, ORB orb)
    throws DPEvalFailure
    {
      Any prop_value = null;
      
      if (! Property_Evaluator.is_dynamic_property (prop))
	prop_value = prop.value;
      else
	{
	  DynamicProp dp_struct =
	    DynamicPropHelper.extract (prop.value);
	  String prop_name = prop.name;
	  
	  // This is because of the bug in TAO:
	  // Now we extract the object reference from the dynamic
	  // property structure.
	  org.omg.CORBA.Object obj =
	    orb.string_to_object (dp_struct.eval_if);
	  DynamicPropEval dp_eval = DynamicPropEvalHelper.narrow (obj);

	  if (dp_eval != null)
	    {
	      prop_value = dp_eval.evalDP (prop_name,
					   dp_struct.returned_type,
					   dp_struct.extra_info);
	    }
	  else
	    {
	      throw new DPEvalFailure (prop_name,
				       dp_struct.returned_type,
				       dp_struct.extra_info);	      
	    }
	}

      return prop_value;
    }
  
  /**
    * Returns the type of the property whose index is <index>. If the
    * property is dynamic and the trader supports dynamic properties,
    * then the method returns the <returned_type> field of the
    * CosTradingDynamic::DynamicProp struct associated with the
    * property name. If the index is out of bounds, the method returns 
    * a null pointer (that is, 0).
    */
  
  public TypeCode property_type (int index)
    {
      TypeCode prop_type = null;

      if (! this.is_dynamic_property (index))
	prop_type = this.props_[index].value.type ();
      else
	{
	  Any value = this.props_[index].value;
	  DynamicProp dp_struct = DynamicPropHelper.extract (value);

	  prop_type = dp_struct.returned_type;
	}

      return prop_type;
    }

  public String property_to_string (int index, ORB orb)
    throws DPEvalFailure
    {
      String return_value = null;
      Any any_value = this.property_value (index, orb);
      TypeCode type = any_value.type ();
      int kind = type.kind ().value ();
      
      if (kind == TCKind._tk_null)
	return_value = "Null";

      else if (kind == TCKind._tk_void)
	return_value = "Void";
      
      else if (kind == TCKind._tk_short)
	return_value = String.valueOf (any_value.extract_short ());

      else if (kind == TCKind._tk_long)
	return_value = String.valueOf (any_value.extract_long ());

      else if (kind == TCKind._tk_ushort)
	return_value = String.valueOf (any_value.extract_ushort ());

      else if (kind == TCKind._tk_ulong)
	return_value = String.valueOf (any_value.extract_ulong ());

      else if (kind == TCKind._tk_float)
	return_value = String.valueOf (any_value.extract_float ());

      else if (kind == TCKind._tk_double)
	return_value = String.valueOf (any_value.extract_double ());

      else if (kind == TCKind._tk_boolean)
	return_value = String.valueOf (any_value.extract_boolean ());

      else if (kind  == TCKind._tk_char)
	return_value = String.valueOf (any_value.extract_char ());

      else if (kind == TCKind._tk_string)
	return_value = any_value.extract_string ();

      else if (type.equal (StringSeqHelper.type ()))
	{
	  System.out.println ("Printing string sequence.");
	  String[] str_seq = StringSeqHelper.extract (any_value);	  

	  for (int length = str_seq.length, i = 0; i < length; i++)
	    return_value = str_seq[i] + " ";	 
	}

      else if (type.equal (ULongSeqHelper.type ()))
	{
	  System.out.println ("Printing ulong sequence.");
	  int[] ulong_seq = ULongSeqHelper.extract (any_value);	  

	  for (int length = ulong_seq.length, i = 0; i < length; i++)
	    return_value = ulong_seq[i] + " ";
	}

      else
	System.out.println ("TCKind" + kind);

      return return_value;
    }
}