org.apache.ivy.osgi.repo.AbstractOSGiResolver Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of driver-cql-shaded Show documentation
Show all versions of driver-cql-shaded Show documentation
A Shaded CQL ActivityType driver for http://nosqlbench.io/
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/
package org.apache.ivy.osgi.repo;
import java.io.File;
import java.io.IOException;
import java.net.URL;
import java.text.ParseException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.Date;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;
import org.apache.ivy.core.IvyPatternHelper;
import org.apache.ivy.core.module.descriptor.Artifact;
import org.apache.ivy.core.module.descriptor.Configuration;
import org.apache.ivy.core.module.descriptor.DefaultDependencyDescriptor;
import org.apache.ivy.core.module.descriptor.DefaultModuleDescriptor;
import org.apache.ivy.core.module.descriptor.DependencyDescriptor;
import org.apache.ivy.core.module.descriptor.ModuleDescriptor;
import org.apache.ivy.core.module.id.ModuleRevisionId;
import org.apache.ivy.core.report.DownloadStatus;
import org.apache.ivy.core.report.MetadataArtifactDownloadReport;
import org.apache.ivy.core.resolve.IvyNode;
import org.apache.ivy.core.resolve.ResolveData;
import org.apache.ivy.core.resolve.ResolvedModuleRevision;
import org.apache.ivy.osgi.core.BundleInfo;
import org.apache.ivy.osgi.core.BundleInfoAdapter;
import org.apache.ivy.osgi.util.Version;
import org.apache.ivy.plugins.repository.Resource;
import org.apache.ivy.plugins.repository.url.URLRepository;
import org.apache.ivy.plugins.repository.url.URLResource;
import org.apache.ivy.plugins.resolver.BasicResolver;
import org.apache.ivy.plugins.resolver.util.MDResolvedResource;
import org.apache.ivy.plugins.resolver.util.ResolvedResource;
import org.apache.ivy.plugins.resolver.util.ResourceMDParser;
import org.apache.ivy.util.Message;
public abstract class AbstractOSGiResolver extends BasicResolver {
private static final String CAPABILITY_EXTRA_ATTR = "osgi_bundle";
private RepoDescriptor repoDescriptor = null;
private URLRepository repository = new URLRepository();
public static class RequirementStrategy {
// take the first matching
public static RequirementStrategy first = new RequirementStrategy();
// if there are any ambiguity, fail to resolve
public static RequirementStrategy noambiguity = new RequirementStrategy();
public static RequirementStrategy valueOf(String strategy) {
if (strategy.equals("first")) {
return first;
}
if (strategy.equals("noambiguity")) {
return noambiguity;
}
throw new IllegalStateException();
}
}
private RequirementStrategy requirementStrategy = RequirementStrategy.noambiguity;
public void setRequirementStrategy(RequirementStrategy importPackageStrategy) {
this.requirementStrategy = importPackageStrategy;
}
public void setRequirementStrategy(String strategy) {
setRequirementStrategy(RequirementStrategy.valueOf(strategy));
}
protected void setRepoDescriptor(RepoDescriptor repoDescriptor) {
this.repoDescriptor = repoDescriptor;
}
public URLRepository getRepository() {
return repository;
}
protected void ensureInit() {
if (repoDescriptor == null) {
init();
}
}
abstract protected void init();
private RepoDescriptor getRepoDescriptor() {
ensureInit();
return repoDescriptor;
}
public boolean isAllownomd() {
// this a repo based resolver, we always have md
return false;
}
public ResolvedResource findIvyFileRef(DependencyDescriptor dd, ResolveData data) {
ModuleRevisionId mrid = dd.getDependencyRevisionId();
String osgiType = mrid.getOrganisation();
if (osgiType == null) {
throw new RuntimeException("Unsupported OSGi module Id: " + mrid.getModuleId());
}
String id = mrid.getName();
Set/* */mds = getRepoDescriptor().findModule(osgiType, id);
if (mds == null || mds.isEmpty()) {
Message.verbose("\t " + id + " not found.");
return null;
}
ResolvedResource[] ret;
if (BundleInfo.BUNDLE_TYPE.equals(osgiType)) {
ret = findBundle(dd, data, mds);
} else {
ret = findCapability(dd, data, mds);
}
ResolvedResource found = findResource(ret, getDefaultRMDParser(dd.getDependencyId()), mrid,
data.getDate());
if (found == null) {
Message.debug("\t" + getName() + ": no resource found for " + mrid);
}
return found;
}
public ResolvedResource[] findBundle(DependencyDescriptor dd, ResolveData data, Set/*
* <
* ModuleDescriptor
* >
*/mds) {
ResolvedResource[] ret = new ResolvedResource[mds.size()];
int i = 0;
Iterator itMd = mds.iterator();
while (itMd.hasNext()) {
ModuleDescriptor md = (ModuleDescriptor) itMd.next();
MetadataArtifactDownloadReport report = new MetadataArtifactDownloadReport(null);
report.setDownloadStatus(DownloadStatus.NO);
report.setSearched(true);
ResolvedModuleRevision rmr = new ResolvedModuleRevision(this, this, md, report);
MDResolvedResource mdrr = new MDResolvedResource(null, md.getRevision(), rmr);
ret[i++] = mdrr;
}
return ret;
}
public ResolvedResource[] findCapability(DependencyDescriptor dd, ResolveData data, Set/*
* <
* ModuleDescriptor
* >
*/mds) {
ResolvedResource[] ret = new ResolvedResource[mds.size()];
int i = 0;
Iterator itMd = mds.iterator();
while (itMd.hasNext()) {
ModuleDescriptor md = (ModuleDescriptor) itMd.next();
IvyNode node = data.getNode(md.getModuleRevisionId());
if (node != null && node.getDescriptor() != null) {
// already resolved import, no need to go further
return new ResolvedResource[] {buildResolvedCapabilityMd(dd, node.getDescriptor())};
}
ret[i++] = buildResolvedCapabilityMd(dd, md);
}
return ret;
}
private MDResolvedResource buildResolvedCapabilityMd(DependencyDescriptor dd,
ModuleDescriptor md) {
String org = dd.getDependencyRevisionId().getOrganisation();
String name = dd.getDependencyRevisionId().getName();
String rev = (String) md.getExtraInfo().get(
BundleInfoAdapter.EXTRA_INFO_EXPORT_PREFIX + name);
ModuleRevisionId capabilityRev = ModuleRevisionId.newInstance(org, name, rev,
Collections.singletonMap(CAPABILITY_EXTRA_ATTR, md.getModuleRevisionId().toString()));
DefaultModuleDescriptor capabilityMd = new DefaultModuleDescriptor(capabilityRev,
"release", new Date());
String useConf = BundleInfoAdapter.CONF_USE_PREFIX + dd.getDependencyRevisionId().getName();
capabilityMd.addConfiguration(BundleInfoAdapter.CONF_DEFAULT);
capabilityMd.addConfiguration(BundleInfoAdapter.CONF_OPTIONAL);
capabilityMd.addConfiguration(BundleInfoAdapter.CONF_TRANSITIVE_OPTIONAL);
capabilityMd.addConfiguration(new Configuration(useConf));
DefaultDependencyDescriptor capabilityDD = new DefaultDependencyDescriptor(
md.getModuleRevisionId(), false);
capabilityDD.addDependencyConfiguration(BundleInfoAdapter.CONF_NAME_DEFAULT,
BundleInfoAdapter.CONF_NAME_DEFAULT);
capabilityDD.addDependencyConfiguration(BundleInfoAdapter.CONF_NAME_OPTIONAL,
BundleInfoAdapter.CONF_NAME_OPTIONAL);
capabilityDD.addDependencyConfiguration(BundleInfoAdapter.CONF_NAME_TRANSITIVE_OPTIONAL,
BundleInfoAdapter.CONF_NAME_TRANSITIVE_OPTIONAL);
capabilityDD.addDependencyConfiguration(useConf, useConf);
capabilityMd.addDependency(capabilityDD);
MetadataArtifactDownloadReport report = new MetadataArtifactDownloadReport(null);
report.setDownloadStatus(DownloadStatus.NO);
report.setSearched(true);
ResolvedModuleRevision rmr = new ResolvedModuleRevision(this, this, capabilityMd, report);
return new MDResolvedResource(null, capabilityMd.getRevision(), rmr);
}
public ResolvedResource findResource(ResolvedResource[] rress, ResourceMDParser rmdparser,
ModuleRevisionId mrid, Date date) {
ResolvedResource found = super.findResource(rress, rmdparser, mrid, date);
String osgiType = mrid.getOrganisation();
// for non bundle requirement : log the selected bundle
if (!BundleInfo.BUNDLE_TYPE.equals(osgiType)) {
// several candidates with different symbolic name : make an warning about the ambiguity
if (rress.length != 1) {
// several candidates with different symbolic name ?
Map/* > */matching = new HashMap();
for (int i = 0; i < rress.length; i++) {
String name = ((MDResolvedResource) rress[i]).getResolvedModuleRevision()
.getDescriptor().getExtraAttribute(CAPABILITY_EXTRA_ATTR);
List/* */list = (List) matching.get(name);
if (list == null) {
list = new ArrayList/* */();
matching.put(name, list);
}
list.add(rress[i]);
}
if (matching.keySet().size() != 1) {
if (requirementStrategy == RequirementStrategy.first) {
Message.warn("Ambiguity for the '" + osgiType + "' requirement "
+ mrid.getName() + ";version=" + mrid.getRevision());
Iterator itMatching = matching.entrySet().iterator();
while (itMatching.hasNext()) {
Entry/* > */entry = (Entry) itMatching
.next();
Message.warn("\t" + entry.getKey());
Iterator itB = ((List) entry.getValue()).iterator();
while (itB.hasNext()) {
MDResolvedResource c = (MDResolvedResource) itB.next();
Message.warn("\t\t" + c.getRevision()
+ (found == c ? " (selected)" : ""));
}
}
} else if (requirementStrategy == RequirementStrategy.noambiguity) {
Message.error("Ambiguity for the '" + osgiType + "' requirement "
+ mrid.getName() + ";version=" + mrid.getRevision());
Iterator itMatching = matching.entrySet().iterator();
while (itMatching.hasNext()) {
Entry/* > */entry = (Entry) itMatching
.next();
Message.error("\t" + entry.getKey());
Iterator itB = ((List) entry.getValue()).iterator();
while (itB.hasNext()) {
MDResolvedResource c = (MDResolvedResource) itB.next();
Message.error("\t\t" + c.getRevision()
+ (found == c ? " (best match)" : ""));
}
}
return null;
}
}
}
Message.info("'" + osgiType + "' requirement " + mrid.getName() + ";version="
+ mrid.getRevision() + " satisfied by "
+ ((MDResolvedResource) found).getResolvedModuleRevision().getId().getName()
+ ";" + found.getRevision());
}
return found;
}
public ResolvedResource findArtifactRef(Artifact artifact, Date date) {
URL url = artifact.getUrl();
if (url == null) {
// not an artifact resolved by this resolver
return null;
}
Message.verbose("\tusing url for " + artifact + ": " + url);
logArtifactAttempt(artifact, url.toExternalForm());
Resource resource = new URLResource(url);
return new ResolvedResource(resource, artifact.getModuleRevisionId().getRevision());
}
protected void checkModuleDescriptorRevision(ModuleDescriptor systemMd,
ModuleRevisionId systemMrid) {
String osgiType = systemMrid.getOrganisation();
// only check revision if we're searching for a bundle (package and bundle have different
// version
if (osgiType == null || osgiType.equals(BundleInfo.BUNDLE_TYPE)) {
super.checkModuleDescriptorRevision(systemMd, systemMrid);
}
}
protected Collection/* */filterNames(Collection/* */names) {
getSettings().filterIgnore(names);
return names;
}
protected Collection findNames(Map tokenValues, String token) {
if (IvyPatternHelper.ORGANISATION_KEY.equals(token)) {
return getRepoDescriptor().getModuleByCapbilities().keySet();
}
String osgiType = (String) tokenValues.get(IvyPatternHelper.ORGANISATION_KEY);
if (osgiType == null || osgiType.length() == 0) {
return Collections.EMPTY_LIST;
}
String rev = (String) tokenValues.get(IvyPatternHelper.REVISION_KEY);
if (IvyPatternHelper.MODULE_KEY.equals(token)) {
Map/* >> */moduleByCapbilities = getRepoDescriptor()
.getModuleByCapbilities();
if (osgiType != null) {
Map/* > */moduleByCapabilityValue = (Map) moduleByCapbilities
.get(osgiType);
if (moduleByCapabilityValue == null) {
return Collections.EMPTY_LIST;
}
Set/* */capabilityValues = new HashSet();
return moduleByCapabilityValue.keySet();
} else {
Set/* */capabilityValues = new HashSet();
Iterator/*
© 2015 - 2025 Weber Informatics LLC | Privacy Policy