1 23 package com.sun.enterprise.connectors; 24 25 import java.io.Serializable ; 26 import java.security.AccessController ; 27 import java.security.PrivilegedAction ; 28 import java.security.Principal ; 29 import java.util.Properties ; 30 import javax.resource.ResourceException ; 31 import javax.resource.spi.*; 32 import javax.resource.spi.IllegalStateException ; 33 import javax.resource.NotSupportedException ; 34 import com.sun.enterprise.*; 35 import com.sun.enterprise.security.SecurityContext; 36 import com.sun.enterprise.util.i18n.StringManager; 37 import com.sun.enterprise.deployment.ResourceReferenceDescriptor; 38 import com.sun.enterprise.deployment.ConnectorDescriptor; 39 import com.sun.enterprise.connectors.ConnectorConnectionPool; 40 import com.sun.enterprise.connectors.ConnectorDescriptorInfo; 41 import com.sun.enterprise.connectors.authentication.AuthenticationService; 42 import com.sun.enterprise.deployment.ResourcePrincipal; 43 import com.sun.enterprise.distributedtx.J2EETransactionManagerOpt; 44 import com.sun.enterprise.naming.NamingManagerImpl; 45 import com.sun.enterprise.log.Log; 46 import com.sun.enterprise.iiop.PEORBConfigurator; 47 import com.sun.enterprise.resource.*; 48 import javax.security.auth.Subject ; 49 import javax.naming.InitialContext ; 50 import javax.resource.spi.security.PasswordCredential ; 51 import java.util.logging.*; 52 import java.util.Set ; 53 import com.sun.logging.*; 54 import com.sun.enterprise.connectors.util.ConnectionPoolObjectsUtils; 55 56 59 public class ConnectionManagerImpl 60 implements ConnectionManager, Serializable { 61 62 protected String jndiName; 63 64 protected String poolName; 65 66 69 static Logger _logger = LogDomains.getLogger(LogDomains.RSR_LOGGER); 70 71 protected String rarName; 75 76 protected ResourcePrincipal defaultPrin = null; 77 78 public ConnectionManagerImpl(String poolName) { 79 this.poolName = poolName; 80 } 81 82 public void setJndiName(String jndiName) { 83 this.jndiName = jndiName; 84 } 85 86 public String getJndiName() { 87 return jndiName; 88 } 89 90 public void setPoolName(String poolName) { 91 this.poolName = poolName; 92 } 93 94 108 public Object allocateNonTxConnection( ManagedConnectionFactory mcf, 109 ConnectionRequestInfo cxRequestInfo ) throws ResourceException 110 { 111 String localJndiName = jndiName; 112 113 if (_logger.isLoggable(Level.FINE)) { 114 _logger.fine("Allocating NonTxConnection"); 115 } 116 117 if ( ! jndiName.endsWith( ConnectorConstants.NON_TX_JNDI_SUFFIX ) ) { 124 localJndiName = jndiName + ConnectorConstants.NON_TX_JNDI_SUFFIX ; 125 126 if (_logger.isLoggable(Level.FINE)) { 127 _logger.fine("Adding __nontx to jndiname"); 128 } 129 } else { 130 if (_logger.isLoggable(Level.FINE)) { 131 _logger.fine("lookup happened from a __nontx datasource directly"); 132 } 133 } 134 135 return allocateConnection( mcf, cxRequestInfo, localJndiName); 136 137 138 } 139 140 public Object 141 allocateConnection(ManagedConnectionFactory mcf, 142 ConnectionRequestInfo cxRequestInfo) 143 throws ResourceException { 144 145 return this.allocateConnection( mcf, cxRequestInfo, jndiName); 146 } 147 148 public Object allocateConnection(ManagedConnectionFactory mcf, 149 ConnectionRequestInfo cxRequestInfo, String jndiNameToUse) 150 throws ResourceException 151 { 152 return this.allocateConnection( mcf, cxRequestInfo, jndiNameToUse, null ); 153 } 154 155 public Object allocateConnection(ManagedConnectionFactory mcf, 156 ConnectionRequestInfo cxRequestInfo, String jndiNameToUse, Object conn) 157 throws ResourceException 158 { 159 validatePool(); 160 PoolManager poolmgr = Switch.getSwitch().getPoolManager(); 161 boolean resourceShareable = true; 162 163 ResourceReferenceDescriptor ref = poolmgr.getResourceReference(jndiNameToUse); 164 if (ref != null) { 165 String shareableStr = ref.getSharingScope(); 166 167 if (shareableStr.equals(ref.RESOURCE_UNSHAREABLE)) { 170 resourceShareable = false; 171 } 172 } 173 174 if (ref == null) { 175 _logger.log(Level.FINE,"poolmgr.no_resource_reference",jndiNameToUse); 176 return internalGetConnection(mcf, defaultPrin, cxRequestInfo, 177 resourceShareable, jndiNameToUse, conn, true); 178 } 179 String auth = ref.getAuthorization(); 180 181 if (auth.equals(ref.APPLICATION_AUTHORIZATION) ) { 182 if (cxRequestInfo == null ) { 183 StringManager localStrings = 184 StringManager.getManager(ConnectionManagerImpl.class); 185 String msg = localStrings.getString( 186 "con_mgr.null_userpass"); 187 throw new ResourceException ( msg ); 188 } 189 ConnectorRuntime.getRuntime().switchOnMatching(rarName, poolName); 190 return internalGetConnection(mcf, null, cxRequestInfo, 191 resourceShareable, jndiNameToUse, conn, false ); 192 } else { 193 ResourcePrincipal prin =null; 194 Set principalSet =null; 195 Principal callerPrincipal = null; 196 SecurityContext securityContext = null; 197 ConnectorRuntime connectorRuntime = ConnectorRuntime.getRuntime(); 198 if(connectorRuntime.isServer() && 199 (securityContext = SecurityContext.getCurrent()) != null && 200 (callerPrincipal = securityContext.getCallerPrincipal()) != null && 201 (principalSet = securityContext.getPrincipalSet()) != null) { 202 AuthenticationService authService = 203 connectorRuntime.getAuthenticationService(rarName,poolName); 204 if(authService != null) { 205 prin = (ResourcePrincipal)authService.mapPrincipal( 206 callerPrincipal, principalSet); 207 } 208 } 209 210 if (prin == null) { 211 prin = ref.getResourcePrincipal(); 212 if (prin == null) { 213 _logger.log(Level.FINE,"default-resource-principal not" + 214 "specified for " + jndiNameToUse + ". Defaulting to" + 215 " user/password specified in the pool"); 216 217 prin = defaultPrin; 218 } else if (!prin.equals(defaultPrin)){ 219 ConnectorRuntime.getRuntime().switchOnMatching(rarName, poolName); 220 } 221 } 222 return internalGetConnection(mcf, prin, cxRequestInfo, 223 resourceShareable, jndiNameToUse, conn, false); 224 } 225 226 } 227 228 229 protected Object internalGetConnection(ManagedConnectionFactory mcf, 230 final ResourcePrincipal prin, ConnectionRequestInfo cxRequestInfo, 231 boolean shareable, String jndiNameToUse, Object conn, boolean isUnknownAuth) 232 throws ResourceException 233 { 234 try { 235 PoolManager poolmgr = Switch.getSwitch().getPoolManager(); 236 ConnectorRegistry registry = ConnectorRegistry.getInstance(); 237 PoolMetaData pmd = registry.getPoolMetaData( poolName ); 238 239 ResourceSpec spec = new ResourceSpec(jndiNameToUse, 240 ResourceSpec.JNDI_NAME, pmd); 241 spec.setConnectionPoolName(this.poolName); 242 ManagedConnectionFactory freshMCF = pmd.getMCF(); 243 244 if (! freshMCF.equals(mcf)) { 245 _logger.log(Level.INFO, "conmgr.mcf_not_equal"); 246 } 247 ConnectorDescriptor desc = registry.getDescriptor(rarName); 248 249 Subject subject = null; 250 ClientSecurityInfo info = null; 251 boolean subjectDefined = false; 252 if (isUnknownAuth && rarName.equals(ConnectorConstants.DEFAULT_JMS_ADAPTER) 253 && !(pmd.isAuthCredentialsDefinedInPool())) { 254 subject = ConnectionPoolObjectsUtils.createSubject(mcf, null); 264 } else { 265 if (prin == null) { 266 info = new ClientSecurityInfo(cxRequestInfo); 267 } else { 268 info = new ClientSecurityInfo(prin); 269 if (subject == null) { 271 if (prin.equals(defaultPrin)) { 272 subject = pmd.getSubject(); 273 } else { 274 subject = ConnectionPoolObjectsUtils.createSubject(mcf, prin); 275 } 276 } 277 } 278 } 279 280 int txLevel = pmd.getTransactionSupport(); 281 if (_logger.isLoggable(Level.FINE)) { 282 _logger.fine( "ConnectionMgr: poolName " + poolName + 283 " txLevel : " + txLevel ); 284 } 285 286 ResourceAllocator alloc = null; 287 if ( conn != null ) { 288 spec.setConnectionToAssociate( conn ); 289 } 290 291 switch (txLevel) { 292 293 case ConnectorConstants.NO_TRANSACTION_INT: 294 alloc = 295 new NoTxConnectorAllocator(poolmgr, mcf, spec, 296 subject, cxRequestInfo, info, 297 desc); 298 return poolmgr.getResource(spec, alloc, info); 299 300 case ConnectorConstants.LOCAL_TRANSACTION_INT: 301 if (!shareable) { 302 StringManager localStrings = 303 StringManager.getManager(ConnectionManagerImpl.class); 304 String i18nMsg = localStrings.getString( 305 "con_mgr.resource_not_shareable"); 306 throw new ResourceAllocationException( i18nMsg ); 307 } 308 alloc = 309 new LocalTxConnectorAllocator(poolmgr, mcf, spec, 310 subject, cxRequestInfo, 311 info, desc); 312 return poolmgr.getResource(spec, alloc, info); 313 case ConnectorConstants.XA_TRANSACTION_INT: 314 if (rarName.equals(ConnectorRuntime.DEFAULT_JMS_ADAPTER)) { 315 shareable = false; 316 } 317 spec.markAsXA(); 318 alloc = 319 new ConnectorAllocator(poolmgr, mcf, spec, 320 subject, cxRequestInfo, info, desc, 321 shareable); 322 return poolmgr.getResource(spec, alloc, info); 323 default: 324 StringManager localStrings = 325 StringManager.getManager(ConnectionManagerImpl.class); 326 String i18nMsg = localStrings.getString( 327 "con_mgr.illegal_tx_level", txLevel+ " "); 328 throw new IllegalStateException (i18nMsg); 329 } 330 331 } catch (PoolingException ex) { 332 _logger.log(Level.WARNING,"poolmgr.get_connection_failure",ex.getMessage()); 333 StringManager localStrings = 334 StringManager.getManager(ConnectionManagerImpl.class); 335 String i18nMsg = localStrings.getString( 336 "con_mgr.error_creating_connection", ex.getMessage() ); 337 ResourceAllocationException rae = new ResourceAllocationException( 338 i18nMsg ); 339 rae.initCause( ex ); 340 throw rae; 341 } 342 } 343 344 345 public void setRarName( String _rarName ) { 346 rarName = _rarName; 347 } 348 349 public String getRarName() { 350 return rarName; 351 } 352 353 359 public void initialize() throws ConnectorRuntimeException { 360 361 Switch sw = Switch.getSwitch(); 362 if (sw.getContainerType() == ConnectorConstants.NON_ACC_CLIENT) { 363 try { 365 PEORBConfigurator.initTransactionService(null, new Properties () ); 369 370 if (sw.getTransactionManager() == null) { 371 sw.setTransactionManager(new J2EETransactionManagerOpt()); 372 } 373 jndiName = ResourceInstaller.getPMJndiName(jndiName); 376 } catch(Exception e) { 377 throw (ConnectorRuntimeException) 378 new ConnectorRuntimeException(e.getMessage()).initCause(e); 379 } 380 } 381 382 ConnectorRuntime runtime = ConnectorRuntime.getRuntime(); 383 ManagedConnectionFactory mcf = runtime.obtainManagedConnectionFactory(poolName); 384 ConnectorRegistry registry = ConnectorRegistry.getInstance(); 385 PoolMetaData pmd = registry.getPoolMetaData( poolName ); 386 defaultPrin = pmd.getResourcePrincipal(); 387 388 } 389 390 private void validatePool() throws ResourceException { 391 ConnectorRegistry registry = ConnectorRegistry.getInstance(); 392 if (registry.getPoolMetaData(poolName) == null){ 393 StringManager localStrings = 394 StringManager.getManager(ConnectionManagerImpl.class); 395 String msg = localStrings.getString("con_mgr.no_pool_meta_data"); 396 throw new ResourceException (poolName + ": " + msg); 397 } 398 } 399 } 400 | Popular Tags |