KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jacorb > trading > db > pse > 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.pse.offers;
15
16
17 import java.io.*;
18 import java.util.*;
19 import COM.odi.*;
20 import COM.odi.util.*;
21 import org.omg.CORBA.*;
22 import org.omg.CosTrading.Lookup;
23 import org.omg.CosTrading.RegisterPackage.*;
24 import org.omg.CosTrading.ProxyPackage.*;
25 import org.omg.CosTrading.Property;
26 import org.omg.CosTrading.Policy;
27 import org.omg.CosTradingDynamic.*;
28 import org.jacorb.trading.db.OfferDatabase;
29 import org.jacorb.trading.db.pse.util.TransactionMgr;
30 import org.jacorb.trading.util.*;
31
32
33 public class OfferDatabaseImpl implements OfferDatabase
34 {
35   private Database m_database;
36   private TransactionMgr m_txnMgr;
37   private OSHashtable m_offerLists;
38
39   private static final String JavaDoc LISTS_ROOT = "offer_lists";
40
41
42   private OfferDatabaseImpl()
43   {
44   }
45
46
47   public OfferDatabaseImpl(COM.odi.Database database, TransactionMgr txnMgr)
48   {
49     m_database = database;
50     m_txnMgr = txnMgr;
51
52     boolean foundRoot = false;
53     Transaction tr = null;
54
55     try {
56       tr = Transaction.begin(ObjectStore.READONLY);
57       m_offerLists = (OSHashtable)m_database.getRoot(LISTS_ROOT);
58       tr.commit(ObjectStore.RETAIN_HOLLOW);
59       foundRoot = true;
60     }
61     catch (DatabaseRootNotFoundException e) {
62       tr.abort(ObjectStore.RETAIN_HOLLOW);
63     }
64
65     if (! foundRoot) {
66       tr = Transaction.begin(ObjectStore.UPDATE);
67       m_offerLists = new OSHashtable();
68       m_database.createRoot(LISTS_ROOT, m_offerLists);
69       tr.commit(ObjectStore.RETAIN_HOLLOW);
70     }
71   }
72
73
74   /** Returns true if the offerId is legal, false otherwise */
75   public boolean validateOfferId(String JavaDoc offerId)
76   {
77     return OfferList.validateOfferId(offerId);
78   }
79
80
81   /** Returns true if the database can store a property with the given value */
82   public boolean isTypeSupported(org.omg.CORBA.Any JavaDoc any)
83   {
84     boolean result;
85
86       // if the given value is a dynamic property, then make sure
87
// we can store the value of the extra_info field
88
if (PropUtil.isDynamicProperty(any.type())) {
89       DynamicProp dp = DynamicPropHelper.extract(any);
90       result = AnyValue.isTypeSupported(dp.extra_info.type());
91     }
92     else
93       result = AnyValue.isTypeSupported(any.type());
94
95     return result;
96   }
97
98
99   /** Must precede any use of the database */
100   public void begin(int mode)
101   {
102     m_txnMgr.begin();
103   }
104
105
106   /** Must follow any use of the database */
107   public void end()
108   {
109     m_txnMgr.commit(ObjectStore.RETAIN_HOLLOW);
110   }
111
112
113   /** Returns true if the offer with the given ID exists */
114   public boolean exists(String JavaDoc offerId)
115   {
116     boolean result = false;
117
118     OfferList list = (OfferList)m_offerLists.get(whichService(offerId));
119     if (list != null)
120       result = list.exists(offerId);
121
122     return result;
123   }
124
125
126   /** Returns true if the offer with the given ID is a proxy offer */
127   public boolean isProxy(String JavaDoc offerId)
128   {
129     boolean result = false;
130
131     OfferList list = (OfferList)m_offerLists.get(whichService(offerId));
132     if (list != null)
133       result = list.isProxy(offerId);
134
135     return result;
136   }
137
138
139   /** Creates a new offer, returning the assigned offer ID */
140   public String JavaDoc create(
141     String JavaDoc serviceType,
142     org.omg.CORBA.Object JavaDoc obj,
143     Property[] props)
144   {
145     OfferList list = (OfferList)m_offerLists.get(serviceType);
146     if (list == null) {
147       list = new OfferList(serviceType);
148       m_offerLists.put(serviceType, list);
149     }
150
151     return list.create(obj, props);
152   }
153
154
155   /** Creates a new proxy offer, returning the assigned offer ID */
156   public String JavaDoc createProxy(
157     Lookup target,
158     String JavaDoc serviceType,
159     Property[] props,
160     boolean ifMatchAll,
161     String JavaDoc recipe,
162     Policy[] policies)
163   {
164     OfferList list = (OfferList)m_offerLists.get(serviceType);
165     if (list == null) {
166       list = new OfferList(serviceType);
167       m_offerLists.put(serviceType, list);
168     }
169
170     return list.createProxy(target, props, ifMatchAll, recipe, policies);
171   }
172
173
174   /** Removes the offer with the given ID */
175   public void remove(String JavaDoc offerId)
176   {
177     OfferList list = (OfferList)m_offerLists.get(whichService(offerId));
178     if (list != null)
179       list.remove(offerId);
180   }
181
182
183   /** Removes the proxy offer with the given ID */
184   public void removeProxy(String JavaDoc offerId)
185   {
186     OfferList list = (OfferList)m_offerLists.get(whichService(offerId));
187     if (list != null)
188       list.removeProxy(offerId);
189   }
190
191
192   /** Returns a description of the offer with the given ID */
193   public OfferInfo describe(String JavaDoc offerId)
194   {
195     OfferInfo result = null;
196
197     OfferList list = (OfferList)m_offerLists.get(whichService(offerId));
198     if (list != null)
199       result = list.describe(offerId);
200
201     return result;
202   }
203
204
205   /** Returns a description of the proxy offer with the given ID */
206   public ProxyInfo describeProxy(String JavaDoc offerId)
207   {
208     ProxyInfo result = null;
209
210     OfferList list = (OfferList)m_offerLists.get(whichService(offerId));
211     if (list != null)
212       result = list.describeProxy(offerId);
213
214     return result;
215   }
216
217
218   /** Updates the properties of an offer */
219   public void modify(String JavaDoc offerId, Property[] props)
220   {
221     OfferList list = (OfferList)m_offerLists.get(whichService(offerId));
222     if (list != null)
223       list.modify(offerId, props);
224   }
225
226
227   /** Returns all offers of the given service type */
228   public Hashtable getOffers(String JavaDoc serviceType)
229   {
230     Hashtable result = null;
231
232     OfferList list = (OfferList)m_offerLists.get(serviceType);
233     if (list != null)
234       result = list.getOffers();
235
236     return result;
237   }
238
239
240   /** Returns all offer IDs of the given service type */
241   public String JavaDoc[] getOfferIds(String JavaDoc serviceType)
242   {
243     String JavaDoc[] result = null;
244
245     OfferList list = (OfferList)m_offerLists.get(serviceType);
246     if (list != null)
247       result = list.getOfferIds();
248
249     return result;
250   }
251
252
253   /** Returns all proxy offers of the given service type */
254   public Hashtable getProxyOffers(String JavaDoc serviceType)
255   {
256     Hashtable result = null;
257
258     OfferList list = (OfferList)m_offerLists.get(serviceType);
259     if (list != null)
260       result = list.getProxyOffers();
261
262     return result;
263   }
264
265
266   /** Returns all proxy offer IDs of the given service type */
267   public String JavaDoc[] getProxyOfferIds(String JavaDoc serviceType)
268   {
269     String JavaDoc[] result = null;
270
271     OfferList list = (OfferList)m_offerLists.get(serviceType);
272     if (list != null)
273       result = list.getProxyOfferIds();
274
275     return result;
276   }
277
278
279   /** Returns the service type of the given offer */
280   public String JavaDoc whichService(String JavaDoc offerId)
281   {
282     return OfferList.whichService(offerId);
283   }
284 }
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
Popular Tags