public ResolvedModuleRevision getDependency()

in org.apache.ivyde.eclipse/src/java/org/apache/ivyde/internal/eclipse/workspaceresolver/WorkspaceResolver.java [176:338]


    public ResolvedModuleRevision getDependency(DependencyDescriptor dd, ResolveData data)
            throws ParseException {
        IvyContext context = IvyContext.getContext();

        String contextId = "ivyde.workspaceresolver." + getName() + "."
                + dd.getDependencyRevisionId().toString();
        DependencyDescriptor parentDD = context.get(contextId);
        if (parentDD != null
                && parentDD.getDependencyRevisionId().equals(dd.getDependencyRevisionId())) {
            // this very workspace resolver has been already called for the same dependency
            // we are going into a circular dependency, let's return 'not found'
            return null;
        }
        context.set(contextId, dd);

        ModuleRevisionId dependencyMrid = dd.getDependencyRevisionId();
        String org = dependencyMrid.getModuleId().getOrganisation();
        String module = dependencyMrid.getModuleId().getName();

        VersionMatcher versionMatcher = getSettings().getVersionMatcher();

        // Iterate over workspace to find Java project which has an Ivy
        // container for this dependency
        for (IProject p : projects) {
            if (!p.exists()) {
                continue;
            }
            for (IvyClasspathContainer container : IvyClasspathContainerHelper.getContainers(p)) {
                ModuleDescriptor md = ((IvyClasspathContainerImpl) container).getState().getCachedModuleDescriptor();
                if (md == null) {
                    continue;
                }
                ModuleRevisionId candidateMrid = md.getModuleRevisionId();

                // search a match on the organization and the module name

                if (osgiResolveInWorkspaceAvailable && org.equals(BundleInfo.BUNDLE_TYPE)) {
                    // looking for an OSGi bundle via its symbolic name
                    if (!module.equals(md.getExtraInfoContentByTagName("Bundle-SymbolicName"))) {
                        // not found, skip to next
                        continue;
                    }
                } else if (osgiResolveInWorkspaceAvailable && org.equals(BundleInfo.PACKAGE_TYPE)) {
                    // looking for an OSGi bundle via its exported package
                    String exportedPackages = md.getExtraInfoContentByTagName("Export-Package");
                    if (exportedPackages == null) {
                        // not found, skip to next
                        continue;
                    }
                    boolean found = false;
                    String version = null;
                    ManifestHeaderValue exportElements = new ManifestHeaderValue(exportedPackages);
                    for (ManifestHeaderElement exportElement : exportElements.getElements()) {
                        if (exportElement.getValues().contains(module)) {
                            found = true;
                            version = exportElement.getAttributes().get("version");
                            break;
                        }
                    }
                    if (!found) {
                        // not found, skip to next
                        continue;
                    }
                    if (version == null) {
                        // no version means anything can match. Let's trick the version matcher by
                        // setting the exact expected version
                        version = dependencyMrid.getRevision();
                    }
                    md.setResolvedModuleRevisionId(ModuleRevisionId.newInstance(org, module,
                        version));
                } else {
                    if (!candidateMrid.getModuleId().equals(dependencyMrid.getModuleId())) {
                        // it doesn't match org#module, skip to next
                        continue;
                    }
                }

                IvyDEMessage.verbose("Workspace resolver found potential matching project "
                        + p.getName() + " with module " + candidateMrid + " for module "
                        + dependencyMrid);

                if (!ignoreBranchOnWorkspaceProjects) {
                    ModuleId mid = dependencyMrid.getModuleId();
                    String defaultBranch = getSettings().getDefaultBranch(mid);
                    String dependencyBranch = dependencyMrid.getBranch();
                    String candidateBranch = candidateMrid.getBranch();
                    if (dependencyBranch == null) {
                        dependencyBranch = defaultBranch;
                    }
                    if (candidateBranch == null) {
                        candidateBranch = defaultBranch;
                    }
                    if (dependencyBranch != candidateBranch) {
                        // Both cannot be null
                        if (dependencyBranch == null || candidateBranch == null) {
                            IvyDEMessage
                                    .verbose("\t\trejected since branches doesn't match (one is set, the other isn't)");
                            continue;
                        }
                        if (!dependencyBranch.equals(candidateBranch)) {
                            IvyDEMessage.verbose("\t\trejected since branches doesn't match");
                            continue;
                        }
                    }
                }

                // Found one; check if it is for the module we need
                if (ignoreVersionOnWorkspaceProjects
                        || md.getModuleRevisionId().getRevision().equals(Ivy.getWorkingRevision())
                        || versionMatcher.accept(dd.getDependencyRevisionId(), md)) {

                    if (ignoreVersionOnWorkspaceProjects) {
                        IvyDEMessage.verbose("\t\tmatched (version are ignored)");
                    } else {
                        IvyDEMessage.verbose("\t\tversion matched");
                    }

                    Artifact af = new DefaultArtifact(md.getModuleRevisionId(),
                            md.getPublicationDate(), p.getFullPath().toString(),
                            ECLIPSE_PROJECT_TYPE, ECLIPSE_PROJECT_EXTENSION);

                    DependencyArtifactDescriptor[] dArtifacts = dd.getAllDependencyArtifacts();
                    if (dArtifacts != null) {
                        // the dependency is declaring explicitly some artifacts to download
                        // we need to trick to and map these requested artifact by the Eclipse
                        // project

                        // we need the context which is used when downloading data, which is the
                        // parent of the current one so let's hack: popContext (the child),
                        // getContext (the parent), setVar, pushContext (child)
                        IvyContext currentContext = IvyContext.popContext();
                        IvyContext parentContext = IvyContext.getContext();
                        Map<Artifact, Artifact> workspaceArtifacts = parentContext.get(IVYDE_WORKSPACE_ARTIFACTS);
                        if (workspaceArtifacts == null) {
                            workspaceArtifacts = new HashMap<>();
                            parentContext.set(IVYDE_WORKSPACE_ARTIFACTS, workspaceArtifacts);
                        }
                        for (DependencyArtifactDescriptor dArtifact : dArtifacts) {
                            Artifact artifact = new MDArtifact(md, dArtifact.getName(),
                                    dArtifact.getType(), dArtifact.getExt(),
                                    dArtifact.getUrl(),
                                    dArtifact.getQualifiedExtraAttributes());
                            workspaceArtifacts.put(artifact, af);
                        }
                        IvyContext.pushContext(currentContext);
                    }

                    DefaultModuleDescriptor workspaceMd = cloneMd(md, af);

                    MetadataArtifactDownloadReport madr = new MetadataArtifactDownloadReport(af);
                    madr.setDownloadStatus(DownloadStatus.SUCCESSFUL);
                    madr.setSearched(true);

                    return new ResolvedModuleRevision(this, this, workspaceMd, madr);
                } else {
                    IvyDEMessage.verbose("\t\treject as version didn't match");
                }
            }
        }

        // Didn't find module in any open project, proceed to other resolvers.
        return null;
    }