Search in sources :

Example 11 with PrismSchema

use of com.evolveum.midpoint.prism.schema.PrismSchema in project midpoint by Evolveum.

the class ObjectImporter method validateWithDynamicSchemas.

protected <T extends ObjectType> void validateWithDynamicSchemas(PrismObject<T> object, Element objectElement, RepositoryService repository, OperationResult objectResult) {
    // TODO: check extension schema (later)
    OperationResult result = objectResult.createSubresult(OPERATION_VALIDATE_DYN_SCHEMA);
    if (object.canRepresent(ConnectorType.class)) {
        ConnectorType connector = (ConnectorType) object.asObjectable();
        checkSchema(connector.getSchema(), "connector", result);
        result.computeStatus("Connector schema error");
        result.recordSuccessIfUnknown();
    } else if (object.canRepresent(ResourceType.class)) {
        // Only two object types have XML snippets that conform to the dynamic schema
        PrismObject<ResourceType> resource = (PrismObject<ResourceType>) object;
        ResourceType resourceType = resource.asObjectable();
        PrismContainer<ConnectorConfigurationType> configurationContainer = ResourceTypeUtil.getConfigurationContainer(resource);
        if (configurationContainer == null || configurationContainer.isEmpty()) {
            // Nothing to check
            result.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)) {
            result.recordFatalError("The connector reference (connectorRef) is null or empty");
            return;
        }
        PrismObject<ConnectorType> connector;
        ConnectorType connectorType;
        try {
            connector = repository.getObject(ConnectorType.class, connectorOid, null, result);
            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
            result.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
            result.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;
        if (connectorSchemaElement == null) {
            // No schema to validate with
            result.recordSuccessIfUnknown();
            return;
        }
        try {
            connectorSchema = PrismSchemaImpl.parse(connectorSchemaElement, true, "schema for " + connector, prismContext);
        } catch (SchemaException e) {
            result.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) {
            result.recordFatalError("Definition of configuration container " + configContainerQName + " not found in the schema of of " + connector);
            return;
        }
        try {
            configurationContainer.applyDefinition(configContainerDef);
        } catch (SchemaException e) {
            result.recordFatalError("Configuration error in " + resource + ": " + e.getMessage(), e);
            return;
        }
        // now we check for raw data - their presence means e.g. that there is a connector property that is unknown in connector schema (applyDefinition does not scream in such a case!)
        try {
            // require definitions and prohibit raw
            configurationContainer.checkConsistence(true, true, ConsistencyCheckScope.THOROUGH);
        } catch (IllegalStateException e) {
            // TODO do this error checking and reporting in a cleaner and more user-friendly way
            result.recordFatalError("Configuration error in " + resource + " (probably incorrect connector property, see the following error): " + e.getMessage(), e);
            return;
        }
        // Also check integrity of the resource schema
        checkSchema(resourceType.getSchema(), "resource", result);
        result.computeStatus("Dynamic schema error");
    } else if (object.canRepresent(ShadowType.class)) {
    // TODO
    //objectResult.computeStatus("Dynamic schema error");
    }
    result.recordSuccessIfUnknown();
}
Also used : QName(javax.xml.namespace.QName) Element(org.w3c.dom.Element) OperationResult(com.evolveum.midpoint.schema.result.OperationResult) PrismSchema(com.evolveum.midpoint.prism.schema.PrismSchema)

Example 12 with PrismSchema

use of com.evolveum.midpoint.prism.schema.PrismSchema in project midpoint by Evolveum.

the class SchemaDocMojo method execute.

public void execute() throws MojoExecutionException, MojoFailureException {
    getLog().info("SchemaDoc plugin started");
    PrismContext prismContext = createInitializedPrismContext();
    File outDir = initializeOutDir();
    PathGenerator pathGenerator = new PathGenerator(outDir);
    VelocityEngine velocityEngine = createVelocityEngine();
    SchemaRegistry schemaRegistry = prismContext.getSchemaRegistry();
    try {
        renderSchemaIndex(schemaRegistry, prismContext, velocityEngine, pathGenerator);
    } catch (IOException e) {
        throw new MojoExecutionException(e.getMessage(), e);
    }
    for (PrismSchema schema : schemaRegistry.getSchemas()) {
        try {
            renderSchema(schema, prismContext, velocityEngine, pathGenerator);
        } catch (IOException e) {
            throw new MojoExecutionException(e.getMessage(), e);
        }
    }
    try {
        copyResources(outDir);
    } catch (IOException e) {
        throw new MojoExecutionException(e.getMessage(), e);
    }
    File archiveFile = null;
    try {
        archiveFile = generateArchive(outDir, finalName + "-schemadoc.zip");
    } catch (IOException e) {
        throw new MojoExecutionException(e.getMessage(), e);
    } catch (ArchiverException e) {
        throw new MojoExecutionException(e.getMessage(), e);
    }
    projectHelper.attachArtifact(project, "zip", "schemadoc", archiveFile);
    getLog().info("SchemaDoc plugin finished");
}
Also used : PrismSchema(com.evolveum.midpoint.prism.schema.PrismSchema) VelocityEngine(org.apache.velocity.app.VelocityEngine) MojoExecutionException(org.apache.maven.plugin.MojoExecutionException) ArchiverException(org.codehaus.plexus.archiver.ArchiverException) PrismContext(com.evolveum.midpoint.prism.PrismContext) SchemaRegistry(com.evolveum.midpoint.prism.schema.SchemaRegistry)

Example 13 with PrismSchema

use of com.evolveum.midpoint.prism.schema.PrismSchema in project midpoint by Evolveum.

the class ReportTypeUtil method applyConfigurationDefinition.

public static void applyConfigurationDefinition(PrismObject<ReportType> report, ObjectDelta delta, PrismContext prismContext) throws SchemaException {
    PrismSchema schema = ReportTypeUtil.parseReportConfigurationSchema(report, prismContext);
    PrismContainerDefinition<ReportConfigurationType> definition = ReportTypeUtil.findReportConfigurationDefinition(schema);
    if (definition == null) {
        //no definition found for container
        throw new SchemaException("Couldn't find definitions for report type " + report + ".");
    }
    Collection<ItemDelta> modifications = delta.getModifications();
    for (ItemDelta itemDelta : modifications) {
        if (itemDelta.hasCompleteDefinition()) {
            continue;
        }
        ItemDefinition def = definition.findItemDefinition(itemDelta.getPath().tail());
        if (def != null) {
            itemDelta.applyDefinition(def);
        }
    }
}
Also used : PrismSchema(com.evolveum.midpoint.prism.schema.PrismSchema) SchemaException(com.evolveum.midpoint.util.exception.SchemaException) ItemDefinition(com.evolveum.midpoint.prism.ItemDefinition) ItemDelta(com.evolveum.midpoint.prism.delta.ItemDelta) ReportConfigurationType(com.evolveum.midpoint.xml.ns._public.common.common_3.ReportConfigurationType)

Example 14 with PrismSchema

use of com.evolveum.midpoint.prism.schema.PrismSchema in project midpoint by Evolveum.

the class ConnectorTypeUtil method parseConnectorSchema.

/**
	 * Returns parsed connector schema
	 */
public static PrismSchema parseConnectorSchema(ConnectorType connectorType, PrismContext prismContext) throws SchemaException {
    Element connectorSchemaElement = ConnectorTypeUtil.getConnectorXsdSchema(connectorType);
    if (connectorSchemaElement == null) {
        return null;
    }
    PrismSchema connectorSchema = PrismSchemaImpl.parse(connectorSchemaElement, true, "schema for " + connectorType, prismContext);
    // Make sure that the config container definition has a correct compile-time class name
    QName configContainerQName = new QName(connectorType.getNamespace(), ResourceType.F_CONNECTOR_CONFIGURATION.getLocalPart());
    PrismContainerDefinition<ConnectorConfigurationType> configurationContainerDefintion = connectorSchema.findContainerDefinitionByElementName(configContainerQName);
    ((PrismContainerDefinitionImpl) configurationContainerDefintion).setCompileTimeClass(ConnectorConfigurationType.class);
    return connectorSchema;
}
Also used : PrismSchema(com.evolveum.midpoint.prism.schema.PrismSchema) ConnectorConfigurationType(com.evolveum.midpoint.xml.ns._public.common.common_3.ConnectorConfigurationType) QName(javax.xml.namespace.QName) Element(org.w3c.dom.Element)

Example 15 with PrismSchema

use of com.evolveum.midpoint.prism.schema.PrismSchema in project midpoint by Evolveum.

the class AbstractBasicDummyTest method test010ListConnectors.

/**
	 * Check whether the connectors were discovered correctly and were added to
	 * the repository.
	 */
@Test
public void test010ListConnectors() throws Exception {
    final String TEST_NAME = "test010ListConnectors";
    TestUtil.displayTestTile(TEST_NAME);
    // GIVEN
    OperationResult result = new OperationResult(AbstractBasicDummyTest.class.getName() + "." + TEST_NAME);
    // WHEN
    List<PrismObject<ConnectorType>> connectors = repositoryService.searchObjects(ConnectorType.class, new ObjectQuery(), null, result);
    // THEN
    result.computeStatus();
    display("searchObjects result", result);
    TestUtil.assertSuccess(result);
    assertFalse("No connector found", connectors.isEmpty());
    for (PrismObject<ConnectorType> connPrism : connectors) {
        ConnectorType conn = connPrism.asObjectable();
        display("Found connector " + conn, conn);
        display("XML " + conn, PrismTestUtil.serializeObjectToString(connPrism, PrismContext.LANG_XML));
        XmlSchemaType xmlSchemaType = conn.getSchema();
        assertNotNull("xmlSchemaType is null", xmlSchemaType);
        Element connectorXsdSchemaElement = ConnectorTypeUtil.getConnectorXsdSchema(conn);
        assertNotNull("No schema", connectorXsdSchemaElement);
        // Try to parse the schema
        PrismSchema schema = PrismSchemaImpl.parse(connectorXsdSchemaElement, true, "connector schema " + conn, prismContext);
        assertNotNull("Cannot parse schema", schema);
        assertFalse("Empty schema", schema.isEmpty());
        display("Parsed connector schema " + conn, schema);
        QName configurationElementQname = new QName(conn.getNamespace(), ResourceType.F_CONNECTOR_CONFIGURATION.getLocalPart());
        PrismContainerDefinition configurationContainer = schema.findContainerDefinitionByElementName(configurationElementQname);
        assertNotNull("No " + configurationElementQname + " element in schema of " + conn, configurationContainer);
        PrismContainerDefinition definition = schema.findItemDefinition(ResourceType.F_CONNECTOR_CONFIGURATION.getLocalPart(), PrismContainerDefinition.class);
        assertNotNull("Definition of <configuration> property container not found", definition);
        PrismContainerDefinition pcd = (PrismContainerDefinition) definition;
        assertFalse("Empty definition", pcd.isEmpty());
    }
}
Also used : ConnectorType(com.evolveum.midpoint.xml.ns._public.common.common_3.ConnectorType) QName(javax.xml.namespace.QName) Element(org.w3c.dom.Element) OperationResult(com.evolveum.midpoint.schema.result.OperationResult) XmlSchemaType(com.evolveum.midpoint.xml.ns._public.common.common_3.XmlSchemaType) ObjectQuery(com.evolveum.midpoint.prism.query.ObjectQuery) PrismSchema(com.evolveum.midpoint.prism.schema.PrismSchema) PrismObject(com.evolveum.midpoint.prism.PrismObject) PrismContainerDefinition(com.evolveum.midpoint.prism.PrismContainerDefinition) Test(org.testng.annotations.Test)

Aggregations

PrismSchema (com.evolveum.midpoint.prism.schema.PrismSchema)44 Test (org.testng.annotations.Test)20 QName (javax.xml.namespace.QName)10 SchemaRegistry (com.evolveum.midpoint.prism.schema.SchemaRegistry)8 OperationResult (com.evolveum.midpoint.schema.result.OperationResult)8 ConnectorType (com.evolveum.midpoint.xml.ns._public.common.common_3.ConnectorType)8 Element (org.w3c.dom.Element)8 SchemaException (com.evolveum.midpoint.util.exception.SchemaException)7 AbstractInitializedModelIntegrationTest (com.evolveum.midpoint.model.intest.AbstractInitializedModelIntegrationTest)6 Task (com.evolveum.midpoint.task.api.Task)6 ConnectorConfigurationType (com.evolveum.midpoint.xml.ns._public.common.common_3.ConnectorConfigurationType)6 UserType (com.evolveum.midpoint.xml.ns._public.common.common_3.UserType)6 ObjectNotFoundException (com.evolveum.midpoint.util.exception.ObjectNotFoundException)4 Document (org.w3c.dom.Document)4 PrismContainerDefinition (com.evolveum.midpoint.prism.PrismContainerDefinition)3 PrismContext (com.evolveum.midpoint.prism.PrismContext)3 PrismObject (com.evolveum.midpoint.prism.PrismObject)3 ItemPath (com.evolveum.midpoint.prism.path.ItemPath)3 PrismSchemaImpl (com.evolveum.midpoint.prism.schema.PrismSchemaImpl)3 SchemaRegistryImpl (com.evolveum.midpoint.prism.schema.SchemaRegistryImpl)3