KickJava   Java API By Example, From Geeks To Geeks.

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


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.RegisterPackage.*;
20 import org.omg.CosTradingRepos.*;
21 import org.omg.CosTradingRepos.ServiceTypeRepositoryPackage.*;
22 import org.omg.CosTradingDynamic.*;
23 import org.jacorb.trading.constraint.*;
24 import org.jacorb.trading.db.OfferDatabase;
25 import org.jacorb.trading.util.*;
26
27 /**
28  * Implementation of CosTrading::Register
29  */

30 public class RegisterImpl extends org.omg.CosTrading.RegisterPOA
31 {
32     private TraderComp m_traderComp;
33     private SupportAttrib m_support;
34     private OfferDatabase m_db;
35     private ServiceTypeRepository m_repos;
36     private org.omg.CORBA.Repository JavaDoc m_interfaceRepos;
37
38
39     private RegisterImpl()
40     {
41     }
42
43
44     public RegisterImpl(
45             TraderComp traderComp,
46             SupportAttrib supportAttrib,
47             OfferDatabase db,
48             org.omg.CORBA.Repository JavaDoc interfaceRepos)
49     {
50     m_traderComp = traderComp;
51     m_support = supportAttrib;
52     m_db = db;
53     m_interfaceRepos = interfaceRepos;
54     org.omg.CORBA.Object JavaDoc obj = supportAttrib.getTypeRepos();
55     m_repos = ServiceTypeRepositoryHelper.narrow(obj);
56     }
57
58
59     // operations inherited from CosTrading::TraderComponents
60

61     public Lookup lookup_if()
62     {
63     return m_traderComp.getLookupInterface();
64     }
65
66
67     public Register register_if()
68     {
69     return m_traderComp.getRegisterInterface();
70     }
71
72
73     public Link link_if()
74     {
75     return m_traderComp.getLinkInterface();
76     }
77
78
79     public Proxy proxy_if()
80     {
81     return m_traderComp.getProxyInterface();
82     }
83
84
85     public Admin admin_if()
86     {
87     return m_traderComp.getAdminInterface();
88     }
89
90
91     // operations inherited from CosTrading::SupportAttributes
92

93     public boolean supports_modifiable_properties()
94     {
95     return m_support.getModifiableProperties();
96     }
97
98
99     public boolean supports_dynamic_properties()
100     {
101     return m_support.getDynamicProperties();
102     }
103
104
105     public boolean supports_proxy_offers()
106     {
107     return m_support.getProxyOffers();
108     }
109
110
111     public org.omg.CORBA.Object JavaDoc type_repos()
112     {
113     return m_support.getTypeRepos();
114     }
115
116
117
118     // operations inherited from CosTrading::Register
119

120     public String JavaDoc export(
121              org.omg.CORBA.Object JavaDoc reference,
122              String JavaDoc type,
123              Property[] properties)
124     throws InvalidObjectRef,
125     IllegalServiceType,
126     UnknownServiceType,
127     org.omg.CosTrading.RegisterPackage.InterfaceTypeMismatch,
128     IllegalPropertyName,
129     PropertyTypeMismatch,
130     ReadonlyDynamicProperty,
131     MissingMandatoryProperty,
132     DuplicatePropertyName
133     {
134
135     String JavaDoc result = null;
136
137     if (reference == null)
138         throw new InvalidObjectRef(reference);
139
140     // retrieve complete information about the service type from the
141
// repository - may throw IllegalServiceType, UnknownServiceType
142
TypeStruct ts = m_repos.fully_describe_type(type);
143
144     // do not allow exporting for a masked service type
145
if (ts.masked)
146         throw new UnknownServiceType(type);
147
148     // validate the interface - may throw InterfaceTypeMismatch
149
validateInterface(reference, type, ts);
150
151     // validate the exported properties - may throw
152
// IllegalPropertyName, PropertyTypeMismatch,
153
// MissingMandatoryProperty, DuplicatePropertyName
154
OfferUtil.validateProperties(m_db, properties, type, ts);
155
156     // save the offer in the database
157

158     m_db.begin(OfferDatabase.WRITE);
159
160     try {
161         result = m_db.create(type, reference, properties);
162     }
163     finally {
164         m_db.end();
165     }
166
167     return result;
168     }
169
170
171     public void withdraw(String JavaDoc id)
172     throws IllegalOfferId,
173     UnknownOfferId,
174     ProxyOfferId
175     {
176     if (! m_db.validateOfferId(id))
177         throw new IllegalOfferId(id);
178
179     m_db.begin(OfferDatabase.WRITE);
180
181     try {
182         if (! m_db.exists(id))
183         throw new UnknownOfferId(id);
184
185         if (m_db.isProxy(id))
186         throw new ProxyOfferId(id);
187
188         m_db.remove(id);
189     }
190     finally {
191         m_db.end();
192     }
193     }
194
195
196     public OfferInfo describe(String JavaDoc id)
197     throws IllegalOfferId,
198     UnknownOfferId,
199     ProxyOfferId
200     {
201     OfferInfo result;
202
203     if (! m_db.validateOfferId(id))
204         throw new IllegalOfferId(id);
205
206
207     m_db.begin(OfferDatabase.READ);
208
209     try {
210         if (! m_db.exists(id))
211         throw new UnknownOfferId(id);
212
213         if (m_db.isProxy(id))
214         throw new ProxyOfferId(id);
215
216         result = m_db.describe(id);
217     }
218     finally {
219         m_db.end();
220     }
221
222     return result;
223     }
224
225
226     public void modify(
227                String JavaDoc id,
228                String JavaDoc[] del_list,
229                Property[] modify_list)
230     throws NotImplemented,
231     IllegalOfferId,
232     UnknownOfferId,
233     ProxyOfferId,
234     IllegalPropertyName,
235     UnknownPropertyName,
236     PropertyTypeMismatch,
237     ReadonlyDynamicProperty,
238     MandatoryProperty,
239     ReadonlyProperty,
240     DuplicatePropertyName
241     {
242     if (! m_support.getModifiableProperties())
243         throw new NotImplemented();
244
245     if (! m_db.validateOfferId(id))
246         throw new IllegalOfferId(id);
247
248     try {
249         m_db.begin(OfferDatabase.WRITE);
250
251         if (! m_db.exists(id))
252         throw new UnknownOfferId(id);
253
254         if (m_db.isProxy(id))
255         throw new ProxyOfferId(id);
256
257         String JavaDoc type = m_db.whichService(id);
258
259         // retrieve complete information about the service type from the
260
// repository - may throw IllegalServiceType, UnknownServiceType
261
TypeStruct ts = m_repos.fully_describe_type(type);
262
263         OfferInfo info = m_db.describe(id);
264
265         // check the proposed deletion list - may throw several
266
// exceptions
267
checkDelete(del_list, type, ts, info);
268
269         // check the proposed modification list - may throw several
270
// exceptions
271
checkModify(modify_list, type, ts, info);
272
273         // Build a vector of the offer's new properties
274
//
275
// Algorithm:
276
// - copy in the modify_list
277
// - for each property in info.properties
278
// - if not already in vector, and not in del_list, add
279
// to vector
280

281         Vector props = new Vector();
282         for (int i = 0; i < modify_list.length; i++)
283         props.addElement(modify_list[i]);
284
285         for (int i = 0; i < info.properties.length; i++) {
286         boolean found = false;
287
288         // check del_list to see if property is to be deleted
289
for (int d = 0; d < del_list.length && ! found; d++)
290             if (info.properties[i].name.equals(del_list[d]))
291             found = true;
292
293         // check props to see if we already have this property
294
Enumeration e = props.elements();
295         while (e.hasMoreElements() && ! found) {
296             Property p = (Property)e.nextElement();
297             if (info.properties[i].name.equals(p.name))
298             found = true;
299         }
300
301         // if we didn't find it in del_list, and it's not already
302
// in props, then add it now
303
if (! found)
304             props.addElement(info.properties[i]);
305         }
306
307         // create an array from the vector
308
Property[] arr = new Property[props.size()];
309         props.copyInto((java.lang.Object JavaDoc[])arr);
310
311         // make the changes
312
m_db.modify(id, arr);
313     }
314     catch (IllegalServiceType e) {
315         throw new UnknownOfferId(id);
316     }
317     catch (UnknownServiceType e) {
318         throw new UnknownOfferId(id);
319     }
320     finally {
321         m_db.end();
322     }
323     }
324
325
326     public void withdraw_using_constraint(String JavaDoc type, String JavaDoc constr)
327     throws IllegalServiceType,
328     UnknownServiceType,
329     IllegalConstraint,
330     NoMatchingOffers
331     {
332     // retrieve complete information about the service type from the
333
// repository - may throw IllegalServiceType, UnknownServiceType
334
TypeStruct ts = m_repos.fully_describe_type(type);
335
336     // instantiate the schema object required by the constraint parser
337
SchemaAdapter schema = new SchemaAdapter(ts);
338
339     Constraint c = new Constraint(schema);
340
341     int count = 0;
342
343     try {
344         m_db.begin(OfferDatabase.WRITE);
345
346         // attempt to parse the constraint expression
347
c.parse(constr);
348
349         // retrieve all of the offers of this type and process them;
350
// we get a Hashtable whose keys are offer IDs and whose values
351
// are OfferInfo structs
352
Hashtable offers = m_db.getOffers(type);
353
354         if (offers != null) {
355         Enumeration e = offers.keys();
356         while (e.hasMoreElements()) {
357             String JavaDoc id = (String JavaDoc)e.nextElement();
358             OfferInfo info = (OfferInfo)offers.get(id);
359
360             SourceAdapter source =
361             new SourceAdapter(info.reference, info.properties);
362
363             // NOTE: dynamic properties will be evaluated
364

365             if (c.evaluate(source)) {
366             m_db.remove(id);
367             count++;
368             }
369         }
370         }
371     }
372     catch (ParseException ex) {
373         // the exception doesn't include a reason, so we just print it
374
System.out.println("Illegal constraint '" + constr + "'");
375         System.out.println(ex.getMessage());
376         throw new IllegalConstraint(constr);
377     }
378     finally {
379         m_db.end();
380     }
381
382     if (count == 0)
383         throw new NoMatchingOffers(constr);
384     }
385
386
387     public Register resolve(String JavaDoc[] name)
388     throws IllegalTraderName,
389     UnknownTraderName,
390     RegisterNotSupported
391     {
392     throw new RegisterNotSupported(name);
393     }
394
395
396     protected void validateInterface(
397                      org.omg.CORBA.Object JavaDoc ref,
398                      String JavaDoc typeName,
399                      TypeStruct type)
400     throws org.omg.CosTrading.RegisterPackage.InterfaceTypeMismatch
401     {
402     // Verify that the given object is compatible with the interface
403
// specified by the service type
404

405     // To validate an interface, we must check that the object reference
406
// supplied is the same as the interface specified in the service
407
// type, or is a subtype of the interface
408

409     if (m_interfaceRepos != null) {
410         // retrieve the InterfaceDef object for the interface
411
try {
412         org.omg.CORBA.InterfaceDef JavaDoc def = InterfaceDefHelper.narrow(ref._get_interface_def());
413         org.omg.CORBA.Contained JavaDoc c = m_interfaceRepos.lookup(type.if_name);
414
415         // we validate only if we got both the object's definition
416
// and the type's interface definition
417
if (def != null && c != null) {
418             String JavaDoc id = c.id();
419             if (! def.is_a(id))
420             throw new org.omg.CosTrading.RegisterPackage.InterfaceTypeMismatch(
421                                                typeName, ref);
422         }
423         }
424         catch (org.omg.CORBA.SystemException JavaDoc e) {
425         // ignore
426
}
427     }
428     }
429
430
431     protected void checkDelete(
432                    String JavaDoc[] del_list,
433                    String JavaDoc typeName,
434                    TypeStruct type,
435                    OfferInfo info)
436     throws IllegalPropertyName,
437     UnknownPropertyName,
438     MandatoryProperty,
439     ReadonlyProperty,
440     DuplicatePropertyName
441     {
442     // create a hashtable of the offer's properties
443
Hashtable offerProps = new Hashtable();
444     for (int i = 0; i < info.properties.length; i++)
445         offerProps.put(info.properties[i].name, info.properties[i]);
446
447     // create a hashtable of the service type's properties
448
Hashtable typeProps = new Hashtable();
449     for (int i = 0; i < type.props.length; i++)
450         typeProps.put(type.props[i].name, type.props[i]);
451
452     // also create a hashtable of the deleted property names we've processed
453
Hashtable deletedProps = new Hashtable();
454
455     // for each property name in del_list, check to see if the offer
456
// contains the property, make sure it isn't mandatory, and make
457
// sure there aren't duplicate names in del_list
458
for (int i = 0; i < del_list.length; i++) {
459         String JavaDoc propName = del_list[i];
460
461         // check for a duplicate
462
if (deletedProps.containsKey(propName))
463         throw new DuplicatePropertyName(propName);
464
465         deletedProps.put(propName, propName);
466
467         // find the property with the matching name
468
Property prop = (Property)offerProps.get(propName);
469
470         if (prop != null) {
471         PropStruct ps = (PropStruct)typeProps.get(propName);
472         if (ps != null) {
473             if (OfferUtil.isMandatory(ps.mode))
474             throw new MandatoryProperty(typeName, propName);
475         }
476         }
477         else
478         throw new UnknownPropertyName(propName);
479     }
480     }
481
482
483     protected void checkModify(
484                    Property[] modify_list,
485                    String JavaDoc typeName,
486                    TypeStruct type,
487                    OfferInfo info)
488     throws IllegalPropertyName,
489     UnknownPropertyName,
490     PropertyTypeMismatch,
491     ReadonlyDynamicProperty,
492     ReadonlyProperty,
493     DuplicatePropertyName
494     {
495     // create a hashtable of the modified properties
496
Hashtable modProps = new Hashtable();
497     for (int i = 0; i < modify_list.length; i++)
498         modProps.put(modify_list[i].name, modify_list[i]);
499
500     // create a hashtable of the offer's properties
501
Hashtable offerProps = new Hashtable();
502     for (int i = 0; i < info.properties.length; i++)
503         offerProps.put(info.properties[i].name, info.properties[i]);
504
505     // create a hashtable of the service type's properties
506
Hashtable typeProps = new Hashtable();
507     for (int i = 0; i < type.props.length; i++)
508         typeProps.put(type.props[i].name, type.props[i]);
509
510     // also create a hashtable of the properties we've processed
511
Hashtable checkedProps = new Hashtable();
512
513
514     // check the modification list for readonly properties, type
515
// mismatches and duplicates
516

517     Enumeration e = modProps.elements();
518     while (e.hasMoreElements()) {
519         Property prop = (Property)e.nextElement();
520
521         if (checkedProps.containsKey(prop.name))
522         throw new DuplicatePropertyName(prop.name);
523
524         checkedProps.put(prop.name, prop);
525
526         // lookup the property information in the service type
527
PropStruct ps = (PropStruct)typeProps.get(prop.name);
528         if (ps == null)
529         throw new UnknownPropertyName(prop.name);
530
531         // if the property is present in the offer, and the service
532
// type says the property mode is read-only, then it's an error
533
if (offerProps.containsKey(prop.name) && OfferUtil.isReadonly(ps.mode))
534         throw new ReadonlyProperty(typeName, prop.name);
535
536         OfferUtil.checkProperty(typeName, prop, ps);
537     }
538     }
539 }
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
Popular Tags