KickJava   Java API By Example, From Geeks To Geeks.

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


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
17 import java.util.*;
18 import org.omg.CORBA.*;
19 import org.omg.CosTrading.*;
20 import org.omg.CosTradingRepos.*;
21 import org.omg.CosTradingRepos.ServiceTypeRepositoryPackage.*;
22 import org.omg.CosTradingDynamic.*;
23 import org.jacorb.trading.db.OfferDatabase;
24 import org.jacorb.trading.util.*;
25
26
27 /**
28  * Convenience methods
29  */

30 public class OfferUtil
31 {
32     private OfferUtil()
33     {
34     }
35
36
37     /** Validate the properties of a service offer against the service type */
38     public static void validateProperties(
39                       OfferDatabase db,
40                       Property[] exportProps,
41                       String JavaDoc typeName,
42                       TypeStruct type)
43     throws IllegalPropertyName,
44     PropertyTypeMismatch,
45     ReadonlyDynamicProperty,
46     MissingMandatoryProperty,
47     DuplicatePropertyName
48     {
49     // create a hashtable of the service type's properties
50
Hashtable typeProps = new Hashtable();
51     for (int i = 0; i < type.props.length; i++)
52         typeProps.put(type.props[i].name, type.props[i]);
53
54     // also create a hashtable of the exported properties we've processed
55
Hashtable checkedProps = new Hashtable();
56
57     // iterate through each of the exported properties
58
for (int i = 0; i < exportProps.length; i++)
59     {
60         // try to find the property with the same name in the service type
61
PropStruct ps = (PropStruct)typeProps.get(exportProps[i].name);
62         if (ps != null)
63         {
64         // make sure we haven't already processed this property
65
if (checkedProps.containsKey(exportProps[i].name))
66             throw new DuplicatePropertyName(exportProps[i].name);
67
68         checkProperty(typeName, exportProps[i], ps);
69         checkedProps.put(exportProps[i].name, exportProps[i]);
70         }
71
72         // whether or not the property is defined in the service type,
73
// we need to make sure the database can store this property;
74
// the PropertyTypeMismatch exception is about the best we can do
75
//
76
// NOTE: For dynamic properties, we may not be able to store
77
// the extra_info member of the DynamicProp struct;
78
// we assume the database object also validates extra_info
79

80         if (!db.isTypeSupported(exportProps[i].value))
81         {
82         throw new PropertyTypeMismatch(typeName, exportProps[i]);
83         }
84     }
85
86     // now we need to check for mandatory properties that were not
87
// included in the export list
88
Enumeration e = typeProps.elements();
89     while (e.hasMoreElements())
90     {
91         PropStruct ps = (PropStruct)e.nextElement();
92
93         // if property is not being exported...
94
if (checkedProps.get(ps.name) == null) {
95         if (isMandatory(ps.mode))
96             throw new MissingMandatoryProperty(typeName, ps.name);
97         }
98     }
99     }
100
101
102     public static boolean isMandatory(PropertyMode mode)
103     {
104     boolean result;
105
106     result = (
107           mode == PropertyMode.PROP_MANDATORY ||
108           mode == PropertyMode.PROP_MANDATORY_READONLY
109           );
110
111     return result;
112     }
113
114
115     public static boolean isReadonly(PropertyMode mode)
116     {
117     boolean result;
118
119     result = (
120           mode == PropertyMode.PROP_READONLY ||
121           mode == PropertyMode.PROP_MANDATORY_READONLY
122           );
123
124     return result;
125     }
126
127
128     /** Check the property's type against the expected type */
129     public static void checkProperty(
130                      String JavaDoc typeName,
131                      Property prop,
132                      PropStruct ps)
133     throws PropertyTypeMismatch,
134     ReadonlyDynamicProperty
135     {
136     try {
137         TypeCode propType = ps.value_type;
138         while (propType.kind() == TCKind.tk_alias)
139         propType = propType.content_type();
140
141
142         if (PropUtil.isDynamicProperty(prop.value.type()))
143         {
144         // cannot allow dynamic readonly properties
145
if (isReadonly(ps.mode))
146             throw new ReadonlyDynamicProperty(typeName, prop.name);
147
148         // verify the dynamic property's type code against that
149
// specified by the service type
150
DynamicProp dp = DynamicPropHelper.extract(prop.value);
151
152         TypeCode tc = dp.returned_type;
153         while (tc.kind() == TCKind.tk_alias)
154             tc = tc.content_type();
155
156         if (! tc.equal(propType))
157             throw new PropertyTypeMismatch(typeName, prop);
158         }
159         else {
160         // verify the property's type code against that
161
// specified by the service type
162
TypeCode tc = prop.value.type();
163         while (tc.kind() == TCKind.tk_alias)
164             tc = tc.content_type();
165
166         if (! tc.equal(propType))
167             throw new PropertyTypeMismatch(typeName, prop);
168         }
169     }
170     catch (org.omg.CORBA.TypeCodePackage.BadKind JavaDoc e) {
171         throw new RuntimeException JavaDoc();
172     }
173     }
174 }
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
Popular Tags