package org.saiku.plugin;

import javax.sql.DataSource;

import org.apache.log4j.Logger;
import org.pentaho.platform.api.data.DatasourceServiceException;
import org.pentaho.platform.api.data.IDatasourceService;
import org.pentaho.platform.api.engine.ObjectFactoryException;
import org.pentaho.platform.engine.core.system.PentahoSessionHolder;
import org.pentaho.platform.engine.core.system.PentahoSystem;
import org.pentaho.platform.web.servlet.messages.Messages;

import mondrian.spi.DataSourceResolver;

/**
 * This class provides SPI functionality to Mondrian.
 * It resolves relational data sources by their name.
 * It uses the {@link PentahoSessionHolder}.
 * @author Luc Boudreau
 */
public class PentahoDataSourceResolver implements DataSourceResolver {
  Logger logger = Logger.getLogger(PentahoDataSourceResolver.class);
  public DataSource lookup(String dataSourceName) throws Exception {
    try {
      IDatasourceService datasourceSvc =
        PentahoSystem.getObjectFactory().get(
          IDatasourceService.class,
          PentahoSessionHolder.getSession());
      javax.sql.DataSource datasource =
        datasourceSvc.getDataSource(
          datasourceSvc.getDSUnboundName(dataSourceName));
      return datasource;
    } catch (ObjectFactoryException e) {
      throw e;
    } catch (DatasourceServiceException e) {
      throw e;
    }
  }
}

@@ -35,30 +35,140 @@
 t javax.
-sql.DataSource
+naming.Context;%0D%0Aimport javax.naming.InitialContext;%0D%0Aimport javax.sql.DataSource;%0D%0A%0D%0Aimport mondrian.spi.DataSourceResolver
 ;%0D%0A%0D%0Aimp
@@ -325,72 +325,8 @@
 e;%0D%0A
-import org.pentaho.platform.api.engine.ObjectFactoryException;%0D%0A
 impo
@@ -457,111 +457,8 @@
 em;%0D
-%0Aimport org.pentaho.platform.web.servlet.messages.Messages;%0D%0A%0D%0Aimport mondrian.spi.DataSourceResolver;%0D
 %0A%0D%0A/
@@ -719,18 +719,17 @@
 lver %7B%0D%0A
-  
+%09
 Logger l
@@ -788,18 +788,17 @@
 lass);%0D%0A
-  
+%09
 public D
@@ -861,25 +861,10 @@
  %7B%0D%0A
-    try %7B%0D%0A      
+%09%09
 IDat
@@ -895,24 +895,19 @@
 eSvc =%0D%0A
-        
+%09%09%09
 PentahoS
@@ -937,26 +937,21 @@
 ).get(%0D%0A
-          
+%09%09%09%09%09
 IDatasou
@@ -969,26 +969,21 @@
 class,%0D%0A
-          
+%09%09%09%09%09
 PentahoS
@@ -1011,22 +1011,18 @@
 on());%0D%0A
-      
+%09%09
 javax.sq
@@ -1048,20 +1048,41 @@
 urce
- =%0D%0A        
+;%0D%0A%09%09try %7B%0D%0A%09%09%09datasource =%0D%0A%09%09%09%09
 data
@@ -1111,18 +1111,14 @@
 e(%0D%0A
-          
+%09%09%09%09%09%09
 data
@@ -1167,163 +1167,346 @@
 );%0D%0A
-      return 
+%09%09%7D%0D%0A%09%09catch (DatasourceServiceException e) %7B%0D%0A%09%09%09Context initContext = new InitialContext();%0D%0A%09%09%09if (
 data
-s
+S
 ource
-;%0D%0A    %7D catch (ObjectFactoryException e) %7B%0D%0A      throw e;%0D%0A    %7D catch
+Name != null && !dataSourceName.startsWith(%22java:/comp/env/%22)) %7B%0D%0A%09%09%09%09dataSourceName = %22java:/comp/env/%22 + dataSourceName;%0D%0A%09%09%09%7D%0D%0A%09%09%09datasource  =
  (Data
-s
+S
 ource
-ServiceException e) %7B%0D%0A      throw e;%0D%0A    %7D%0D%0A  
+)initContext.lookup(dataSourceName);%0D%0A%09%09%7D%0D%0A%09%09return datasource;%0D%0A%0D%0A%09
 %7D%0D%0A%7D

improve DS resolving to pentaho + standard behaviour