use of mondrian.olap.MondrianException in project pentaho-platform by pentaho.
the class PentahoXmlaServlet method createConnectionFactory.
@Override
protected ConnectionFactory createConnectionFactory(final ServletConfig servletConfig) throws ServletException {
final ConnectionFactory delegate = super.createConnectionFactory(servletConfig);
/*
* This wrapper for the connection factory allows us to
* override the list of roles with the ones defined in
* the IPentahoSession and filter it through the
* IConnectionUserRoleMapper.
*/
return new ConnectionFactory() {
public Map<String, Object> getPreConfiguredDiscoverDatasourcesResponse() {
return delegate.getPreConfiguredDiscoverDatasourcesResponse();
}
public OlapConnection getConnection(String databaseName, String catalogName, String roleName, Properties props) throws SQLException {
// What we do here is to filter the role names with the mapper.
// First, get a user role mapper, if one is configured.
final IPentahoSession session = PentahoSessionHolder.getSession();
final IConnectionUserRoleMapper mondrianUserRoleMapper = PentahoSystem.get(IConnectionUserRoleMapper.class, MDXConnection.MDX_CONNECTION_MAPPER_KEY, // Don't use the user session here yet.
null);
String[] effectiveRoles = new String[0];
/*
* If Catalog/Schema are null (this happens with high level metadata requests,
* like DISCOVER_DATASOURCES) we can't use the role mapper, even if it
* is present and configured.
*/
if (mondrianUserRoleMapper != null && catalogName != null) {
// Use the role mapper.
try {
effectiveRoles = mondrianUserRoleMapper.mapConnectionRoles(session, catalogName);
if (effectiveRoles == null) {
effectiveRoles = new String[0];
}
} catch (PentahoAccessControlException e) {
throw new SQLException(e);
}
}
// Now we tokenize that list.
boolean addComma = false;
// $NON-NLS-1$
roleName = "";
for (String role : effectiveRoles) {
if (addComma) {
// $NON-NLS-1$
roleName = roleName.concat(",");
}
roleName = roleName.concat(role);
addComma = true;
}
// Now let the delegate connection factory do its magic.
if (catalogName == null) {
return delegate.getConnection(databaseName, catalogName, roleName.equals("") ? null : roleName, props);
} else {
// We create a connection differently so we can ensure that
// the XMLA servlet shares the same MondrianServer instance as the rest
// of the platform
IMondrianCatalogService mcs = PentahoSystem.get(IMondrianCatalogService.class);
MondrianCatalog mc = mcs.getCatalog(catalogName, PentahoSessionHolder.getSession());
if (mc == null) {
throw new XmlaException(CLIENT_FAULT_FC, HSB_BAD_RESTRICTION_LIST_CODE, HSB_BAD_RESTRICTION_LIST_FAULT_FS, new MondrianException("No such catalog: " + catalogName));
}
Connection con = DriverManager.getConnection(mc.getDataSourceInfo() + ";Catalog=" + mc.getDefinition(), catalogLocator);
try {
final MondrianServer server = MondrianServer.forConnection(con);
FileRepository fr = new FileRepository(makeContentFinder(makeDataSourcesUrl(servletConfig)), catalogLocator);
OlapConnection connection = fr.getConnection(server, databaseName, catalogName, roleName, props);
fr.shutdown();
return connection;
} finally {
con.close();
}
}
}
};
}
use of mondrian.olap.MondrianException in project pentaho-platform by pentaho.
the class MondrianCatalogHelper method loadCatalogsIntoCache.
protected void loadCatalogsIntoCache(final DataSourcesConfig.DataSources dataSources, final IPentahoSession pentahoSession) {
// Create the cache region if necessary.
ICacheManager cacheMgr = PentahoSystem.getCacheManager(pentahoSession);
if (!cacheMgr.cacheEnabled(MONDRIAN_CATALOG_CACHE_REGION)) {
// Create the region
cacheMgr.addCacheRegion(MONDRIAN_CATALOG_CACHE_REGION);
}
Map<String, MondrianCatalog> catalogs = (Map<String, MondrianCatalog>) cacheMgr.getFromRegionCache(MONDRIAN_CATALOG_CACHE_REGION, getLocale().toString());
if (catalogs == null) {
catalogs = new HashMap<String, MondrianCatalog>();
} else {
return;
}
for (DataSourcesConfig.DataSource dataSource : dataSources.dataSources) {
List<String> catalogNames = new ArrayList<String>();
for (DataSourcesConfig.Catalog catalog : dataSource.catalogs.catalogs) {
catalogNames.add(catalog.name);
}
for (DataSourcesConfig.Catalog catalog : dataSource.catalogs.catalogs) {
if (catalog.definition.startsWith("mondrian:") || catalog.definition.startsWith("solution:")) {
// $NON-NLS-1$
// try catch here so the whole thing doesn't blow up if one datasource is configured incorrectly.
MondrianSchema schema = null;
try {
schema = makeSchema(getCatalogAsString(pentahoSession, catalog));
} catch (Exception e) {
MondrianCatalogHelper.logger.error(Messages.getInstance().getErrorString("MondrianCatalogHelper.ERROR_0013_FAILED_TO_LOAD_SCHEMA", catalog.definition), // $NON-NLS-1$
e);
if (e instanceof MondrianException) {
throw (MondrianException) e;
}
}
MondrianCatalog mondrianCatalog = null;
if (schema == null) {
mondrianCatalog = new MondrianCatalog(catalog.name, catalog.dataSourceInfo, catalog.definition, schema);
} else {
mondrianCatalog = new MondrianCatalog(useSchemaNameAsCatalogName ? schema.getName() : catalog.name, catalog.dataSourceInfo, catalog.definition, schema);
}
catalogs.put(mondrianCatalog.getName(), mondrianCatalog);
catalogs.put(mondrianCatalog.getDefinition(), mondrianCatalog);
} else {
MondrianCatalogHelper.logger.warn(Messages.getInstance().getString("MondrianCatalogHelper.WARN_SKIPPING_DATASOURCE_DEF", // $NON-NLS-1$
catalog.definition));
}
}
}
cacheMgr.putInRegionCache(MONDRIAN_CATALOG_CACHE_REGION, getLocale().toString(), catalogs);
}
use of mondrian.olap.MondrianException in project pentaho-platform by pentaho.
the class MondrianModelComponent method getInitialQuery.
public static String getInitialQuery(final Connection connection, final String cubeName) throws Throwable {
String measuresMdx = null;
String columnsMdx = null;
// $NON-NLS-1$
String whereMdx = "";
StringBuffer rowsMdx = new StringBuffer();
// Get catalog info, if exists
String catalog = connection.getCatalogName();
MondrianCatalogComplementInfo catalogComplementInfo = MondrianCatalogHelper.getInstance().getCatalogComplementInfoMap(catalog);
try {
Schema schema = connection.getSchema();
if (schema == null) {
Logger.error("MondrianModelComponent", Messages.getInstance().getErrorString("MondrianModel.ERROR_0002_INVALID_SCHEMA", // $NON-NLS-1$ //$NON-NLS-2$
connection.getConnectString()));
return null;
}
Cube[] cubes = schema.getCubes();
if ((cubes == null) || (cubes.length == 0)) {
Logger.error("MondrianModelComponent", Messages.getInstance().getErrorString("MondrianModel.ERROR_0003_NO_CUBES", // $NON-NLS-1$ //$NON-NLS-2$
connection.getConnectString()));
return null;
}
if ((cubes.length > 1) && (cubeName == null)) {
Logger.error("MondrianModelComponent", Messages.getInstance().getErrorString("MondrianModel.ERROR_0004_CUBE_NOT_SPECIFIED", // $NON-NLS-1$ //$NON-NLS-2$
connection.getConnectString()));
return null;
}
Cube cube = null;
if (cubes.length == 1) {
cube = cubes[0];
} else {
for (Cube element : cubes) {
if (element.getName().equals(cubeName)) {
cube = element;
break;
}
}
}
if (cube == null) {
Logger.error("MondrianModelComponent", Messages.getInstance().getErrorString("MondrianModel.ERROR_0005_CUBE_NOT_FOUND", cubeName, // $NON-NLS-1$ //$NON-NLS-2$
connection.getConnectString()));
return null;
}
// If we have any whereConditions block, we need to find which hierarchies they are in
// and not include them in the rows
HashSet<Hierarchy> whereHierarchies = new HashSet<Hierarchy>();
if (catalogComplementInfo != null && catalogComplementInfo.getWhereCondition(cube.getName()) != null && !catalogComplementInfo.getWhereCondition(cube.getName()).equals("")) {
// $NON-NLS-1$
final String rawString = catalogComplementInfo.getWhereCondition(cube.getName());
try {
// According to Julian, the better way to resolve the names is to build a query
final String queryStr = // $NON-NLS-1$ //$NON-NLS-2$
"select " + rawString + " on columns, {} on rows from " + cube.getName();
final Query query = connection.parseQuery(queryStr);
final Hierarchy[] hierarchies = query.getMdxHierarchiesOnAxis(AxisOrdinal.StandardAxisOrdinal.COLUMNS);
boolean isWhereValid = true;
for (int i = 0; i < hierarchies.length && isWhereValid; i++) {
final Hierarchy hierarchy = hierarchies[i];
if (connection.getRole().canAccess(hierarchy)) {
whereHierarchies.add(hierarchy);
} else {
isWhereValid = false;
whereHierarchies.clear();
}
}
if (isWhereValid) {
// $NON-NLS-1$
whereMdx = " WHERE " + rawString;
}
} catch (Exception e) {
// We found an error in the where slicer, so we'll just act like it wasn't here
whereHierarchies.clear();
}
}
Dimension[] dimensions = cube.getDimensions();
if ((dimensions == null) || (dimensions.length == 0)) {
Logger.error("MondrianModelComponent", Messages.getInstance().getErrorString("MondrianModel.ERROR_0006_NO_DIMENSIONS", cubeName, // $NON-NLS-1$ //$NON-NLS-2$
connection.getConnectString()));
return null;
}
for (Dimension element : dimensions) {
final Hierarchy hierarchy = element.getHierarchy();
if (hierarchy == null) {
Logger.error("MondrianModelComponent", Messages.getInstance().getErrorString("MondrianModel.ERROR_0007_NO_HIERARCHIES", element.getName(), cubeName, // $NON-NLS-1$ //$NON-NLS-2$
connection.getConnectString()));
return null;
}
if (!connection.getRole().canAccess(hierarchy)) {
// We can't access this element
continue;
}
if (whereHierarchies.contains(hierarchy)) {
// We have it on the where condition - skip it
continue;
}
Member member = Locus.execute((RolapConnection) connection, "Retrieving default members in plugin", new Locus.Action<Member>() {
public Member execute() {
return connection.getSchemaReader().getHierarchyDefaultMember(hierarchy);
}
});
if (member == null) {
Logger.error("MondrianModelComponent", Messages.getInstance().getErrorString("MondrianModel.ERROR_0008_NO_DEFAULT_MEMBER", element.getName(), cubeName, // $NON-NLS-1$ //$NON-NLS-2$
connection.getConnectString()));
return null;
}
if (element.isMeasures()) {
// measuresMdx = "with member "+ member.getUniqueName();
// //$NON-NLS-1$
// $NON-NLS-1$
measuresMdx = "";
// $NON-NLS-1$ //$NON-NLS-2$
columnsMdx = " select NON EMPTY {" + member.getUniqueName() + "} ON columns, ";
} else {
if (rowsMdx.length() > 0) {
// $NON-NLS-1$
rowsMdx.append(", ");
}
rowsMdx.append(member.getUniqueName());
}
}
if ((measuresMdx != null) && (columnsMdx != null) && (rowsMdx.length() > 0)) {
StringBuffer result = new StringBuffer(measuresMdx.length() + columnsMdx.length() + rowsMdx.length() + 50);
// $NON-NLS-1$
result.append(measuresMdx).append(columnsMdx).append("NON EMPTY {(").append(rowsMdx).append(// $NON-NLS-1$
")} ON rows ").append(// $NON-NLS-1$ //$NON-NLS-2$
"from [" + cube.getName() + "]").append(whereMdx);
return result.toString();
}
return null;
} catch (Throwable t) {
if (t instanceof MondrianException) {
// pull the cause out, otherwise it never gets logged
Throwable cause = ((MondrianException) t).getCause();
if (cause != null) {
throw cause;
} else {
throw t;
}
} else {
throw t;
}
}
}
Aggregations