Search in sources :

Example 1 with IDBDatasourceService

use of org.pentaho.platform.api.data.IDBDatasourceService in project pentaho-platform by pentaho.

the class SQLConnection method initWithJNDI.

protected void initWithJNDI(final String jndiName) {
    // myCtr = connectionCtr;
    try {
        IDBDatasourceService datasourceService = PentahoSystem.getObjectFactory().get(IDBDatasourceService.class, null);
        DataSource dataSource = datasourceService.getDataSource(jndiName);
        if (dataSource != null) {
            nativeConnection = captureConnection(dataSource.getConnection());
            if (nativeConnection == null) {
                logger.error(Messages.getInstance().getErrorString("ConnectFactory.ERROR_0001_INVALID_CONNECTION", // $NON-NLS-1$
                jndiName));
                // clear datasource cache
                datasourceService.clearDataSource(jndiName);
            } else {
                enhanceConnection(nativeConnection);
            }
        } else {
            logger.error(Messages.getInstance().getErrorString("ConnectFactory.ERROR_0001_INVALID_CONNECTION", // $NON-NLS-1$
            jndiName));
            // clear datasource cache
            datasourceService.clearDataSource(jndiName);
        }
    } catch (Exception e) {
        logger.error(Messages.getInstance().getErrorString("ConnectFactory.ERROR_0001_INVALID_CONNECTION", jndiName), // $NON-NLS-1$
        e);
        // do not allow connection to be used as it might not be enhanced
        close();
        // clear datasource cache
        try {
            IDBDatasourceService datasourceService = PentahoSystem.getObjectFactory().get(IDBDatasourceService.class, null);
            datasourceService.clearDataSource(jndiName);
        } catch (ObjectFactoryException objface) {
            logger.error(Messages.getInstance().getErrorString("ConnectFactory.ERROR_0002_UNABLE_TO_FACTORY_OBJECT=Unable to factory object", jndiName), // $NON-NLS-1$
            e);
        }
    }
}
Also used : ObjectFactoryException(org.pentaho.platform.api.engine.ObjectFactoryException) IDBDatasourceService(org.pentaho.platform.api.data.IDBDatasourceService) ObjectFactoryException(org.pentaho.platform.api.engine.ObjectFactoryException) SQLException(java.sql.SQLException) PentahoSystemException(org.pentaho.platform.api.engine.PentahoSystemException) DataSource(javax.sql.DataSource)

Example 2 with IDBDatasourceService

use of org.pentaho.platform.api.data.IDBDatasourceService in project pentaho-platform by pentaho.

the class MDXBaseComponent method getConnectionOrig.

protected IPentahoConnection getConnectionOrig() {
    IPentahoConnection localConnection = null;
    MdxConnectionAction connAction = (MdxConnectionAction) getActionDefinition();
    try {
        String mdxConnectionStr = connAction.getMdxConnectionString().getStringValue();
        Properties mdxConnectionProps = (Properties) connAction.getConnectionProps().getValue();
        String jdbcStr = connAction.getConnection().getStringValue();
        String jndiStr = connAction.getJndi().getStringValue();
        String location = connAction.getLocation().getStringValue();
        String role = connAction.getRole().getStringValue();
        String catalog = connAction.getCatalog().getStringValue();
        if ((catalog == null) && (connAction.getCatalogResource() != null)) {
            IActionSequenceResource resource = getResource(connAction.getCatalogResource().getName());
            catalog = resource.getAddress();
            if (resource.getSourceType() == IActionSequenceResource.URL_RESOURCE) {
                if (!catalog.startsWith("solution:") && !catalog.startsWith("http:")) {
                    // $NON-NLS-1$
                    if (fileExistsInRepository(catalog)) {
                        // About allowed "solution:"
                        // Extra step to make sure that remote mondrian models
                        // fully qualified aren't munged
                        // MB
                        // $NON-NLS-1$
                        catalog = "solution:" + catalog;
                    }
                }
            } else if ((resource.getSourceType() == IActionSequenceResource.SOLUTION_FILE_RESOURCE) || (resource.getSourceType() == IActionSequenceResource.FILE_RESOURCE)) {
                if (!catalog.startsWith("solution:")) {
                    // $NON-NLS-1$
                    catalog = "solution:" + catalog;
                }
            }
        }
        if (catalog == null) {
            // $NON-NLS-1$
            warn(Messages.getInstance().getString("MDXBaseComponent.ERROR_0007_CATALOG_NOT_DEFINED", getActionName()));
        } else {
            if (mdxConnectionProps != null) {
                mdxConnectionProps.put(MdxConnectionAction.CATALOG_ELEMENT, catalog);
            }
        }
        String userId = connAction.getUserId().getStringValue();
        String password = connAction.getPassword().getStringValue();
        if (mdxConnectionProps != null) {
            localConnection = PentahoConnectionFactory.getConnection(IPentahoConnection.MDX_DATASOURCE, mdxConnectionProps, getSession(), this);
        } else {
            if (mdxConnectionStr != null) {
                localConnection = PentahoConnectionFactory.getConnection(IPentahoConnection.MDX_DATASOURCE, mdxConnectionStr, getSession(), this);
            } else {
                String connectStr = null;
                if (jdbcStr != null) {
                    // $NON-NLS-1$
                    connectStr = jdbcStr + "; Catalog=" + catalog;
                } else if (jndiStr != null) {
                    IDBDatasourceService datasourceService = PentahoSystem.getObjectFactory().get(IDBDatasourceService.class, null);
                    if (datasourceService.getDataSource(jndiStr) == null) {
                        // $NON-NLS-1$
                        error(Messages.getInstance().getErrorString("MDXBaseComponent.ERROR_0005_INVALID_CONNECTION"));
                        return null;
                    }
                    // $NON-NLS-1$ //$NON-NLS-2$
                    connectStr = "dataSource=" + jndiStr + "; Catalog=" + catalog;
                    // Add extra definitions from platform mondrian metadata
                    MondrianCatalog mc = getMondrianCatalog(catalog);
                    try {
                        connectStr += ";" + mc.getDataSourceInfo();
                    } catch (Exception e) {
                    // Just swallow the exception
                    }
                }
                if (role != null) {
                    // $NON-NLS-1$
                    connectStr += "; Role=" + role;
                }
                Properties props = new Properties();
                props.setProperty(IPentahoConnection.CONNECTION, connectStr);
                props.setProperty(IPentahoConnection.PROVIDER, location);
                if (userId != null) {
                    props.setProperty(IPentahoConnection.USERNAME_KEY, userId);
                }
                if (password != null) {
                    props.setProperty(IPentahoConnection.PASSWORD_KEY, password);
                }
                localConnection = PentahoConnectionFactory.getConnection(IPentahoConnection.MDX_DATASOURCE, props, getSession(), this);
            }
            if (localConnection == null) {
                // $NON-NLS-1$
                error(Messages.getInstance().getErrorString("MDXBaseComponent.ERROR_0005_INVALID_CONNECTION"));
                return null;
            }
        }
        if (localConnection instanceof MDXConnection) {
            MDXConnection mdxConn = (MDXConnection) localConnection;
            if (connAction != null) {
                if ((connAction.getExtendedColumnNames() != ActionInputConstant.NULL_INPUT)) {
                    mdxConn.setUseExtendedColumnNames(connAction.getExtendedColumnNames().getBooleanValue());
                }
            }
        }
        return localConnection;
    } catch (Exception e) {
        // $NON-NLS-1$
        error(Messages.getInstance().getErrorString("MDXBaseComponent.ERROR_0006_EXECUTE_FAILED", getActionName()), e);
    }
    return null;
}
Also used : IPentahoConnection(org.pentaho.commons.connection.IPentahoConnection) MDXConnection(org.pentaho.platform.plugin.services.connections.mondrian.MDXConnection) MondrianCatalog(org.pentaho.platform.plugin.action.mondrian.catalog.MondrianCatalog) MdxConnectionAction(org.pentaho.actionsequence.dom.actions.MdxConnectionAction) RolapConnectionProperties(mondrian.rolap.RolapConnectionProperties) Properties(java.util.Properties) IDBDatasourceService(org.pentaho.platform.api.data.IDBDatasourceService) UnifiedRepositoryException(org.pentaho.platform.api.repository2.unified.UnifiedRepositoryException) IActionSequenceResource(org.pentaho.platform.api.engine.IActionSequenceResource)

Example 3 with IDBDatasourceService

use of org.pentaho.platform.api.data.IDBDatasourceService in project pentaho-platform by pentaho.

the class HibernateUtil method initialize.

protected static boolean initialize() {
    IApplicationContext applicationContext = PentahoSystem.getApplicationContext();
    // Add to entry/exit points list
    HibernateUtil hUtil = new HibernateUtil();
    applicationContext.addEntryPointHandler(hUtil);
    applicationContext.addExitPointHandler(hUtil);
    // Look for some hibernate-specific properties...
    String hibernateConfigurationFile = lookupSetting(// $NON-NLS-1$
    applicationContext, // $NON-NLS-1$
    "hibernateConfigPath", // $NON-NLS-1$
    "settings/config-file", // $NON-NLS-1$
    "hibernate/hibernateConfigPath");
    String hibernateManagedString = lookupSetting(// $NON-NLS-1$
    applicationContext, // $NON-NLS-1$
    "hibernateManaged", // $NON-NLS-1$
    "settings/managed", // $NON-NLS-1$
    "hibernate/hibernateManaged");
    if (hibernateManagedString != null) {
        hibernateManaged = Boolean.parseBoolean(hibernateManagedString);
    }
    try {
        HibernateUtil.configuration = new Configuration();
        HibernateUtil.configuration.setEntityResolver(new PentahoEntityResolver());
        // $NON-NLS-1$
        HibernateUtil.configuration.setListener("load", new HibernateLoadEventListener());
        if (hibernateConfigurationFile != null) {
            String configPath = applicationContext.getSolutionPath(hibernateConfigurationFile);
            File cfgFile = new File(configPath);
            if (cfgFile.exists()) {
                HibernateUtil.configuration.configure(cfgFile);
            } else {
                HibernateUtil.log.error(Messages.getInstance().getErrorString("HIBUTIL.ERROR_0012_CONFIG_NOT_FOUND", // $NON-NLS-1$
                configPath));
                return false;
            }
        } else {
            // Assume defaults which means we hope Hibernate finds a configuration
            // file in a file named hibernate.cfg.xml
            HibernateUtil.log.error(Messages.getInstance().getErrorString(// $NON-NLS-1$
            "HIBUTIL.ERROR_0420_CONFIGURATION_ERROR_NO_HIB_CFG_FILE_SETTING"));
            HibernateUtil.configuration.configure();
        }
        // $NON-NLS-1$
        String dsName = HibernateUtil.configuration.getProperty("connection.datasource");
        if ((dsName != null) && dsName.toUpperCase().endsWith("HIBERNATE")) {
            // $NON-NLS-1$
            // IDBDatasourceService datasourceService =  (IDBDatasourceService) PentahoSystem.getObjectFactory().getObject("IDBDatasourceService",null);     //$NON-NLS-1$
            IDBDatasourceService datasourceService = getDatasourceService();
            // $NON-NLS-1$
            String actualDSName = datasourceService.getDSBoundName("Hibernate");
            // $NON-NLS-1$
            HibernateUtil.configuration.setProperty("hibernate.connection.datasource", actualDSName);
        }
        // $NON-NLS-1$
        HibernateUtil.dialect = HibernateUtil.configuration.getProperty("dialect");
        /*
       * configuration.addResource("org/pentaho/platform/repository/runtime/RuntimeElement.hbm.xml"); //$NON-NLS-1$
       * configuration.addResource("org/pentaho/platform/repository/content/ContentLocation.hbm.xml"); //$NON-NLS-1$
       * configuration.addResource("org/pentaho/platform/repository/content/ContentItem.hbm.xml"); //$NON-NLS-1$
       * configuration.addResource("org/pentaho/platform/repository/content/ContentItemFile.hbm.xml"); //$NON-NLS-1$
       */
        if (!HibernateUtil.hibernateManaged) {
            // $NON-NLS-1$
            HibernateUtil.log.info(Messages.getInstance().getString("HIBUTIL.USER_HIBERNATEUNMANAGED"));
            HibernateUtil.sessionFactory = HibernateUtil.configuration.buildSessionFactory();
        } else {
            HibernateUtil.factoryJndiName = HibernateUtil.configuration.getProperty(Environment.SESSION_FACTORY_NAME);
            if (HibernateUtil.factoryJndiName == null) {
                HibernateUtil.log.error(Messages.getInstance().getErrorString("HIBUTIL.ERROR_0013_NO_SESSION_FACTORY"));
                return false;
            }
            // $NON-NLS-1$
            HibernateUtil.log.info(Messages.getInstance().getString("HIBUTIL.USER_HIBERNATEMANAGED"));
            // Let hibernate Bind it
            HibernateUtil.configuration.buildSessionFactory();
            // to JNDI...
            // BISERVER-2006: Below content is a community contribution see the JIRA case for more info
            // -------- Begin Contribution --------
            // Build the initial context to use when looking up the session
            Properties contextProperties = new Properties();
            if (configuration.getProperty("hibernate.jndi.url") != null) {
                // $NON-NLS-1$
                // $NON-NLS-1$
                contextProperties.put(Context.PROVIDER_URL, configuration.getProperty("hibernate.jndi.url"));
            }
            if (configuration.getProperty("hibernate.jndi.class") != null) {
                // $NON-NLS-1$
                // $NON-NLS-1$
                contextProperties.put(Context.INITIAL_CONTEXT_FACTORY, configuration.getProperty("hibernate.jndi.class"));
            }
            iniCtx = new InitialContext(contextProperties);
        // --------- End Contribution ---------
        }
        Dialect.getDialect(HibernateUtil.configuration.getProperties());
        return true;
    } catch (Throwable ex) {
        // $NON-NLS-1$
        HibernateUtil.log.error(Messages.getInstance().getErrorString("HIBUTIL.ERROR_0006_BUILD_SESSION_FACTORY"), ex);
        throw new ExceptionInInitializerError(ex);
    }
}
Also used : Configuration(org.hibernate.cfg.Configuration) IApplicationContext(org.pentaho.platform.api.engine.IApplicationContext) Properties(java.util.Properties) PentahoEntityResolver(org.pentaho.platform.engine.services.solution.PentahoEntityResolver) File(java.io.File) IDBDatasourceService(org.pentaho.platform.api.data.IDBDatasourceService) InitialContext(javax.naming.InitialContext)

Example 4 with IDBDatasourceService

use of org.pentaho.platform.api.data.IDBDatasourceService in project pdi-platform-plugin by pentaho.

the class PlatformKettleDataSourceProvider method getNamedDataSourceFromService.

protected <T extends IDBDatasourceService> DataSource getNamedDataSourceFromService(Class<T> dataSourceServiceInterface, String dataSourceName) throws DataSourceNamingException {
    T datasourceService = PentahoSystem.get(dataSourceServiceInterface, null);
    IDBDatasourceService service = (datasourceService == null) ? PentahoSystem.get(IDBDatasourceService.class, null) : datasourceService;
    if (service != null) {
        try {
            return service.getDataSource(dataSourceName);
        } catch (DBDatasourceServiceException ex) {
            throw new DataSourceNamingException(ex);
        }
    }
    return null;
}
Also used : DBDatasourceServiceException(org.pentaho.platform.api.data.DBDatasourceServiceException) DataSourceNamingException(org.pentaho.di.core.database.DataSourceNamingException) IDBDatasourceService(org.pentaho.platform.api.data.IDBDatasourceService)

Example 5 with IDBDatasourceService

use of org.pentaho.platform.api.data.IDBDatasourceService in project data-access by pentaho.

the class PlatformStagingDatabase method getConnection.

@Override
public Connection getConnection() throws Exception {
    IDBDatasourceService datasourceService = PentahoSystem.get(IDBDatasourceService.class);
    DataSource dataSource = datasourceService.getDataSource(getName());
    return dataSource.getConnection();
}
Also used : IDBDatasourceService(org.pentaho.platform.api.data.IDBDatasourceService) DataSource(javax.sql.DataSource)

Aggregations

IDBDatasourceService (org.pentaho.platform.api.data.IDBDatasourceService)14 DataSource (javax.sql.DataSource)6 Properties (java.util.Properties)5 DBDatasourceServiceException (org.pentaho.platform.api.data.DBDatasourceServiceException)5 ObjectFactoryException (org.pentaho.platform.api.engine.ObjectFactoryException)5 SQLException (java.sql.SQLException)3 IPentahoConnection (org.pentaho.commons.connection.IPentahoConnection)3 IPentahoResultSet (org.pentaho.commons.connection.IPentahoResultSet)2 DataSourceNamingException (org.pentaho.di.core.database.DataSourceNamingException)2 MondrianCatalog (org.pentaho.platform.plugin.action.mondrian.catalog.MondrianCatalog)2 File (java.io.File)1 IOException (java.io.IOException)1 ArrayList (java.util.ArrayList)1 InitialContext (javax.naming.InitialContext)1 RolapConnectionProperties (mondrian.rolap.RolapConnectionProperties)1 Configuration (org.hibernate.cfg.Configuration)1 Test (org.junit.Test)1 ActionInputConstant (org.pentaho.actionsequence.dom.ActionInputConstant)1 MdxConnectionAction (org.pentaho.actionsequence.dom.actions.MdxConnectionAction)1 IActionSequenceResource (org.pentaho.platform.api.engine.IActionSequenceResource)1