Skip to content

Commit

Permalink
Document thread-safety assumptions for SchemaDescription
Browse files Browse the repository at this point in the history
  - getDomElement, getSource - are package private contracts now
  - document methods used only for testing with @testonly
  - dynamic sax.InputSource is instance-level synchronized
  • Loading branch information
tonydamage committed Mar 2, 2021
1 parent ffd2c30 commit a20fc3a
Show file tree
Hide file tree
Showing 6 changed files with 21 additions and 16 deletions.
Expand Up @@ -9,9 +9,6 @@

import com.evolveum.midpoint.prism.Freezable;
import com.evolveum.midpoint.util.DebugDumpable;
import org.w3c.dom.Element;

import javax.xml.transform.Source;
import java.io.InputStream;

/**
Expand Down Expand Up @@ -53,7 +50,4 @@ public interface SchemaDescription extends DebugDumpable, Freezable {

InputStream openInputStream();

Source getSource();

Element getDomElement();
}
Expand Up @@ -154,8 +154,12 @@ public InputStream openInputStream() {
return streamable.openInputStream();
}

@Override
public Source getSource() {
/**
* Thread-unsafe if no stream is available, but method is called only
* during SchemaRegistryImpl.initialize, which is invoked from single thread.
*/
Source transformSource() {
Source source;
if (canInputStream()) {
InputStream inputStream = openInputStream();
Expand All @@ -169,8 +173,12 @@ public Source getSource() {
return source;
}

@Override
public Element getDomElement() {
/**
* Thread-unsafe, but used only during construction of SchemaDescriptionImpl
* in single threaded context
*
*/
Element getDomElement() {
return source.element();
}

Expand Down
Expand Up @@ -16,6 +16,8 @@
import com.evolveum.midpoint.util.exception.TunnelException;
import com.evolveum.midpoint.util.logging.Trace;
import com.evolveum.midpoint.util.logging.TraceManager;

import org.jetbrains.annotations.TestOnly;
import org.w3c.dom.Element;
import org.w3c.dom.Node;

Expand Down Expand Up @@ -135,6 +137,7 @@ private static void parseFromInputStream(SchemaDescriptionImpl desc) throws Sche
fetchBasicInfoFromSchema(desc);
}

@TestOnly
static SchemaDescriptionImpl parseNode(Node node, String sourceDescription) throws SchemaException {
SchemaDescriptionImpl desc = new SchemaDescriptionImpl(sourceDescription, null);
desc.setNode(node);
Expand Down
Expand Up @@ -363,6 +363,7 @@ private void registerPrismSchemaResource(String resourcePath, String usualPrefix
/**
* Must be called before call to initialize()
*/
@TestOnly
public void registerSchema(Node node, String sourceDescription) throws SchemaException {
registerSchemaDescription(SchemaDescriptionParser.parseNode(node, sourceDescription));
}
Expand Down Expand Up @@ -510,8 +511,8 @@ private void parseJavaxSchema() throws SAXException {
SchemaFactory schemaFactory = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
Source[] sources = new Source[schemaDescriptions.size()];
int i = 0;
for (SchemaDescription schemaDescription : schemaDescriptions) {
Source source = schemaDescription.getSource();
for (SchemaDescriptionImpl schemaDescription : schemaDescriptions) {
Source source = schemaDescription.transformSource();
sources[i] = source;
i++;
}
Expand Down
Expand Up @@ -12,7 +12,7 @@
import com.evolveum.midpoint.prism.impl.schema.SchemaDescriptionImpl.InputStreamable;


public abstract class SchemaSource {
abstract class SchemaSource {

private final Element element;

Expand All @@ -33,7 +33,7 @@ static SchemaSource from(Element element, SchemaDescriptionImpl.InputStreamable

public abstract InputSource saxInputSource();

public Element element() {
Element element() {
return element;
}

Expand All @@ -44,7 +44,7 @@ private Dynamic(Element element) {
}

@Override
public InputSource saxInputSource() {
public synchronized InputSource saxInputSource() {
InputSource inputSource = new InputSource(DomToSchemaProcessor.inputStreamFrom(this.element()));
inputSource.setEncoding("UTF-8");
return inputSource;
Expand Down
Expand Up @@ -107,7 +107,6 @@ private InputSource resolveResourceFromRegisteredSchemasByNamespace(String names
Collection<SchemaDescription> schemaDescriptions = schemaRegistry.getParsedSchemas().get(namespaceURI);
if (schemaDescriptions.size() == 1) {
SchemaDescription schemaDescription = schemaDescriptions.iterator().next();
InputStream inputStream;
InputSource source = ((SchemaDescriptionImpl) schemaDescription).saxInputSource();

//source.setSystemId(schemaDescription.getPath());
Expand Down

0 comments on commit a20fc3a

Please sign in to comment.