use of com.sun.gjc.common.DataSourceObjectBuilder in project Payara by payara.
the class DMManagedConnectionFactory method createManagedConnection.
/**
* Creates a new physical connection to the underlying EIS resource
* manager.
*
* @param subject <code>Subject</code> instance passed by the application server
* @param cxRequestInfo <code>ConnectionRequestInfo</code> which may be created
* as a result of the invocation <code>getConnection(user, password)</code>
* on the <code>DataSource</code> object
* @return <code>ManagedConnection</code> object created
* @throws ResourceException if there is an error in instantiating the
* <code>DataSource</code> object used for the
* creation of the <code>ManagedConnection</code> object
* @throws SecurityException if there ino <code>PasswordCredential</code> object
* satisfying this request
*/
public javax.resource.spi.ManagedConnection createManagedConnection(javax.security.auth.Subject subject, ConnectionRequestInfo cxRequestInfo) throws ResourceException {
logFine("In createManagedConnection");
if (dsObjBuilder == null) {
dsObjBuilder = new DataSourceObjectBuilder(spec);
}
PasswordCredential pc = SecurityUtils.getPasswordCredential(this, subject, cxRequestInfo);
try {
Class.forName(spec.getDetail(DataSourceSpec.CLASSNAME));
} catch (ClassNotFoundException cnfe) {
_logger.log(Level.SEVERE, "jdbc.exc_cnfe", cnfe);
throw new ResourceException("The driver could not be loaded: " + spec.getDetail(DataSourceSpec.CLASSNAME));
}
java.sql.Connection dsConn = null;
ManagedConnectionImpl mc = null;
Properties driverProps = new Properties();
// Will return a set of properties that would have setURL and <url> as objects
// Get a set of normal case properties
Hashtable properties = dsObjBuilder.parseDriverProperties(spec, false);
Set<Map.Entry<String, Vector>> entries = (Set<Map.Entry<String, Vector>>) properties.entrySet();
for (Map.Entry<String, Vector> entry : entries) {
String value = "";
String key = (String) entry.getKey();
Vector values = (Vector) entry.getValue();
if (!values.isEmpty() && values.size() == 1) {
value = (String) values.firstElement();
} else if (values.size() > 1) {
logFine("More than one value for key : " + key);
}
String prop = getParsedKey(key);
driverProps.put(prop, value);
if (prop.equalsIgnoreCase("URL")) {
if (spec.getDetail(DataSourceSpec.URL) == null) {
setConnectionURL(value);
}
}
}
try {
if (cxRequestInfo != null) {
driverProps.setProperty("user", pc.getUserName());
driverProps.setProperty("password", new String(pc.getPassword()));
} else {
String user = spec.getDetail(DataSourceSpec.USERNAME);
String password = spec.getDetail(DataSourceSpec.PASSWORD);
if (user != null) {
driverProps.setProperty("user", user);
}
if (password != null) {
driverProps.setProperty("password", password);
}
}
dsConn = DriverManager.getConnection(spec.getDetail(DataSourceSpec.URL), driverProps);
} catch (java.sql.SQLException sqle) {
_logger.log(Level.SEVERE, "jdbc.exc_create_mc", sqle);
throw new javax.resource.spi.ResourceAllocationException("The connection could not be allocated: " + sqle.getMessage());
}
try {
mc = constructManagedConnection(null, dsConn, pc, this);
// GJCINT
validateAndSetIsolation(mc);
} finally {
if (mc == null) {
if (dsConn != null) {
try {
dsConn.close();
} catch (SQLException e) {
_logger.log(Level.FINEST, "Exception while closing connection : createManagedConnection" + dsConn);
}
}
}
}
return mc;
}
Aggregations