use of aQute.bnd.service.repository.SearchableRepository.ResourceDescriptor in project bnd by bndtools.
the class RepoCommand method _copy.
public void _copy(CopyOptions options) throws Exception {
List<String> args = options._arguments();
String srcName = args.remove(0);
String dstName = args.remove(0);
RepositoryPlugin source = workspace.getRepository(srcName);
RepositoryPlugin dest = workspace.getRepository(dstName);
if (source == null) {
bnd.error("No such source repository: %s, available repos %s", srcName, workspace.getRepositories());
}
if (dest == null) {
bnd.error("No such destination repository: %s, available repos %s", dstName, workspace.getRepositories());
} else if (!dest.canWrite())
bnd.error("Destination repository cannot write: %s", dest);
if (!bnd.isOk() || source == null || dest == null) {
return;
}
logger.debug("src = {} -> {}", srcName, source);
logger.debug("dst = {} -> {}", dstName, dest);
@SuppressWarnings("unused")
class Spec {
DownloadBlocker src;
DownloadBlocker dst;
String bsn;
Version version;
public byte[] digest;
}
List<Spec> sources = new ArrayList<Spec>();
for (String bsn : source.list(null)) {
for (Version version : source.versions(bsn)) {
logger.debug("src: {} {}", bsn, version);
Spec spec = new Spec();
spec.bsn = bsn;
spec.version = version;
spec.src = new DownloadBlocker(bnd);
File src = source.get(bsn, version, null, spec.src);
if (src == null) {
bnd.error("No such entry: %s-%s", bsn, version);
} else {
spec.dst = findMatchingVersion(dest, bsn, version);
sources.add(spec);
}
}
}
for (Spec spec : sources) {
String reason = spec.src.getReason();
if (reason != null) {
bnd.error("Failed to find %s because: %s", spec.src.getFile(), reason);
}
File src = spec.src.getFile();
if (!src.isFile()) {
bnd.error("Not a valid file %s", spec.src.getFile());
}
spec.digest = SHA1.digest(src).digest();
}
//
// See if we can prune the list by diffing
//
ResourceRepository resources = null;
if (dest instanceof ResourceRepository)
resources = (ResourceRepository) dest;
nextFile: for (Iterator<Spec> i = sources.iterator(); i.hasNext(); ) {
Spec spec = i.next();
if (resources != null) {
ResourceDescriptor rd = resources.getResourceDescriptor(spec.digest);
if (rd != null)
// Already exists
continue nextFile;
}
// TODO Diff
}
if (!bnd.isOk())
return;
for (Spec spec : sources) {
File src = spec.src.getFile();
if (!options.dry()) {
try (InputStream fin = IO.stream(src)) {
PutResult put = dest.put(fin, null);
if (put.digest != null) {
if (!Arrays.equals(spec.digest, put.digest)) {
bnd.error("Digest error in upload %s", src);
}
}
} catch (Exception e) {
bnd.exception(e, "Exception %s in upload %s", e, src);
}
}
}
for (String bsn : source.list(null)) {
for (Version version : source.versions(bsn)) {
System.out.println(bsn + ";version=" + version);
}
}
}
use of aQute.bnd.service.repository.SearchableRepository.ResourceDescriptor in project bnd by bndtools.
the class InfoRepositoryWrapper method init.
boolean init() {
try {
if (System.currentTimeMillis() < lastTime + 10000)
return true;
} finally {
lastTime = System.currentTimeMillis();
}
Set<String> errors = new LinkedHashSet<String>();
try {
//
// Get the current repo contents
//
Set<String> toBeDeleted = new HashSet<String>(persistent.keySet());
Map<String, DownloadBlocker> blockers = new HashMap<String, DownloadBlocker>();
for (InfoRepository repo : repos) {
Map<String, ResourceDescriptor> map = collectKeys(repo);
for (final Map.Entry<String, ResourceDescriptor> entry : map.entrySet()) {
final String id = entry.getKey();
toBeDeleted.remove(id);
if (persistent.containsKey(id))
continue;
final ResourceDescriptor rd = entry.getValue();
DownloadBlocker blocker = new DownloadBlocker(null) {
//
// We steal the thread of the downloader to index
//
@Override
public void success(File file) throws Exception {
IndexResult index = null;
try {
index = repoIndexer.indexFile(file);
ResourceBuilder rb = new ResourceBuilder();
for (org.osgi.service.indexer.Capability capability : index.capabilities) {
CapReqBuilder cb = new CapReqBuilder(capability.getNamespace());
cb.addAttributes(capability.getAttributes());
cb.addDirectives(capability.getDirectives());
rb.addCapability(cb.buildSyntheticCapability());
}
for (org.osgi.service.indexer.Requirement requirement : index.requirements) {
CapReqBuilder cb = new CapReqBuilder(requirement.getNamespace());
cb.addAttributes(requirement.getAttributes());
cb.addDirectives(requirement.getDirectives());
rb.addRequirement(cb.buildSyntheticRequirement());
}
Resource resource = rb.build();
PersistentResource pr = new PersistentResource(resource);
persistent.put(id, pr);
} finally {
super.success(file);
if (index != null) {
index.resource.close();
}
}
}
};
blockers.put(entry.getKey(), blocker);
repo.get(rd.bsn, rd.version, null, blocker);
}
}
for (Entry<String, DownloadBlocker> entry : blockers.entrySet()) {
String key = entry.getKey();
DownloadBlocker blocker = entry.getValue();
String reason = blocker.getReason();
if (reason != null) {
errors.add(key + ": " + reason);
}
}
persistent.keySet().removeAll(toBeDeleted);
} catch (Exception e) {
throw new RuntimeException(e);
}
if (!errors.isEmpty())
throw new IllegalStateException("Cannot index " + repos + " due to " + errors);
return true;
}
use of aQute.bnd.service.repository.SearchableRepository.ResourceDescriptor in project bndtools by bndtools.
the class RepositoryBundleSelectionPart method createSection.
void createSection(Section section, FormToolkit toolkit) {
// Toolbar buttons
ToolBar toolbar = new ToolBar(section, SWT.FLAT);
section.setTextClient(toolbar);
fillToolBar(toolbar);
Composite composite = toolkit.createComposite(section);
section.setClient(composite);
table = toolkit.createTable(composite, SWT.FULL_SELECTION | SWT.MULTI | SWT.BORDER | SWT.H_SCROLL);
viewer = new TableViewer(table);
viewer.setContentProvider(new ArrayContentProvider());
viewer.setLabelProvider(getLabelProvider());
// Listeners
viewer.addSelectionChangedListener(new ISelectionChangedListener() {
@Override
public void selectionChanged(SelectionChangedEvent event) {
ToolItem remove = getRemoveItemTool();
if (remove != null)
remove.setEnabled(isRemovable(event.getSelection()));
}
});
ViewerDropAdapter dropAdapter = new ViewerDropAdapter(viewer) {
@Override
public void dragEnter(DropTargetEvent event) {
super.dragEnter(event);
event.detail = DND.DROP_COPY;
}
@Override
public boolean validateDrop(Object target, int operation, TransferData transferType) {
if (FileTransfer.getInstance().isSupportedType(transferType))
return true;
if (ResourceTransfer.getInstance().isSupportedType(transferType))
return true;
if (URLTransfer.getInstance().isSupportedType(transferType))
return true;
ISelection selection = LocalSelectionTransfer.getTransfer().getSelection();
if (selection.isEmpty() || !(selection instanceof IStructuredSelection)) {
return false;
}
Iterator<?> iterator = ((IStructuredSelection) selection).iterator();
while (iterator.hasNext()) {
if (!selectionIsDroppable(iterator.next())) {
return false;
}
}
return true;
}
private boolean selectionIsDroppable(Object element) {
return element instanceof RepositoryBundle || element instanceof RepositoryBundleVersion || element instanceof ProjectBundle || element instanceof RepositoryResourceElement;
}
@Override
public boolean performDrop(Object data) {
TransferData transfer = getCurrentEvent().currentDataType;
if (URLTransfer.getInstance().isSupportedType(transfer)) {
String urlStr = (String) URLTransfer.getInstance().nativeToJava(transfer);
return handleURLDrop(urlStr);
} else if (data instanceof String[]) {
return handleFileNameDrop((String[]) data);
} else if (data instanceof IResource[]) {
return handleResourceDrop((IResource[]) data);
} else {
return handleSelectionDrop();
}
}
private boolean handleResourceDrop(IResource[] resources) {
File[] files = new File[resources.length];
for (int i = 0; i < resources.length; i++) {
files[i] = resources[i].getLocation().toFile();
}
return handleFileDrop(files);
}
private boolean handleFileNameDrop(String[] paths) {
File[] files = new File[paths.length];
for (int i = 0; i < paths.length; i++) {
files[i] = new File(paths[i]);
}
return handleFileDrop(files);
}
private boolean handleFileDrop(File[] files) {
if (files.length > 0) {
BndPreferences prefs = new BndPreferences();
boolean hideWarning = prefs.getHideWarningExternalFile();
if (!hideWarning) {
MessageDialogWithToggle dialog = MessageDialogWithToggle.openWarning(getSection().getShell(), "Add External Files", "External files cannot be directly added to a project, they must be added to a local repository first.", "Do not show this warning again.", false, null, null);
if (Window.CANCEL == dialog.getReturnCode())
return false;
if (dialog.getToggleState()) {
prefs.setHideWarningExternalFile(true);
}
}
AddFilesToRepositoryWizard wizard = new AddFilesToRepositoryWizard(null, files);
WizardDialog dialog = new WizardDialog(getSection().getShell(), wizard);
if (Window.OK == dialog.open()) {
List<Pair<String, String>> addingBundles = wizard.getSelectedBundles();
List<VersionedClause> addingClauses = new ArrayList<VersionedClause>(addingBundles.size());
for (Pair<String, String> addingBundle : addingBundles) {
Attrs attribs = new Attrs();
attribs.put(Constants.VERSION_ATTRIBUTE, addingBundle.getSecond());
addingClauses.add(new VersionedClause(addingBundle.getFirst(), attribs));
}
handleAdd(addingClauses);
}
return true;
}
return false;
}
private boolean handleSelectionDrop() {
ISelection selection = LocalSelectionTransfer.getTransfer().getSelection();
if (selection.isEmpty() || !(selection instanceof IStructuredSelection)) {
return false;
}
List<VersionedClause> adding = new LinkedList<VersionedClause>();
Iterator<?> iterator = ((IStructuredSelection) selection).iterator();
while (iterator.hasNext()) {
Object item = iterator.next();
if (item instanceof RepositoryBundle) {
VersionedClause newClause = RepositoryBundleUtils.convertRepoBundle((RepositoryBundle) item);
adding.add(newClause);
} else if (item instanceof RepositoryBundleVersion) {
RepositoryBundleVersion bundleVersion = (RepositoryBundleVersion) item;
VersionedClause newClause = RepositoryBundleUtils.convertRepoBundleVersion(bundleVersion, phase);
adding.add(newClause);
} else if (item instanceof RepositoryResourceElement) {
RepositoryResourceElement elt = (RepositoryResourceElement) item;
VersionedClause newClause = RepositoryBundleUtils.convertRepoBundleVersion(elt.getRepositoryBundleVersion(), phase);
adding.add(newClause);
}
}
handleAdd(adding);
return true;
}
private boolean handleURLDrop(String urlStr) {
try {
URI uri = new URI(sanitizeUrl(urlStr));
AddJpmDependenciesWizard wizard = new AddJpmDependenciesWizard(uri);
WizardDialog dialog = new WizardDialog(getSection().getShell(), wizard);
if (dialog.open() == Window.OK) {
Set<ResourceDescriptor> resources = wizard.getResult();
List<VersionedClause> newBundles = new ArrayList<VersionedClause>(resources.size());
for (ResourceDescriptor resource : resources) {
Attrs attrs = new Attrs();
attrs.put(Constants.VERSION_ATTRIBUTE, resource.version != null ? resource.version.toString() : Version.emptyVersion.toString());
VersionedClause clause = new VersionedClause(resource.bsn, attrs);
newBundles.add(clause);
}
handleAdd(newBundles);
return true;
}
return false;
} catch (URISyntaxException e) {
MessageDialog.openError(getSection().getShell(), "Error", "The dropped URL was invalid: " + urlStr);
return false;
}
}
private String sanitizeUrl(String urlStr) {
int newline = urlStr.indexOf('\n');
if (newline > -1)
return urlStr.substring(0, newline).trim();
return urlStr;
}
private void handleAdd(Collection<VersionedClause> newClauses) {
if (newClauses == null || newClauses.isEmpty())
return;
List<VersionedClause> toAdd = new LinkedList<VersionedClause>();
for (VersionedClause newClause : newClauses) {
boolean found = false;
for (ListIterator<VersionedClause> iter = bundles.listIterator(); iter.hasNext(); ) {
VersionedClause existing = iter.next();
if (newClause.getName().equals(existing.getName())) {
int index = iter.previousIndex();
iter.set(newClause);
viewer.replace(newClause, index);
found = true;
break;
}
}
if (!found)
toAdd.add(newClause);
}
bundles.addAll(toAdd);
viewer.add(toAdd.toArray());
markDirty();
}
};
dropAdapter.setFeedbackEnabled(false);
dropAdapter.setExpandEnabled(false);
viewer.addDropSupport(DND.DROP_COPY | DND.DROP_MOVE, new Transfer[] { LocalSelectionTransfer.getTransfer(), FileTransfer.getInstance(), ResourceTransfer.getInstance(), URLTransfer.getInstance() }, dropAdapter);
table.addKeyListener(new KeyAdapter() {
@Override
public void keyReleased(KeyEvent e) {
if (e.character == SWT.DEL) {
doRemove();
} else if (e.character == '+') {
doAdd();
}
}
});
// Layout
GridLayout layout = new GridLayout(1, false);
layout.horizontalSpacing = 0;
layout.verticalSpacing = 0;
layout.marginHeight = 0;
layout.marginWidth = 0;
composite.setLayout(layout);
GridData gd = new GridData(SWT.FILL, SWT.FILL, true, true);
gd.widthHint = 50;
gd.heightHint = getTableHeightHint();
table.setLayoutData(gd);
}
use of aQute.bnd.service.repository.SearchableRepository.ResourceDescriptor in project bnd by bndtools.
the class bnd method _bsn2url.
public void _bsn2url(Bsn2UrlOptions opts) throws Exception {
Project p = getProject(opts.project());
if (p == null) {
error("You need to be in a project or specify the project with -p/--project");
return;
}
MultiMap<String, Version> revisions = new MultiMap<String, Version>();
for (RepositoryPlugin repo : p.getPlugins(RepositoryPlugin.class)) {
if (!(repo instanceof InfoRepository))
continue;
for (String bsn : repo.list(null)) {
revisions.addAll(bsn, repo.versions(bsn));
}
}
for (List<Version> versions : revisions.values()) {
Collections.sort(versions, Collections.reverseOrder());
}
List<String> files = opts._arguments();
for (String f : files) {
try (BufferedReader r = IO.reader(getFile(f))) {
String line;
nextLine: while ((line = r.readLine()) != null) {
Matcher matcher = LINE_P.matcher(line);
if (!matcher.matches())
continue nextLine;
line = matcher.group(1);
Parameters bundles = new Parameters(line, this);
for (Map.Entry<String, Attrs> entry : bundles.entrySet()) {
String bsn = entry.getKey();
VersionRange range = new VersionRange(entry.getValue().getVersion());
List<Version> versions = revisions.get(bsn);
if (versions == null) {
error("No for versions for %s", bsn);
break nextLine;
}
for (Version version : versions) {
if (range.includes(version)) {
for (RepositoryPlugin repo : p.getPlugins(RepositoryPlugin.class)) {
if (!(repo instanceof InfoRepository))
continue;
InfoRepository rp = (InfoRepository) repo;
ResourceDescriptor descriptor = rp.getDescriptor(bsn, version);
if (descriptor == null) {
error("Found bundle, but no descriptor %s;version=%s", bsn, version);
return;
}
out.println(descriptor.url + " #" + descriptor.bsn + ";version=" + descriptor.version);
}
}
}
}
}
} catch (Exception e) {
error("failed to create url list from file %s : %s", f, e);
}
}
}
use of aQute.bnd.service.repository.SearchableRepository.ResourceDescriptor in project bnd by bndtools.
the class FileRepoTest method testIndex.
/**
* Test a repo with an index
*/
public void testIndex() throws Exception {
//
// Check if the index property works
// by verifying the diff between the
// testRepo and the indexed Repo
//
assertNull(testRepo.getResources());
assertNotNull(indexedRepo.getResources());
//
// Check that we can actually put a resource
//
PutResult put = indexedRepo.put(IO.getFile("jar/osgi.jar").toURI().toURL().openStream(), null);
assertNotNull(put);
// Can we get it?
ResourceDescriptor desc = indexedRepo.getDescriptor("osgi", new Version("4.0"));
assertNotNull(desc);
// Got the same file?
assertTrue(Arrays.equals(put.digest, desc.id));
//
// Check if the description was copied
//
assertEquals("OSGi Service Platform Release 4 Interfaces and Classes for use in compiling bundles.", desc.description);
//
// We must be able to access by its sha1
//
ResourceDescriptor resource = indexedRepo.getResource(put.digest);
assertTrue(Arrays.equals(resource.id, desc.id));
//
// Check if we now have a set of resources
//
SortedSet<ResourceDescriptor> resources = indexedRepo.getResources();
assertEquals(1, resources.size());
ResourceDescriptor rd = resources.iterator().next();
assertTrue(Arrays.equals(rd.id, put.digest));
//
// Check if the bsn brings us back
//
File file = indexedRepo.get(desc.bsn, desc.version, null);
assertNotNull(file);
assertTrue(Arrays.equals(put.digest, SHA1.digest(file).digest()));
byte[] digest = SHA256.digest(file).digest();
assertTrue(Arrays.equals(rd.sha256, digest));
//
// Delete and see if it is really gone
//
indexedRepo.delete(desc.bsn, desc.version);
resources = indexedRepo.getResources();
assertEquals(0, resources.size());
file = indexedRepo.get(desc.bsn, desc.version, null);
assertNull(file);
resource = indexedRepo.getResource(put.digest);
assertNull(resource);
}
Aggregations