0
votes

I want to load and process json schema files from within two different deployments, the first being a WAR with JAX-RS endpoint and the second an EAR with Singleton-EJB + a resources JAR containing the schema files (I've read that packaging resources files for use in EJBs is only possible when bundling them in a separate JAR inside the EAR).

Development environment is eclipse 2019-03 with JBoss Wildfly 16.

WAR deployment with JAX-RS endpoint

The WAR part is fine, I have an @ApplicationScoped Bean and can access the schema files located in src/main/webapp/schemas/ via ServletContext, see the following code snippet:

@ForWarDeployment
@ApplicationScoped
public class JsonSchemaValidatorWar extends JsonSchemaValidatorBase {
...
@PostConstruct
public void init() {
    Consumer<Path> readSchema = schemaFile -> {
        String schemaName = schemaFile.getName(schemaFile.getNameCount() - 1).toString();
        JsonSchema js = jvs.readSchema(schemaFile);
        map.put(schemaName, js); // this is a concurrent hash map in base class
        log.info("Schema " + schemaName + " added: " + js.toJson());
    };
    URI schemaFolder;
    try {
        schemaFolder = servletContext.getResource("/schemas").toURI();
        try (Stream<Path> paths = Files.walk(Paths.get(schemaFolder))) {
            paths.filter(Files::isRegularFile).forEach(readSchema);
        }
    } catch (URISyntaxException | IOException e) {
        throw new RuntimeException("Error loading schema files!", e);
    }
}

Output on first request:

... (default task-1) Schema person.schema.json added: {"$id": ...

EAR deployment with EJB and resources JAR

The EJB part is tricky, I haven't found a solution for reading all schema files yet.

What I currently have is a multi-module maven project with the following structure:

- parent
- | ear 
- | ejb3
- | resources

pom.xml for parent project

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <groupId>mdv</groupId>
  <artifactId>json-parent</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  <packaging>pom</packaging>

  <modules>
    <module>json-ejb3</module>
    <module>json-ear</module>
    <module>json-resources</module>
  </modules>

  <properties>
        <maven.compiler.source>1.8</maven.compiler.source>
        <maven.compiler.target>1.8</maven.compiler.target>
    </properties>
</project>

pom.xml for ear project

<project xmlns="http://maven.apache.org/POM/4.0.0"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>mdv</groupId>
        <artifactId>json-parent</artifactId>
        <version>0.0.1-SNAPSHOT</version>
    </parent>
    <artifactId>json-ear</artifactId>
    <packaging>ear</packaging>

    <properties>
        <maven.compiler.source>1.8</maven.compiler.source>
        <maven.compiler.target>1.8</maven.compiler.target>
    </properties>

    <dependencies>
        <dependency>
            <groupId>mdv</groupId>
            <artifactId>json-ejb3</artifactId>
            <version>${project.version}</version>
            <type>ejb</type>
        </dependency>
        <dependency>
            <groupId>mdv</groupId>
            <artifactId>json-resources</artifactId>
            <version>${project.version}</version>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-ear-plugin</artifactId>
                <version>3.0.1</version>
                <configuration>
                    <version>7</version>
                    <defaultLibBundleDir>lib</defaultLibBundleDir>
                    <earSourceDirectory>${basedir}/src/main/resources</earSourceDirectory>
                    <outputFileNameMapping>@{artifactId}@@{dashClassifier?}@.@{extension}@</outputFileNameMapping>
                </configuration>
            </plugin>
        </plugins>
    </build>
</project>

pom.xml for resources project

<project xmlns="http://maven.apache.org/POM/4.0.0"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>mdv</groupId>
        <artifactId>json-parent</artifactId>
        <version>0.0.1-SNAPSHOT</version>
    </parent>
    <artifactId>json-resources</artifactId>
</project>

pom.xml for ejb3 project

<project xmlns="http://maven.apache.org/POM/4.0.0"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>mdv</groupId>
        <artifactId>json-parent</artifactId>
        <version>0.0.1-SNAPSHOT</version>
    </parent>
    <artifactId>json-ejb3</artifactId>
    <packaging>ejb</packaging>

    <properties>
        <maven.compiler.source>1.8</maven.compiler.source>
        <maven.compiler.target>1.8</maven.compiler.target>
    </properties>

    <build>
        <finalName>${project.artifactId}</finalName>
        <plugins>
            <plugin>
                <artifactId>maven-ejb-plugin</artifactId>
                <version>3.0.1</version>
                <configuration>
                    <ejbVersion>3.2</ejbVersion>
                </configuration>
            </plugin>
        </plugins>
    </build>

    <dependencies>
        <dependency>
            <groupId>javax</groupId>
            <artifactId>javaee-api</artifactId>
            <version>7.0</version>
            <scope>provided</scope>
        </dependency>
        <dependency>
        <!-- contains a json schema processing library and the class JsonSchemaValidatorEjb -->
            <groupId>mdv</groupId>
            <artifactId>json</artifactId>
            <version>0.0.1-SNAPSHOT</version>
        </dependency>
    </dependencies>
</project>

The problem with loading schema files in EJB

I want to load the schema files in an @ApplicationScoped bean for use in a Singleton EJB, the corresponding class is JsonSchemaValidatorService:

package mdv;

import java.util.logging.Logger;

import javax.annotation.PostConstruct;
import javax.ejb.Singleton;
import javax.ejb.Startup;
import javax.inject.Inject;

import json.ForEjbDeployment;
import json.IJsonSchemaValidator;

@Singleton
@Startup
public class JsonSchemaValidatorService {

    Logger log = Logger.getLogger("JsonSchemaValidatorService");

    @Inject
    @ForEjbDeployment
    IJsonSchemaValidator jsonSchemaValidator;
    // this is where json schema files should be loaded

    public JsonSchemaValidatorService() {
        //
    }

    @PostConstruct
    public void init() {
        log.info("Started JsonSchemaValidatorService.");
        log.info("Loaded schemas in jsonSchemaValidator: " + jsonSchemaValidator.getLoadedSchemas());
    }

}

The class for loading json schema files in EJB environment is this bean:

package json;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.URISyntaxException;
import java.nio.charset.StandardCharsets;
import java.nio.file.Path;
import java.util.function.Consumer;
import java.util.logging.Logger;

import javax.annotation.PostConstruct;
import javax.enterprise.context.ApplicationScoped;
import javax.resource.spi.IllegalStateException;

import org.leadpony.justify.api.JsonSchema;

@ForEjbDeployment
@ApplicationScoped
public class JsonSchemaValidatorEjb extends JsonSchemaValidatorBase {

    Logger log = Logger.getLogger("JsonSchemaValidator");

    public JsonSchemaValidatorEjb() {
        //
    }

    @PostConstruct
    public void init() {
        try {
            // This is where I can't manage to get a list of the json schema files and process them
            final ClassLoader loader = Thread.currentThread().getContextClassLoader();
            try(
                    final InputStream is = loader.getResourceAsStream("schemas");
                    final InputStreamReader isr = new InputStreamReader(is, StandardCharsets.UTF_8);
                    final BufferedReader br = new BufferedReader(isr)) {
                log.info("schema files in directory: ");
                br.lines().forEach(x -> log.info(x));
            }
        } catch (Exception e) {
            throw new RuntimeException("Error trying to parse schema files!", e);
        }
    }
}

No Exception is thrown but also no files will be found in the directory provided, e.g. "schemas". The shortened output after EJB started is:

[JsonSchemaValidatorService] Started JsonSchemaValidatorService.
[JsonSchemaValidator] schema files in directory: 
[JsonSchemaValidatorService] Loaded schemas in jsonSchemaValidator: {}

The file structure of the deployed ear is this:

- lib
| - icu4j.jar
| - javax.json-api.jar
| - javax.json.jar
| - json-resources.jar // jar with resources, in this case the schemas
| | - schemas
| | | - person.schema.json
| - json.jar // jar containing @ApplicationScoped beans for war und ejb
| - justify.jar // json schema processing library used
- META-INF
| - maven
| | ...
| - schemas
| | - person.schema.json
| - application.xml
| - MANIFEST.MF
- schemas
| -person.schema.json
- json-ejb3.jar

As you can see I have managed to bundle the schemas folder and the single json schema file in multiple locations, but none of this works.

Is this even possible to achieve? Am I wrong with the path specified at getResourceAsStream("schemas")?

The goal is to load all existing json schema files at startup to parse them to JsonSchema objects once to validate them later on (it will be a message-driven bean, by the way).

2
Have you checked docs.jboss.org/author/display/WFLY/Class+Loading+in+WildFly? I would start with defining jboss-deployment-structure.xml. As a side note - any explicit reason to use EAR packaging? Almost all features of JavaEE are available with WAR packaging (which is much simpler) starting from JavaEE 6.Illya Kysil
The way to iterate over your schemas is answered in How to list the files inside a JAR file?. Given that, there is no reason these resources cannot be in the same jar as your EJBs. They only need to be in a separate jar in the EAR/lib directory if you need to access them from other jars or WARs in the EAR. You do not need to be concerned with jboss-deployment-structure.xml unless you're doing something funky - which you are not.Steve C
@IllyaKysil No there is no particular reason for an ear, a co-worker also told me so and I have managed to package my EJB into the war, thanks! Still no success in reading the folder though :(devrys

2 Answers

1
votes

Finally I found a nice solution that works both in Servlet and EJB context and eliminates the need for distinguishing between them.

Since I am not able to list the files inside the schemas folder from within an EJB but access and read single files I came up with the idea to use an auto-generated – at build time – file containing the list of all JSON schema files and using this to process the schemas

Moving EJB to WAR deployment

First I followed the advice from @IllyaKysil and moved my EJB from an EAR deployment to the already existing and working WAR deployment

Move schema files into JAR

The original approach had the JSON schema files in both WAR and EAR deployments. I now have the files inside src/main/resources/schemas folder of the JAR project which I have a maven dependency on in the WAR project. Result structure of archive is:

| jee7-test.war
| - WEB-INF
| | - lib
| | | - json-validator-0.0.1-SNAPSHOT.jar
| | | | - schemas
| | | | | - person.schema.json
| | | | | - schemaList.txt

Generate schemaList.txt at build

Using the maven antrun plugin a file is created in src/main/resources/schemas with each file in the schemas directory with an extension of .schema.json on a seperate line:

<plugin>
   <artifactId>maven-antrun-plugin</artifactId>
   <version>1.8</version>
   <executions>
      <execution>
         <phase>generate-sources</phase>
         <configuration>
            <target>
               <fileset id="schemaFiles"
                  dir="src/main/resources/schemas/" includes="*.schema.json" />
               <pathconvert pathsep="${line.separator}"
                  property="schemaFileList" refid="schemaFiles">
                  <map from="${basedir}\src\main\resources\schemas\" to="" />
               </pathconvert>
               <echo
               file="${basedir}\src\main\resources\schemas\schemaList.txt">${schemaFileList}</echo>
            </target>
         </configuration>
         <goals>
            <goal>run</goal>
         </goals>
      </execution>
   </executions>
</plugin>

Content of generated file then is:

person.schema.json

Reading schemaList.txt and parsing schemas

Final step is reading the file with the list of JSON schema files and process each line to parse the corresponding schema file:

@ApplicationScoped
public class JsonSchemaValidator implements IJsonSchemaValidator {

    protected JsonValidationService jvs = JsonValidationService.newInstance();
    protected ConcurrentHashMap<String, JsonSchema> schemaMap = new ConcurrentHashMap<String, JsonSchema>();
    private Logger log = Logger.getLogger("JsonSchemaValidator");

    public JsonSchemaValidator() {
        //
    }

    private String SCHEMA_FOLDER = "schemas/";
    private String SCHEMA_LIST_FILE = "schemaList.txt";

    @PostConstruct
    public void init() {
        try {
            final ClassLoader loader = Thread.currentThread().getContextClassLoader();
            // load file containing list of JSON schema files
            try (final InputStream is = loader.getResourceAsStream(SCHEMA_FOLDER + SCHEMA_LIST_FILE);
                    final InputStreamReader isr = new InputStreamReader(is, StandardCharsets.UTF_8);
                    final BufferedReader br = new BufferedReader(isr)) {
                // each line is a name of a JSON schema file that has to be processed
                br.lines().forEach(line -> readSchema(line, loader));
            }
            log.info("Number of JsonSchema objects in schemaMap: " + schemaMap.size());
            log.info("Keys in schemaMap: ");
            schemaMap.forEachKey(1L, key -> log.info(key));
        } catch (Exception e) {
            throw new RuntimeException("Error trying to parse schema files!", e);
        }
    }

    private void readSchema(String schemaFileName, ClassLoader classLoader) {
        // only use part of the file name to first dot, which leaves me with "person"
        // for "person.schema.json" file name
        String schemaName = schemaFileName.substring(0, schemaFileName.indexOf("."));
        JsonSchema js = jvs.readSchema(classLoader.getResourceAsStream(SCHEMA_FOLDER + schemaFileName));
        // put JsonSchema object in map with schema name as key
        schemaMap.put(schemaName, js);
        log.info("Schema " + schemaName + " added: " + js.toJson());
    }

    @Override
    public List<Problem> validate(String json, String schemaName) {
        List<Problem> result = new ArrayList<Problem>();
        JsonSchema jsonSchema = schemaMap.get(schemaName);
        JsonReader reader = jvs.createReader(new StringReader(json), jsonSchema, ProblemHandler.collectingTo(result));
        reader.read();

        return result;
    }

    @Override
    public Map<String, JsonSchema> getLoadedSchemas() {
        return Collections.unmodifiableMap(schemaMap);
    }
}

Result

JSON strings from input can now be validated against a JSON schema without the need to parse the schema over and over again

@Inject
IJsonSchemaValidator jsv;
...
List<Problem> problems = jsv.validate(inputJson, "person");

Logged output after JsonSchemaValidator instance has been created:

Schema person added: {"$id":"....}
Number of JsonSchema objects in schemaMap: 1
Keys in schemaMap: 
person
0
votes

The way to iterate over your schemas is answered in How to list the files inside a JAR file?.

Given that, there is no reason these resources cannot be in the same jar as your EJBs. They only need to be in a separate jar in the EAR/lib directory if you need to access them from other jars or WARs in the EAR.

You do not need to be concerned with jboss-deployment-structure.xml unless you're doing something funky - which you are not.

Also, you cannot read resources from inside the EAR itself, such as your ear/META-INF/schemas. This counts as funky and you will still need the solution indicated above to iterate.