Skip to content

Commit

Permalink
Synchronize connector schema parsing
Browse files Browse the repository at this point in the history
After recent changes, connector schema parsing was no longer
synchronized. This commit (along with related one in prism) fixed
that.

This should fix failing
TestDummyExtra#test990ParseConnectorSchemaMultithreaded.
  • Loading branch information
mederly committed Apr 22, 2024
1 parent 422b2da commit 0bf7112
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 36 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,12 @@

package com.evolveum.midpoint.schema.processor;

import com.evolveum.midpoint.prism.impl.schema.SchemaParsingUtil;
import com.evolveum.midpoint.util.DOMUtil;
import com.evolveum.midpoint.util.exception.SchemaException;

import com.evolveum.midpoint.xml.ns._public.common.common_3.ConnectorConfigurationType;

import org.jetbrains.annotations.NotNull;
import org.w3c.dom.Element;

Expand All @@ -18,7 +22,34 @@ public class ConnectorSchemaFactory {
return new ConnectorSchemaImpl(namespace);
}

/**
* Creates the connector schema from provided DOM {@link Element}.
*
* Implementation note: Regarding the synchronization, please see the explanation in
* {@link SchemaParsingUtil#createAndParse(Element, boolean, String, boolean)}.
*/
public static @NotNull ConnectorSchemaImpl parse(@NotNull Element element, String shortDesc) throws SchemaException {
return new ConnectorSchemaImpl(element, shortDesc);
//noinspection SynchronizationOnLocalVariableOrMethodParameter
synchronized (element) {
var schema = newConnectorSchema(DOMUtil.getSchemaTargetNamespace(element));
SchemaParsingUtil.parse(schema, element, true, shortDesc, false);
fixConnectorConfigurationDefinition(schema);
return schema;
}
}

/**
* Make sure that the connector configuration definition has a correct compile-time class name and maxOccurs setting:
*
* . For the compile-time class: the type is {@link ConnectorSchemaImpl#CONNECTOR_CONFIGURATION_TYPE_LOCAL_NAME}
* (ConnectorConfigurationType), but the standard schema parser does not know what compile time class to use.
* So we need to fix it here.
*
* . For the maxOccurs, it is currently not being serialized for the top-level schema items. So we must fix that here.
*/
private static void fixConnectorConfigurationDefinition(ConnectorSchemaImpl schema) throws SchemaException {
var configurationContainerDefinition = schema.getConnectorConfigurationContainerDefinition();
configurationContainerDefinition.mutator().setCompileTimeClass(ConnectorConfigurationType.class);
configurationContainerDefinition.mutator().setMaxOccurs(1);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,7 @@
*/
package com.evolveum.midpoint.schema.processor;

import com.evolveum.midpoint.prism.impl.schema.SchemaParsingUtil;

import com.evolveum.midpoint.xml.ns._public.common.common_3.ConnectorConfigurationType;

import org.w3c.dom.Element;

import com.evolveum.midpoint.prism.impl.schema.PrismSchemaImpl;
import com.evolveum.midpoint.util.DOMUtil;
import com.evolveum.midpoint.util.exception.SchemaException;

/**
* @author semancik
Expand All @@ -26,27 +18,6 @@ public class ConnectorSchemaImpl extends PrismSchemaImpl implements ConnectorSch
super(namespace);
}

/** Please use only from {@link ConnectorSchemaFactory}. */
ConnectorSchemaImpl(Element element, String shortDesc) throws SchemaException {
this(DOMUtil.getSchemaTargetNamespace(element));
SchemaParsingUtil.parse(this, element, true, shortDesc, false);
fixConnectorConfigurationDefinition();
}

/**
* Make sure that the connector configuration definition has a correct compile-time class name and maxOccurs setting:
*
* . For the compile-time class: the type is {@link #CONNECTOR_CONFIGURATION_TYPE_LOCAL_NAME} (ConnectorConfigurationType),
* but the standard schema parser does not know what compile time class to use. So we need to fix it here.
*
* . For the maxOccurs, it is currently not being serialized for the top-level schema items. So we must fix that here.
*/
private void fixConnectorConfigurationDefinition() throws SchemaException {
var configurationContainerDefinition = getConnectorConfigurationContainerDefinition();
configurationContainerDefinition.mutator().setCompileTimeClass(ConnectorConfigurationType.class);
configurationContainerDefinition.mutator().setMaxOccurs(1);
}

@Override
public String getUsualNamespacePrefix() {
// This functionality was either never present or was disabled at some point in time.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -278,12 +278,9 @@ public void test990ParseConnectorSchemaMultithreaded() throws CommonException, I
latch.await();
long time = System.nanoTime();
ConnectorType connector = getCsvConnector(result);
Element elem = ConnectorTypeUtil.getConnectorXsdSchemaElement(connector);
if (elem != null) {
ConnectorTypeUtil.parseConnectorSchema(connector);
}
System.out.println(time + ": " + Thread.currentThread().getId() + ": " + System.identityHashCode(elem));
return elem != null;
var schema = ConnectorTypeUtil.parseConnectorSchemaIfPresent(connector);
System.out.println(time + ": " + Thread.currentThread().getId() + ": " + System.identityHashCode(schema));
return schema != null;
} catch (SchemaException e) {
throw new AssertionError(e);
}
Expand Down

0 comments on commit 0bf7112

Please sign in to comment.