final List<ResourceRoot> resourceRoots = DeploymentUtils.allResourceRoots(deploymentUnit);
final DeploymentUnit parent = deploymentUnit.getParent();
final DeploymentUnit topLevelDeployment = parent == null ? deploymentUnit : parent;
final VirtualFile topLevelRoot = topLevelDeployment.getAttachment(Attachments.DEPLOYMENT_ROOT).getRoot();
final ExternalModuleService externalModuleService = topLevelDeployment.getAttachment(Attachments.EXTERNAL_MODULE_SERVICE);
final List<AdditionalModuleSpecification> additionalModuleList = topLevelDeployment.getAttachment(Attachments.ADDITIONAL_MODULES);
final List<ResourceRoot> topLevelResourceRoots = topLevelDeployment.getAttachment(Attachments.RESOURCE_ROOTS);
final ResourceRoot deploymentRoot = deploymentUnit.getAttachment(Attachments.DEPLOYMENT_ROOT);
final List<DeploymentUnit> subDeployments;
if (deploymentUnit.getParent() == null) {
subDeployments = deploymentUnit.getAttachmentList(Attachments.SUB_DEPLOYMENTS);
} else {
subDeployments = deploymentUnit.getParent().getAttachmentList(Attachments.SUB_DEPLOYMENTS);
}
final Map<VirtualFile, ModuleIdentifier> subDeploymentModules = new HashMap<VirtualFile, ModuleIdentifier>();
for (DeploymentUnit deployment : subDeployments) {
final ResourceRoot root = deployment.getAttachment(Attachments.DEPLOYMENT_ROOT);
final ModuleIdentifier identifier = deployment.getAttachment(Attachments.MODULE_IDENTIFIER);
if (root == null || identifier == null) {
continue;
}
subDeploymentModules.put(root.getRoot(), identifier);
}
// build a map of the additional module locations
final Map<VirtualFile, AdditionalModuleSpecification> additionalModules;
if (additionalModuleList == null) {
additionalModules = Collections.emptyMap();
} else {
additionalModules = new HashMap<VirtualFile, AdditionalModuleSpecification>();
for (AdditionalModuleSpecification module : additionalModuleList) {
for (ResourceRoot additionalModuleResourceRoot : module.getResourceRoots()) {
additionalModules.put(additionalModuleResourceRoot.getRoot(), module);
}
}
}
// build a set of ear/lib jars. references to these classes can be ignored as they are already on the class-path
final Set<VirtualFile> earLibJars = new HashSet<VirtualFile>();
if (deploymentUnit.getParent() != null && topLevelResourceRoots != null) {
for (ResourceRoot resourceRoot : topLevelResourceRoots) {
if (ModuleRootMarker.isModuleRoot(resourceRoot) && !SubDeploymentMarker.isSubDeployment(resourceRoot)) {
earLibJars.add(resourceRoot.getRoot());
}
}
}
for (ResourceRoot resourceRoot : resourceRoots) {
if (SubDeploymentMarker.isSubDeployment(resourceRoot) && resourceRoot != deploymentRoot) {
continue;
}
// if this resource root represents an additional module then we need
// to add the class path entry to the additional module
final Attachable target;
if (additionalModules.containsKey(resourceRoot.getRoot())) {
target = additionalModules.get(resourceRoot.getRoot());
} else {
target = deploymentUnit;
}
final String[] items = getClassPathEntries(resourceRoot);
for (String item : items) {
//first try and resolve relative to the manifest resource root
boolean found = false;
final VirtualFile classPathFile = resourceRoot.getRoot().getParent().getChild(item);
final VirtualFile topLevelClassPathFile = deploymentRoot.getRoot().getParent().getChild(item);
if (isInside(classPathFile, topLevelRoot)) {
if (earLibJars.contains(classPathFile)) {
log.debugf("Class-Path entry %s in %s ignored, as target is in or referenced by /lib", classPathFile,
resourceRoot.getRoot());
} else if (additionalModules.containsKey(classPathFile)) {
target.addToAttachmentList(Attachments.CLASS_PATH_ENTRIES, additionalModules.get(classPathFile)
.getModuleIdentifier());
} else if (subDeploymentModules.containsKey(classPathFile)) {
target.addToAttachmentList(Attachments.CLASS_PATH_ENTRIES, subDeploymentModules.get(classPathFile));
} else if (additionalModules.containsKey(topLevelClassPathFile)) {
//if not found try resolving the class path entry from the deployment root
target.addToAttachmentList(Attachments.CLASS_PATH_ENTRIES, additionalModules.get(topLevelClassPathFile)
.getModuleIdentifier());
} else if (subDeploymentModules.containsKey(topLevelClassPathFile)) {
//if not found try resolving the class path entry from the deployment root
target.addToAttachmentList(Attachments.CLASS_PATH_ENTRIES, subDeploymentModules.get(topLevelClassPathFile));
} else if(classPathFile.exists() && classPathFile.isDirectory()) {
} else if(topLevelClassPathFile.exists() && topLevelClassPathFile.isDirectory()) {
} else {
log.warn("Class Path entry " + item + " in " + resourceRoot.getRoot() + " does not point to a valid jar for a Class-Path reference.");
}
} else if (item.startsWith("/")) {
ModuleIdentifier moduleIdentifier = externalModuleService.addExternalModule(item);
target.addToAttachmentList(Attachments.CLASS_PATH_ENTRIES, moduleIdentifier);
log.debugf("Resource %s added as external jar %s", classPathFile, resourceRoot.getRoot());
} else {
//ignore
log.debugf("Ignoring missing Class-Path entry %s", classPathFile);