KickJava   Java API By Example, From Geeks To Geeks.

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


1 // Copyright (C) 1998-1999
2
// Object Oriented Concepts, Inc.
3
// **********************************************************************
4
//
5
// Copyright (c) 1997
6
// Mark Spruiell (mark@intellisoft.com)
7
//
8
// See the COPYING file for more information
9
//
10
// **********************************************************************
11

12 package org.jacorb.trading.db.simple.offers;
13
14 import java.io.*;
15 import java.util.*;
16 import org.omg.CORBA.*;
17 import org.omg.CosTrading.Lookup;
18 import org.omg.CosTrading.RegisterPackage.*;
19 import org.omg.CosTrading.ProxyPackage.*;
20 import org.omg.CosTrading.Property;
21 import org.omg.CosTrading.Policy;
22
23 public class OfferList implements Serializable
24 {
25   private String JavaDoc m_serviceType;
26   private Hashtable m_offers;
27   private Hashtable m_proxies;
28   private int m_nextId;
29   private transient boolean m_dirty = true;
30   private static final char ID_SEP = '/';
31
32   static final long serialVersionUID = -921492441298318523L;
33
34
35   private OfferList()
36   {
37   }
38
39
40   public OfferList(String JavaDoc serviceType)
41   {
42     m_serviceType = serviceType;
43     m_offers = new Hashtable();
44     m_proxies = new Hashtable();
45     m_nextId = 1;
46   }
47
48
49   public String JavaDoc getServiceType()
50   {
51     return m_serviceType;
52   }
53
54
55   /** Returns true if the offerId is legal, false otherwise */
56   public static boolean validateOfferId(String JavaDoc offerId)
57   {
58     boolean result = false;
59
60     if (offerId != null) {
61         // not a very complete check
62
int index = offerId.indexOf(ID_SEP);
63       result = (index > 0);
64     }
65
66     return result;
67   }
68
69
70   public boolean exists(String JavaDoc offerId)
71   {
72     return (m_offers.containsKey(offerId) || m_proxies.containsKey(offerId));
73   }
74
75
76   public boolean isProxy(String JavaDoc offerId)
77   {
78     return m_proxies.containsKey(offerId);
79   }
80
81
82   public String JavaDoc create(org.omg.CORBA.Object JavaDoc obj, Property[] props)
83   {
84     String JavaDoc result = null;
85
86     result = m_serviceType + ID_SEP + m_nextId;
87     m_nextId++;
88     Offer offer = new Offer(result, obj, props);
89     m_offers.put(result, offer);
90     m_dirty = true;
91     return result;
92   }
93
94
95   public String JavaDoc createProxy(
96     Lookup target,
97     Property[] props,
98     boolean ifMatchAll,
99     String JavaDoc recipe,
100     Policy[] policies)
101   {
102     String JavaDoc result = null;
103
104     result = m_serviceType + ID_SEP + m_nextId;
105     m_nextId++;
106     ProxyOffer proxy =
107       new ProxyOffer(result, target, props, ifMatchAll, recipe, policies);
108     m_proxies.put(result, proxy);
109
110     m_dirty = true;
111
112     return result;
113   }
114
115
116   public void remove(String JavaDoc offerId)
117   {
118     if (m_offers.containsKey(offerId)) {
119       m_offers.remove(offerId);
120       m_dirty = true;
121     }
122   }
123
124
125   public void removeProxy(String JavaDoc offerId)
126   {
127     if (m_proxies.containsKey(offerId)) {
128       m_proxies.remove(offerId);
129       m_dirty = true;
130     }
131   }
132
133
134   public OfferInfo describe(String JavaDoc offerId)
135   {
136     OfferInfo result = null;
137
138     Offer offer = (Offer)m_offers.get(offerId);
139     if (offer != null) {
140       result = offer.describe();
141       result.type = m_serviceType;
142     }
143
144     return result;
145   }
146
147
148   public ProxyInfo describeProxy(String JavaDoc offerId)
149   {
150     ProxyInfo result = null;
151
152     ProxyOffer proxy = (ProxyOffer)m_proxies.get(offerId);
153     if (proxy != null) {
154       result = proxy.describe();
155       result.type = m_serviceType;
156     }
157
158     return result;
159   }
160
161
162   public boolean modify(String JavaDoc offerId, Property[] props)
163   {
164     boolean result = false;
165
166     Offer offer = (Offer)m_offers.get(offerId);
167     if (offer != null) {
168       offer.modify(props);
169       result = true;
170       m_dirty = true;
171     }
172
173     return result;
174   }
175
176
177   public Hashtable getOffers()
178   {
179     Hashtable result = new Hashtable();
180
181     Enumeration e = m_offers.keys();
182     while (e.hasMoreElements()) {
183       String JavaDoc offerId = (String JavaDoc)e.nextElement();
184       Offer offer = (Offer)m_offers.get(offerId);
185       OfferInfo info = offer.describe();
186       info.type = m_serviceType;
187       result.put(offerId, info);
188     }
189
190     return result;
191   }
192
193
194   public String JavaDoc[] getOfferIds()
195   {
196     String JavaDoc[] result = new String JavaDoc[m_offers.size()];
197
198     int count = 0;
199     Enumeration e = m_offers.keys();
200     while (e.hasMoreElements())
201       result[count++] = (String JavaDoc)e.nextElement();
202
203     return result;
204   }
205
206
207   public Hashtable getProxyOffers()
208   {
209     Hashtable result = new Hashtable();
210
211     Enumeration e = m_proxies.keys();
212     while (e.hasMoreElements()) {
213       String JavaDoc offerId = (String JavaDoc)e.nextElement();
214       ProxyOffer proxy = (ProxyOffer)m_proxies.get(offerId);
215       ProxyInfo info = proxy.describe();
216       info.type = m_serviceType;
217       result.put(offerId, info);
218     }
219
220     return result;
221   }
222
223
224   public String JavaDoc[] getProxyOfferIds()
225   {
226     String JavaDoc[] result = new String JavaDoc[m_proxies.size()];
227
228     int count = 0;
229     Enumeration e = m_proxies.keys();
230     while (e.hasMoreElements())
231       result[count++] = (String JavaDoc)e.nextElement();
232
233     return result;
234   }
235
236
237   public static String JavaDoc whichService(String JavaDoc offerId)
238   {
239     String JavaDoc result = null;
240
241     int index = offerId.indexOf(ID_SEP);
242     if (index > 0)
243       result = offerId.substring(0, index);
244
245     return result;
246   }
247
248
249   public int hashCode()
250   {
251     return m_serviceType.hashCode();
252   }
253
254
255   public boolean equals(java.lang.Object JavaDoc o)
256   {
257     OfferList list = (OfferList)o;
258     return m_serviceType.equals(list.m_serviceType);
259   }
260
261
262   public boolean getDirty()
263   {
264     return m_dirty;
265   }
266
267
268   private void writeObject(ObjectOutputStream out)
269     throws IOException
270   {
271     out.defaultWriteObject();
272     m_dirty = false;
273   }
274
275
276   private void readObject(ObjectInputStream in)
277     throws IOException, ClassNotFoundException JavaDoc
278   {
279     in.defaultReadObject();
280     m_dirty = false;
281   }
282 }
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
Popular Tags