1 23 package com.sun.enterprise.repository; 24 25 import java.security.AccessController ; 26 import java.security.PrivilegedAction ; 27 import java.security.PrivilegedExceptionAction ; 28 import java.security.PrivilegedActionException ; 29 import java.lang.reflect.Method ; 30 import java.io.Serializable ; 31 import java.util.*; 32 import javax.naming.*; 33 import javax.resource.spi.*; 34 import javax.resource.ResourceException ; 35 import com.sun.enterprise.util.Utility; 36 import com.sun.enterprise.util.JarClassLoader; 37 import com.sun.enterprise.resource.*; 38 import com.sun.enterprise.deployment.ConnectorDescriptor; 39 import com.sun.enterprise.deployment.EnvironmentProperty; 40 import com.sun.enterprise.Switch; 41 import com.sun.enterprise.log.Log; 42 import java.util.logging.*; 44 import com.sun.logging.*; 45 47 import com.sun.enterprise.connectors.ConnectionManagerImpl; 48 53 public class ConnectorResource extends J2EEResourceBase implements Serializable { 54 55 private static Logger _logger=null; 57 static{ 58 _logger=LogDomains.getLogger(LogDomains.ROOT_LOGGER); 59 } 60 static public final String CONNECTOR_OBJECT_FACTORY = 62 "com.sun.enterprise.naming.factory.ConnectorObjectFactory"; 63 64 private String dbuser_; 65 private String dbpassword_; 66 private String connectorname_; 67 private String appname_; 68 private String connectionFactoryClass_; 69 70 public ConnectorResource(String name) { 71 super(name); 72 dbuser_ = ""; 73 dbpassword_ = ""; 74 appname_ = ""; 75 } 76 77 protected J2EEResource doClone(String name) { 78 ConnectorResource clone = new ConnectorResource(name); 79 clone.setDbuser(getDbuser()); 80 clone.setDbpassword(getDbpassword()); 81 clone.setConnectorName(getConnectorName()); 82 clone.setApplicationName(getApplicationName()); 83 clone.setConnectionFactoryClass(getConnectionFactoryClass()); 84 return clone; 85 } 86 87 public int getType() { 88 return J2EEResource.CONNECTOR_RESOURCE; 89 } 90 91 public String getApplicationName() { 92 return appname_; 93 } 94 95 public void setApplicationName(String appname) { 96 if (appname == null) { 97 appname_ = ""; 98 } else { 99 appname_ = appname; 100 } 101 } 102 103 public String getConnectorName() { 104 return connectorname_; 105 } 106 107 public void setConnectorName(String connectorname) { 108 connectorname_ = connectorname; 109 } 110 111 public String getConnectionFactoryClass() { 112 return connectionFactoryClass_; 113 } 114 115 public void setConnectionFactoryClass(String cfclass) { 116 connectionFactoryClass_ = cfclass; 117 } 118 119 public String getDbuser() { 120 return dbuser_; 121 } 122 123 public void setDbuser(String dbuser) { 124 if (dbuser == null) { 125 dbuser_ = ""; 126 } else { 127 dbuser_ = dbuser; 128 } 129 } 130 131 public String getDbpassword() { 132 return dbpassword_; 133 } 134 135 public void setDbpassword(String dbpassword) { 136 if (dbpassword == null) { 137 dbpassword_ = ""; 138 } else { 139 dbpassword_ = dbpassword; 140 } 141 } 142 143 144 public Reference createConnectionFactoryReference() { 145 Reference ref = 146 new Reference(getConnectionFactoryClass(), 147 new StringRefAddr("jndiName", getName()), 148 CONNECTOR_OBJECT_FACTORY, null); 149 return ref; 150 } 151 152 153 public ManagedConnectionFactory 156 createManagedConnectionFactory(ClassLoader jcl) 157 throws PoolingException { 158 159 try { 160 if (jcl == null) { 161 jcl = (ClassLoader ) AccessController.doPrivileged 163 (new PrivilegedAction () { 164 public Object run() { 165 return Thread.currentThread().getContextClassLoader(); 166 } 167 }); 168 } 169 170 ResourceInstaller ri = Switch.getSwitch().getResourceInstaller(); 171 ConnectorDescriptor desc = 172 ri.getDescriptorByConnectorName(getApplicationName(), 173 getConnectorName()); 174 String jndiName = getName(); 175 String mcfClass = desc.getManagedConnectionFactoryImpl(); 176 177 String appName = getApplicationName(); 178 179 184 _logger.log(Level.FINE,"ClassLoader: " + jcl.getClass()); 186 _logger.log(Level.FINE,"ClassLoader: " + jcl.getParent().getClass()); 187 _logger.log(Level.FINE,"ClassLoader: " + jcl.getParent().getParent().getClass()); 188 ManagedConnectionFactory mcf = (ManagedConnectionFactory) 190 Class.forName(mcfClass, true, jcl).newInstance(); 191 AccessController.doPrivileged 192 (new SetMethodAction(mcf, desc)); 193 return mcf; 194 } catch (PrivilegedActionException ex) { 195 Exception nested = ex.getException(); 196 _logger.log(Level.SEVERE,"enterprise.privilegedaction_exception",nested.toString()); 199 Log.err.flush(); 201 throw new PoolingException(nested.toString(), nested); 202 } catch (Exception ex) { 203 _logger.log(Level.SEVERE,"enterprise.managedconnfactory_exception",ex.toString()); 206 Log.err.flush(); 208 throw new PoolingException(ex.toString(), ex); 209 } 210 211 } 212 213 public Object createConnectionFactory() 215 throws PoolingException { 216 217 ManagedConnectionFactory mcf = createManagedConnectionFactory(null); 218 try { 219 Object cf = mcf.createConnectionFactory 220 (new ConnectionManagerImpl(getName())); 221 return cf; 222 } catch (ResourceException ex) { 223 _logger.log(Level.SEVERE,"enterprise.resource_exception",ex.toString()); 226 Log.err.flush(); 228 throw new PoolingException(ex.toString(), ex); 229 } 230 } 231 232 class SetMethodAction implements PrivilegedExceptionAction { 234 ManagedConnectionFactory mcf; 235 ConnectorDescriptor desc; 236 237 SetMethodAction(ManagedConnectionFactory mcf, 238 ConnectorDescriptor desc) { 239 this.mcf = mcf; 240 this.desc = desc; 241 } 242 243 public Object run() throws Exception { 244 245 Iterator iter = desc.getConfigProperties().iterator(); 246 while (iter.hasNext()) { 247 EnvironmentProperty envProp = 248 (EnvironmentProperty) iter.next(); 249 String propName = envProp.getName(); 250 String propType = envProp.getType(); 251 String propValue = (String ) getProperty(propName).getValue(); 252 EnvironmentProperty newProp = 254 new EnvironmentProperty(propName, propValue, "", propType); 255 256 String setMeth = 259 "set" + propName.substring(0,1).toUpperCase() + 260 propName.substring(1); 261 Method meth = mcf.getClass().getMethod 262 (setMeth, new Class [] {newProp.getValueType()}); 263 meth.invoke(mcf, new Object [] {newProp.getValueObject()}); 264 } 265 return null; 266 } 267 268 } 269 270 public String toString() { 271 return "< Connector Resource : " + getName() + 272 " , " + getApplicationName() + 273 " , " + getConnectorName() + " , " + getDbuser() + 274 " , " + getDbpassword() + " >"; 275 276 } 277 } 278 | Popular Tags |