1 2 5 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 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 LIST_NAME = "OFR_"; 42 private static String LIST_EXT = ".dat"; 43 private static String INDEX_FILE = "offeridx.dat"; 44 45 46 private OfferDatabaseImpl() 47 { 48 } 49 50 51 public OfferDatabaseImpl(String 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 m_offerLists = new Hashtable(); 66 67 m_lock = new RWLock(); 68 } 69 70 71 72 public boolean validateOfferId(String offerId) 73 { 74 return OfferList.validateOfferId(offerId); 75 } 76 77 78 79 public boolean isTypeSupported(org.omg.CORBA.Any any) 80 { 81 boolean result; 82 83 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 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 ("Invalid lock mode"); 105 } 106 107 108 109 public void end() 110 { 111 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 129 public boolean exists(String 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 142 public boolean isProxy(String 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 155 public String create( 156 String serviceType, 157 org.omg.CORBA.Object 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 172 public String createProxy( 173 Lookup target, 174 String serviceType, 175 Property[] props, 176 boolean ifMatchAll, 177 String 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 189 public void remove(String offerId) 190 { 191 OfferList list = getList(whichService(offerId)); 192 if (list != null) 193 list.remove(offerId); 194 } 195 196 197 198 public void removeProxy(String offerId) 199 { 200 OfferList list = getList(whichService(offerId)); 201 if (list != null) 202 list.removeProxy(offerId); 203 } 204 205 206 207 public OfferInfo describe(String 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 220 public ProxyInfo describeProxy(String 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 233 public void modify(String offerId, Property[] props) 234 { 235 OfferList list = getList(whichService(offerId)); 236 if (list != null) 237 list.modify(offerId, props); 238 } 239 240 241 242 public Hashtable getOffers(String 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 255 public String [] getOfferIds(String serviceType) 256 { 257 String [] result = null; 258 259 OfferList list = getList(serviceType); 260 if (list != null) 261 result = list.getOfferIds(); 262 263 return result; 264 } 265 266 267 268 public Hashtable getProxyOffers(String 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 281 public String [] getProxyOfferIds(String serviceType) 282 { 283 String [] result = null; 284 285 OfferList list = getList(serviceType); 286 if (list != null) 287 result = list.getProxyOfferIds(); 288 289 return result; 290 } 291 292 293 294 public String whichService(String offerId) 295 { 296 return OfferList.whichService(offerId); 297 } 298 299 300 305 protected synchronized OfferList getList(String 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 serviceType) 321 { 322 OfferList result = new OfferList(serviceType); 323 m_offerLists.put(serviceType, result); 324 m_counter++; 325 m_offerIndex.put(serviceType, new Integer (m_counter)); 326 m_indexDirty = true; 327 return result; 328 } 329 330 331 protected OfferList readList(String 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 (e.getMessage()); 345 } 346 catch (ClassNotFoundException e) { 347 throw new RuntimeException (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 ("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 (e.getMessage()); 368 } 369 } 370 371 372 protected File getListFile(String serviceType) 373 { 374 File result = null; 375 376 Integer counter = (Integer )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 i = (Integer )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 (e.getMessage()); 399 } 400 catch (ClassNotFoundException e) { 401 throw new RuntimeException (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 (m_counter)); 412 objOut.writeObject(m_offerIndex); 413 fileOut.close(); 414 } 415 catch (IOException e) { 416 throw new RuntimeException (e.getMessage()); 417 } 418 } 419 } 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 | Popular Tags |