Skip to content

Commit

Permalink
Performed light refactoring and created javadocs for the databind mod…
Browse files Browse the repository at this point in the history
…ule. Cleaned up stream closure.
  • Loading branch information
david-waltermire committed Jul 3, 2024
1 parent 61447eb commit 2f3d3bf
Show file tree
Hide file tree
Showing 11 changed files with 93 additions and 48 deletions.
4 changes: 4 additions & 0 deletions databind/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,10 @@
<scope>test</scope>
</dependency>

<dependency>
<groupId>org.eclipse.jdt</groupId>
<artifactId>org.eclipse.jdt.annotation</artifactId>
</dependency>
</dependencies>
<build>
<pluginManagement>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.eclipse.jdt.annotation.Owning;

import java.io.IOException;
import java.lang.module.ModuleDescriptor;
Expand Down Expand Up @@ -138,21 +139,26 @@ public static IProduction compileModule(
* load a class
* @return the new class loader
*/
@SuppressWarnings("null")
@NonNull
public static ClassLoader newClassLoader(
@NonNull final Path classDir,
@NonNull final ClassLoader parent) {
return AccessController.doPrivileged(new PrivilegedAction<>() {
@Override
public URLClassLoader run() {
try {
return new URLClassLoader(new URL[] { classDir.toUri().toURL() }, parent);
} catch (MalformedURLException ex) {
throw new IllegalStateException("unable to configure class loader", ex);
}
}
});
ClassLoader retval = AccessController
.doPrivileged((PrivilegedAction<ClassLoader>) () -> newClassLoaderInternal(classDir, parent));
assert retval != null;
return retval;
}

@Owning
@NonNull
private static ClassLoader newClassLoaderInternal(
@NonNull final Path classDir,
@NonNull final ClassLoader parent) {
try {
return new URLClassLoader(new URL[] { classDir.toUri().toURL() }, parent);
} catch (MalformedURLException ex) {
throw new IllegalStateException("unable to configure class loader", ex);
}
}

@SuppressWarnings({
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,12 @@ public String getClassName(IModelDefinition definition) {
return retval;
}

@Override
public @NonNull String getClassName(@NonNull IModule module) {
// TODO: make this configurable
return ClassUtils.toClassName(module.getShortName() + "Module");
}

@Override
public List<String> getQualifiedSuperinterfaceClassNames(IModelDefinition definition) {
IDefinitionBindingConfiguration config = getBindingConfigurationForDefinition(definition);
Expand All @@ -134,12 +140,6 @@ public List<String> getQualifiedSuperinterfaceClassNames(IModelDefinition defini
: config.getInterfacesToImplement();
}

@Override
public @NonNull String getClassName(@NonNull IModule module) {
// TODO: make this configurable
return ClassUtils.toClassName(module.getShortName() + "Module");
}

/**
* Binds an XML namespace, which is normally associated with one or more Module,
* with a provided Java package name.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -277,9 +277,9 @@ public ClassName getBaseClassName(IModelDefinition definition) {
@Override
public List<ClassName> getSuperinterfaces(IModelDefinition definition) {
List<String> classNames = bindingConfiguration.getQualifiedSuperinterfaceClassNames(definition);
return classNames.stream()
return ObjectUtils.notNull(classNames.stream()
.map(ClassName::bestGuess)
.collect(Collectors.toUnmodifiableList());
.collect(Collectors.toUnmodifiableList()));
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,16 @@ IGroupedNamedModelInstanceTypeInfo getTypeInfo(
@NonNull
ClassName getClassName(@NonNull IModelDefinition definition);

/**
* Get the name of the class associated with the provided Metaschema definition.
*
* @param typeInfo
* the type information to get the class name for
* @return the class name information for the definition
*/
@NonNull
ClassName getClassName(@NonNull INamedModelInstanceTypeInfo typeInfo);

/**
* Get the name of the super interfaces associated with the provided Metaschema
* definition.
Expand All @@ -180,16 +190,6 @@ IGroupedNamedModelInstanceTypeInfo getTypeInfo(
@NonNull
List<ClassName> getSuperinterfaces(@NonNull IModelDefinition definition);

/**
* Get the name of the class associated with the provided Metaschema definition.
*
* @param typeInfo
* the type information to get the class name for
* @return the class name information for the definition
*/
@NonNull
ClassName getClassName(@NonNull INamedModelInstanceTypeInfo typeInfo);

/**
* Get the name of the class associated with the provided Metaschema definition
* using the provided {@code postfix}. This class will be a child of the
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,9 @@
import gov.nist.secauto.metaschema.databind.IBindingContext;
import gov.nist.secauto.metaschema.databind.io.ModelDetector.Result;

import org.eclipse.jdt.annotation.NotOwning;
import org.eclipse.jdt.annotation.Owning;

import java.io.IOException;
import java.io.InputStream;
import java.net.URI;
Expand Down Expand Up @@ -156,7 +159,8 @@ private ModelDetector getModelDetector() {
}

@Override
public Result detectModel(InputStream is, Format format) throws IOException {
@Owning
public Result detectModel(@NotOwning InputStream is, Format format) throws IOException {
return getModelDetector().detect(is, format);
}

Expand Down Expand Up @@ -244,15 +248,17 @@ public IDocumentNodeItem loadAsNodeItem(Format format, URI uri) throws IOExcepti
}

@Override
public IDocumentNodeItem loadAsNodeItem(Format format, InputStream is, URI documentUri) throws IOException {
ModelDetector.Result modelMatch = detectModel(is, format);

IDeserializer<?> deserializer = getDeserializer(
modelMatch.getBoundClass(),
format,
getConfiguration());
try (InputStream modelStream = modelMatch.getDataStream()) {
return (IDocumentNodeItem) deserializer.deserializeToNodeItem(modelStream, documentUri);
public IDocumentNodeItem loadAsNodeItem(Format format, InputStream is, URI documentUri)
throws IOException {
try (ModelDetector.Result modelMatch = detectModel(is, format)) {

IDeserializer<?> deserializer = getDeserializer(
modelMatch.getBoundClass(),
format,
getConfiguration());
try (InputStream modelStream = modelMatch.getDataStream()) {
return (IDocumentNodeItem) deserializer.deserializeToNodeItem(modelStream, documentUri);
}
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
import gov.nist.secauto.metaschema.databind.DefaultBindingContext;
import gov.nist.secauto.metaschema.databind.IBindingContext;

import org.eclipse.jdt.annotation.Owning;
import org.xml.sax.InputSource;

import java.io.File;
Expand Down Expand Up @@ -168,6 +169,7 @@ default Format detectFormat(@NonNull URL url) throws IOException {
* if an error occurred while reading the resource
*/
@NonNull
@Owning
ModelDetector.Result detectModel(@NonNull InputStream is, @NonNull Format format) throws IOException;

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,11 @@

import org.codehaus.stax2.XMLEventReader2;
import org.codehaus.stax2.XMLInputFactory2;
import org.eclipse.jdt.annotation.NotOwning;
import org.eclipse.jdt.annotation.Owning;

import java.io.ByteArrayInputStream;
import java.io.Closeable;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
Expand Down Expand Up @@ -122,23 +125,30 @@ private IConfiguration<DeserializationFeature<?>> getConfiguration() {
* if an error occurred while reading the resource
*/
@NonNull
public Result detect(@NonNull InputStream inputStream, @NonNull Format format)
@Owning
public Result detect(@NonNull @NotOwning InputStream inputStream, @NonNull Format format)
throws IOException {
byte[] buf = ObjectUtils.notNull(inputStream.readNBytes(getLookaheadLimit()));

Class<? extends IBoundObject> clazz;
try (InputStream bis = new ByteArrayInputStream(buf)) {
assert bis != null;
switch (format) {
case JSON:
clazz = detectModelJsonClass(ObjectUtils.notNull(
JsonFactoryFactory.instance().createParser(bis)));
try (JsonParser parser = JsonFactoryFactory.instance().createParser(bis)) {
assert parser != null;
clazz = detectModelJsonClass(parser);
}
break;
case YAML:
YAMLFactory factory = YamlFactoryFactory.newParserFactoryInstance(getConfiguration());
clazz = detectModelJsonClass(ObjectUtils.notNull(factory.createParser(bis)));
try (JsonParser parser = factory.createParser(bis)) {
assert parser != null;
clazz = detectModelJsonClass(parser);
}
break;
case XML:
clazz = detectModelXmlClass(ObjectUtils.notNull(bis));
clazz = detectModelXmlClass(bis);
break;
default:
throw new UnsupportedOperationException(
Expand Down Expand Up @@ -204,11 +214,11 @@ private Class<? extends IBoundObject> detectModelJsonClass(@NonNull JsonParser p
return retval;
}

public static final class Result {
public static final class Result implements Closeable {
@NonNull
private final Class<? extends IBoundObject> boundClass;
@NonNull
private final InputStream dataStream;
@Owning
private InputStream dataStream;

private Result(
@NonNull Class<? extends IBoundObject> clazz,
Expand All @@ -235,8 +245,15 @@ public Class<? extends IBoundObject> getBoundClass() {
* @return the stream
*/
@NonNull
@Owning
public InputStream getDataStream() {
return dataStream;
return ObjectUtils.requireNonNull(dataStream, "data stream already closed");
}

@Override
public void close() throws IOException {
this.dataStream.close();
this.dataStream = null;
}
}
}
1 change: 1 addition & 0 deletions databind/src/main/java/module-info.java
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@

requires flexmark.util.sequence;
requires transitive com.google.auto.service;
requires org.eclipse.jdt.annotation;

exports gov.nist.secauto.metaschema.databind;
exports gov.nist.secauto.metaschema.databind.codegen;
Expand Down
4 changes: 4 additions & 0 deletions metaschema-cli/src/main/assembly/bin.xml
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,10 @@
<outputDirectory>/lib</outputDirectory>
<outputFileNameMapping>${artifact.groupId}.${artifact.artifactId}-${artifact.version}.${artifact.extension}</outputFileNameMapping>
<useProjectArtifact>true</useProjectArtifact>
<excludes>
<exclude>org.jetbrains:annotations</exclude>
<exclude>org.eclipse.jdt:org.eclipse.jdt.annotation</exclude>
</excludes>
</dependencySet>
</dependencySets>
<fileSets>
Expand Down
5 changes: 5 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -533,6 +533,11 @@
<artifactId>biz.aQute.bnd.util</artifactId>
<version>6.4.1</version>
</dependency>
<dependency>
<groupId>org.eclipse.jdt</groupId>
<artifactId>org.eclipse.jdt.annotation</artifactId>
<version>2.3.0</version>
</dependency>
</dependencies>
</dependencyManagement>
<dependencies>
Expand Down

0 comments on commit 2f3d3bf

Please sign in to comment.