KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jacorb > trading > db > simple > offers > OfferDatabaseImpl


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.offers;
15
16 import java.io.*;
17 import java.util.*;
18 import org.omg.CORBA.*;
19 import org.omg.CosTrading.Lookup;
20 import org.omg.CosTrading.RegisterPackage.*;
21 import org.omg.CosTrading.ProxyPackage.*;
22 import org.omg.CosTrading.Property;
23 import org.omg.CosTrading.Policy;
24 import org.omg.CosTradingDynamic.*;
25 import org.jacorb.trading.db.OfferDatabase;
26 import org.jacorb.trading.util.*;
27
28 /**
29  * Simple implementation of OfferDatabase using serialized objects
30  */

31 public class OfferDatabaseImpl implements OfferDatabase
32 {
33   private File m_dirPath;
34   private Hashtable m_offerLists;
35   private Hashtable m_offerIndex;
36   private File m_indexFile;
37   private int m_counter;
38   private boolean m_indexDirty = false;
39   private RWLock m_lock;
40
41   private static String JavaDoc LIST_NAME = "OFR_";
42   private static String JavaDoc LIST_EXT = ".dat";
43   private static String JavaDoc INDEX_FILE = "offeridx.dat";
44
45
46   private OfferDatabaseImpl()
47   {
48   }
49
50
51   public OfferDatabaseImpl(String JavaDoc dirPath)
52   {
53     m_dirPath = new File(dirPath);
54     m_indexFile = new File(dirPath, INDEX_FILE);
55
56     if (m_indexFile.exists())
57       readIndex();
58     else {
59       m_offerIndex = new Hashtable();
60       m_counter = 0;
61       writeIndex();
62     }
63
64       // once an offer list is loaded, it is cached in m_offerLists
65
m_offerLists = new Hashtable();
66
67     m_lock = new RWLock();
68   }
69
70
71   /** Returns true if the offerId is legal, false otherwise */
72   public boolean validateOfferId(String JavaDoc offerId)
73   {
74     return OfferList.validateOfferId(offerId);
75   }
76
77
78   /** Returns true if the database can store a property with the given value */
79   public boolean isTypeSupported(org.omg.CORBA.Any JavaDoc any)
80   {
81     boolean result;
82
83       // if the given value is a dynamic property, then make sure
84
// we can store the value of the extra_info field
85
if (PropUtil.isDynamicProperty(any.type())) {
86       DynamicProp dp = DynamicPropHelper.extract(any);
87       result = AnyValue.isTypeSupported(dp.extra_info.type());
88     }
89     else
90       result = AnyValue.isTypeSupported(any.type());
91
92     return result;
93   }
94
95
96   /** Must precede any use of the database */
97   public void begin(int mode)
98   {
99     if (mode == OfferDatabase.READ)
100       m_lock.acquireRead();
101     else if (mode == OfferDatabase.WRITE)
102       m_lock.acquireWrite();
103     else
104       throw new RuntimeException JavaDoc("Invalid lock mode");
105   }
106
107
108   /** Must follow any use of the database */
109   public void end()
110   {
111       // save any modified offer lists
112
Enumeration e = m_offerLists.elements();
113     while (e.hasMoreElements()) {
114       OfferList list = (OfferList)e.nextElement();
115       if (list.getDirty())
116         writeList(list);
117     }
118
119     if (m_indexDirty) {
120       writeIndex();
121       m_indexDirty = false;
122     }
123
124     m_lock.release();
125   }
126
127
128   /** Returns true if the offer with the given ID exists */
129   public boolean exists(String JavaDoc offerId)
130   {
131     boolean result = false;
132
133     OfferList list = getList(whichService(offerId));
134     if (list != null)
135       result = list.exists(offerId);
136
137     return result;
138   }
139
140
141   /** Returns true if the offer with the given ID is a proxy offer */
142   public boolean isProxy(String JavaDoc offerId)
143   {
144     boolean result = false;
145
146     OfferList list = getList(whichService(offerId));
147     if (list != null)
148       result = list.isProxy(offerId);
149
150     return result;
151   }
152
153
154   /** Creates a new offer, returning the assigned offer ID */
155   public String JavaDoc create(
156     String JavaDoc serviceType,
157     org.omg.CORBA.Object JavaDoc obj,
158     Property[] props)
159   {
160     OfferList list = getList(serviceType);
161
162     if (list == null)
163     {
164       list = createList(serviceType);
165     }
166
167     return list.create(obj, props);
168   }
169
170
171   /** Creates a new proxy offer, returning the assigned offer ID */
172   public String JavaDoc createProxy(
173     Lookup target,
174     String JavaDoc serviceType,
175     Property[] props,
176     boolean ifMatchAll,
177     String JavaDoc recipe,
178     Policy[] policies)
179   {
180     OfferList list = getList(serviceType);
181     if (list == null)
182       list = createList(serviceType);
183
184     return list.createProxy(target, props, ifMatchAll, recipe, policies);
185   }
186
187
188   /** Removes the offer with the given ID */
189   public void remove(String JavaDoc offerId)
190   {
191     OfferList list = getList(whichService(offerId));
192     if (list != null)
193       list.remove(offerId);
194   }
195
196
197   /** Removes the proxy offer with the given ID */
198   public void removeProxy(String JavaDoc offerId)
199   {
200     OfferList list = getList(whichService(offerId));
201     if (list != null)
202       list.removeProxy(offerId);
203   }
204
205
206   /** Returns a description of the offer with the given ID */
207   public OfferInfo describe(String JavaDoc offerId)
208   {
209     OfferInfo result = null;
210
211     OfferList list = getList(whichService(offerId));
212     if (list != null)
213       result = list.describe(offerId);
214
215     return result;
216   }
217
218
219   /** Returns a description of the proxy offer with the given ID */
220   public ProxyInfo describeProxy(String JavaDoc offerId)
221   {
222     ProxyInfo result = null;
223
224     OfferList list = getList(whichService(offerId));
225     if (list != null)
226       result = list.describeProxy(offerId);
227
228     return result;
229   }
230
231
232   /** Updates the properties of an offer */
233   public void modify(String JavaDoc offerId, Property[] props)
234   {
235     OfferList list = getList(whichService(offerId));
236     if (list != null)
237       list.modify(offerId, props);
238   }
239
240
241   /** Returns all offers of the given service type */
242   public Hashtable getOffers(String JavaDoc serviceType)
243   {
244     Hashtable result = null;
245
246     OfferList list = getList(serviceType);
247     if (list != null)
248       result = list.getOffers();
249
250     return result;
251   }
252
253
254   /** Returns all offer IDs of the given service type */
255   public String JavaDoc[] getOfferIds(String JavaDoc serviceType)
256   {
257     String JavaDoc[] result = null;
258
259     OfferList list = getList(serviceType);
260     if (list != null)
261       result = list.getOfferIds();
262
263     return result;
264   }
265
266
267   /** Returns all proxy offers of the given service type */
268   public Hashtable getProxyOffers(String JavaDoc serviceType)
269   {
270     Hashtable result = null;
271
272     OfferList list = getList(serviceType);
273     if (list != null)
274       result = list.getProxyOffers();
275
276     return result;
277   }
278
279
280   /** Returns all proxy offer IDs of the given service type */
281   public String JavaDoc[] getProxyOfferIds(String JavaDoc serviceType)
282   {
283     String JavaDoc[] result = null;
284
285     OfferList list = getList(serviceType);
286     if (list != null)
287       result = list.getProxyOfferIds();
288
289     return result;
290   }
291
292
293   /** Returns the service type of the given offer */
294   public String JavaDoc whichService(String JavaDoc offerId)
295   {
296     return OfferList.whichService(offerId);
297   }
298
299
300   /**
301    * First checks for the list in the cache (m_offerLists); if not
302    * found, attempts to read list from file. Method must be
303    * synchronized because multiple readers may be active.
304    */

305   protected synchronized OfferList getList(String JavaDoc serviceType)
306   {
307     OfferList result = null;
308
309     result = (OfferList)m_offerLists.get(serviceType);
310     if (result == null) {
311       result = readList(serviceType);
312       if (result != null)
313         m_offerLists.put(serviceType, result);
314     }
315
316     return result;
317   }
318
319
320   protected OfferList createList(String JavaDoc serviceType)
321   {
322     OfferList result = new OfferList(serviceType);
323     m_offerLists.put(serviceType, result);
324     m_counter++;
325     m_offerIndex.put(serviceType, new Integer JavaDoc(m_counter));
326     m_indexDirty = true;
327     return result;
328   }
329
330
331   protected OfferList readList(String JavaDoc serviceType)
332   {
333     OfferList result = null;
334
335     File listFile = getListFile(serviceType);
336     if (listFile != null && listFile.exists()) {
337       try {
338         FileInputStream fileIn = new FileInputStream(listFile);
339         ObjectInputStream objIn = new ObjectInputStream(fileIn);
340         result = (OfferList)objIn.readObject();
341         fileIn.close();
342       }
343       catch (IOException e) {
344         throw new RuntimeException JavaDoc(e.getMessage());
345       }
346       catch (ClassNotFoundException JavaDoc e) {
347         throw new RuntimeException JavaDoc(e.getMessage());
348       }
349     }
350
351     return result;
352   }
353
354
355   protected void writeList(OfferList list)
356   {
357     try {
358       File listFile = getListFile(list.getServiceType());
359       if (listFile == null)
360         throw new RuntimeException JavaDoc("listFile should not be null!");
361       FileOutputStream fileOut = new FileOutputStream(listFile);
362       ObjectOutputStream objOut = new ObjectOutputStream(fileOut);
363       objOut.writeObject(list);
364       fileOut.close();
365     }
366     catch (IOException e) {
367       throw new RuntimeException JavaDoc(e.getMessage());
368     }
369   }
370
371
372   protected File getListFile(String JavaDoc serviceType)
373   {
374     File result = null;
375
376       // obtain the counter associated with this service type
377
// from the offer index
378
Integer JavaDoc counter = (Integer JavaDoc)m_offerIndex.get(serviceType);
379
380     if (counter != null)
381       result = new File(m_dirPath, LIST_NAME + counter + LIST_EXT);
382
383     return result;
384   }
385
386
387   protected void readIndex()
388   {
389     try {
390       FileInputStream fileIn = new FileInputStream(m_indexFile);
391       ObjectInputStream objIn = new ObjectInputStream(fileIn);
392       Integer JavaDoc i = (Integer JavaDoc)objIn.readObject();
393       m_counter = i.intValue();
394       m_offerIndex = (Hashtable)objIn.readObject();
395       fileIn.close();
396     }
397     catch (IOException e) {
398       throw new RuntimeException JavaDoc(e.getMessage());
399     }
400     catch (ClassNotFoundException JavaDoc e) {
401       throw new RuntimeException JavaDoc(e.getMessage());
402     }
403   }
404
405
406   protected void writeIndex()
407   {
408     try {
409       FileOutputStream fileOut = new FileOutputStream(m_indexFile);
410       ObjectOutputStream objOut = new ObjectOutputStream(fileOut);
411       objOut.writeObject(new Integer JavaDoc(m_counter));
412       objOut.writeObject(m_offerIndex);
413       fileOut.close();
414     }
415     catch (IOException e) {
416       throw new RuntimeException JavaDoc(e.getMessage());
417     }
418   }
419 }
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
Popular Tags