protected void init(Util.PropertyList properties) {
    try {
      if (nativeConnection != null) { // Assume we're open
        close();
      }

      // Set a locale for this connection if specified in the platform's mondrian metadata
      // This is required if mondrian.i18n.LocalizingDynamicSchemaProcessor is being used
      if (properties.get(RolapConnectionProperties.Locale.name()) == null) {
        properties.put(
            RolapConnectionProperties.Locale.name(), LocaleHelper.getLocale().toString());
      }

      String dataSourceName = properties.get(RolapConnectionProperties.DataSource.name());

      mapPlatformRolesToMondrianRoles(properties);

      if (dataSourceName != null) {
        IDBDatasourceService datasourceService =
            PentahoSystem.getObjectFactory().get(IDBDatasourceService.class, null);
        DataSource dataSourceImpl = datasourceService.getDataSource(dataSourceName);
        if (dataSourceImpl != null) {
          properties.remove(RolapConnectionProperties.DataSource.name());
          nativeConnection = DriverManager.getConnection(properties, null, dataSourceImpl);
        } else {
          nativeConnection = DriverManager.getConnection(properties, null);
        }
      } else {
        nativeConnection = DriverManager.getConnection(properties, null);
      }

      if (nativeConnection != null) {
        if (role != null) {
          nativeConnection.setRole(role);
        }
      }

      if (nativeConnection == null) {
        logger.error(
            Messages.getInstance()
                .getErrorString(
                    "MDXConnection.ERROR_0002_INVALID_CONNECTION",
                    properties != null
                        ? properties.toString()
                        : "null")); //$NON-NLS-1$ //$NON-NLS-2$
      }
    } catch (Throwable t) {
      if (logger != null) {
        logger.error(
            Messages.getInstance()
                .getErrorString(
                    "MDXConnection.ERROR_0002_INVALID_CONNECTION",
                    properties != null ? properties.toString() : "null"),
            t); //$NON-NLS-1$ //$NON-NLS-2$
      } else {
        Logger.error(
            this.getClass().getName(),
            Messages.getInstance()
                .getErrorString(
                    "MDXConnection.ERROR_0002_INVALID_CONNECTION",
                    properties != null ? properties.toString() : "null"),
            t); //$NON-NLS-1$ //$NON-NLS-2$
      }
    }
  }
  public OlapConnection getConnection(String catalogName, IPentahoSession session)
      throws IOlapServiceException {

    if (catalogName == null) {
      // This is normal. It happens on XMLA's DISCOVER_DATASOURCES
      try {
        return getServer().getConnection(DATASOURCE_NAME, null, null, new Properties());
      } catch (Exception e) {
        throw new IOlapServiceException(e);
      }
    }

    // Check Access
    if (!hasAccess(catalogName, EnumSet.of(RepositoryFilePermission.READ), session)) {
      LOG.debug("user does not have access; throwing exception"); // $NON-NLS-1$
      throw new IOlapServiceException(
          Messages.getInstance()
              .getErrorString("OlapServiceImpl.ERROR_0003_INSUFFICIENT_PERMISSION"), // $NON-NLS-1$
          IOlapServiceException.Reason.ACCESS_DENIED);
    }

    // Check its existence.
    if (!getCatalogNames(session).contains(catalogName)) {
      throw new IOlapServiceException(
          Messages.getInstance()
              .getErrorString("MondrianCatalogHelper.ERROR_0015_CATALOG_NOT_FOUND", catalogName));
    }

    // Check if it is a remote server
    if (getHelper().getOlap4jServers().contains(catalogName)) {
      return makeOlap4jConnection(catalogName);
    }

    final StringBuilder roleName = new StringBuilder();
    Entry roleMonikor = null;
    if (this.role != null) {
      // We must use a custom role implementation.
      // Register the instance with the mondrian server.
      roleMonikor = getServer().getLockBox().register(this.role);
      roleName.append(roleMonikor.getMoniker());
    } else {
      final IConnectionUserRoleMapper mapper =
          PentahoSystem.get(
              IConnectionUserRoleMapper.class,
              MDXConnection.MDX_CONNECTION_MAPPER_KEY,
              null); // Don't use the user session here yet.

      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 (session != null && mapper != null) {
        // Use the role mapper.
        try {
          effectiveRoles = mapper.mapConnectionRoles(session, catalogName);
          if (effectiveRoles == null) {
            effectiveRoles = new String[0];
          }
        } catch (PentahoAccessControlException e) {
          throw new IOlapServiceException(e);
        }
      }

      // Now we tokenize that list.
      boolean addComma = false;
      for (String role : effectiveRoles) {
        if (addComma) {
          roleName.append(","); // $NON-NLS-1$
        }
        roleName.append(role);
        addComma = true;
      }
    }

    // Populate some properties, like locale.
    final Properties properties = new Properties();
    properties.put(RolapConnectionProperties.Locale.name(), getLocale().toString());

    // Return a connection
    try {
      return getServer()
          .getConnection(
              DATASOURCE_NAME,
              catalogName,
              Util.isEmpty(roleName.toString()) ? null : roleName.toString(),
              properties);
    } catch (Exception e) {
      throw new IOlapServiceException(e);
    } finally {
      // Cleanup our lockbox entry.
      if (roleMonikor != null) {
        getServer().getLockBox().deregister(roleMonikor);
      }
    }
  }