1 2 5 14 package org.jacorb.trading.impl; 15 16 import java.io.*; 17 import java.util.*; 18 import org.omg.CORBA.*; 19 import org.omg.CosTrading.*; 20 import org.omg.CosTradingRepos.ServiceTypeRepositoryPackage.*; 21 import org.jacorb.trading.db.TypeDatabase; 22 import org.jacorb.trading.util.*; 23 24 25 28 public class RepositoryImpl 29 extends org.omg.CosTradingRepos.ServiceTypeRepositoryPOA 30 { 31 private TypeDatabase m_database; 32 private org.omg.CORBA.Repository m_interfaceRepos; 33 34 35 public RepositoryImpl( 36 TypeDatabase db, 37 org.omg.CORBA.Repository interfaceRepos) 38 { 39 m_database = db; 40 m_interfaceRepos = interfaceRepos; 41 } 42 43 44 public IncarnationNumber incarnation() 45 { 46 IncarnationNumber result; 47 48 m_database.begin(TypeDatabase.READ); 49 result = m_database.getIncarnation(); 50 m_database.end(); 51 52 return result; 53 } 54 55 56 public IncarnationNumber add_type( 57 String name, 58 String if_name, 59 PropStruct[] props, 60 String [] super_types) 61 throws IllegalServiceType, 62 ServiceTypeExists, 63 InterfaceTypeMismatch, 64 IllegalPropertyName, 65 DuplicatePropertyName, 66 ValueTypeRedefinition, 67 UnknownServiceType, 68 DuplicateServiceTypeName 69 { 70 IncarnationNumber result = null; 71 72 try { 73 m_database.begin(TypeDatabase.WRITE); 74 75 checkTypeName(name); 76 77 if (m_database.describeType(name) != null) 79 throw new ServiceTypeExists(name); 80 81 Hashtable typeInfo = new Hashtable(); 83 84 for (int i = 0; i < super_types.length; i++) { 86 TypeStruct ts = findType(super_types[i]); 87 if (typeInfo.containsKey(super_types[i])) 88 throw new DuplicateServiceTypeName(super_types[i]); 89 typeInfo.put(super_types[i], ts); 90 } 91 92 94 for (int i = 0; i < super_types.length; i++) { 95 String [] names = m_database.getAllSuperTypes(super_types[i]); 96 for (int n = 0; n < names.length; n++) { 97 if (! typeInfo.containsKey(names[n])) { 98 TypeStruct ts = findType(names[n]); 99 if (ts == null) 100 throw new UnknownServiceType(names[n]); 101 typeInfo.put(names[n], ts); 102 } 103 } 104 } 105 106 validateInterface(name, if_name, super_types, typeInfo); 108 109 validateProperties(name, props, super_types, typeInfo); 111 112 result = m_database.createType(name, if_name, props, super_types); 113 } 114 finally { 115 m_database.end(); 116 } 117 118 return result; 119 } 120 121 122 public void remove_type(String name) 123 throws IllegalServiceType, 124 UnknownServiceType, 125 HasSubTypes 126 { 127 try { 128 m_database.begin(TypeDatabase.WRITE); 129 130 checkTypeName(name); 131 132 String subTypeName = m_database.findSubType(name); 133 134 if (subTypeName != null) 135 throw new HasSubTypes(name, subTypeName); 136 137 if (! m_database.removeType(name)) 138 throw new UnknownServiceType(name); 139 } 140 finally { 141 m_database.end(); 142 } 143 } 144 145 146 public String [] list_types( 147 SpecifiedServiceTypes which_types) 148 { 149 String [] result = null; 150 151 try { 152 m_database.begin(TypeDatabase.READ); 153 154 if (which_types.discriminator() == ListOption.all) 155 result = m_database.getTypes(); 156 else { 157 IncarnationNumber inc = which_types.incarnation(); 158 result = m_database.getTypesSince(inc); 159 } 160 } 161 finally { 162 m_database.end(); 163 } 164 165 return result; 166 } 167 168 169 public TypeStruct describe_type(String name) 170 throws IllegalServiceType, 171 UnknownServiceType 172 { 173 TypeStruct result; 174 175 try { 176 m_database.begin(TypeDatabase.READ); 177 178 checkTypeName(name); 179 180 result = findType(name); 181 } 182 finally { 183 m_database.end(); 184 } 185 186 return result; 187 } 188 189 190 public TypeStruct fully_describe_type(String name) 191 throws IllegalServiceType, 192 UnknownServiceType 193 { 194 TypeStruct result = null; 195 Enumeration e; 196 197 try { 198 m_database.begin(TypeDatabase.READ); 199 200 checkTypeName(name); 201 202 result = findType(name); 203 204 String [] superTypeNames = m_database.getAllSuperTypes(name); 205 206 result.super_types = superTypeNames; 208 209 Vector desc = new Vector(); 211 for (int i = 0; i < superTypeNames.length; i++) { 212 TypeStruct ts = findType(superTypeNames[i]); 213 desc.addElement(ts); 214 } 215 216 Vector props = new Vector(); 219 for (int i = 0; i < result.props.length; i++) 220 props.addElement(result.props[i]); 221 222 e = desc.elements(); 223 while (e.hasMoreElements()) { 224 TypeStruct ts = (TypeStruct)e.nextElement(); 225 for (int i = 0; i < ts.props.length; i++) { 226 boolean found = false; 229 Enumeration p = props.elements(); 230 while (p.hasMoreElements() && ! found) { 231 PropStruct ps = (PropStruct)p.nextElement(); 232 if (ts.props[i].name.equals(ps.name)) 233 found = true; 234 } 235 236 if (! found) 237 props.addElement(ts.props[i]); 238 } 239 } 240 241 result.props = new PropStruct[props.size()]; 242 props.copyInto((java.lang.Object [])result.props); 243 } 244 finally { 245 m_database.end(); 246 } 247 248 return result; 249 } 250 251 252 public void mask_type(String name) 253 throws IllegalServiceType, 254 UnknownServiceType, 255 AlreadyMasked 256 { 257 try { 258 m_database.begin(TypeDatabase.WRITE); 259 260 checkTypeName(name); 261 262 TypeStruct ts = findType(name); 263 264 if (ts.masked) 265 throw new AlreadyMasked(name); 266 else if (! m_database.maskType(name)) 267 throw new UnknownServiceType(name); 268 } 269 finally { 270 m_database.end(); 271 } 272 } 273 274 275 public void unmask_type(String name) 276 throws IllegalServiceType, 277 UnknownServiceType, 278 NotMasked 279 { 280 try { 281 m_database.begin(TypeDatabase.WRITE); 282 283 checkTypeName(name); 284 285 TypeStruct ts = findType(name); 286 287 if (! ts.masked) 288 throw new NotMasked(name); 289 else if (! m_database.unmaskType(name)) 290 throw new UnknownServiceType(name); 291 } 292 finally { 293 m_database.end(); 294 } 295 } 296 297 298 protected void checkTypeName(String name) 299 throws IllegalServiceType 300 { 301 if (name == null || name.trim().length() == 0) 302 throw new IllegalServiceType(""); 303 304 StringTokenizer tokenizer = new StringTokenizer(name, ":", true); 305 boolean seenIdent = false; 306 String lastToken = null; 307 int colonCount = 0; 308 309 while (tokenizer.hasMoreTokens()) { 310 String tok = tokenizer.nextToken(); 311 if (tok.equals(":")) { 312 colonCount++; 313 if (colonCount > 2) 314 throw new IllegalServiceType(name); 315 } 316 else { 317 colonCount = 0; 318 seenIdent = true; 319 320 if (! Character.isLetter(tok.charAt(0))) 322 throw new IllegalServiceType(name); 323 324 for (int i = 1; i < tok.length(); i++) { 325 char ch = tok.charAt(i); 326 if (! Character.isLetterOrDigit(ch) && ch != '_') 327 throw new IllegalServiceType(name); 328 } 329 } 330 331 lastToken = tok; 332 } 333 334 if (! seenIdent) 335 throw new IllegalServiceType(name); 336 337 if (lastToken.equals(":")) 338 throw new IllegalServiceType(name); 339 } 340 341 342 protected TypeStruct findType(String name) 343 throws UnknownServiceType 344 { 345 TypeStruct result = m_database.describeType(name); 346 347 if (result == null) 348 throw new UnknownServiceType(name); 349 350 return result; 351 } 352 353 354 protected void validateInterface( 355 String name, 356 String interfaceName, 357 String [] superTypes, 358 Hashtable typeInfo) 359 throws InterfaceTypeMismatch 360 { 361 if (m_interfaceRepos != null) { 364 org.omg.CORBA.InterfaceDef def = null; 365 366 try { 368 org.omg.CORBA.Contained c = m_interfaceRepos.lookup(interfaceName); 369 if (c != null) 370 def = org.omg.CORBA.InterfaceDefHelper.narrow(c); 371 } 372 catch (org.omg.CORBA.SystemException e) { 373 } 375 376 if (def != null) { 377 for (int i = 0; i < superTypes.length; i++) { 378 try { 380 TypeStruct ts = (TypeStruct)typeInfo.get(superTypes[i]); 381 org.omg.CORBA.Contained c = m_interfaceRepos.lookup(ts.if_name); 382 if (c != null) { 383 String id = c.id(); 384 if (! def.is_a(id)) 385 throw new InterfaceTypeMismatch(superTypes[i], ts.if_name, 386 name, interfaceName); 387 } 388 } 389 catch (org.omg.CORBA.SystemException e) { 390 } 392 } 393 } 394 } 395 } 396 397 398 protected void validateProperties( 399 String name, 400 PropStruct[] props, 401 String [] superTypes, 402 Hashtable typeInfo) 403 throws IllegalPropertyName, 404 DuplicatePropertyName, 405 ValueTypeRedefinition 406 { 407 Vector goodProps = new Vector(); 408 409 for (int i = 0; i < props.length; i++) { 412 if (goodProps.contains(props[i].name)) 414 throw new DuplicatePropertyName(props[i].name); 415 416 for (int s = 0; s < superTypes.length; s++) { 420 TypeStruct ts = (TypeStruct)typeInfo.get(superTypes[s]); 421 422 Vector superProps = new Vector(); 423 findProperties(ts, props[i].name, superProps, typeInfo); 424 425 Enumeration e = superProps.elements(); 426 while (e.hasMoreElements()) { 427 PropStruct ps = (PropStruct)e.nextElement(); 428 429 if (! props[i].value_type.equal(ps.value_type) || 432 ! validateMode(props[i].mode, ps.mode)) 433 throw new ValueTypeRedefinition(name, props[i], superTypes[s], ps); 434 } 435 } 436 437 goodProps.addElement(props[i].name); 440 } 441 } 442 443 444 protected boolean validateMode(PropertyMode subMode, PropertyMode superMode) 445 { 446 boolean result = false; 447 448 if (superMode == subMode) 450 result = true; 451 else if (superMode == PropertyMode.PROP_NORMAL) 453 result = true; 454 else if (superMode == PropertyMode.PROP_READONLY && 455 (subMode == PropertyMode.PROP_READONLY || 456 subMode == PropertyMode.PROP_MANDATORY_READONLY)) 457 result = true; 458 else if (superMode == PropertyMode.PROP_MANDATORY && 459 (subMode == PropertyMode.PROP_MANDATORY || 460 subMode == PropertyMode.PROP_MANDATORY_READONLY)) 461 result = true; 462 else if ( superMode == PropertyMode.PROP_MANDATORY_READONLY && 464 subMode == PropertyMode.PROP_MANDATORY_READONLY) 465 result = true; 466 467 return result; 468 } 469 470 471 474 protected void findProperties( 475 TypeStruct ts, 476 String propName, 477 Vector v, 478 Hashtable typeInfo) 479 { 480 PropStruct prop = findProperty(ts, propName); 481 if (prop != null) 482 v.addElement(prop); 483 else { 484 for (int i = 0; i < ts.super_types.length; i++) { 487 TypeStruct superTS = (TypeStruct)typeInfo.get(ts.super_types[i]); 488 findProperties(superTS, propName, v, typeInfo); 489 } 490 } 491 } 492 493 494 protected PropStruct findProperty(TypeStruct ts, String name) 495 { 496 PropStruct result = null; 497 498 for (int i = 0; i < ts.props.length && result == null; i++) 499 if (name.equals(ts.props[i].name)) 500 result = ts.props[i]; 501 502 return result; 503 } 504 } 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 | Popular Tags |