1 23 24 package com.sun.enterprise.deployment; 25 26 import java.io.*; 27 import java.util.*; 28 import java.util.logging.*; 29 import java.lang.reflect.*; 30 31 import com.sun.enterprise.util.TypeUtil; 32 import com.sun.enterprise.util.*; 33 import com.sun.enterprise.deployment.xml.*; 34 import com.sun.enterprise.deployment.interfaces.QueryParser; 35 import com.sun.enterprise.deployment.runtime.*; 36 import com.sun.enterprise.deployment.util.LogDomains; 37 38 41 42 public class IASEjbCMPEntityDescriptor extends EjbCMPEntityDescriptor { 43 44 private Class ejbClass = null; 45 private String pcImplClassName = null; 46 private String concreteImplClassName = null; 47 private String ejbImplClassName = null; 48 private String mappingProperties; 49 private ClassLoader jcl = null; 50 private String uniqueName = null; 51 52 private String moduleDir = null; 53 54 private static LocalStringManagerImpl localStrings = 56 new LocalStringManagerImpl(IASEjbCMPEntityDescriptor.class); 57 private static final Logger _logger = LogDomains.getLogger(LogDomains.DPL_LOGGER); 58 59 private static final char DOT = '.'; private static final char LIST_START = '('; private static final char LIST_END = ')'; private static final char LIST_SEPARATOR = ','; private static final char NAME_PART_SEPARATOR = '_'; private static final char NAME_CONCATENATOR = ' '; private static final String FIND = "find"; private static final String EJB_SELECT = "ejbSelect"; private static final String JDOSTATE = "_JDOState"; private static final String CONCRETE_IMPL = "_ConcreteImpl"; private static final String MAPPINGEXT = DOT + "mapping"; 72 private Collection finders = null; 73 private Collection selectors = null; 74 private QueryParser queryParser = null; 75 private PrefetchDisabledDescriptor prefetchDisabledDescriptor = null; 76 private static final Map conversionTable = createConversionTable(); 77 private Map oneOneFinders = new HashMap(); 78 private List arrOneOneFinders = new ArrayList(); 79 80 private void addAllInterfaceMethodsIn(Collection methodDescriptors, Class c) { 81 Method[] methods = c.getMethods(); 82 for (int i=0; i<methods.length; i++) { 83 methodDescriptors.add(methods[i]); 84 } 85 } 86 87 private void addAllUniqueInterfaceMethodsIn(Collection methodDescriptors, Class c) { 88 Method[] methods = c.getMethods(); 89 for (int i=0; i<methods.length; i++) { 90 if(findEquivalentMethod(methodDescriptors, methods[i]) == null) 91 methodDescriptors.add(methods[i]); 92 } 93 } 94 95 public Collection getAllUniqueMethods() { 96 HashSet methods = new HashSet(); 97 98 try { 99 if (isRemoteInterfacesSupported()) { 100 addAllUniqueInterfaceMethodsIn(methods, jcl.loadClass(getHomeClassName())); 101 addAllUniqueInterfaceMethodsIn(methods, jcl.loadClass(getRemoteClassName())); 102 } 103 if (isLocalInterfacesSupported()) { 104 addAllUniqueInterfaceMethodsIn(methods, jcl.loadClass(getLocalHomeClassName())); 105 addAllUniqueInterfaceMethodsIn(methods, jcl.loadClass(getLocalClassName())); 106 } 107 } catch (Throwable t) { 108 _logger.log( Level.WARNING, 109 "enterprise.deployment_error_loading_class_excp", t ); throw new RuntimeException (t.getMessage()); 111 } 112 return methods; 113 114 } 115 116 public Collection getAllMethods() { 117 118 HashSet methods = new HashSet(); 119 120 try { 121 if (isRemoteInterfacesSupported()) { 122 addAllInterfaceMethodsIn(methods, jcl.loadClass(getHomeClassName())); 123 addAllInterfaceMethodsIn(methods, jcl.loadClass(getRemoteClassName())); 124 } 125 126 if (isLocalInterfacesSupported()) { 127 addAllInterfaceMethodsIn(methods, jcl.loadClass(getLocalHomeClassName())); 128 addAllInterfaceMethodsIn(methods, jcl.loadClass(getLocalClassName())); 129 } 130 } catch (Throwable t) { 131 _logger.log( Level.WARNING, 132 "enterprise.deployment_error_loading_class_excp", t ); throw new RuntimeException (t.getMessage()); 134 } 135 return methods; 136 } 137 138 139 private Method findEquivalentMethod(Collection methods, 140 Method methodToMatch) { 141 if(methods == null) 142 return null; 143 144 Method matchedMethod = null; 145 for(Iterator iter = methods.iterator(); iter.hasNext();) { 146 Method next = (Method) iter.next(); 147 if( methodsEqual(next, methodToMatch, false) ) { 149 matchedMethod = next; 150 break; 151 } 152 } 153 return matchedMethod; 154 } 155 156 162 private boolean methodsEqual(Method m1, Method m2, 163 boolean compareDeclaringClass) { 164 boolean equal = false; 165 166 do { 167 String m1Name = m1.getName(); 168 String m2Name = m2.getName(); 169 170 if( !m1Name.equals(m2Name) ) { break; } 171 172 String m1DeclaringClass = m1.getDeclaringClass().getName(); 173 String m2DeclaringClass = m2.getDeclaringClass().getName(); 174 175 if( compareDeclaringClass ) { 176 if( !m1DeclaringClass.equals(m2DeclaringClass) ) { break; } 177 } 178 179 Class [] m1ParamTypes = m1.getParameterTypes(); 180 Class [] m2ParamTypes = m2.getParameterTypes(); 181 182 if( m1ParamTypes.length != m2ParamTypes.length ) { break; } 183 184 equal = true; 185 for(int pIndex = 0; pIndex < m1ParamTypes.length; pIndex++) { 186 String m1ParamClass = m1ParamTypes[pIndex].getName(); 187 String m2ParamClass = m2ParamTypes[pIndex].getName(); 188 if( !m1ParamClass.equals(m2ParamClass) ) { 189 equal = false; 190 break; 191 } 192 } 193 194 } while(false); 195 196 return equal; 197 } 198 199 203 private Class getEjbClass() { 204 if (ejbClass == null) { 205 String ejbClassName = getEjbClassName(); 206 if(_logger.isLoggable(Level.FINE)) 207 _logger.fine("@@@@@@ Ejb name is "+ ejbClassName); if (jcl == null) { 209 String msg = localStrings.getLocalString( 210 "enterprise.deployment.error_missing_classloader", "IASEjbCMPEntityDescriptor.getEjbClass"); _logger.log(Level.WARNING, msg); 213 throw new RuntimeException (msg); 214 } 215 216 try { 217 ejbClass=Class.forName(ejbClassName, true, jcl); 218 219 } catch(ClassNotFoundException e) { 220 String msg = localStrings.getLocalString( 221 "enterprise.deployment.error_cannot_find_ejbclass", ejbClassName); 223 _logger.log(Level.WARNING, msg); 224 throw new RuntimeException (msg); 225 } 226 } 227 return ejbClass; 228 } 229 230 233 public Collection getFinders() { 234 if (finders == null) { 235 String ejbClassName = getEjbClassName(); 236 Class ejbClass = getEjbClass(); 237 238 if ( super.isRemoteInterfacesSupported() ) { 239 Class remoteHomeIntf = null; 240 if(_logger.isLoggable(Level.FINE)) 241 _logger.fine("@@@@@@ " + ejbClassName + " : Remote Interface is supported "); 244 try { 245 remoteHomeIntf = ejbClass.getClassLoader().loadClass( 246 super.getHomeClassName()); 247 } catch (ClassNotFoundException ex) { 248 _logger.log( Level.WARNING, 249 "enterprise.deployment_class_not_found", ex ); 251 return null; 252 } 253 254 finders = getFinders(remoteHomeIntf); 255 if(_logger.isLoggable(Level.FINE)) { 256 for(Iterator iter = finders.iterator(); iter.hasNext();) { 257 Method remoteHomeMethod=(Method)iter.next(); 258 _logger.fine("@@@@ adding Remote interface method " + remoteHomeMethod.getName() ); 260 } 261 } 262 } 264 if ( super.isLocalInterfacesSupported() ) { 265 Class localHomeIntf = null; 266 267 if(_logger.isLoggable(Level.FINE)) 268 _logger.fine("@@@@@@ " + ejbClassName + ": Local Interface is supported "); 270 try { 271 localHomeIntf = ejbClass.getClassLoader().loadClass( 272 super.getLocalHomeClassName()); 273 } catch (ClassNotFoundException ex) { 274 _logger.log( Level.WARNING, 275 "enterprise.deployment_class_not_found", ex ); return null; 277 } 278 279 Collection localFinders = getFinders(localHomeIntf); 280 if(finders == null) { 281 finders = localFinders; 284 285 } else if(localFinders != null) { 286 if(_logger.isLoggable(Level.FINE)) 289 _logger.fine("@@@@@@ Trying to remove the Common Elements from HashSet....... "); 291 for(Iterator iter = localFinders.iterator(); iter.hasNext();) { 292 Method localHomeMethod=(Method)iter.next(); 293 if(findEquivalentMethod(finders, localHomeMethod) == null) { 294 if(_logger.isLoggable(Level.FINE)) 295 _logger.fine("@@@@ adding local interface method " + localHomeMethod.getName() ); 297 298 finders.add(localHomeMethod); 299 } 300 } 301 } 302 } 304 if (finders == null) 305 finders = new HashSet(); 307 } 308 309 return finders; 310 } 311 312 316 public Collection getFinders(Class homeIntf) { 317 Method[] methods = homeIntf.getMethods(); 318 Collection finders = new HashSet(); 319 for(int i=0; i<methods.length; i++) { 320 if(methods[i].getName().startsWith(FIND)) { 321 finders.add(methods[i]); 322 } 323 } 324 325 return finders; 326 } 327 328 public void setClassLoader(ClassLoader jcl) { 329 this.jcl = jcl; 330 } 331 332 public ClassLoader getClassLoader() { 333 return jcl; 334 } 335 336 public Collection getAllPersistentFields() { 337 PersistenceDescriptor pers = getPersistenceDescriptor(); 338 PersistentFieldInfo[] persFields = pers.getPersistentFieldInfo(); 339 PersistentFieldInfo[] pkeyFields = pers.getPkeyFieldInfo(); 340 HashMap fields = new HashMap(); 341 342 for(int i=0; i<persFields.length; i++) { 343 fields.put(persFields[i].name, persFields[i]); 344 } 345 346 for(int i=0; i<pkeyFields.length; i++) { 347 fields.put(pkeyFields[i].name, pkeyFields[i]); 348 } 349 350 return fields.values(); 351 } 352 353 public Collection getPersistentFields() { 354 355 PersistenceDescriptor pers = getPersistenceDescriptor(); 356 PersistentFieldInfo[] persFields = pers.getPersistentFieldInfo(); 357 358 HashMap fields = new HashMap(); 359 360 for(int i=0; i<persFields.length; i++) { 361 fields.put(persFields[i].name, persFields[i]); 362 } 363 364 return fields.values(); 365 } 366 367 368 public Collection getPrimaryKeyFields() { 369 370 PersistenceDescriptor pers = getPersistenceDescriptor(); 371 PersistentFieldInfo[] pkeyFields = pers.getPkeyFieldInfo(); 372 373 HashMap pkey = new HashMap(); 374 for(int i=0; i<pkeyFields.length; i++) { 375 pkey.put(pkeyFields[i].name, pkeyFields[i]); 376 } 377 378 return pkey.values(); 379 380 } 381 382 385 public Collection getSelectors() { 386 if (selectors == null) { 387 selectors = new HashSet(); 388 Class ejbClass = getEjbClass(); 389 Method[] methods = ejbClass.getMethods(); 390 for(int i=0; i<methods.length; i++) { 391 if(methods[i].getName().startsWith(EJB_SELECT)) { selectors.add(methods[i]); 393 } 394 } 395 } 396 397 return selectors; 398 } 399 400 401 402 public String getBaseName(String className) { 403 if (className == null) 404 return null; 405 406 int dot = className.lastIndexOf(DOT); 407 if (dot == -1) 408 return className; 409 return className.substring(dot+1); 410 } 411 412 public IASEjbCMPEntityDescriptor() { 413 } 414 415 418 public IASEjbCMPEntityDescriptor(EjbDescriptor other) { 419 super(other); 420 421 setPersistenceType(CONTAINER_PERSISTENCE); 422 423 if ( other instanceof EjbCMPEntityDescriptor ) { 424 EjbCMPEntityDescriptor entity = (EjbCMPEntityDescriptor)other; 425 } 426 } 427 428 429 432 public void setPcImplClassName(String name) { 433 pcImplClassName = name; 434 } 435 436 public String getUniqueName() { 437 if(uniqueName == null) { 438 BundleDescriptor bundle = getEjbBundleDescriptor(); 439 Application application = bundle.getApplication(); 440 441 StringBuffer rc = new StringBuffer (). 443 append(getName()). 444 append(NAME_CONCATENATOR). 445 append(application.getRegistrationName()); 446 447 if (!application.isVirtual()) { 449 rc.append(NAME_CONCATENATOR). 450 append(bundle.getModuleDescriptor().getArchiveUri()); 451 } 452 453 uniqueName = getBaseName(getEjbClassName()) 454 + getUniqueNumber(rc.toString()); 455 } 456 457 return uniqueName; 458 } 459 460 public String getUniqueNumber(String num) { 461 String newNum= "" + num.hashCode(); newNum = newNum.replace('-', NAME_PART_SEPARATOR); return newNum; 465 } 466 467 468 public String getPcImplClassName() { 469 if (pcImplClassName == null) { 470 pcImplClassName = getUniqueName() + JDOSTATE; 472 String packageName = getPackageName(getEjbClassName()); 473 if(packageName != null) 474 pcImplClassName = packageName + DOT + pcImplClassName; 475 476 if(_logger.isLoggable(Level.FINE)) 477 _logger.fine("##### PCImplClass Name is " + pcImplClassName); } 479 return pcImplClassName; 480 } 481 482 483 486 public void setConcreteImplClassName(String name) { 487 concreteImplClassName = name; 488 } 489 490 public String getPackageName(String className) { 491 int dot = className.lastIndexOf(DOT); 492 if (dot == -1) 493 return null; 494 return className.substring(0, dot); 495 } 496 497 498 506 public String getEjbImplClassName() { 507 if (ejbImplClassName == null) { 508 String packageName = getPackageName(getEjbClassName()); 509 ejbImplClassName = getConcreteImplClassName(); 510 if(packageName != null) 511 ejbImplClassName = packageName + DOT + ejbImplClassName; 512 } 513 return ejbImplClassName; 514 } 515 516 519 520 public String getConcreteImplClassName() { 521 if (concreteImplClassName == null) { 522 524 concreteImplClassName = getUniqueName() + CONCRETE_IMPL; 525 } 526 527 return concreteImplClassName; 528 } 529 530 public void setModuleDir(String moduleRootDir) { 531 moduleDir = moduleRootDir; 532 } 533 534 537 public String getModuleDir() { 538 if(moduleDir != null) 540 return moduleDir; 541 else 542 return null; 543 } 544 545 public void setMappingProperties(String mappingProperties) { 546 this.mappingProperties = mappingProperties; 547 } 548 549 550 553 public String getMappingProperties() { 554 return mappingProperties; 555 } 556 557 558 562 public boolean classesChanged() { 563 564 576 577 return false; 578 } 579 580 585 public void setQueryParser(QueryParser inParser) { 586 queryParser = inParser; 587 } 588 589 592 public QueryParser getQueryParser() { 593 return queryParser; 594 } 595 596 601 private static Map createConversionTable () { 602 603 HashMap conversionTable = new HashMap(); 604 conversionTable.put("Boolean", "java.lang.Boolean"); conversionTable.put("Byte", "java.lang.Byte"); conversionTable.put("Character", "java.lang.Character"); conversionTable.put("Double", "java.lang.Double"); conversionTable.put("Float", "java.lang.Float"); conversionTable.put("Integer", "java.lang.Integer"); conversionTable.put("Long", "java.lang.Long"); conversionTable.put("Number", "java.lang.Number"); conversionTable.put("Short", "java.lang.Short"); conversionTable.put("String", "java.lang.String"); conversionTable.put("Object", "java.lang.Object"); return conversionTable; 616 } 617 618 private String getFullyQualifiedType(String type) { 619 String knownType=(String )conversionTable.get(type); 620 return knownType == null ? type : knownType; 621 } 622 623 627 public PrefetchDisabledDescriptor getPrefetchDisabledDescriptor() { 628 return prefetchDisabledDescriptor; 629 } 630 631 636 public void setPrefetchDisabledDescriptor( 637 PrefetchDisabledDescriptor prefetchDisabledDescriptor) { 638 this.prefetchDisabledDescriptor = prefetchDisabledDescriptor; 639 } 640 641 642 646 public void addOneOneFinder (IASEjbCMPFinder finder) { 647 arrOneOneFinders.add(finder); 648 } 649 650 655 public Map getOneOneFinders() { 656 if (!arrOneOneFinders.isEmpty()) { 659 if (queryParser == null) { 660 String msg = localStrings.getLocalString( 661 "enterprise.deployment.error_missing_queryparser", "IASEjbCMPEntityDescriptor.getOneOneFinders"); _logger.log(Level.WARNING, msg); 664 throw new RuntimeException (msg); 665 } 666 667 for ( Iterator i = arrOneOneFinders.iterator(); i.hasNext(); ) { 669 IASEjbCMPFinder finder = ( IASEjbCMPFinder )i.next(); 670 String key = generateKey(finder, queryParser); 671 oneOneFinders.put(key, finder); 672 } 673 arrOneOneFinders.clear(); 674 } 675 return oneOneFinders; 676 } 677 678 683 private String generateKey(IASEjbCMPFinder finder, QueryParser parser) { 684 685 StringBuffer key = new StringBuffer (); 686 key.append(finder.getMethodName()).append(LIST_START); 687 688 String queryParams = finder.getQueryParameterDeclaration(); 689 Iterator iter = parser.parameterTypeIterator(queryParams); 690 while ( iter.hasNext() ) { 691 String type = ( String ) iter.next() ; 692 key.append(getFullyQualifiedType(type)) ; 693 if( iter.hasNext() ) { 694 key.append(LIST_SEPARATOR); 695 } 696 } 697 key.append(LIST_END); 698 699 return key.toString().intern(); 700 } 701 702 706 public IASEjbCMPFinder getIASEjbCMPFinder(Method method) { 707 if(findEquivalentMethod(getFinders(), method) == null ) { 709 return null; 710 } 711 String methodName = method.getName(); 712 713 StringBuffer key = new StringBuffer (); 715 key.append(methodName); 716 key.append(LIST_START); 717 Class paramList[] = method.getParameterTypes(); 718 for (int index = 0 ; index < paramList.length ; index++ ) { 719 if(index>0) { 720 key.append(LIST_SEPARATOR); 721 } 722 key.append(paramList[index].getName()); 723 } 724 key.append(LIST_END); 725 return (IASEjbCMPFinder)getOneOneFinders().get(key.toString()); 726 } 727 } 728 | Popular Tags |