Search in sources :

Example 1 with MetadataException

use of com.thinkbiganalytics.metadata.api.MetadataException in project kylo by Teradata.

the class JcrDatasourceProvider method ensureDatasourceDetails.

@Override
public <D extends DatasourceDetails> Optional<D> ensureDatasourceDetails(@Nonnull final Datasource.ID id, @Nonnull final Class<D> type) {
    try {
        // Ensure the data source exists
        final Optional<JcrUserDatasource> parent = Optional.ofNullable(getDatasource(id)).filter(JcrUserDatasource.class::isInstance).map(JcrUserDatasource.class::cast);
        if (!parent.isPresent()) {
            return Optional.empty();
        }
        // Create the details
        final Class<? extends JcrDatasourceDetails> implType = JcrUserDatasource.resolveDetailsClass(type);
        final boolean isNew = !hasEntityNode(parent.get().getPath(), JcrUserDatasource.DETAILS);
        final Node node = findOrCreateEntityNode(parent.get().getPath(), JcrUserDatasource.DETAILS, implType);
        @SuppressWarnings("unchecked") final D details = (D) JcrUtil.createJcrObject(node, implType);
        // Re-assign permissions to data source
        if (isNew) {
            final UsernamePrincipal owner = parent.map(JcrUserDatasource::getOwner).map(Principal::getName).map(UsernamePrincipal::new).orElse(JcrMetadataAccess.getActiveUser());
            if (accessController.isEntityAccessControlled()) {
                final List<SecurityRole> roles = roleProvider.getEntityRoles(SecurityRole.DATASOURCE);
                actionsProvider.getAvailableActions(AllowedActions.DATASOURCE).ifPresent(actions -> parent.get().enableAccessControl((JcrAllowedActions) actions, owner, roles));
            } else {
                actionsProvider.getAvailableActions(AllowedActions.DATASOURCE).ifPresent(actions -> parent.get().disableAccessControl((JcrAllowedActions) actions, owner));
            }
        }
        return Optional.of(details);
    } catch (final IllegalArgumentException e) {
        throw new MetadataException("Unable to create datasource details: " + type, e);
    }
}
Also used : SecurityRole(com.thinkbiganalytics.security.role.SecurityRole) Node(javax.jcr.Node) MetadataException(com.thinkbiganalytics.metadata.api.MetadataException) UsernamePrincipal(com.thinkbiganalytics.security.UsernamePrincipal) JcrAllowedActions(com.thinkbiganalytics.metadata.modeshape.security.action.JcrAllowedActions) UsernamePrincipal(com.thinkbiganalytics.security.UsernamePrincipal) Principal(java.security.Principal)

Example 2 with MetadataException

use of com.thinkbiganalytics.metadata.api.MetadataException in project kylo by Teradata.

the class InMemoryDatasourceProvider method ensureDatasource.

@Override
public <D extends Datasource> D ensureDatasource(String name, String descr, Class<D> type) {
    synchronized (this.datasets) {
        try {
            D ds = null;
            BaseDatasource existing = getExistingDataset(name);
            if (existing == null) {
                ds = (D) ConstructorUtils.invokeConstructor(type, name, descr);
                this.datasets.put(ds.getId(), ds);
            } else if (type.isInstance(ds)) {
                ds = (D) existing;
            } else {
                throw new MetadataException("A datasource already exists of type: " + (ds.getClass() == null ? "UNKNOWN" : ds.getClass()) + " but expected one of type: " + type);
            }
            return ds;
        } catch (NoSuchMethodException | IllegalAccessException | InvocationTargetException | InstantiationException e) {
            throw new MetadataException("Failed to create a datasource to type: " + type, e);
        }
    }
}
Also used : ID(com.thinkbiganalytics.metadata.api.datasource.Datasource.ID) MetadataException(com.thinkbiganalytics.metadata.api.MetadataException) InvocationTargetException(java.lang.reflect.InvocationTargetException)

Example 3 with MetadataException

use of com.thinkbiganalytics.metadata.api.MetadataException in project kylo by Teradata.

the class JcrDatasourceProvider method getNodeType.

@Override
public String getNodeType(Class<? extends JcrEntity> jcrEntityType) {
    try {
        Field folderField = FieldUtils.getField(jcrEntityType, "NODE_TYPE", true);
        String jcrType = (String) folderField.get(null);
        return jcrType;
    } catch (IllegalArgumentException | IllegalAccessException e) {
        // Shouldn't really happen.
        throw new MetadataException("Unable to determine JCR node the for entity class: " + jcrEntityType, e);
    }
}
Also used : Field(java.lang.reflect.Field) MetadataException(com.thinkbiganalytics.metadata.api.MetadataException)

Example 4 with MetadataException

use of com.thinkbiganalytics.metadata.api.MetadataException in project kylo by Teradata.

the class JcrDatasourceProvider method createImpl.

private <J extends JcrDatasource> J createImpl(String name, String descr, Class<? extends Datasource> type) {
    try {
        JcrTool tool = new JcrTool();
        Class<J> implType = deriveImplType(type);
        Field folderField = FieldUtils.getField(implType, "PATH_NAME", true);
        String subfolderName = (String) folderField.get(null);
        String dsPath = EntityUtil.pathForDataSource();
        Node dsNode = getSession().getNode(dsPath);
        Node subfolderNode = tool.findOrCreateChild(dsNode, subfolderName, "nt:folder");
        Map<String, Object> props = new HashMap<>();
        props.put(JcrDatasource.SYSTEM_NAME, name);
        String encodedName = org.modeshape.jcr.value.Path.DEFAULT_ENCODER.encode(name);
        final boolean isNew = !hasEntityNode(subfolderNode.getPath(), encodedName);
        @SuppressWarnings("unchecked") J datasource = (J) findOrCreateEntity(subfolderNode.getPath(), encodedName, implType, props);
        if (isNew && JcrUserDatasource.class.isAssignableFrom(type)) {
            if (this.accessController.isEntityAccessControlled()) {
                final List<SecurityRole> roles = roleProvider.getEntityRoles(SecurityRole.DATASOURCE);
                actionsProvider.getAvailableActions(AllowedActions.DATASOURCE).ifPresent(actions -> ((JcrUserDatasource) datasource).enableAccessControl((JcrAllowedActions) actions, JcrMetadataAccess.getActiveUser(), roles));
            } else {
                actionsProvider.getAvailableActions(AllowedActions.DATASOURCE).ifPresent(actions -> ((JcrUserDatasource) datasource).disableAccessControl((JcrAllowedActions) actions, JcrMetadataAccess.getActiveUser()));
            }
        }
        datasource.setTitle(name);
        datasource.setDescription(descr);
        return datasource;
    } catch (IllegalArgumentException | IllegalAccessException | RepositoryException e) {
        throw new MetadataException("Unable to create datasource: " + type, e);
    }
}
Also used : SecurityRole(com.thinkbiganalytics.security.role.SecurityRole) HashMap(java.util.HashMap) Node(javax.jcr.Node) MetadataRepositoryException(com.thinkbiganalytics.metadata.modeshape.MetadataRepositoryException) RepositoryException(javax.jcr.RepositoryException) MetadataException(com.thinkbiganalytics.metadata.api.MetadataException) Field(java.lang.reflect.Field) JcrAllowedActions(com.thinkbiganalytics.metadata.modeshape.security.action.JcrAllowedActions) JcrTool(com.thinkbiganalytics.metadata.modeshape.support.JcrTool)

Aggregations

MetadataException (com.thinkbiganalytics.metadata.api.MetadataException)4 JcrAllowedActions (com.thinkbiganalytics.metadata.modeshape.security.action.JcrAllowedActions)2 SecurityRole (com.thinkbiganalytics.security.role.SecurityRole)2 Field (java.lang.reflect.Field)2 Node (javax.jcr.Node)2 ID (com.thinkbiganalytics.metadata.api.datasource.Datasource.ID)1 MetadataRepositoryException (com.thinkbiganalytics.metadata.modeshape.MetadataRepositoryException)1 JcrTool (com.thinkbiganalytics.metadata.modeshape.support.JcrTool)1 UsernamePrincipal (com.thinkbiganalytics.security.UsernamePrincipal)1 InvocationTargetException (java.lang.reflect.InvocationTargetException)1 Principal (java.security.Principal)1 HashMap (java.util.HashMap)1 RepositoryException (javax.jcr.RepositoryException)1