blob: 3e974fc9b106481908d32857431d2be4677e2dd5 [file] [log] [blame]
/*
* Copyright (c) 2012, 2018 Oracle and/or its affiliates. All rights reserved.
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License v. 2.0, which is available at
* http://www.eclipse.org/legal/epl-2.0.
*
* This Source Code may also be made available under the following Secondary
* Licenses when the conditions for such availability set forth in the
* Eclipse Public License v. 2.0 are satisfied: GNU General Public License,
* version 2 with the GNU Classpath Exception, which is available at
* https://www.gnu.org/software/classpath/license.html.
*
* SPDX-License-Identifier: EPL-2.0 OR GPL-2.0 WITH Classpath-exception-2.0
*/
package org.glassfish.jdbcruntime.service;
import static com.sun.appserv.connectors.internal.api.ConnectorsUtil.getValidSuffix;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.SQLException;
import java.sql.SQLFeatureNotSupportedException;
import java.util.logging.Logger;
import javax.sql.DataSource;
import org.glassfish.jdbc.config.JdbcResource;
import org.glassfish.resourcebase.resources.api.ResourceInfo;
import com.sun.appserv.connectors.internal.api.ConnectorRuntimeException;
import com.sun.enterprise.connectors.ConnectorRuntime;
import com.sun.enterprise.connectors.util.ResourcesUtil;
public class JdbcDataSource implements DataSource {
private ResourceInfo resourceInfo;
private PrintWriter logWriter;
private int loginTimeout;
public void setResourceInfo(ResourceInfo resourceInfo) throws ConnectorRuntimeException {
validateResource(resourceInfo);
this.resourceInfo = resourceInfo;
}
private void validateResource(ResourceInfo resourceInfo) throws ConnectorRuntimeException {
ResourcesUtil resourcesUtil = ResourcesUtil.createInstance();
String jndiName = resourceInfo.getName();
String suffix = getValidSuffix(jndiName);
if (suffix != null) {
// Typically, resource is created without suffix. Try without suffix.
String tmpJndiName = jndiName.substring(0, jndiName.lastIndexOf(suffix));
if (resourcesUtil.getResource(tmpJndiName, resourceInfo.getApplicationName(), resourceInfo.getModuleName(), JdbcResource.class) != null) {
return;
}
}
if (resourcesUtil.getResource(resourceInfo, JdbcResource.class) == null) {
throw new ConnectorRuntimeException("Invalid resource : " + resourceInfo);
}
}
@Override
public Connection getConnection() throws SQLException {
return ConnectorRuntime.getRuntime().getConnection(resourceInfo);
}
@Override
public Connection getConnection(String username, String password) throws SQLException {
return ConnectorRuntime.getRuntime().getConnection(resourceInfo, username, password);
}
@Override
public PrintWriter getLogWriter() throws SQLException {
return logWriter;
}
@Override
public void setLogWriter(PrintWriter out) throws SQLException {
this.logWriter = out;
}
@Override
public void setLoginTimeout(int seconds) throws SQLException {
loginTimeout = seconds;
}
@Override
public int getLoginTimeout() throws SQLException {
return loginTimeout;
}
@Override
public boolean isWrapperFor(Class<?> iface) throws SQLException {
throw new SQLException("Not supported operation");
}
@Override
public <T> T unwrap(Class<T> iface) throws SQLException {
throw new SQLException("Not supported operation");
}
@Override
public Logger getParentLogger() throws SQLFeatureNotSupportedException {
throw new SQLFeatureNotSupportedException("Not supported operation");
}
}