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;
}
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());
}
}
}
Aggregations