KickJava   Java API By Example, From Geeks To Geeks.

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


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.io.*;
17 import java.util.*;
18 import org.omg.CORBA.*;
19 import org.omg.CosTrading.*;
20 import org.omg.CosTradingRepos.ServiceTypeRepositoryPackage.*;
21 import org.jacorb.trading.db.TypeDatabase;
22 import org.jacorb.trading.util.*;
23
24
25 /**
26  * Implementation of CosTradingRepos::ServiceTypeRepository
27  */

28 public class RepositoryImpl
29   extends org.omg.CosTradingRepos.ServiceTypeRepositoryPOA
30 {
31   private TypeDatabase m_database;
32   private org.omg.CORBA.Repository JavaDoc m_interfaceRepos;
33
34
35   public RepositoryImpl(
36     TypeDatabase db,
37     org.omg.CORBA.Repository JavaDoc interfaceRepos)
38   {
39     m_database = db;
40     m_interfaceRepos = interfaceRepos;
41   }
42
43
44   public IncarnationNumber incarnation()
45   {
46     IncarnationNumber result;
47
48     m_database.begin(TypeDatabase.READ);
49     result = m_database.getIncarnation();
50     m_database.end();
51
52     return result;
53   }
54
55
56   public IncarnationNumber add_type(
57     String JavaDoc name,
58     String JavaDoc if_name,
59     PropStruct[] props,
60     String JavaDoc[] super_types)
61     throws IllegalServiceType,
62            ServiceTypeExists,
63            InterfaceTypeMismatch,
64            IllegalPropertyName,
65            DuplicatePropertyName,
66            ValueTypeRedefinition,
67            UnknownServiceType,
68            DuplicateServiceTypeName
69   {
70     IncarnationNumber result = null;
71
72     try {
73       m_database.begin(TypeDatabase.WRITE);
74
75       checkTypeName(name);
76
77         // make sure another type doesn't already exist with the same name
78
if (m_database.describeType(name) != null)
79         throw new ServiceTypeExists(name);
80
81         // we'll build up a hashtable of type descriptions for later use
82
Hashtable typeInfo = new Hashtable();
83
84         // check for duplicate super types
85
for (int i = 0; i < super_types.length; i++) {
86         TypeStruct ts = findType(super_types[i]);
87         if (typeInfo.containsKey(super_types[i]))
88           throw new DuplicateServiceTypeName(super_types[i]);
89         typeInfo.put(super_types[i], ts);
90       }
91
92         // collect descriptions of all super types
93

94       for (int i = 0; i < super_types.length; i++) {
95         String JavaDoc[] names = m_database.getAllSuperTypes(super_types[i]);
96         for (int n = 0; n < names.length; n++) {
97           if (! typeInfo.containsKey(names[n])) {
98             TypeStruct ts = findType(names[n]);
99             if (ts == null)
100               throw new UnknownServiceType(names[n]);
101             typeInfo.put(names[n], ts);
102           }
103         }
104       }
105
106         // check for interface compatibility
107
validateInterface(name, if_name, super_types, typeInfo);
108
109         // check for duplicate properties and for value type redefinition
110
validateProperties(name, props, super_types, typeInfo);
111
112       result = m_database.createType(name, if_name, props, super_types);
113     }
114     finally {
115       m_database.end();
116     }
117
118     return result;
119   }
120
121
122   public void remove_type(String JavaDoc name)
123     throws IllegalServiceType,
124            UnknownServiceType,
125            HasSubTypes
126   {
127     try {
128       m_database.begin(TypeDatabase.WRITE);
129
130       checkTypeName(name);
131
132       String JavaDoc subTypeName = m_database.findSubType(name);
133
134       if (subTypeName != null)
135         throw new HasSubTypes(name, subTypeName);
136
137       if (! m_database.removeType(name))
138         throw new UnknownServiceType(name);
139     }
140     finally {
141       m_database.end();
142     }
143   }
144
145
146   public String JavaDoc[] list_types(
147     SpecifiedServiceTypes which_types)
148   {
149     String JavaDoc[] result = null;
150
151     try {
152       m_database.begin(TypeDatabase.READ);
153
154       if (which_types.discriminator() == ListOption.all)
155         result = m_database.getTypes();
156       else {
157         IncarnationNumber inc = which_types.incarnation();
158         result = m_database.getTypesSince(inc);
159       }
160     }
161     finally {
162       m_database.end();
163     }
164
165     return result;
166   }
167
168
169   public TypeStruct describe_type(String JavaDoc name)
170     throws IllegalServiceType,
171            UnknownServiceType
172   {
173     TypeStruct result;
174
175     try {
176       m_database.begin(TypeDatabase.READ);
177
178       checkTypeName(name);
179
180       result = findType(name);
181     }
182     finally {
183       m_database.end();
184     }
185
186     return result;
187   }
188
189
190   public TypeStruct fully_describe_type(String JavaDoc name)
191     throws IllegalServiceType,
192            UnknownServiceType
193   {
194     TypeStruct result = null;
195     Enumeration e;
196
197     try {
198       m_database.begin(TypeDatabase.READ);
199
200       checkTypeName(name);
201
202       result = findType(name);
203
204       String JavaDoc[] superTypeNames = m_database.getAllSuperTypes(name);
205
206         // replace the super_types member
207
result.super_types = superTypeNames;
208
209         // get descriptions of all the supertypes
210
Vector desc = new Vector();
211       for (int i = 0; i < superTypeNames.length; i++) {
212         TypeStruct ts = findType(superTypeNames[i]);
213         desc.addElement(ts);
214       }
215
216         // build up the complete list of properties, starting with the
217
// properties in the requested type
218
Vector props = new Vector();
219       for (int i = 0; i < result.props.length; i++)
220         props.addElement(result.props[i]);
221
222       e = desc.elements();
223       while (e.hasMoreElements()) {
224         TypeStruct ts = (TypeStruct)e.nextElement();
225         for (int i = 0; i < ts.props.length; i++) {
226             // iterate through the current list, making sure we don't
227
// add a property that already exists
228
boolean found = false;
229           Enumeration p = props.elements();
230           while (p.hasMoreElements() && ! found) {
231             PropStruct ps = (PropStruct)p.nextElement();
232             if (ts.props[i].name.equals(ps.name))
233               found = true;
234           }
235
236           if (! found)
237             props.addElement(ts.props[i]);
238         }
239       }
240
241       result.props = new PropStruct[props.size()];
242       props.copyInto((java.lang.Object JavaDoc[])result.props);
243     }
244     finally {
245       m_database.end();
246     }
247
248     return result;
249   }
250
251
252   public void mask_type(String JavaDoc name)
253     throws IllegalServiceType,
254            UnknownServiceType,
255            AlreadyMasked
256   {
257     try {
258       m_database.begin(TypeDatabase.WRITE);
259
260       checkTypeName(name);
261
262       TypeStruct ts = findType(name);
263
264       if (ts.masked)
265         throw new AlreadyMasked(name);
266       else if (! m_database.maskType(name))
267         throw new UnknownServiceType(name);
268     }
269     finally {
270       m_database.end();
271     }
272   }
273
274
275   public void unmask_type(String JavaDoc name)
276     throws IllegalServiceType,
277            UnknownServiceType,
278            NotMasked
279   {
280     try {
281       m_database.begin(TypeDatabase.WRITE);
282
283       checkTypeName(name);
284
285       TypeStruct ts = findType(name);
286
287       if (! ts.masked)
288         throw new NotMasked(name);
289       else if (! m_database.unmaskType(name))
290         throw new UnknownServiceType(name);
291     }
292     finally {
293       m_database.end();
294     }
295   }
296
297
298   protected void checkTypeName(String JavaDoc name)
299     throws IllegalServiceType
300   {
301     if (name == null || name.trim().length() == 0)
302       throw new IllegalServiceType("");
303
304     StringTokenizer tokenizer = new StringTokenizer(name, ":", true);
305     boolean seenIdent = false;
306     String JavaDoc lastToken = null;
307     int colonCount = 0;
308
309     while (tokenizer.hasMoreTokens()) {
310       String JavaDoc tok = tokenizer.nextToken();
311       if (tok.equals(":")) {
312         colonCount++;
313         if (colonCount > 2)
314           throw new IllegalServiceType(name);
315       }
316       else {
317         colonCount = 0;
318         seenIdent = true;
319
320           // must start with letter
321
if (! Character.isLetter(tok.charAt(0)))
322           throw new IllegalServiceType(name);
323
324         for (int i = 1; i < tok.length(); i++) {
325           char ch = tok.charAt(i);
326           if (! Character.isLetterOrDigit(ch) && ch != '_')
327             throw new IllegalServiceType(name);
328         }
329       }
330
331       lastToken = tok;
332     }
333
334     if (! seenIdent)
335       throw new IllegalServiceType(name);
336
337     if (lastToken.equals(":"))
338       throw new IllegalServiceType(name);
339   }
340
341
342   protected TypeStruct findType(String JavaDoc name)
343     throws UnknownServiceType
344   {
345     TypeStruct result = m_database.describeType(name);
346
347     if (result == null)
348       throw new UnknownServiceType(name);
349
350     return result;
351   }
352
353
354   protected void validateInterface(
355     String JavaDoc name,
356     String JavaDoc interfaceName,
357     String JavaDoc[] superTypes,
358     Hashtable typeInfo)
359     throws InterfaceTypeMismatch
360   {
361       // if we have an interface repository, then validate the type's
362
// interface against the interface of each of the supertypes
363
if (m_interfaceRepos != null) {
364       org.omg.CORBA.InterfaceDef JavaDoc def = null;
365
366         // retrieve the InterfaceDef object for the interface
367
try {
368         org.omg.CORBA.Contained JavaDoc c = m_interfaceRepos.lookup(interfaceName);
369         if (c != null)
370           def = org.omg.CORBA.InterfaceDefHelper.narrow(c);
371       }
372       catch (org.omg.CORBA.SystemException JavaDoc e) {
373         // ignore
374
}
375
376       if (def != null) {
377         for (int i = 0; i < superTypes.length; i++) {
378             // retrieve the InterfaceDef object for the supertype
379
try {
380             TypeStruct ts = (TypeStruct)typeInfo.get(superTypes[i]);
381             org.omg.CORBA.Contained JavaDoc c = m_interfaceRepos.lookup(ts.if_name);
382             if (c != null) {
383               String JavaDoc id = c.id();
384               if (! def.is_a(id))
385                 throw new InterfaceTypeMismatch(superTypes[i], ts.if_name,
386                   name, interfaceName);
387             }
388           }
389           catch (org.omg.CORBA.SystemException JavaDoc e) {
390             // ignore
391
}
392         }
393       }
394     }
395   }
396
397
398   protected void validateProperties(
399     String JavaDoc name,
400     PropStruct[] props,
401     String JavaDoc[] superTypes,
402     Hashtable typeInfo)
403     throws IllegalPropertyName,
404            DuplicatePropertyName,
405            ValueTypeRedefinition
406   {
407     Vector goodProps = new Vector();
408
409       // for each property, we need to validate it against the
410
// property with the same name in all supertypes
411
for (int i = 0; i < props.length; i++) {
412         // first make sure we haven't seen this property already
413
if (goodProps.contains(props[i].name))
414         throw new DuplicatePropertyName(props[i].name);
415
416         // now ask each supertype if they have a property of the same name;
417
// we don't stop after the first match - we have to ask ALL of the
418
// super types
419
for (int s = 0; s < superTypes.length; s++) {
420         TypeStruct ts = (TypeStruct)typeInfo.get(superTypes[s]);
421
422         Vector superProps = new Vector();
423         findProperties(ts, props[i].name, superProps, typeInfo);
424
425         Enumeration e = superProps.elements();
426         while (e.hasMoreElements()) {
427           PropStruct ps = (PropStruct)e.nextElement();
428
429             // make sure the type codes are equivalent and the modes
430
// are compatible
431
if (! props[i].value_type.equal(ps.value_type) ||
432               ! validateMode(props[i].mode, ps.mode))
433             throw new ValueTypeRedefinition(name, props[i], superTypes[s], ps);
434         }
435       }
436
437         // if we've made it here without throwing an exception, add this
438
// property to our running list of validated properties
439
goodProps.addElement(props[i].name);
440     }
441   }
442
443
444   protected boolean validateMode(PropertyMode subMode, PropertyMode superMode)
445   {
446     boolean result = false;
447
448       // if both are equal, we're done
449
if (superMode == subMode)
450       result = true;
451       // if mode of superMode is normal, subMode mode can be anything
452
else if (superMode == PropertyMode.PROP_NORMAL)
453       result = true;
454     else if (superMode == PropertyMode.PROP_READONLY &&
455       (subMode == PropertyMode.PROP_READONLY ||
456        subMode == PropertyMode.PROP_MANDATORY_READONLY))
457       result = true;
458     else if (superMode == PropertyMode.PROP_MANDATORY &&
459       (subMode == PropertyMode.PROP_MANDATORY ||
460        subMode == PropertyMode.PROP_MANDATORY_READONLY))
461       result = true;
462     else if ( // redundant, but included for completeness
463
superMode == PropertyMode.PROP_MANDATORY_READONLY &&
464       subMode == PropertyMode.PROP_MANDATORY_READONLY)
465       result = true;
466
467     return result;
468   }
469
470
471   /**
472    * This function is called recursively
473    */

474   protected void findProperties(
475     TypeStruct ts,
476     String JavaDoc propName,
477     Vector v,
478     Hashtable typeInfo)
479   {
480     PropStruct prop = findProperty(ts, propName);
481     if (prop != null)
482       v.addElement(prop);
483     else {
484         // ask all of our supertypes to add to the list - typeInfo should
485
// contain the descriptions for the complete set of supertypes
486
for (int i = 0; i < ts.super_types.length; i++) {
487         TypeStruct superTS = (TypeStruct)typeInfo.get(ts.super_types[i]);
488         findProperties(superTS, propName, v, typeInfo);
489       }
490     }
491   }
492
493
494   protected PropStruct findProperty(TypeStruct ts, String JavaDoc name)
495   {
496     PropStruct result = null;
497
498     for (int i = 0; i < ts.props.length && result == null; i++)
499       if (name.equals(ts.props[i].name))
500         result = ts.props[i];
501
502     return result;
503   }
504 }
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
Popular Tags