1 2 5 14 package org.jacorb.trading.impl; 15 16 import java.util.*; 17 import org.omg.CORBA.*; 18 import org.omg.CosTrading.*; 19 import org.omg.CosTrading.RegisterPackage.*; 20 import org.omg.CosTradingRepos.*; 21 import org.omg.CosTradingRepos.ServiceTypeRepositoryPackage.*; 22 import org.omg.CosTradingDynamic.*; 23 import org.jacorb.trading.constraint.*; 24 import org.jacorb.trading.db.OfferDatabase; 25 import org.jacorb.trading.util.*; 26 27 30 public class RegisterImpl extends org.omg.CosTrading.RegisterPOA 31 { 32 private TraderComp m_traderComp; 33 private SupportAttrib m_support; 34 private OfferDatabase m_db; 35 private ServiceTypeRepository m_repos; 36 private org.omg.CORBA.Repository m_interfaceRepos; 37 38 39 private RegisterImpl() 40 { 41 } 42 43 44 public RegisterImpl( 45 TraderComp traderComp, 46 SupportAttrib supportAttrib, 47 OfferDatabase db, 48 org.omg.CORBA.Repository interfaceRepos) 49 { 50 m_traderComp = traderComp; 51 m_support = supportAttrib; 52 m_db = db; 53 m_interfaceRepos = interfaceRepos; 54 org.omg.CORBA.Object obj = supportAttrib.getTypeRepos(); 55 m_repos = ServiceTypeRepositoryHelper.narrow(obj); 56 } 57 58 59 61 public Lookup lookup_if() 62 { 63 return m_traderComp.getLookupInterface(); 64 } 65 66 67 public Register register_if() 68 { 69 return m_traderComp.getRegisterInterface(); 70 } 71 72 73 public Link link_if() 74 { 75 return m_traderComp.getLinkInterface(); 76 } 77 78 79 public Proxy proxy_if() 80 { 81 return m_traderComp.getProxyInterface(); 82 } 83 84 85 public Admin admin_if() 86 { 87 return m_traderComp.getAdminInterface(); 88 } 89 90 91 93 public boolean supports_modifiable_properties() 94 { 95 return m_support.getModifiableProperties(); 96 } 97 98 99 public boolean supports_dynamic_properties() 100 { 101 return m_support.getDynamicProperties(); 102 } 103 104 105 public boolean supports_proxy_offers() 106 { 107 return m_support.getProxyOffers(); 108 } 109 110 111 public org.omg.CORBA.Object type_repos() 112 { 113 return m_support.getTypeRepos(); 114 } 115 116 117 118 120 public String export( 121 org.omg.CORBA.Object reference, 122 String type, 123 Property[] properties) 124 throws InvalidObjectRef, 125 IllegalServiceType, 126 UnknownServiceType, 127 org.omg.CosTrading.RegisterPackage.InterfaceTypeMismatch, 128 IllegalPropertyName, 129 PropertyTypeMismatch, 130 ReadonlyDynamicProperty, 131 MissingMandatoryProperty, 132 DuplicatePropertyName 133 { 134 135 String result = null; 136 137 if (reference == null) 138 throw new InvalidObjectRef(reference); 139 140 TypeStruct ts = m_repos.fully_describe_type(type); 143 144 if (ts.masked) 146 throw new UnknownServiceType(type); 147 148 validateInterface(reference, type, ts); 150 151 OfferUtil.validateProperties(m_db, properties, type, ts); 155 156 158 m_db.begin(OfferDatabase.WRITE); 159 160 try { 161 result = m_db.create(type, reference, properties); 162 } 163 finally { 164 m_db.end(); 165 } 166 167 return result; 168 } 169 170 171 public void withdraw(String id) 172 throws IllegalOfferId, 173 UnknownOfferId, 174 ProxyOfferId 175 { 176 if (! m_db.validateOfferId(id)) 177 throw new IllegalOfferId(id); 178 179 m_db.begin(OfferDatabase.WRITE); 180 181 try { 182 if (! m_db.exists(id)) 183 throw new UnknownOfferId(id); 184 185 if (m_db.isProxy(id)) 186 throw new ProxyOfferId(id); 187 188 m_db.remove(id); 189 } 190 finally { 191 m_db.end(); 192 } 193 } 194 195 196 public OfferInfo describe(String id) 197 throws IllegalOfferId, 198 UnknownOfferId, 199 ProxyOfferId 200 { 201 OfferInfo result; 202 203 if (! m_db.validateOfferId(id)) 204 throw new IllegalOfferId(id); 205 206 207 m_db.begin(OfferDatabase.READ); 208 209 try { 210 if (! m_db.exists(id)) 211 throw new UnknownOfferId(id); 212 213 if (m_db.isProxy(id)) 214 throw new ProxyOfferId(id); 215 216 result = m_db.describe(id); 217 } 218 finally { 219 m_db.end(); 220 } 221 222 return result; 223 } 224 225 226 public void modify( 227 String id, 228 String [] del_list, 229 Property[] modify_list) 230 throws NotImplemented, 231 IllegalOfferId, 232 UnknownOfferId, 233 ProxyOfferId, 234 IllegalPropertyName, 235 UnknownPropertyName, 236 PropertyTypeMismatch, 237 ReadonlyDynamicProperty, 238 MandatoryProperty, 239 ReadonlyProperty, 240 DuplicatePropertyName 241 { 242 if (! m_support.getModifiableProperties()) 243 throw new NotImplemented(); 244 245 if (! m_db.validateOfferId(id)) 246 throw new IllegalOfferId(id); 247 248 try { 249 m_db.begin(OfferDatabase.WRITE); 250 251 if (! m_db.exists(id)) 252 throw new UnknownOfferId(id); 253 254 if (m_db.isProxy(id)) 255 throw new ProxyOfferId(id); 256 257 String type = m_db.whichService(id); 258 259 TypeStruct ts = m_repos.fully_describe_type(type); 262 263 OfferInfo info = m_db.describe(id); 264 265 checkDelete(del_list, type, ts, info); 268 269 checkModify(modify_list, type, ts, info); 272 273 281 Vector props = new Vector(); 282 for (int i = 0; i < modify_list.length; i++) 283 props.addElement(modify_list[i]); 284 285 for (int i = 0; i < info.properties.length; i++) { 286 boolean found = false; 287 288 for (int d = 0; d < del_list.length && ! found; d++) 290 if (info.properties[i].name.equals(del_list[d])) 291 found = true; 292 293 Enumeration e = props.elements(); 295 while (e.hasMoreElements() && ! found) { 296 Property p = (Property)e.nextElement(); 297 if (info.properties[i].name.equals(p.name)) 298 found = true; 299 } 300 301 if (! found) 304 props.addElement(info.properties[i]); 305 } 306 307 Property[] arr = new Property[props.size()]; 309 props.copyInto((java.lang.Object [])arr); 310 311 m_db.modify(id, arr); 313 } 314 catch (IllegalServiceType e) { 315 throw new UnknownOfferId(id); 316 } 317 catch (UnknownServiceType e) { 318 throw new UnknownOfferId(id); 319 } 320 finally { 321 m_db.end(); 322 } 323 } 324 325 326 public void withdraw_using_constraint(String type, String constr) 327 throws IllegalServiceType, 328 UnknownServiceType, 329 IllegalConstraint, 330 NoMatchingOffers 331 { 332 TypeStruct ts = m_repos.fully_describe_type(type); 335 336 SchemaAdapter schema = new SchemaAdapter(ts); 338 339 Constraint c = new Constraint(schema); 340 341 int count = 0; 342 343 try { 344 m_db.begin(OfferDatabase.WRITE); 345 346 c.parse(constr); 348 349 Hashtable offers = m_db.getOffers(type); 353 354 if (offers != null) { 355 Enumeration e = offers.keys(); 356 while (e.hasMoreElements()) { 357 String id = (String )e.nextElement(); 358 OfferInfo info = (OfferInfo)offers.get(id); 359 360 SourceAdapter source = 361 new SourceAdapter(info.reference, info.properties); 362 363 365 if (c.evaluate(source)) { 366 m_db.remove(id); 367 count++; 368 } 369 } 370 } 371 } 372 catch (ParseException ex) { 373 System.out.println("Illegal constraint '" + constr + "'"); 375 System.out.println(ex.getMessage()); 376 throw new IllegalConstraint(constr); 377 } 378 finally { 379 m_db.end(); 380 } 381 382 if (count == 0) 383 throw new NoMatchingOffers(constr); 384 } 385 386 387 public Register resolve(String [] name) 388 throws IllegalTraderName, 389 UnknownTraderName, 390 RegisterNotSupported 391 { 392 throw new RegisterNotSupported(name); 393 } 394 395 396 protected void validateInterface( 397 org.omg.CORBA.Object ref, 398 String typeName, 399 TypeStruct type) 400 throws org.omg.CosTrading.RegisterPackage.InterfaceTypeMismatch 401 { 402 405 409 if (m_interfaceRepos != null) { 410 try { 412 org.omg.CORBA.InterfaceDef def = InterfaceDefHelper.narrow(ref._get_interface_def()); 413 org.omg.CORBA.Contained c = m_interfaceRepos.lookup(type.if_name); 414 415 if (def != null && c != null) { 418 String id = c.id(); 419 if (! def.is_a(id)) 420 throw new org.omg.CosTrading.RegisterPackage.InterfaceTypeMismatch( 421 typeName, ref); 422 } 423 } 424 catch (org.omg.CORBA.SystemException e) { 425 } 427 } 428 } 429 430 431 protected void checkDelete( 432 String [] del_list, 433 String typeName, 434 TypeStruct type, 435 OfferInfo info) 436 throws IllegalPropertyName, 437 UnknownPropertyName, 438 MandatoryProperty, 439 ReadonlyProperty, 440 DuplicatePropertyName 441 { 442 Hashtable offerProps = new Hashtable(); 444 for (int i = 0; i < info.properties.length; i++) 445 offerProps.put(info.properties[i].name, info.properties[i]); 446 447 Hashtable typeProps = new Hashtable(); 449 for (int i = 0; i < type.props.length; i++) 450 typeProps.put(type.props[i].name, type.props[i]); 451 452 Hashtable deletedProps = new Hashtable(); 454 455 for (int i = 0; i < del_list.length; i++) { 459 String propName = del_list[i]; 460 461 if (deletedProps.containsKey(propName)) 463 throw new DuplicatePropertyName(propName); 464 465 deletedProps.put(propName, propName); 466 467 Property prop = (Property)offerProps.get(propName); 469 470 if (prop != null) { 471 PropStruct ps = (PropStruct)typeProps.get(propName); 472 if (ps != null) { 473 if (OfferUtil.isMandatory(ps.mode)) 474 throw new MandatoryProperty(typeName, propName); 475 } 476 } 477 else 478 throw new UnknownPropertyName(propName); 479 } 480 } 481 482 483 protected void checkModify( 484 Property[] modify_list, 485 String typeName, 486 TypeStruct type, 487 OfferInfo info) 488 throws IllegalPropertyName, 489 UnknownPropertyName, 490 PropertyTypeMismatch, 491 ReadonlyDynamicProperty, 492 ReadonlyProperty, 493 DuplicatePropertyName 494 { 495 Hashtable modProps = new Hashtable(); 497 for (int i = 0; i < modify_list.length; i++) 498 modProps.put(modify_list[i].name, modify_list[i]); 499 500 Hashtable offerProps = new Hashtable(); 502 for (int i = 0; i < info.properties.length; i++) 503 offerProps.put(info.properties[i].name, info.properties[i]); 504 505 Hashtable typeProps = new Hashtable(); 507 for (int i = 0; i < type.props.length; i++) 508 typeProps.put(type.props[i].name, type.props[i]); 509 510 Hashtable checkedProps = new Hashtable(); 512 513 514 517 Enumeration e = modProps.elements(); 518 while (e.hasMoreElements()) { 519 Property prop = (Property)e.nextElement(); 520 521 if (checkedProps.containsKey(prop.name)) 522 throw new DuplicatePropertyName(prop.name); 523 524 checkedProps.put(prop.name, prop); 525 526 PropStruct ps = (PropStruct)typeProps.get(prop.name); 528 if (ps == null) 529 throw new UnknownPropertyName(prop.name); 530 531 if (offerProps.containsKey(prop.name) && OfferUtil.isReadonly(ps.mode)) 534 throw new ReadonlyProperty(typeName, prop.name); 535 536 OfferUtil.checkProperty(typeName, prop, ps); 537 } 538 } 539 } 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 | Popular Tags |