KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jacorb > trading > db > simple > 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.simple.types;
15
16 import java.io.*;
17 import java.util.*;
18 import org.omg.CORBA.*;
19 import org.omg.CosTradingRepos.ServiceTypeRepositoryPackage.*;
20 import org.jacorb.trading.db.TypeDatabase;
21 import org.jacorb.trading.util.RWLock;
22
23
24 public class TypeDatabaseImpl implements TypeDatabase
25 {
26   private Incarnation m_incarnation;
27   private Hashtable m_types;
28   private RWLock m_lock;
29   private File m_dbFile;
30   private boolean m_dirty = false;
31
32   private static final String JavaDoc DB_FILE = "types.dat";
33
34
35   private TypeDatabaseImpl()
36   {
37   }
38
39
40   public TypeDatabaseImpl(String JavaDoc dirName)
41   {
42     m_dbFile = new File(dirName, DB_FILE);
43
44     if (m_dbFile.exists())
45       readObjects();
46     else {
47       m_incarnation = new Incarnation();
48       m_types = new Hashtable();
49       writeObjects();
50     }
51
52     m_lock = new RWLock();
53   }
54
55
56   public void begin(int mode)
57   {
58     if (mode == TypeDatabase.READ)
59       m_lock.acquireRead();
60     else if (mode == TypeDatabase.WRITE)
61       m_lock.acquireWrite();
62     else
63       throw new RuntimeException JavaDoc("Invalid lock mode");
64   }
65
66
67   public void end()
68   {
69       // flush all of our dirty objects
70
if (m_dirty) {
71       writeObjects();
72       m_dirty = false;
73     }
74
75     m_lock.release();
76   }
77
78
79   public TypeStruct describeType(String JavaDoc name)
80   {
81     TypeStruct result = null;
82
83     Type impl = (Type)m_types.get(name);
84     if (impl != null)
85       result = impl.describe();
86
87     return result;
88   }
89
90
91   public boolean maskType(String JavaDoc name)
92   {
93     boolean result = false;
94
95     Type impl = (Type)m_types.get(name);
96     if (impl != null) {
97       impl.mask();
98       m_dirty = true;
99       result = true;
100     }
101
102     return result;
103   }
104
105
106   public boolean unmaskType(String JavaDoc name)
107   {
108     boolean result = false;
109
110     Type impl = (Type)m_types.get(name);
111     if (impl != null) {
112       impl.unmask();
113       m_dirty = true;
114       result = true;
115     }
116
117     return result;
118   }
119
120
121   public String JavaDoc[] getTypes()
122   {
123     String JavaDoc[] result = new String JavaDoc[m_types.size()];
124
125     Enumeration e = m_types.keys();
126     int count = 0;
127     while (e.hasMoreElements())
128       result[count++] = (String JavaDoc)e.nextElement();
129
130     return result;
131   }
132
133
134   public String JavaDoc[] getTypesSince(IncarnationNumber inc)
135   {
136     String JavaDoc[] result;
137
138     Vector types = new Vector();
139
140     Incarnation i = new Incarnation(inc);
141
142     Enumeration e = m_types.elements();
143     while (e.hasMoreElements()) {
144       Type type = (Type)e.nextElement();
145       if (type.getIncarnation().compareTo(i) >= 0)
146         types.addElement(type.getName());
147     }
148
149     result = new String JavaDoc[types.size()];
150     types.copyInto((java.lang.Object JavaDoc[])result);
151
152     return result;
153   }
154
155
156   public IncarnationNumber getIncarnation()
157   {
158     return m_incarnation.getIncarnationNumber();
159   }
160
161
162   public IncarnationNumber createType(
163     String JavaDoc name,
164     String JavaDoc interfaceName,
165     PropStruct[] props,
166     String JavaDoc[] superTypes)
167   {
168     IncarnationNumber result = null;
169
170       // assign a new incarnation number and add the type to our list
171
result = m_incarnation.getIncarnationNumber();
172     Type type = new Type(name, interfaceName, props, superTypes, result);
173     m_types.put(name, type);
174     m_incarnation.increment();
175     m_dirty = true;
176
177     return result;
178   }
179
180
181   public boolean removeType(String JavaDoc name)
182   {
183     boolean result = false;
184
185     if (m_types.containsKey(name)) {
186       m_types.remove(name);
187       m_dirty = true;
188       result = true;
189     }
190
191     return result;
192   }
193
194
195   public String JavaDoc findSubType(String JavaDoc name)
196   {
197     String JavaDoc result = null;
198
199     Enumeration e = m_types.elements();
200     while (e.hasMoreElements() && result == null) {
201       Type type = (Type)e.nextElement();
202       String JavaDoc[] superTypes = type.getSuperTypes();
203       for (int t = 0; t < superTypes.length; t++) {
204         if (name.equals(superTypes[t])) {
205           result = type.getName();
206           break;
207         }
208       }
209     }
210
211     return result;
212   }
213
214
215   public String JavaDoc[] getAllSuperTypes(String JavaDoc name)
216   {
217     String JavaDoc[] result = null;
218
219     Type type = (Type)m_types.get(name);
220     if (type != null) {
221       Vector vec = findAllSuperTypes(type);
222
223       result = new String JavaDoc[vec.size()];
224       vec.copyInto((java.lang.Object JavaDoc[])result);
225     }
226
227     return result;
228   }
229
230
231   protected void readObjects()
232   {
233     try {
234       FileInputStream fileIn = new FileInputStream(m_dbFile);
235       ObjectInputStream objIn = new ObjectInputStream(fileIn);
236       m_incarnation = (Incarnation)objIn.readObject();
237       m_types = (Hashtable)objIn.readObject();
238       fileIn.close();
239     }
240     catch (IOException e) {
241       throw new RuntimeException JavaDoc(e.getMessage());
242     }
243     catch (ClassNotFoundException JavaDoc e) {
244       throw new RuntimeException JavaDoc(e.getMessage());
245     }
246   }
247
248
249   protected void writeObjects()
250   {
251     try {
252       FileOutputStream fileOut = new FileOutputStream(m_dbFile);
253       ObjectOutputStream objOut = new ObjectOutputStream(fileOut);
254       objOut.writeObject(m_incarnation);
255       objOut.writeObject(m_types);
256       fileOut.close();
257     }
258     catch (IOException e) {
259       throw new RuntimeException JavaDoc(e.getMessage());
260     }
261   }
262
263
264   /**
265    * Returns a vector of strings representing all the super types of
266    * this type; this method is called recursively
267    */

268   protected Vector findAllSuperTypes(Type type)
269   {
270     Vector result = new Vector();
271
272       // start with our immediate super types
273
String JavaDoc[] supers = type.getSuperTypes();
274
275       // merge in all of the super types of our super types
276
for (int i = 0; i < supers.length; i++) {
277       result.addElement(supers[i]);
278       Type impl = (Type)m_types.get(supers[i]);
279       Vector v = findAllSuperTypes(impl);
280       Enumeration e = v.elements();
281       while (e.hasMoreElements()) {
282         String JavaDoc s = (String JavaDoc)e.nextElement();
283         if (! result.contains(s))
284           result.addElement(s);
285       }
286     }
287
288     return result;
289   }
290 }
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
Popular Tags