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);
}
}
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);
}
}
}
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);
}
}
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);
}
}
Aggregations