KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jacorb > trading > db > pse > types > TypeDatabaseImpl


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.db.pse.types;
15
16 import java.io.*;
17 import java.util.*;
18 import COM.odi.*;
19 import COM.odi.util.*;
20 import org.omg.CORBA.*;
21 import org.omg.CosTradingRepos.ServiceTypeRepositoryPackage.*;
22 import org.jacorb.trading.db.TypeDatabase;
23 import org.jacorb.trading.db.pse.util.TransactionMgr;
24
25
26 public class TypeDatabaseImpl implements TypeDatabase
27 {
28   private Database m_database;
29   private OSHashtable m_types;
30   private Incarnation m_incarnation;
31   private TransactionMgr m_txnMgr;
32
33   private static final String JavaDoc TYPES_ROOT = "types_root";
34   private static final String JavaDoc TYPESINC_ROOT = "typesinc_root";
35
36
37   private TypeDatabaseImpl()
38   {
39   }
40
41
42   public TypeDatabaseImpl(COM.odi.Database database, TransactionMgr txnMgr)
43   {
44     m_database = database;
45     m_txnMgr = txnMgr;
46
47     boolean foundRoots = false;
48     Transaction tr = null;
49
50     try {
51       tr = Transaction.begin(ObjectStore.READONLY);
52       m_types = (OSHashtable)m_database.getRoot(TYPES_ROOT);
53       m_incarnation = (Incarnation)m_database.getRoot(TYPESINC_ROOT);
54       tr.commit(ObjectStore.RETAIN_HOLLOW);
55       foundRoots = true;
56     }
57     catch (DatabaseRootNotFoundException e) {
58       tr.abort(ObjectStore.RETAIN_HOLLOW);
59     }
60
61     if (! foundRoots) {
62       tr = Transaction.begin(ObjectStore.UPDATE);
63       m_types = new OSHashtable();
64       m_incarnation = new Incarnation();
65       m_database.createRoot(TYPES_ROOT, m_types);
66       m_database.createRoot(TYPESINC_ROOT, m_incarnation);
67       tr.commit(ObjectStore.RETAIN_HOLLOW);
68     }
69   }
70
71
72   public void begin(int mode)
73   {
74     m_txnMgr.begin();
75   }
76
77
78   public void end()
79   {
80     m_txnMgr.commit(ObjectStore.RETAIN_HOLLOW);
81   }
82
83
84   public TypeStruct describeType(String JavaDoc name)
85   {
86     TypeStruct result = null;
87
88     Type t = (Type)m_types.get(name);
89     if (t != null)
90       result = t.describe();
91
92     return result;
93   }
94
95
96   public boolean maskType(String JavaDoc name)
97   {
98     boolean result = false;
99
100     Type t = (Type)m_types.get(name);
101     if (t != null) {
102       t.mask();
103       result = true;
104     }
105
106     return result;
107   }
108
109
110   public boolean unmaskType(String JavaDoc name)
111   {
112     boolean result = false;
113
114     Type t = (Type)m_types.get(name);
115     if (t != null) {
116       t.unmask();
117       result = true;
118     }
119
120     return result;
121   }
122
123
124   public String JavaDoc[] getTypes()
125   {
126     String JavaDoc[] result = new String JavaDoc[m_types.size()];
127
128     int count = 0;
129     Enumeration e = m_types.keys();
130     while (e.hasMoreElements()) {
131       String JavaDoc name = (String JavaDoc)e.nextElement();
132       result[count] = name;
133       count++;
134     }
135
136     return result;
137   }
138
139
140   public String JavaDoc[] getTypesSince(IncarnationNumber inc)
141   {
142     String JavaDoc[] result;
143
144     Vector types = new Vector();
145     Incarnation i = new Incarnation(inc);
146
147     Enumeration e = m_types.elements();
148     while (e.hasMoreElements()) {
149       Type type = (Type)e.nextElement();
150       if (type.getIncarnation().compareTo(i) >= 0)
151         types.addElement(type.getName());
152     }
153
154     result = new String JavaDoc[types.size()];
155     types.copyInto((java.lang.Object JavaDoc[])result);
156
157     return result;
158   }
159
160
161   public IncarnationNumber getIncarnation()
162   {
163     return m_incarnation.getIncarnationNumber();
164   }
165
166
167   public IncarnationNumber createType(
168     String JavaDoc name,
169     String JavaDoc interfaceName,
170     PropStruct[] props,
171     String JavaDoc[] superTypes)
172   {
173     IncarnationNumber result = null;
174
175       // assign a new incarnation number and add the type to our list
176
result = m_incarnation.getIncarnationNumber();
177     m_incarnation.increment();
178     Type type =
179       new Type(name, interfaceName, props, superTypes, result);
180     m_types.put(name, type);
181
182     return result;
183   }
184
185
186   public boolean removeType(String JavaDoc name)
187   {
188     boolean result = false;
189
190     if (m_types.containsKey(name)) {
191       m_types.remove(name);
192       result = true;
193     }
194
195     return result;
196   }
197
198
199   public String JavaDoc findSubType(String JavaDoc name)
200   {
201     String JavaDoc result = null;
202
203     Enumeration e = m_types.elements();
204     while (e.hasMoreElements() && result == null) {
205       Type type = (Type)e.nextElement();
206       String JavaDoc[] superTypes = type.getSuperTypes();
207       for (int t = 0; t < superTypes.length; t++) {
208         if (name.equals(superTypes[t])) {
209           result = type.getName();
210           break;
211         }
212       }
213     }
214
215     return result;
216   }
217
218
219   public String JavaDoc[] getAllSuperTypes(String JavaDoc name)
220   {
221     String JavaDoc[] result = null;
222
223     Type type = (Type)m_types.get(name);
224     if (type != null) {
225       Vector vec = findAllSuperTypes(type);
226
227       result = new String JavaDoc[vec.size()];
228       vec.copyInto((java.lang.Object JavaDoc[])result);
229     }
230
231     return result;
232   }
233
234
235   /**
236    * Returns a vector of strings representing all the super types of
237    * this type; this method is called recursively
238    */

239   protected Vector findAllSuperTypes(Type type)
240   {
241     Vector result = new Vector();
242
243       // start with our immediate super types
244
String JavaDoc[] supers = type.getSuperTypes();
245
246       // merge in all of the super types of our super types
247
for (int i = 0; i < supers.length; i++) {
248       result.addElement(supers[i]);
249       Type impl = (Type)m_types.get(supers[i]);
250       Vector v = findAllSuperTypes(impl);
251       Enumeration e = v.elements();
252       while (e.hasMoreElements()) {
253         String JavaDoc s = (String JavaDoc)e.nextElement();
254         if (! result.contains(s))
255           result.addElement(s);
256       }
257     }
258
259     return result;
260   }
261 }
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
Popular Tags