Skip to content

Commit

Permalink
Merge bug fixes in branch 6.1
Browse files Browse the repository at this point in the history
  • Loading branch information
zhicwu committed Feb 2, 2017
1 parent f636a26 commit 2853d43
Show file tree
Hide file tree
Showing 3 changed files with 83 additions and 34 deletions.
11 changes: 11 additions & 0 deletions pentaho-platform/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
<properties>
<main.basedir>${project.parent.basedir}</main.basedir>
<quartz.version>1.7.2</quartz.version>
<spring.version>4.3.2.RELEASE</spring.version>
</properties>

<dependencies>
Expand Down Expand Up @@ -58,6 +59,16 @@
<artifactId>quartz</artifactId>
<version>${quartz.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-tx</artifactId>
<version>${spring.version}</version>
</dependency>
</dependencies>
<licenses>
<license>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
package org.pentaho.platform.engine.services.connection.datasource.dbcp;

import com.google.common.annotations.VisibleForTesting;
import com.google.common.base.Strings;
import org.apache.commons.dbcp.*;
import org.apache.commons.lang.StringUtils;
import org.apache.commons.lang.math.NumberUtils;
Expand All @@ -36,6 +37,7 @@
import org.pentaho.platform.api.data.IDBDatasourceService;
import org.pentaho.platform.api.engine.ICacheManager;
import org.pentaho.platform.api.engine.ILogger;
import org.pentaho.platform.api.repository.datasource.IDatasourceMgmtService;
import org.pentaho.platform.engine.core.system.PentahoSessionHolder;
import org.pentaho.platform.engine.core.system.PentahoSystem;
import org.pentaho.platform.engine.services.messages.Messages;
Expand All @@ -47,12 +49,44 @@
import javax.naming.InitialContext;
import javax.naming.NamingException;
import javax.sql.DataSource;
import java.util.Map;
import java.util.Optional;
import java.util.Properties;
import java.util.*;
import java.util.function.Supplier;

public class PooledDatasourceHelper {
public static IDatabaseConnection findUnderlyingDBConnection(String dsName) throws DBDatasourceServiceException {
final IDatasourceMgmtService dmService = PentahoSystem.get(IDatasourceMgmtService.class, null);
final IDBDatasourceService dsService = PentahoSystem.get(IDBDatasourceService.class, null);

final Set<String> names = new HashSet<>(3);
names.add(dsName);

return findUnderlyingDBConnection(dmService, names, dsName);
}

public static IDatabaseConnection findUnderlyingDBConnection(final IDatasourceMgmtService dmService,
final Set<String> names, String dsName) {
IDatabaseConnection dbConn = null;

if (dmService != null) {
try {
dbConn = dmService.getDatasourceByName(dsName);
} catch (Exception e) {
// pretend nothing happened
}

if (dbConn != null) {
// FIXME what if the name contains parameter?
String name = dbConn.getDatabaseName();
if (dbConn.getAccessType() == DatabaseAccessType.JNDI
&& !Strings.isNullOrEmpty(name) && !names.contains(name)) {
names.add(name);
return findUnderlyingDBConnection(dmService, names, name);
}
}
}

return dbConn;
}

public static PoolingDataSource setupPooledDataSource(IDatabaseConnection databaseConnection)
throws DBDatasourceServiceException {
Expand Down Expand Up @@ -439,6 +473,23 @@ public static DataSource getJndiDataSource(final String dsName) throws DBDatasou
} catch (NamingException ignored) {
// ignored
}

// FIXME this introduces unnecessary dependency in Platform, which is not good...
try {
IDatabaseConnection dbConn = findUnderlyingDBConnection(dsName);
if (dbConn != null) {
// hopefully we can get pooled data source from cache...
final ICacheManager cacheManager = PentahoSystem.getCacheManager(null);
Object cached = cacheManager.getFromRegionCache(IDBDatasourceService.JDBC_POOL, dbConn.getName());

rtn = cached instanceof GenericObjectPool
? (DataSource) ((GenericObjectPool) cached).borrowObject() : convert(dbConn);
return rtn;
}
} catch (Exception ignored) {
// ignored
}

if (firstNe != null) {
throw new DBDatasourceServiceException(firstNe);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,10 @@

package org.pentaho.reporting.platform.plugin.connection;

import com.google.common.base.Strings;
import org.pentaho.database.model.DatabaseAccessType;
import org.pentaho.database.model.IDatabaseConnection;
import org.pentaho.platform.api.data.IDBDatasourceService;
import org.pentaho.platform.api.engine.ObjectFactoryException;
import org.pentaho.platform.api.repository.datasource.IDatasourceMgmtService;
import org.pentaho.platform.engine.core.system.PentahoSystem;
import org.pentaho.platform.engine.services.connection.datasource.dbcp.PooledDatasourceHelper;
import org.pentaho.reporting.engine.classic.core.modules.misc.datafactory.sql.ConnectionProvider;

import javax.sql.DataSource;
Expand All @@ -39,6 +36,14 @@ public class PentahoJndiDatasourceConnectionProvider implements ConnectionProvid
private String username;
private String password;

private synchronized void updateProperties(IDatabaseConnection dbConn) {
if (dbConn != null) {
this.jndiName = dbConn.getName();
this.username = dbConn.getUsername();
this.password = dbConn.getPassword();
}
}

/*
* Default constructor
*/
Expand All @@ -55,18 +60,10 @@ public PentahoJndiDatasourceConnectionProvider() {
* @throws java.sql.SQLException
*/
public Connection createConnection(final String user, final String password) throws SQLException {
try {
IDatasourceMgmtService dmService = PentahoSystem.get(IDatasourceMgmtService.class, null);
IDatabaseConnection dbConn = dmService == null ? null : dmService.getDatasourceByName(jndiName);
final IDBDatasourceService datasourceService = PentahoSystem.get(IDBDatasourceService.class, null);

if (dbConn != null && dbConn.getAccessType() == DatabaseAccessType.JNDI
&& !Strings.isNullOrEmpty(dbConn.getDatabaseName())) {
// FIXME this is probably too tricky... let's do it just once for each call to avoid endless-loop
jndiName = dbConn.getDatabaseName();
}

final IDBDatasourceService datasourceService =
PentahoSystem.getObjectFactory().get(IDBDatasourceService.class, null);
try {
updateProperties(PooledDatasourceHelper.findUnderlyingDBConnection(this.jndiName));
final DataSource dataSource = datasourceService.getDataSource(jndiName);
if (dataSource != null) {
final String realUser;
Expand Down Expand Up @@ -125,22 +122,12 @@ public Connection createConnection(final String user, final String password) thr
"PentahoDatasourceConnectionProvider.ERROR_0001_INVALID_CONNECTION", jndiName)); //$NON-NLS-1$
}
} catch (Exception e) {
try {
final IDBDatasourceService datasourceService =
PentahoSystem.getObjectFactory().get(IDBDatasourceService.class, null);
datasourceService.clearDataSource(jndiName);
throw new SQLException(
Messages
.getInstance()
.getErrorString(
"PentahoDatasourceConnectionProvider.ERROR_0002_UNABLE_TO_FACTORY_OBJECT", jndiName, e.getLocalizedMessage())); //$NON-NLS-1$
} catch (ObjectFactoryException objface) {
throw new SQLException(
Messages
.getInstance()
.getErrorString(
"PentahoDatasourceConnectionProvider.ERROR_0002_UNABLE_TO_FACTORY_OBJECT", jndiName, e.getLocalizedMessage())); //$NON-NLS-1$
}
datasourceService.clearDataSource(jndiName);
throw new SQLException(
Messages
.getInstance()
.getErrorString(
"PentahoDatasourceConnectionProvider.ERROR_0002_UNABLE_TO_FACTORY_OBJECT", jndiName, e.getLocalizedMessage())); //$NON-NLS-1$
}
}

Expand Down

0 comments on commit 2853d43

Please sign in to comment.