1 23 package com.sun.enterprise.naming.factory; 24 25 import java.util.*; 26 import javax.naming.*; 27 import javax.naming.spi.*; 28 29 import java.util.Hashtable ; 30 import javax.resource.spi.ConnectionManager ; 31 import javax.resource.spi.ManagedConnectionFactory ; 32 33 import com.sun.enterprise.*; 34 import com.sun.enterprise.util.LocalStringManagerImpl; 35 import com.sun.enterprise.connectors.*; 36 import com.sun.enterprise.deployment.ConnectorDescriptor; 37 38 import java.util.logging.*; 39 import com.sun.logging.*; 40 41 47 public class ConnectorObjectFactory implements ObjectFactory { 48 49 static Logger _logger=LogDomains.getLogger(LogDomains.JNDI_LOGGER); 50 51 private static LocalStringManagerImpl localStrings = 52 new LocalStringManagerImpl(ConnectorObjectFactory.class); 53 private ConnectorRuntime runtime = ConnectorRuntime.getRuntime(); 54 55 public ConnectorObjectFactory() { 56 } 57 58 public Object getObjectInstance(Object obj, 59 Name name, 60 Context nameCtx, 61 Hashtable env) throws Exception 62 { 63 Reference ref = (Reference) obj; 64 if(_logger.isLoggable(Level.FINE)) { 65 _logger.log(Level.FINE,"ConnectorObjectFactory: " + ref + 66 " Name:" + name); 67 } 68 String poolName = (String ) ref.get(0).getContent(); 69 String moduleName = (String ) ref.get(1).getContent(); 70 71 Switch sw = Switch.getSwitch(); 72 73 if(runtime.getEnviron() == ConnectorRuntime.CLIENT) { 74 ConnectorDescriptor connectorDescriptor = null; 75 try { 76 Context ic = new InitialContext(); 77 String descriptorJNDIName = ConnectorAdminServiceUtils. 78 getReservePrefixedJNDINameForDescriptor(moduleName); 79 connectorDescriptor = (ConnectorDescriptor)ic.lookup(descriptorJNDIName); 80 } 81 catch(NamingException ne) { 82 _logger.log(Level.FINE, 83 "Failed to look up ConnectorDescriptor from JNDI", 84 moduleName); 85 throw new ConnectorRuntimeException( 86 "Failed to look up ConnectorDescriptor from JNDI"); 87 } 88 runtime.createActiveResourceAdapter(connectorDescriptor, 89 moduleName,null,false); 90 } 91 92 ClassLoader loader = Thread.currentThread().getContextClassLoader(); 93 if (runtime.checkAccessibility(moduleName, loader) == false) { 94 throw new NamingException( 95 "Only the application that has the embedded resource" + 96 "adapter can access the resource adapter"); 97 } 98 99 100 ManagedConnectionFactory mcf = runtime.obtainManagedConnectionFactory(poolName); 101 if(mcf == null) { 102 _logger.log(Level.FINE,"Failed to create MCF ",poolName); 103 throw new ConnectorRuntimeException("Failed to create MCF"); 104 } 105 106 String jndiName = name.toString(); 107 boolean forceNoLazyAssoc = false; 108 if ( jndiName.endsWith( ConnectorConstants.PM_JNDI_SUFFIX ) ) { 109 forceNoLazyAssoc = true; 110 } 111 ConnectionManagerImpl mgr = (ConnectionManagerImpl) 112 runtime.obtainConnectionManager(poolName, forceNoLazyAssoc); 113 mgr.setJndiName(deriveJndiName(jndiName, env)); 114 mgr.setRarName( moduleName ); 115 mgr.initialize(); 116 117 Object cf = mcf.createConnectionFactory(mgr); 118 if (cf == null) { 119 String msg = localStrings.getLocalString 120 ("no.resource.adapter", ""); 121 throw new ConfigurationException(msg); 122 } 123 if(_logger.isLoggable(Level.FINE)) { 124 _logger.log(Level.FINE,"Connection Factory:" + cf); 125 } 126 127 return cf; 128 } 129 130 private String deriveJndiName(String name, Hashtable env) { 131 String suffix = (String ) env.get(ConnectorConstants.JNDI_SUFFIX_PROPERTY); 132 if (runtime.isValidJndiSuffix(suffix)) { 133 _logger.log(Level.FINE, "JNDI name will be suffixed with :" + suffix); 134 return name + suffix; 135 } 136 return name; 137 } 138 139 } 140 | Popular Tags |