use of com.evolveum.midpoint.prism.schema.PrismSchema in project midpoint by Evolveum.
the class ResourceManager method applyDefinition.
public void applyDefinition(ObjectDelta<ResourceType> delta, ResourceType resourceWhenNoOid, GetOperationOptions options, Task task, OperationResult objectResult) throws SchemaException, ObjectNotFoundException, CommunicationException, ConfigurationException, ExpressionEvaluationException {
if (delta.isAdd()) {
PrismObject<ResourceType> resource = delta.getObjectToAdd();
applyConnectorSchemaToResource(resource, task, objectResult);
return;
} else if (delta.isModify()) {
// Go on
} else {
return;
}
if (delta.hasCompleteDefinition()) {
//nothing to do, all modifications has definitions..just aplly this deltas..
return;
}
PrismObject<ResourceType> resource;
String resourceOid = delta.getOid();
if (resourceOid == null) {
Validate.notNull(resourceWhenNoOid, "Resource oid not specified in the object delta, and resource is not specified as well. Could not apply definition.");
resource = resourceWhenNoOid.asPrismObject();
} else {
resource = getResource(resourceOid, options, task, objectResult);
}
ResourceType resourceType = resource.asObjectable();
// ResourceType resourceType = completeResource(resource.asObjectable(), null, objectResult);
//TODO TODO TODO FIXME FIXME FIXME copied from ObjectImprted..union this two cases
PrismContainer<ConnectorConfigurationType> configurationContainer = ResourceTypeUtil.getConfigurationContainer(resourceType);
if (configurationContainer == null || configurationContainer.isEmpty()) {
// Nothing to check
objectResult.recordWarning("The resource has no configuration");
return;
}
// Check the resource configuration. The schema is in connector, so fetch the connector first
String connectorOid = resourceType.getConnectorRef().getOid();
if (StringUtils.isBlank(connectorOid)) {
objectResult.recordFatalError("The connector reference (connectorRef) is null or empty");
return;
}
//ItemDelta.findItemDelta(delta.getModifications(), ResourceType.F_SCHEMA, ContainerDelta.class) == null ||
ReferenceDelta connectorRefDelta = ReferenceDelta.findReferenceModification(delta.getModifications(), ResourceType.F_CONNECTOR_REF);
if (connectorRefDelta != null) {
Item<PrismReferenceValue, PrismReferenceDefinition> connectorRefNew = connectorRefDelta.getItemNewMatchingPath(null);
if (connectorRefNew.getValues().size() == 1) {
PrismReferenceValue connectorRefValue = connectorRefNew.getValues().iterator().next();
if (connectorRefValue.getOid() != null && !connectorOid.equals(connectorRefValue.getOid())) {
connectorOid = connectorRefValue.getOid();
}
}
}
PrismObject<ConnectorType> connector = null;
ConnectorType connectorType = null;
try {
connector = repositoryService.getObject(ConnectorType.class, connectorOid, null, objectResult);
connectorType = connector.asObjectable();
} catch (ObjectNotFoundException e) {
// No connector, no fun. We can't check the schema. But this is referential integrity problem.
// Mark the error ... there is nothing more to do
objectResult.recordFatalError("Connector (OID:" + connectorOid + ") referenced from the resource is not in the repository", e);
return;
} catch (SchemaException e) {
// Probably a malformed connector. To be kind of robust, lets allow the import.
// Mark the error ... there is nothing more to do
objectResult.recordPartialError("Connector (OID:" + connectorOid + ") referenced from the resource has schema problems: " + e.getMessage(), e);
LOGGER.error("Connector (OID:{}) referenced from the imported resource \"{}\" has schema problems: {}", new Object[] { connectorOid, resourceType.getName(), e.getMessage(), e });
return;
}
Element connectorSchemaElement = ConnectorTypeUtil.getConnectorXsdSchema(connector);
PrismSchema connectorSchema = null;
if (connectorSchemaElement == null) {
// No schema to validate with
return;
}
try {
connectorSchema = PrismSchemaImpl.parse(connectorSchemaElement, true, "schema for " + connector, prismContext);
} catch (SchemaException e) {
objectResult.recordFatalError("Error parsing connector schema for " + connector + ": " + e.getMessage(), e);
return;
}
QName configContainerQName = new QName(connectorType.getNamespace(), ResourceType.F_CONNECTOR_CONFIGURATION.getLocalPart());
PrismContainerDefinition<ConnectorConfigurationType> configContainerDef = connectorSchema.findContainerDefinitionByElementName(configContainerQName);
if (configContainerDef == null) {
objectResult.recordFatalError("Definition of configuration container " + configContainerQName + " not found in the schema of of " + connector);
return;
}
try {
configurationContainer.applyDefinition(configContainerDef);
} catch (SchemaException e) {
objectResult.recordFatalError("Configuration error in " + resource + ": " + e.getMessage(), e);
return;
}
PrismContainer configContainer = resourceType.asPrismObject().findContainer(ResourceType.F_CONNECTOR_CONFIGURATION);
configContainer.applyDefinition(configContainerDef);
for (ItemDelta<?, ?> itemDelta : delta.getModifications()) {
applyItemDefinition(itemDelta, configContainerDef, objectResult);
}
}
use of com.evolveum.midpoint.prism.schema.PrismSchema in project midpoint by Evolveum.
the class ConnectorManager method getConnectorTypeReadOnly.
public ConnectorType getConnectorTypeReadOnly(ConnectorSpec connectorSpec, OperationResult result) throws ObjectNotFoundException, SchemaException {
if (connectorSpec.getConnectorOid() == null) {
result.recordFatalError("Connector OID missing in " + connectorSpec);
throw new ObjectNotFoundException("Connector OID missing in " + connectorSpec);
}
String connOid = connectorSpec.getConnectorOid();
ConnectorType connectorType = connectorTypeCache.get(connOid);
if (connectorType == null) {
Collection<SelectorOptions<GetOperationOptions>> options = SelectorOptions.createCollection(GetOperationOptions.createReadOnly());
PrismObject<ConnectorType> repoConnector = repositoryService.getObject(ConnectorType.class, connOid, options, result);
connectorType = repoConnector.asObjectable();
connectorTypeCache.put(connOid, connectorType);
} else {
String currentConnectorVersion = repositoryService.getVersion(ConnectorType.class, connOid, result);
if (!currentConnectorVersion.equals(connectorType.getVersion())) {
Collection<SelectorOptions<GetOperationOptions>> options = SelectorOptions.createCollection(GetOperationOptions.createReadOnly());
PrismObject<ConnectorType> repoConnector = repositoryService.getObject(ConnectorType.class, connOid, options, result);
connectorType = repoConnector.asObjectable();
connectorTypeCache.put(connOid, connectorType);
}
}
if (connectorType.getConnectorHost() == null && connectorType.getConnectorHostRef() != null) {
// We need to resolve the connector host
String connectorHostOid = connectorType.getConnectorHostRef().getOid();
PrismObject<ConnectorHostType> connectorHost = repositoryService.getObject(ConnectorHostType.class, connectorHostOid, null, result);
connectorType.setConnectorHost(connectorHost.asObjectable());
}
PrismObject<ConnectorType> connector = connectorType.asPrismObject();
Object userDataEntry = connector.getUserData(USER_DATA_KEY_PARSED_CONNECTOR_SCHEMA);
if (userDataEntry == null) {
InternalMonitor.recordConnectorSchemaParse();
PrismSchema connectorSchema = ConnectorTypeUtil.parseConnectorSchema(connectorType, prismContext);
if (connectorSchema == null) {
throw new SchemaException("No connector schema in " + connectorType);
}
connector.setUserData(USER_DATA_KEY_PARSED_CONNECTOR_SCHEMA, connectorSchema);
}
return connectorType;
}
use of com.evolveum.midpoint.prism.schema.PrismSchema in project midpoint by Evolveum.
the class ConnectorManager method getConnectorSchema.
public PrismSchema getConnectorSchema(ConnectorType connectorType) throws SchemaException {
PrismObject<ConnectorType> connector = connectorType.asPrismObject();
PrismSchema connectorSchema;
Object userDataEntry = connector.getUserData(USER_DATA_KEY_PARSED_CONNECTOR_SCHEMA);
if (userDataEntry == null) {
InternalMonitor.recordConnectorSchemaParse();
connectorSchema = ConnectorTypeUtil.parseConnectorSchema(connectorType, prismContext);
if (connectorSchema == null) {
throw new SchemaException("No connector schema in " + connectorType);
}
connector.setUserData(USER_DATA_KEY_PARSED_CONNECTOR_SCHEMA, connectorSchema);
} else {
if (userDataEntry instanceof PrismSchema) {
connectorSchema = (PrismSchema) userDataEntry;
} else {
throw new IllegalStateException("Expected PrismSchema under user data key " + USER_DATA_KEY_PARSED_CONNECTOR_SCHEMA + "in " + connectorType + ", but got " + userDataEntry.getClass());
}
}
return connectorSchema;
}
use of com.evolveum.midpoint.prism.schema.PrismSchema in project midpoint by Evolveum.
the class SchemaProcessorTest method testRoundTripGeneric.
@Test
public void testRoundTripGeneric() throws SchemaException {
// GIVEN
PrismSchemaImpl schema = new PrismSchemaImpl(SCHEMA_NS, PrismTestUtil.getPrismContext());
// Ordinary property
schema.createPropertyDefinition("number1", DOMUtil.XSD_INT);
// Property container
PrismContainerDefinitionImpl containerDefinition = schema.createPropertyContainerDefinition("ContainerType");
// ... in it ordinary property
containerDefinition.createPropertyDefinition("login", DOMUtil.XSD_STRING);
// ... and local property with a type from another schema
containerDefinition.createPropertyDefinition("password", ProtectedStringType.COMPLEX_TYPE);
// ... property reference
containerDefinition.createPropertyDefinition(SchemaConstants.C_CREDENTIALS, CredentialsType.COMPLEX_TYPE);
// ... read-only int property
PrismPropertyDefinition counterProperty = containerDefinition.createPropertyDefinition("counter", DOMUtil.XSD_INT);
((PrismPropertyDefinitionImpl) counterProperty).setReadOnly();
System.out.println("Generic schema before serializing to XSD: ");
System.out.println(schema.debugDump());
System.out.println();
// WHEN
Document xsd = schema.serializeToXsd();
String stringXmlSchema = DOMUtil.serializeDOMToString(xsd);
System.out.println("Generic schema after serializing to XSD: ");
System.out.println(stringXmlSchema);
System.out.println();
Document parsedXsd = DOMUtil.parseDocument(stringXmlSchema);
PrismSchema newSchema = PrismSchemaImpl.parse(DOMUtil.getFirstChildElement(parsedXsd), true, "serialized schema", PrismTestUtil.getPrismContext());
System.out.println("Generic schema after parsing from XSD: ");
System.out.println(newSchema.debugDump());
System.out.println();
// THEN
PrismPropertyDefinition number1def = newSchema.findItemDefinitionByElementName(new QName(SCHEMA_NS, "number1"), PrismPropertyDefinition.class);
assertEquals(new QName(SCHEMA_NS, "number1"), number1def.getName());
assertEquals(DOMUtil.XSD_INT, number1def.getTypeName());
PrismContainerDefinition newContainerDef = schema.findContainerDefinitionByType(new QName(SCHEMA_NS, "ContainerType"));
assertEquals(new QName(SCHEMA_NS, "ContainerType"), newContainerDef.getTypeName());
PrismPropertyDefinition loginDef = newContainerDef.findPropertyDefinition(new QName(SCHEMA_NS, "login"));
assertEquals(new QName(SCHEMA_NS, "login"), loginDef.getName());
assertEquals(DOMUtil.XSD_STRING, loginDef.getTypeName());
assertTrue("Read flag is wrong", loginDef.canRead());
assertTrue("Create flag is wrong", loginDef.canAdd());
assertTrue("Update flag is wrong", loginDef.canModify());
PrismPropertyDefinition passwdDef = newContainerDef.findPropertyDefinition(new QName(SCHEMA_NS, "password"));
assertEquals(new QName(SCHEMA_NS, "password"), passwdDef.getName());
assertEquals(ProtectedStringType.COMPLEX_TYPE, passwdDef.getTypeName());
PrismPropertyDefinition credDef = newContainerDef.findPropertyDefinition(new QName(SchemaConstants.NS_C, "credentials"));
assertEquals(new QName(SchemaConstants.NS_C, "credentials"), credDef.getName());
assertEquals(new QName(SchemaConstants.NS_C, "CredentialsType"), credDef.getTypeName());
PrismPropertyDefinition countDef = newContainerDef.findPropertyDefinition(new QName(SCHEMA_NS, "counter"));
assertEquals(new QName(SCHEMA_NS, "counter"), countDef.getName());
assertEquals(DOMUtil.XSD_INT, countDef.getTypeName());
assertTrue("Read flag is wrong", countDef.canRead());
assertFalse("Create flag is wrong", countDef.canAdd());
assertFalse("Update flag is wrong", countDef.canModify());
}
Aggregations