1 23 24 package com.sun.enterprise.connectors; 25 26 import java.util.logging.*; 27 import java.util.*; 28 import java.security.*; 29 import javax.resource.*; 30 import javax.security.auth.*; 31 import javax.resource.spi.security.*; 32 import javax.naming.*; 33 import javax.resource.spi.*; 34 import com.sun.enterprise.*; 35 import com.sun.enterprise.server.*; 36 import com.sun.enterprise.config.serverbeans.*; 37 import com.sun.enterprise.config.ConfigBean; 38 import com.sun.enterprise.config.ConfigException; 39 import com.sun.enterprise.config.ConfigContext; 40 import com.sun.enterprise.deployment.*; 41 import com.sun.enterprise.resource.*; 42 import com.sun.enterprise.connectors.util.*; 43 import com.sun.enterprise.util.i18n.StringManager; 44 import com.sun.enterprise.resource.UnpooledConnectionEventListener; 45 import com.sun.enterprise.util.RelativePathResolver; 46 import java.sql.Connection ; 47 import java.sql.SQLException ; 48 import com.sun.enterprise.Switch; 49 import com.sun.enterprise.PoolManager; 50 import com.sun.enterprise.connectors.util.ConnectionPoolReconfigHelper.ReconfigAction; 51 import com.sun.enterprise.connectors.authentication.RuntimeSecurityMap; 52 import com.sun.enterprise.connectors.authentication.ConnectorSecurityMap; 53 import com.sun.enterprise.naming.NamingManagerImpl; 54 55 56 61 62 63 public class ConnectorConnectionPoolAdminServiceImpl extends 64 ConnectorServiceImpl implements ConnectorAdminService { 65 66 private static StringManager localStrings = 67 StringManager.getManager( ConnectorConnectionPoolAdminServiceImpl.class); 68 69 72 73 public ConnectorConnectionPoolAdminServiceImpl() { 74 super(); 75 initialize(); 76 } 77 78 79 90 91 public void createConnectorConnectionPool(ConnectorConnectionPool ccp, 92 String connectionDefinitionName , String rarName, 93 ElementProperty[] props, SecurityMap[] securityMaps) 94 throws ConnectorRuntimeException 95 { 96 97 if( (ccp == null) || (connectionDefinitionName == null) 98 || (rarName == null)) { 99 _logger.log(Level.FINE,"Wrong parameters for pool creation "); 100 String i18nMsg = localStrings.getString( 101 "ccp_adm.wrong_params_for_create"); 102 throw new ConnectorRuntimeException( i18nMsg ); 103 } 104 ConnectorDescriptor connectorDescriptor = 105 _registry.getDescriptor(rarName); 106 if(connectorDescriptor == null) { 107 ifSystemRarLoad(rarName); 108 connectorDescriptor = _registry.getDescriptor(rarName); 109 } 110 if (connectorDescriptor == null) { 111 String i18nMsg = localStrings.getString( 112 "ccp_adm.no_conn_pool_obj"); 113 ConnectorRuntimeException cre = new ConnectorRuntimeException( 114 i18nMsg ); 115 _logger.log(Level.SEVERE, 116 "rardeployment.connector_descriptor_notfound_registry",rarName); 117 _logger.log(Level.SEVERE,"",cre); 118 throw cre; 119 } 120 Set connectionDefs = 121 connectorDescriptor.getOutboundResourceAdapter().getConnectionDefs(); 122 ConnectionDefDescriptor cdd = null; 123 Iterator it = connectionDefs.iterator(); 124 while(it.hasNext()) { 125 cdd = (ConnectionDefDescriptor)it.next(); 126 if(connectionDefinitionName.equals(cdd.getConnectionFactoryIntf())) 127 break; 128 129 } 130 ConnectorDescriptorInfo cdi = new ConnectorDescriptorInfo(); 131 132 cdi.setRarName(rarName); 133 cdi.setResourceAdapterClassName( 134 connectorDescriptor.getResourceAdapterClass()); 135 cdi.setConnectionDefinitionName(cdd.getConnectionFactoryIntf()); 136 cdi.setManagedConnectionFactoryClass( 137 cdd.getManagedConnectionFactoryImpl()); 138 cdi.setConnectionFactoryClass(cdd.getConnectionFactoryImpl()); 139 cdi.setConnectionFactoryInterface(cdd.getConnectionFactoryIntf()); 140 cdi.setConnectionClass(cdd.getConnectionImpl()); 141 cdi.setConnectionInterface(cdd.getConnectionIntf()); 142 Set mergedProps = ConnectorDDTransformUtils.mergeProps(props, 143 cdd.getConfigProperties()); 144 cdi.setMCFConfigProperties(mergedProps); 145 cdi.setResourceAdapterConfigProperties( 146 connectorDescriptor.getConfigProperties()); 147 ccp.setSecurityMaps(SecurityMapUtils.getConnectorSecurityMaps(securityMaps)); 148 createConnectorConnectionPool(ccp , cdi); 149 } 150 151 158 159 public void createConnectorConnectionPool(ConnectorConnectionPool ccp, 160 ConnectionDefDescriptor cdd, String rarName) 161 throws ConnectorRuntimeException 162 { 163 164 if( (ccp == null) || (cdd == null) || (rarName == null)) { 165 166 _logger.log(Level.FINE,"Wrong parameters for pool creation "); 167 String i18nMsg = localStrings.getString( 168 "ccp_adm.wrong_params_for_create"); 169 throw new ConnectorRuntimeException( i18nMsg ); 170 } 171 ConnectorDescriptorInfo cdi = new ConnectorDescriptorInfo(); 172 173 ConnectorDescriptor connectorDescriptor = _registry.getDescriptor( 174 rarName); 175 if(connectorDescriptor == null) { 176 ifSystemRarLoad(rarName); 177 connectorDescriptor = _registry.getDescriptor(rarName); 178 } 179 180 if (connectorDescriptor == null) { 181 String i18nMsg = localStrings.getString( 182 "ccp_adm.no_conn_pool_obj"); 183 ConnectorRuntimeException cre = new 184 ConnectorRuntimeException(i18nMsg); 185 _logger.log(Level.SEVERE, 186 "rardeployment.connector_descriptor_notfound_registry", 187 rarName); 188 _logger.log(Level.SEVERE,"",cre); 189 throw cre; 190 } 191 cdi.setRarName(rarName); 192 cdi.setResourceAdapterClassName( 193 connectorDescriptor.getResourceAdapterClass()); 194 cdi.setConnectionDefinitionName(cdd.getConnectionFactoryIntf()); 195 cdi.setManagedConnectionFactoryClass( 196 cdd.getManagedConnectionFactoryImpl()); 197 cdi.setConnectionFactoryClass(cdd.getConnectionFactoryImpl()); 198 cdi.setConnectionFactoryInterface(cdd.getConnectionFactoryIntf()); 199 cdi.setConnectionClass(cdd.getConnectionImpl()); 200 cdi.setConnectionInterface(cdd.getConnectionIntf()); 201 cdi.setMCFConfigProperties(cdd.getConfigProperties()); 202 cdi.setResourceAdapterConfigProperties( 203 connectorDescriptor.getConfigProperties()); 204 createConnectorConnectionPool(ccp , cdi); 205 } 206 207 215 216 private void createConnectorConnectionPool( 217 ConnectorConnectionPool connectorPoolObj , 218 ConnectorDescriptorInfo connectorDescInfo ) 219 throws ConnectorRuntimeException 220 { 221 222 connectorPoolObj.setConnectorDescriptorInfo( connectorDescInfo ); 223 createConnectorConnectionPool( connectorPoolObj ); 224 } 225 226 231 public void createConnectorConnectionPool( 232 ConnectorConnectionPool connectorPoolObj ) 233 throws ConnectorRuntimeException 234 { 235 236 String poolName = connectorPoolObj.getName(); 237 if( connectorPoolObj == null || poolName == null) { 238 _logger.log(Level.FINE,"Wrong parameters for pool creation "); 239 String i18nMsg = localStrings.getString( 240 "ccp_adm.wrong_params_for_create"); 241 throw new ConnectorRuntimeException( i18nMsg ); 242 } 243 String jndiNameForPool = ConnectorAdminServiceUtils. 244 getReservePrefixedJNDINameForPool(poolName); 245 try { 246 Switch.getSwitch().getNamingManager().publishObject( 247 jndiNameForPool,(Object )connectorPoolObj,true); 248 ManagedConnectionFactory mcf = 249 obtainManagedConnectionFactory(poolName); 250 if(mcf == null) { 251 InitialContext ic = new InitialContext(); 252 ic.unbind(jndiNameForPool); 253 String i18nMsg = localStrings.getString( 254 "ccp_adm.failed_to_create_mcf"); 255 ConnectorRuntimeException cre = new 256 ConnectorRuntimeException( i18nMsg ); 257 _logger.log(Level.SEVERE,"rardeployment.mcf_creation_failure", 258 poolName); 259 _logger.log(Level.SEVERE,"",cre); 260 throw cre; 261 } 262 263 } catch(NamingException ex) { 264 265 String i18nMsg = localStrings.getString( 266 "ccp_adm.failed_to_publish_in_jndi"); 267 ConnectorRuntimeException cre = new 268 ConnectorRuntimeException( i18nMsg ); 269 cre.initCause(ex); 270 _logger.log(Level.SEVERE,"rardeployment.pool_jndi_bind_failure", 271 poolName); 272 _logger.log(Level.SEVERE,"",cre); 273 throw cre; 274 } catch(NullPointerException ex) { 275 try { 276 InitialContext ic = new InitialContext(); 277 ic.unbind(jndiNameForPool); 278 } catch(NamingException ne) { 279 _logger.log(Level.FINE, 280 "Failed to unbind connection pool object ",poolName); 281 } 282 283 String i18nMsg = localStrings.getString( 284 "ccp_adm.failed_to_register_mcf"); 285 ConnectorRuntimeException cre = new 286 ConnectorRuntimeException( i18nMsg ); 287 cre.initCause(ex); 288 _logger.log(Level.SEVERE,"rardeployment.mcf_registration_failure", 289 poolName); 290 _logger.log(Level.SEVERE,"",cre); 291 throw cre; 292 } 293 } 294 295 303 304 public void createConnectorConnectionPool( 305 ConnectorConnectionPool connectorPoolObj, String security, 306 Set configProperties) throws ConnectorRuntimeException 307 { 308 if( connectorPoolObj == null || configProperties == null) { 309 _logger.log(Level.FINE,"Wrong parameters for pool creation "); 310 String i18nMsg = localStrings.getString( 311 "ccp_adm.wrong_params_for_create"); 312 throw new ConnectorRuntimeException( i18nMsg ); 313 } 314 String poolName = connectorPoolObj.getName(); 315 String moduleName = 316 connectorPoolObj.getConnectorDescriptorInfo().getRarName(); 317 String connectionDefinitionName = 318 connectorPoolObj.getConnectorDescriptorInfo(). 319 getConnectionDefinitionName(); 320 321 ConnectorDescriptor connectorDescriptor = 322 _registry.getDescriptor(moduleName); 323 if(connectorDescriptor == null) { 324 ifSystemRarLoad(moduleName); 325 connectorDescriptor = _registry.getDescriptor(moduleName); 326 } 327 328 if(connectorDescriptor == null) { 329 String i18nMsg = localStrings.getString( 330 "ccp_adm.null_connector_desc"); 331 ConnectorRuntimeException cre = new 332 ConnectorRuntimeException( i18nMsg ); 333 _logger.log(Level.SEVERE,"rardeployment.null_mcf_in_registry", 334 moduleName); 335 _logger.log(Level.SEVERE,"", cre); 336 throw cre; 337 } 338 339 Set connectionDefs = 340 connectorDescriptor.getOutboundResourceAdapter().getConnectionDefs(); 341 342 Iterator iterator = connectionDefs.iterator(); 343 344 ConnectionDefDescriptor connectionDefDescriptor = null; 345 346 while(iterator.hasNext()){ 347 connectionDefDescriptor = 348 (ConnectionDefDescriptor) iterator.next(); 349 if(connectionDefinitionName.equals( 350 connectionDefDescriptor.getConnectionFactoryIntf())) 351 break; 352 } 353 354 ConnectorDescriptorInfo connectorDescInfo = 355 ConnectorDDTransformUtils.getConnectorDescriptorInfo( 356 connectionDefDescriptor); 357 connectorDescInfo.setMCFConfigProperties(configProperties); 358 connectorDescInfo.setRarName(moduleName); 359 connectorDescInfo.setResourceAdapterClassName( 360 connectorDescriptor.getResourceAdapterClass()); 361 362 createConnectorConnectionPool(connectorPoolObj,connectorDescInfo); 363 } 364 365 369 370 public void deleteConnectorConnectionPool(String poolName) 371 throws ConnectorRuntimeException 372 { 373 deleteConnectorConnectionPool(poolName,false); 374 } 375 376 386 387 public void deleteConnectorConnectionPool(String poolName,boolean cascade) 388 throws ConnectorRuntimeException 389 { 390 391 if(poolName==null){ 392 _logger.log( Level.WARNING,"Deletion of pool : poolName null."); 393 String i18nMsg = localStrings.getString( 394 "ccp_adm.null_pool_name"); 395 throw new ConnectorRuntimeException( i18nMsg ); 396 } 397 398 boolean errorOccured=false; 399 ResourcesUtil resUtil = ResourcesUtil.getInstance(); 400 Object [] connectorResourcesJndiNames = 401 resUtil.getConnectorResourcesJndiNames(poolName); 402 if(cascade==true && connectorResourcesJndiNames != null) { 403 for(int i=0;i<connectorResourcesJndiNames.length;++i) { 404 try { 405 _runtime.deleteConnectorResource( 406 (String )connectorResourcesJndiNames[i]); 407 } catch(ConnectorRuntimeException cre) { 408 errorOccured=true; 409 } 410 } 411 412 } else if(connectorResourcesJndiNames != null && 413 connectorResourcesJndiNames.length != 0) { 414 415 428 } 429 killPool(poolName); 430 boolean result = _registry.removeManagedConnectionFactory(poolName); 431 if(result == false && !resUtil.poolBelongsToSystemRar(poolName)) { 432 _logger.log( Level.FINE, 433 "rardeployment.mcf_removal_failure",poolName); 434 return; 435 } 436 try { 437 String jndiNameForPool = ConnectorAdminServiceUtils. 438 getReservePrefixedJNDINameForPool( poolName ); 439 InitialContext ic = new InitialContext(); 440 ic.unbind(jndiNameForPool); 441 } catch(NamingException ne) { 442 if(resUtil.poolBelongsToSystemRar(poolName)) { 443 return; 444 } 445 _logger.log(Level.SEVERE, 446 "rardeployment.connectionpool_removal_from_jndi_error", 447 poolName); 448 String i18nMsg = localStrings.getString( 449 "ccp_adm.failed_to_remove_from_jndi"); 450 ConnectorRuntimeException cre = new 451 ConnectorRuntimeException( i18nMsg ); 452 cre.initCause(ne); 453 _logger.log(Level.SEVERE,"",cre); 454 throw cre; 455 } 456 if(errorOccured==true && !resUtil.poolBelongsToSystemRar(poolName)) { 457 String i18nMsg = localStrings.getString( 458 "ccp_adm.failed_to_delete_conn_res" ); 459 ConnectorRuntimeException cre = new 460 ConnectorRuntimeException( i18nMsg ); 461 _logger.log(Level.SEVERE, 462 "rardeployment.all_resources_removal_error", poolName); 463 _logger.log(Level.SEVERE,"",cre); 464 throw cre; 465 } 466 } 467 468 473 474 private void unloadAndKillPool(String poolName) 475 throws ConnectorRuntimeException 476 { 477 478 killPool(poolName); 479 boolean result = _registry.removeManagedConnectionFactory(poolName); 480 if(result == false) { 481 _logger.log( Level.SEVERE, 482 "rardeployment.mcf_removal_failure",poolName); 483 String i18nMsg = localStrings.getString( 484 "ccp_adm.wrong_params_for_create"); 485 ConnectorRuntimeException cre = new 486 ConnectorRuntimeException( i18nMsg ); 487 _logger.log( Level.FINE,"",cre); 488 throw cre; 489 } 490 try { 491 String jndiNameForPool = ConnectorAdminServiceUtils. 492 getReservePrefixedJNDINameForPool(poolName); 493 InitialContext ic = new InitialContext(); 494 ic.unbind(jndiNameForPool); 495 } catch(NamingException ne) { 496 String i18nMsg = localStrings.getString( 497 "ccp_adm.failed_to_remove_from_jndi"); 498 ConnectorRuntimeException cre = new 499 ConnectorRuntimeException( i18nMsg ); 500 cre.initCause(ne); 501 _logger.log(Level.SEVERE, 502 "rardeployment.connectionpool_removal_from_jndi_error", 503 poolName); 504 _logger.log(Level.FINE,"",cre); 505 throw cre; 506 } 507 508 } 509 510 526 public boolean testConnectionPool( String poolName ) 527 throws ResourceException { 528 dump(poolName); 529 Object con = null; 530 DASResourcesUtil dasResUtil = null; 531 try { 532 dasResUtil = (DASResourcesUtil) ResourcesUtil.getInstance(); 535 dasResUtil.setGetConnectionFromConnectorRuntime( true ); 536 537 con = getUnpooledConnection( poolName, null, false ); 539 540 } catch( Exception re ) { 541 _logger.log(Level.WARNING, re.getMessage() ); 543 ResourceException e = new ResourceException( re.getMessage()); 544 e.initCause( re ); 545 throw e; 546 } finally { 547 try { 548 dasResUtil.setGetConnectionFromConnectorRuntime( false ); 550 ((ManagedConnection)con).destroy(); 551 552 } catch( Throwable e ) { 553 } 555 } 556 557 return true; 559 } 560 561 567 private ResourcePrincipal getDefaultResourcePrincipal( String poolName, 568 ManagedConnectionFactory mcf ) throws NamingException { 569 ConnectorConnectionPool connectorConnectionPool = null; 571 try { 572 String jndiNameForPool = ConnectorAdminServiceUtils. 573 getReservePrefixedJNDINameForPool( poolName ); 574 InitialContext ic = new InitialContext(); 575 connectorConnectionPool = 576 (ConnectorConnectionPool) ic.lookup(jndiNameForPool); 577 } catch (NamingException ne ) { 578 throw ne; 579 } 580 581 ConnectorDescriptorInfo cdi = connectorConnectionPool. 582 getConnectorDescriptorInfo(); 583 584 Set mcfConfigProperties = cdi.getMCFConfigProperties(); 585 Iterator mcfConfPropsIter = mcfConfigProperties.iterator(); 586 String userName = null; 587 String password = null; 588 while( mcfConfPropsIter.hasNext() ) { 589 EnvironmentProperty prop = 590 (EnvironmentProperty)mcfConfPropsIter.next(); 591 592 if ( prop.getName().toUpperCase().equals("USERNAME") || 593 prop.getName().toUpperCase().equals("USER") ) { 594 userName = prop.getValue(); 595 } else if ( prop.getName().toUpperCase().equals("PASSWORD") ) { 596 password = prop.getValue(); 597 try{ 598 password = RelativePathResolver.getRealPasswordFromAlias(password); 599 }catch(Exception e){ 600 _logger.log(Level.WARNING,"unable_to_get_password_from_alias",e); 601 } 602 } 603 } 604 605 609 if (userName == null || userName.trim().equals("")) { 610 userName = ConnectionPoolObjectsUtils.getValueFromMCF("UserName", poolName, mcf); 611 password = ConnectionPoolObjectsUtils.getValueFromMCF("Password", poolName, mcf); 612 } 613 return new ResourcePrincipal( userName, password ); 615 } 616 617 630 public boolean reconfigureConnectorConnectionPool( ConnectorConnectionPool 631 ccp ) throws ConnectorRuntimeException 632 { 633 return reconfigureConnectorConnectionPool( ccp, new HashSet()); 634 } 635 636 642 public void switchOnMatching(String poolName) throws ConnectorRuntimeException { 643 try { 644 ConnectorConnectionPool origCcp = 645 getOriginalConnectorConnectionPool( poolName ); 646 origCcp.setMatchConnections(true); 647 648 String jndiNameForPool = ConnectorAdminServiceUtils. 650 getReservePrefixedJNDINameForPool( poolName ); 651 652 InitialContext ic = new InitialContext(); 653 ic.unbind( jndiNameForPool ); 654 Switch.getSwitch().getNamingManager().publishObject( 655 jndiNameForPool, (Object )origCcp, true ); 656 } catch (NamingException e) { 657 ConnectorRuntimeException ex = 658 new ConnectorRuntimeException( e.getMessage()); 659 throw (ConnectorRuntimeException) ex.initCause(e); 660 } 661 662 } 663 664 679 public boolean reconfigureConnectorConnectionPool( ConnectorConnectionPool 680 ccp, Set excludedProps ) throws ConnectorRuntimeException 681 { 682 String poolName = ccp.getName(); 685 ConnectorConnectionPool origCcp = null; 686 try { 687 origCcp = getOriginalConnectorConnectionPool( poolName ); 688 } catch( NamingException ne) { 689 throw new ConnectorRuntimeException( ne.getMessage() ); 690 } 691 692 logFine("origCcp :\n" + ((origCcp != null) ? origCcp.toString() : "null")); 693 logFine("ccp :\n" + ((ccp != null) ? ccp.toString(): "null")); 694 695 ReconfigAction action = ConnectionPoolReconfigHelper.compare( origCcp, ccp, 696 excludedProps ); 697 698 if ( action == ReconfigAction.UPDATE_MCF_AND_ATTRIBUTES ) { 699 logFine( "@@@@ action == " + action); 700 updateMCFAndPoolAttributes( ccp ); 701 } else if ( action == ReconfigAction.RECREATE_POOL ) { 702 logFine( "@@@@ action == " + action); 703 return true; 704 } 705 706 return false; 707 } 708 709 712 private ConnectorConnectionPool getOriginalConnectorConnectionPool( 713 String poolName ) throws NamingException 714 { 715 716 ConnectorConnectionPool ccpOrig = null; 717 718 String jndiNameForPool = ConnectorAdminServiceUtils. 719 getReservePrefixedJNDINameForPool( poolName ); 720 InitialContext ic = new InitialContext(); 721 try { 722 ccpOrig = (ConnectorConnectionPool) ic.lookup( jndiNameForPool ); 723 } catch(NamingException ne) { 724 if(checkAndLoadPoolResource(poolName)) { 725 ccpOrig = (ConnectorConnectionPool)ic.lookup( jndiNameForPool ); 726 } else { 727 throw ne; 728 } 729 730 } 731 732 return ccpOrig; 733 } 734 735 private void updateMCFAndPoolAttributes( ConnectorConnectionPool 736 ccp ) throws ConnectorRuntimeException 737 { 738 String poolName = ccp.getName(); 739 try { 740 ConnectorConnectionPool origCcp = 741 getOriginalConnectorConnectionPool( poolName ); 742 743 origCcp.setSteadyPoolSize(ccp.getSteadyPoolSize()); 745 origCcp.setMaxPoolSize(ccp.getMaxPoolSize()); 746 origCcp.setMaxWaitTimeInMillis(ccp.getMaxWaitTimeInMillis()); 747 origCcp.setPoolResizeQuantity(ccp.getPoolResizeQuantity()); 748 origCcp.setIdleTimeoutInSeconds(ccp.getIdleTimeoutInSeconds()); 749 origCcp.setFailAllConnections(ccp.isFailAllConnections()); 750 751 origCcp.setMatchConnections( ccp.matchConnections() ); 755 origCcp.setNonComponent( ccp.isNonComponent() ); 756 origCcp.setNonTransactional( ccp.isNonTransactional() ); 757 758 String jndiNameForPool = ConnectorAdminServiceUtils. 760 getReservePrefixedJNDINameForPool( poolName ); 761 InitialContext ic = new InitialContext(); 762 ic.unbind( jndiNameForPool ); 763 Switch.getSwitch().getNamingManager().publishObject( 764 jndiNameForPool, (Object )origCcp, true ); 765 766 } catch( NamingException ne ) { 767 throw new ConnectorRuntimeException( ne.getMessage() ) ; 768 } 769 770 773 PoolManager poolMgr = Switch.getSwitch().getPoolManager(); 774 try { 775 poolMgr.reconfigPoolProperties( ccp ); 776 } catch( PoolingException pe ) { 777 throw new ConnectorRuntimeException( pe.getMessage() ); 778 } 779 ConnectorRegistry registry = ConnectorRegistry.getInstance(); 784 ManagedConnectionFactory mcf = registry.getManagedConnectionFactory( 785 poolName ); 786 SetMethodAction sma = new SetMethodAction( mcf, 787 ccp.getConnectorDescriptorInfo().getMCFConfigProperties() ); 788 try { 789 sma.run(); 790 } catch( Exception e) { 791 _logger.log( Level.WARNING, e.getMessage() ); 792 ConnectorRuntimeException cre = new ConnectorRuntimeException(e.getMessage()); 793 cre.initCause( e ); 794 throw cre; 795 } 796 797 PoolMetaData pmd = registry.getPoolMetaData( poolName ); 800 pmd.setIsPM( ccp.isNonComponent() ); 801 pmd.setIsNonTx( ccp.isNonTransactional() ); 802 pmd.setLazyEnlistable( ccp.isLazyConnectionEnlist() ); 803 pmd.setAuthCredentialsDefinedInPool(ccp.getAuthCredentialsDefinedInPool()); 804 805 logFine("Pool properties reconfiguration done"); 806 } 807 808 817 public void recreateConnectorConnectionPool( ConnectorConnectionPool ccp) 818 throws ConnectorRuntimeException 819 { 820 ConnectorRegistry registry = ConnectorRegistry.getInstance(); 821 if (registry == null ) { 822 throw new ConnectorRuntimeException( 823 "Cannot get ConnectorRegistry"); 824 } 825 String poolName = ccp.getName(); 826 try { 828 unloadAndKillPool( poolName ); 829 } catch( ConnectorRuntimeException cre ) { 830 throw cre; 831 } 832 837 try { 840 String jndiNameForPool = ConnectorAdminServiceUtils. 841 getReservePrefixedJNDINameForPool( poolName ); 842 Switch.getSwitch().getNamingManager().publishObject( 843 jndiNameForPool, (Object )ccp, true ); 844 ManagedConnectionFactory mcf = null; 845 mcf = obtainManagedConnectionFactory( poolName ); 846 if (mcf == null ) { 847 InitialContext ic = new InitialContext(); 848 ic.unbind( jndiNameForPool ); 849 _logger.log(Level.WARNING, 850 "rardeployment.mcf_creation_failure", poolName); 851 852 String i18nMsg = localStrings.getString( 853 "ccp_adm.failed_to_create_mcf"); 854 throw new ConnectorRuntimeException( i18nMsg ); 855 } 856 857 } catch( NamingException ne ) { 858 _logger.log(Level.SEVERE, 859 "rardeployment.pool_jndi_bind_failure", poolName ); 860 String i18nMsg = localStrings.getString( 861 "ccp_adm.could_not_recreate_pool"); 862 ConnectorRuntimeException crex = new ConnectorRuntimeException( i18nMsg ); 863 crex.initCause(ne); 864 throw crex; 865 } 866 867 } 868 876 public ManagedConnectionFactory obtainManagedConnectionFactory( 877 String poolName) throws ConnectorRuntimeException 878 { 879 try { 880 if (_registry.isMCFCreated(poolName)) { 881 return _registry.getManagedConnectionFactory( poolName ); 882 } else { 883 String jndiNameForPool = ConnectorAdminServiceUtils. 884 getReservePrefixedJNDINameForPool ( poolName ); 885 InitialContext ic = new InitialContext(); 886 ConnectorConnectionPool connectorConnectionPool = 887 (ConnectorConnectionPool) ic.lookup(jndiNameForPool); 888 if(connectorConnectionPool == null) { 889 String i18nMsg = localStrings.getString( 890 "ccp_adm.null_pool"); 891 ConnectorRuntimeException cre = new 892 ConnectorRuntimeException( i18nMsg ); 893 _logger.log(Level.SEVERE, 894 "rardeployment.connectionpool_object_null",poolName); 895 if (_logger.isLoggable( Level.FINE ) ) { 896 _logger.log(Level.FINE,"",cre); 897 } 898 throw cre; 899 } 900 901 String rarName = connectorConnectionPool. 902 getConnectorDescriptorInfo().getRarName(); 903 ActiveResourceAdapter activeResourceAdapter = 904 _registry.getActiveResourceAdapter(rarName); 905 if(activeResourceAdapter == null) { 906 ifSystemRarLoad(rarName); 907 activeResourceAdapter = 908 _registry.getActiveResourceAdapter(rarName); 909 } 910 if( activeResourceAdapter == null) { 911 String i18nMsg = localStrings.getString( 912 "ccp_adm.active_ra_not_init"); 913 914 ConnectorRuntimeException cre = new 915 ConnectorRuntimeException( i18nMsg ); 916 _logger.log(Level.SEVERE, 917 "rardeployment.resourceadapter_not_initialized",rarName); 918 if (_logger.isLoggable( Level.FINE ) ) { 919 _logger.log(Level.FINE,"",cre); 920 } 921 throw cre; 922 } 923 ManagedConnectionFactory mcf = 924 activeResourceAdapter.createManagedConnectionFactory( 925 connectorConnectionPool, null); 926 if(mcf != null) { 927 ResourcePrincipal prin = 928 getDefaultResourcePrincipal(poolName,mcf); 929 Subject s = ConnectionPoolObjectsUtils.createSubject(mcf, prin); 930 int txSupport = connectorConnectionPool.getTransactionSupport(); 931 932 boolean isPM = connectorConnectionPool.isNonComponent(); 933 boolean isNonTx = connectorConnectionPool.isNonTransactional(); 934 ConnectorSecurityMap[] securityMaps = 935 connectorConnectionPool.getSecurityMaps(); 936 RuntimeSecurityMap runtimeSecurityMap = 937 SecurityMapUtils.processSecurityMaps(securityMaps); 938 boolean lazyEnlistable = connectorConnectionPool.isLazyConnectionEnlist(); 939 boolean lazyAssoc = connectorConnectionPool.isLazyConnectionAssoc(); 940 if ( isPM || isNonTx ) { 941 948 lazyEnlistable = false; 949 } 950 951 if ( isPM ) { 952 String str = System.getProperty( 957 "com.sun.enterprise.resource.AllowLazyAssociationWithPM"); 958 if ( str != null && str.toUpperCase().trim().equals("TRUE") ) { 959 lazyAssoc = lazyAssoc & true; 960 } else { 961 lazyAssoc = false; 962 } 963 } 964 965 PoolMetaData pmd = new PoolMetaData(poolName, mcf, s, txSupport, prin, 966 isPM, isNonTx, lazyEnlistable, runtimeSecurityMap, lazyAssoc); 967 logFine( pmd.toString() ); 968 _registry.addManagedConnectionFactory(poolName, pmd); 969 } 970 971 972 PoolType pt = (connectorConnectionPool.isAssociateWithThread() ? 973 PoolType.ASSOCIATE_WITH_THREAD_POOL : 974 PoolType.STANDARD_POOL ); 975 976 createAndAddPool( poolName, pt ); 977 return mcf; 978 } 979 } catch(NamingException ne) { 980 String i18nMsg = localStrings.getString( 981 "pingpool.name_not_bound"); 982 ConnectorRuntimeException cre = new 983 ConnectorRuntimeException( i18nMsg); 984 cre.initCause(ne); 985 _logger.log(Level.FINE,"rardeployment.jndi_lookup_failed", 986 poolName); 987 if (_logger.isLoggable( Level.FINE ) ) { 988 _logger.log(Level.FINE,"",cre); 989 } 990 throw cre; 992 } 993 catch(NullPointerException ne) { 994 String i18nMsg = localStrings.getString( 995 "ccp_adm.failed_to_register_mcf"); 996 ConnectorRuntimeException cre = new 997 ConnectorRuntimeException( i18nMsg ); 998 cre.initCause(ne); 999 _logger.log(Level.SEVERE,"mcf_add_toregistry_failed",poolName); 1000 if (_logger.isLoggable( Level.FINE ) ) { 1001 _logger.log(Level.FINE,"",cre); 1002 } 1003 throw cre; 1005 } 1006 } 1007 1008 1012 1013 public void killAllPools(String moduleName) { 1014 1015 ResourcesUtil resUtil = ResourcesUtil.getInstance(); 1016 Object [] poolNamesArray = resUtil.getConnectorConnectionPoolNames( 1017 moduleName); 1018 String poolName=null; 1019 for(int i=0;poolNamesArray != null && i<poolNamesArray.length;i++) { 1020 poolName = (String )poolNamesArray[i]; 1021 killPool(poolName); 1022 } 1023 } 1024 1025 1029 1030 public void killPool(String poolName) { 1031 Switch.getSwitch().getPoolManager().killPool(poolName); 1032 } 1033 1034 1044 public static Set getConnectionDefinitionProperties(String connectionDefinitionClassName) { 1045 return ConnectionDefinitionUtils.getConnectionDefinitionProperties( 1046 connectionDefinitionClassName); 1047 } 1048 1049 1059 public static Map getConnectionDefinitionPropertiesAndDefaults(String connectionDefinitionClassName) { 1060 return ConnectionDefinitionUtils 1061 .getConnectionDefinitionPropertiesAndDefaults( 1062 connectionDefinitionClassName); 1063 } 1064 1065 1087 1088 private Object getUnpooledConnection( String poolName, ResourcePrincipal prin, 1089 boolean returnConnectionHandle) 1090 throws ResourceException 1091 { 1092 ManagedConnectionFactory mcf = null; 1094 boolean needToUndeployPool = false; 1095 com.sun.enterprise.config.serverbeans.JdbcConnectionPool 1096 jdbcPoolToDeploy = null; 1097 com.sun.enterprise.config.serverbeans.ConnectorConnectionPool 1098 ccPoolToDeploy = null; 1099 1100 1101 try { 1102 mcf = obtainManagedConnectionFactory( poolName ); 1103 1104 } catch( ConnectorRuntimeException cre ) { 1105 logFine("getUnpooledConnection :: obtainManagedConnectionFactory " + 1106 "threw exception. SO doing checkAndLoadPoolResource"); 1107 if(checkAndLoadPoolResource(poolName)) { 1108 logFine("getUnpooledConnection:: checkAndLoadPoolResource is true"); 1109 try { 1110 if ( ! isConnectorConnectionPoolDeployed( poolName ) ) { 1116 logFine("getUnpooledConnection :: " + 1117 "isConnectorConnectionPoolDeployed is false"); 1118 try { 1119 jdbcPoolToDeploy = getJdbcConnectionPoolServerBean( poolName ); 1120 if ( jdbcPoolToDeploy != null ) { 1121 (new JdbcConnectionPoolDeployer()).deployResource( 1122 jdbcPoolToDeploy ); 1123 logFine("getUnpooledConnection :: force deployed the " + 1124 "JdbcConnectionPool : " + poolName); 1125 } else { 1126 ccPoolToDeploy = getConnectorConnectionPoolServerBean( 1127 poolName ); 1128 (new ConnectorConnectionPoolDeployer()).deployResource( 1129 ccPoolToDeploy); 1130 logFine("getUnpooledConnection :: force deployed the " + 1131 "ConnectorConnectionPool :" + poolName); 1132 } 1133 needToUndeployPool = true; 1134 } catch(Exception e ) { 1135 _logger.log( Level.SEVERE, 1136 "jdbc.could_not_do_actual_deploy for : ", poolName ); 1137 throw new ResourceException( e ); 1138 } 1139 } 1140 logFine("getUnpooledConnection :: " + 1141 "Now calling obtainManagedConnectionFactory again"); 1142 mcf = obtainManagedConnectionFactory( poolName ); 1143 logFine("getUnpooledConnection:: " + 1144 "done obtainManagedConnectionFactory again"); 1145 } catch( ConnectorRuntimeException creAgain ) { 1146 String l10nMsg = localStrings.getString( 1147 "pingpool.cannot_obtain_mcf"); 1148 _logger.log( Level.WARNING, "jdbc.pool_not_reachable", 1149 l10nMsg ); 1150 ResourceException e = new ResourceException( l10nMsg ); 1151 e.initCause( creAgain ); 1152 throw e; 1153 } 1154 } else { 1155 _logger.log( Level.WARNING, "jdbc.pool_not_reachable", 1156 cre.getMessage() ); 1157 String l10nMsg = localStrings.getString( 1158 "pingpool.cannot_obtain_mcf"); 1159 ResourceException e = new ResourceException( l10nMsg ); 1160 e.initCause( cre ); 1161 throw e; 1162 1163 } 1164 } 1165 1166 1167 ResourcePrincipal resourcePrincipal = null; 1168 if (prin == null ) { 1169 try { 1170 resourcePrincipal = getDefaultResourcePrincipal( poolName, mcf); 1171 } catch( NamingException ne) { 1172 _logger.log(Level.WARNING, "jdbc.pool_not_reachable", 1173 ne.getMessage() ); 1174 String l10nMsg = localStrings.getString( 1175 "pingpool.name_not_bound"); 1176 ResourceException e = new ResourceException( l10nMsg + poolName ); 1177 e.initCause( ne ); 1178 throw e; 1179 } 1180 } else { 1181 resourcePrincipal = prin; 1182 } 1183 1184 final Subject defaultSubject = 1185 ConnectionPoolObjectsUtils.createSubject( mcf, resourcePrincipal); 1186 1187 1188 if (_logger.isLoggable(Level.FINE)) { 1189 _logger.fine("using subject: " + defaultSubject); 1190 1191 } 1192 1193 ManagedConnection mc = null; 1194 mc = mcf.createManagedConnection( defaultSubject, null ); 1196 1197 if ( needToUndeployPool ) { 1200 if (jdbcPoolToDeploy != null ) { 1201 logFine("getUnpooledConnection :: need to force undeploy pool"); 1202 try { 1203 (new JdbcConnectionPoolDeployer()).undeployResource( 1204 jdbcPoolToDeploy ); 1205 } catch( Exception e ) { 1206 _logger.fine( "getUnpooledConnection: error undeploying pool"); 1207 } 1208 logFine("getUnpooledConnection :: done.. force undeploy of pool"); 1209 } else { 1210 try { 1211 (new ConnectorConnectionPoolDeployer()).undeployResource( 1212 ccPoolToDeploy ); 1213 } catch( Exception e ) { 1214 _logger.fine( "getUnpooledConnection: error undeploying pool"); 1215 } 1216 logFine("getUnpooledConnection :: done.. force undeploy of pool"); 1217 } 1218 } 1219 1220 UnpooledConnectionEventListener el = new 1223 UnpooledConnectionEventListener(); 1224 mc.addConnectionEventListener( new UnpooledConnectionEventListener() ); 1225 return returnConnectionHandle ? 1226 mc.getConnection( defaultSubject, null ) : 1227 mc; 1228 } 1229 1230 1248 public Connection getConnection(String jndiName, String user, String password) 1249 throws SQLException 1250 { 1251 1252 DASResourcesUtil dasResUtil = (DASResourcesUtil) ResourcesUtil.getInstance(); 1253 dasResUtil.setGetConnectionFromConnectorRuntime( true ); 1254 String poolName = getPoolNameFromResourceJndiName( jndiName ); 1255 if (_logger.isLoggable(Level.FINE)) { 1256 _logger.fine("ConnectorRuntime.getConnection :: poolName : " + poolName ); 1257 } 1258 java.sql.Connection con = null; 1259 try { 1260 String passwd = (password == null ) ? "" : password; 1262 1263 ResourcePrincipal prin = (user == null) ? 1268 null : new ResourcePrincipal(user, password); 1269 con = (java.sql.Connection ) getUnpooledConnection( poolName, prin, true); 1270 if ( con == null ) { 1271 String i18nMsg = localStrings.getString( 1272 "ccp_adm.null_unpooled_connection"); 1273 SQLException sqle = new SQLException ( i18nMsg ); 1274 throw sqle; 1275 } 1276 } catch( ResourceException re ) { 1277 SQLException sqle = new SQLException ( re.getMessage() ); 1278 sqle.initCause( re ); 1279 _logger.log( Level.WARNING, "jdbc.exc_get_conn", re.getMessage()); 1280 if (_logger.isLoggable(Level.FINE)) { 1281 _logger.fine( " getConnection in ConnectorRuntime failed : " + re ); 1282 } 1283 } finally { 1284 try { 1285 dasResUtil.setGetConnectionFromConnectorRuntime( false ); 1286 } catch( Exception e ) { 1287 if (_logger.isLoggable(Level.FINE)) { 1288 _logger.fine("caught exception while setting " + 1289 "getConnectionFromConnectorRuntime to false"); 1290 } 1291 } 1292 1293 } 1294 1295 return con; 1296 } 1297 1298 1314 public Connection getConnection(String jndiName) 1315 throws SQLException 1316 { 1317 java.sql.Connection con = null; 1318 DASResourcesUtil dasResUtil = null; 1319 try { 1320 dasResUtil = (DASResourcesUtil) ResourcesUtil.getInstance(); 1321 dasResUtil.setGetConnectionFromConnectorRuntime( true ); 1322 String poolName = getPoolNameFromResourceJndiName( jndiName ); 1323 if (_logger.isLoggable(Level.FINE)) { 1324 _logger.fine("ConnectorRuntime.getConnection :: poolName : " 1325 + poolName ); 1326 } 1327 con = (java.sql.Connection ) getUnpooledConnection( poolName, null, 1328 true ); 1329 if ( con == null ) { 1330 String i18nMsg = localStrings.getString( 1331 "ccp_adm.null_unpooled_connection"); 1332 SQLException sqle = new SQLException ( i18nMsg ); 1333 throw sqle; 1334 } 1335 } catch( ResourceException re ) { 1336 SQLException sqle = new SQLException ( re.getMessage() ); 1337 sqle.initCause( re ); 1338 _logger.log( Level.WARNING, "jdbc.exc_get_conn", re.getMessage()); 1339 if (_logger.isLoggable(Level.FINE)) { 1340 _logger.fine( "Exception : " + re ); 1341 } 1342 throw sqle; 1343 } finally { 1344 try { 1345 dasResUtil.setGetConnectionFromConnectorRuntime( false ); 1346 } catch( Exception e ) { 1347 if (_logger.isLoggable(Level.FINE)) { 1348 _logger.fine("caught exception while setting " + 1349 "getConnectionFromConnectorRuntime to false"); 1350 } 1351 } 1352 } 1353 1354 return con; 1355 } 1356 1357 1366 private String getPoolNameFromResourceJndiName( String jndiName) { 1367 String poolName = null ; 1368 JdbcResource jdbcRes = null; 1369 DASResourcesUtil resourcesUtil = (DASResourcesUtil)ResourcesUtil.getInstance(); 1370 1371 if (resourcesUtil.isPMFResource( jndiName)) { 1373 jdbcRes = resourcesUtil.getJdbcResourceByJndiName( 1374 resourcesUtil.getPMFResourceByJndiName(jndiName ). 1375 getJdbcResourceJndiName()); 1376 } else { 1377 jdbcRes = resourcesUtil.getJdbcResourceByJndiName(jndiName); 1378 } 1379 1380 if ( jdbcRes != null ) { 1381 if (_logger.isLoggable(Level.FINE)) { 1382 _logger.fine( "jdbcRes is ---: " + jdbcRes.getJndiName() ); 1383 _logger.fine( "poolName is ---: " + jdbcRes.getPoolName() ); 1384 } 1385 } 1386 return jdbcRes == null ? null : jdbcRes.getPoolName(); 1387 } 1388 1389 1395 public boolean isConnectorConnectionPoolDeployed(String poolName) { 1396 try { 1397 InitialContext ic = new InitialContext(); 1398 String jndiName = ConnectorAdminServiceUtils. 1399 getReservePrefixedJNDINameForPool( poolName ); 1400 ic.lookup(jndiName); 1401 return true; 1402 } catch (NamingException e) { 1403 return false; 1404 } 1405 } 1406 1407 private void dump(String poolName) { 1408 try { 1409 ConfigContext ctx = com.sun.enterprise.admin.server.core.AdminService. 1410 getAdminService().getAdminContext().getAdminConfigContext(); 1411 Domain dom = ServerBeansFactory.getDomainBean(ctx); 1412 Resources res = dom.getResources(); 1413 1414 ConfigBean dasContextBean = null; 1415 1416 com.sun.enterprise.config.serverbeans.JdbcConnectionPool 1417 jdbcPool = res.getJdbcConnectionPoolByName(poolName); 1418 1419 if (jdbcPool != null) { 1421 dasContextBean = (ConfigBean) jdbcPool; 1422 } else { 1423 com.sun.enterprise.config.serverbeans.ConnectorConnectionPool ccPool = 1424 res.getConnectorConnectionPoolByName(poolName); 1425 if (ccPool != null) { 1426 dasContextBean = (ConfigBean) ccPool; 1427 } 1428 } 1429 1430 if (dasContextBean != null) { 1431 StringBuffer str = new StringBuffer (); 1432 dasContextBean.dump(str, "\t\t"); 1433 _logger.log(Level.INFO, "DAS CONTEXT IS : " + str); 1434 } 1435 } catch (Exception e) { 1436 _logger.log(Level.WARNING, "Exception while dumping pool details ", e.fillInStackTrace()); 1437 } 1438 } 1439 1440 private com.sun.enterprise.config.serverbeans.JdbcConnectionPool 1441 getJdbcConnectionPoolServerBean( String poolName ) 1442 throws ConfigException 1443 { 1444 if ( poolName == null ) { 1445 throw new ConfigException("null poolname"); 1446 } 1447 1448 ConfigContext ctxt = null; 1449 1450 if ( ResourcesUtil.isDAS() ) { 1451 ctxt = com.sun.enterprise.admin.server.core.AdminService. 1452 getAdminService().getAdminContext().getAdminConfigContext(); 1453 } else { 1454 ctxt = ApplicationServer.getServerContext().getConfigContext(); 1455 } 1456 Domain dom = ServerBeansFactory.getDomainBean( ctxt ); 1457 Resources res = dom.getResources(); 1458 1459 return res.getJdbcConnectionPoolByName( poolName ); 1460 1461 } 1462 1463 private com.sun.enterprise.config.serverbeans.ConnectorConnectionPool 1464 getConnectorConnectionPoolServerBean( String poolName ) 1465 throws ConfigException 1466 { 1467 if ( poolName == null ) { 1468 throw new ConfigException("null poolname"); 1469 } 1470 1471 ConfigContext ctxt = null; 1472 1473 if ( ResourcesUtil.isDAS() ) { 1474 ctxt = com.sun.enterprise.admin.server.core.AdminService. 1475 getAdminService().getAdminContext().getAdminConfigContext(); 1476 } else { 1477 ctxt = ApplicationServer.getServerContext().getConfigContext(); 1478 } 1479 Domain dom = ServerBeansFactory.getDomainBean( ctxt ); 1480 Resources res = dom.getResources(); 1481 1482 return res.getConnectorConnectionPoolByName( poolName ); 1483 1484 } 1485 1486 private void logFine( String msg ) { 1487 if ( msg != null ) { 1488 if (_logger.isLoggable(Level.FINE)) { 1489 _logger.fine( msg ); 1490 } 1491 } 1492 } 1493 1494 private void createAndAddPool( String poolName, PoolType pt) throws 1495 ConnectorRuntimeException 1496 { 1497 PoolManager poolMgr = Switch.getSwitch().getPoolManager(); 1498 try { 1499 poolMgr.createEmptyConnectionPool( poolName, pt ); 1500 } catch( PoolingException pe ) { 1501 String i18nMsg = localStrings.getString( 1502 "ccp_adm.failed_to_create_pool_object"); 1503 ConnectorRuntimeException cre = 1504 new ConnectorRuntimeException( i18nMsg ); 1505 cre.initCause( pe ); 1506 throw cre; 1507 } 1508 } 1509 1510 private void initialize() { 1511 Switch sw = Switch.getSwitch(); 1512 if (sw.getContainerType() == ConnectorConstants.NON_ACC_CLIENT) { 1513 try { 1515 if (sw.getPoolManager() == null) { 1516 sw.setPoolManager(new PoolManagerImpl()); 1517 } 1518 1519 if (sw.getNamingManager() == null) { 1520 sw.setNamingManager(new NamingManagerImpl()); 1521 } 1522 } catch(Exception e) { 1523 _logger.log( Level.WARNING, e.getMessage()); 1524 } 1525 } 1526 1527 } 1528} 1529 | Popular Tags |