Search in sources :

Example 1 with JobXmlResolver

use of org.jberet.spi.JobXmlResolver in project wildfly by wildfly.

the class WildFlyJobXmlResolver method forDeployment.

/**
     * Creates the {@linkplain JobXmlResolver resolver} for the deployment inheriting any visible resolvers and job XML
     * files from dependencies.
     *
     * @param deploymentUnit the deployment to process
     *
     * @return the resolve
     *
     * @throws DeploymentUnitProcessingException if an error occurs processing the deployment
     */
public static WildFlyJobXmlResolver forDeployment(final DeploymentUnit deploymentUnit) throws DeploymentUnitProcessingException {
    // If this deployment unit already has a resolver, just use it
    if (deploymentUnit.hasAttachment(BatchAttachments.JOB_XML_RESOLVER)) {
        return deploymentUnit.getAttachment(BatchAttachments.JOB_XML_RESOLVER);
    }
    // Get the module for it's class loader
    final Module module = deploymentUnit.getAttachment(Attachments.MODULE);
    final ClassLoader classLoader = module.getClassLoader();
    WildFlyJobXmlResolver resolver;
    // access to the EAR/lib directory so those resources need to be processed
    if (DeploymentTypeMarker.isType(DeploymentType.EAR, deploymentUnit)) {
        // Create a new WildFlyJobXmlResolver without jobs from sub-deployments as they'll be processed later
        final List<ResourceRoot> resources = deploymentUnit.getAttachmentList(Attachments.RESOURCE_ROOTS).stream().filter(r -> !SubDeploymentMarker.isSubDeployment(r)).collect(Collectors.toList());
        resolver = create(classLoader, resources);
        deploymentUnit.putAttachment(BatchAttachments.JOB_XML_RESOLVER, resolver);
    } else {
        // Create a new resolver for this deployment
        if (deploymentUnit.hasAttachment(Attachments.RESOURCE_ROOTS)) {
            resolver = create(classLoader, deploymentUnit.getAttachmentList(Attachments.RESOURCE_ROOTS));
        } else {
            resolver = create(classLoader, Collections.singletonList(deploymentUnit.getAttachment(Attachments.DEPLOYMENT_ROOT)));
        }
        deploymentUnit.putAttachment(BatchAttachments.JOB_XML_RESOLVER, resolver);
        // Process all accessible sub-deployments
        final List<DeploymentUnit> accessibleDeployments = deploymentUnit.getAttachmentList(Attachments.ACCESSIBLE_SUB_DEPLOYMENTS);
        for (DeploymentUnit subDeployment : accessibleDeployments) {
            // Skip our self
            if (deploymentUnit.equals(subDeployment)) {
                continue;
            }
            if (subDeployment.hasAttachment(BatchAttachments.JOB_XML_RESOLVER)) {
                final WildFlyJobXmlResolver toCopy = subDeployment.getAttachment(BatchAttachments.JOB_XML_RESOLVER);
                WildFlyJobXmlResolver.merge(resolver, toCopy);
            } else {
                // We need to create a resolver for the sub-deployment and merge the two
                final WildFlyJobXmlResolver toCopy = forDeployment(subDeployment);
                subDeployment.putAttachment(BatchAttachments.JOB_XML_RESOLVER, toCopy);
                WildFlyJobXmlResolver.merge(resolver, toCopy);
            }
        }
    }
    return resolver;
}
Also used : JobXmlResolver(org.jberet.spi.JobXmlResolver) BatchLogger(org.wildfly.extension.batch.jberet._private.BatchLogger) ResourceRoot(org.jboss.as.server.deployment.module.ResourceRoot) DeploymentTypeMarker(org.jboss.as.ee.structure.DeploymentTypeMarker) SubDeploymentMarker(org.jboss.as.server.deployment.SubDeploymentMarker) ArrayList(java.util.ArrayList) DeploymentUnit(org.jboss.as.server.deployment.DeploymentUnit) LinkedHashMap(java.util.LinkedHashMap) Map(java.util.Map) XMLStreamException(javax.xml.stream.XMLStreamException) DeploymentUnitProcessingException(org.jboss.as.server.deployment.DeploymentUnitProcessingException) VirtualFile(org.jboss.vfs.VirtualFile) XMLResolver(javax.xml.stream.XMLResolver) LinkedHashSet(java.util.LinkedHashSet) Collection(java.util.Collection) Set(java.util.Set) IOException(java.io.IOException) ServiceLoader(java.util.ServiceLoader) PrivilegedAction(java.security.PrivilegedAction) Collectors(java.util.stream.Collectors) List(java.util.List) WildFlySecurityManager(org.wildfly.security.manager.WildFlySecurityManager) Module(org.jboss.modules.Module) DeploymentType(org.jboss.as.ee.structure.DeploymentType) JobParser(org.jberet.job.model.JobParser) Attachments(org.jboss.as.server.deployment.Attachments) VirtualFileFilter(org.jboss.vfs.VirtualFileFilter) Job(org.jberet.job.model.Job) AccessController(java.security.AccessController) Collections(java.util.Collections) InputStream(java.io.InputStream) ResourceRoot(org.jboss.as.server.deployment.module.ResourceRoot) Module(org.jboss.modules.Module) DeploymentUnit(org.jboss.as.server.deployment.DeploymentUnit)

Example 2 with JobXmlResolver

use of org.jberet.spi.JobXmlResolver in project wildfly by wildfly.

the class WildFlyJobXmlResolver method init.

/**
     * Initializes the state of an instance
     */
private void init(final ClassLoader classLoader) {
    // Load the user defined resolvers
    for (JobXmlResolver resolver : ServiceLoader.load(JobXmlResolver.class, classLoader)) {
        jobXmlResolvers.add(resolver);
        for (String jobXml : resolver.getJobXmlNames(classLoader)) {
            addJob(jobXml, resolver.resolveJobName(jobXml, classLoader));
        }
    }
    // Load the default names
    for (Map.Entry<String, VirtualFile> entry : jobXmlFiles.entrySet()) {
        try {
            // Parsing the entire job XML seems excessive to just get the job name. There are two reasons for this:
            //  1) If an error occurs during parsing there's no real need to consider this a valid job
            //  2) Using the implementation parser seems less error prone for future-proofing
            final Job job = JobParser.parseJob(entry.getValue().openStream(), classLoader, new XMLResolver() {

                // this is essentially what JBeret does, but it's ugly. JBeret might need an API to handle this
                @Override
                public Object resolveEntity(final String publicID, final String systemID, final String baseURI, final String namespace) throws XMLStreamException {
                    try {
                        return (jobXmlFiles.containsKey(systemID) ? jobXmlFiles.get(systemID).openStream() : null);
                    } catch (IOException e) {
                        throw new XMLStreamException(e);
                    }
                }
            });
            addJob(entry.getKey(), job.getId());
        } catch (XMLStreamException | IOException e) {
            // Report the possible error as we don't want to fail the deployment. The job may never be run.
            BatchLogger.LOGGER.invalidJobXmlFile(entry.getKey());
        }
    }
}
Also used : JobXmlResolver(org.jberet.spi.JobXmlResolver) VirtualFile(org.jboss.vfs.VirtualFile) XMLStreamException(javax.xml.stream.XMLStreamException) XMLResolver(javax.xml.stream.XMLResolver) IOException(java.io.IOException) Job(org.jberet.job.model.Job) LinkedHashMap(java.util.LinkedHashMap) Map(java.util.Map)

Aggregations

IOException (java.io.IOException)2 LinkedHashMap (java.util.LinkedHashMap)2 Map (java.util.Map)2 XMLResolver (javax.xml.stream.XMLResolver)2 XMLStreamException (javax.xml.stream.XMLStreamException)2 Job (org.jberet.job.model.Job)2 JobXmlResolver (org.jberet.spi.JobXmlResolver)2 VirtualFile (org.jboss.vfs.VirtualFile)2 InputStream (java.io.InputStream)1 AccessController (java.security.AccessController)1 PrivilegedAction (java.security.PrivilegedAction)1 ArrayList (java.util.ArrayList)1 Collection (java.util.Collection)1 Collections (java.util.Collections)1 LinkedHashSet (java.util.LinkedHashSet)1 List (java.util.List)1 ServiceLoader (java.util.ServiceLoader)1 Set (java.util.Set)1 Collectors (java.util.stream.Collectors)1 JobParser (org.jberet.job.model.JobParser)1