Search in sources :

Example 51 with Requirement

use of org.osgi.resource.Requirement in project bnd by bndtools.

the class ProjectResolver method getResource.

public IdentityCapability getResource(String bsn, String version) {
    Requirement requirement = CapReqBuilder.createBundleRequirement(bsn, version).buildSyntheticRequirement();
    List<Capability> result = getContext().findProviders(requirement);
    if (result == null || result.isEmpty())
        return null;
    return ResourceUtils.getIdentityCapability(result.get(0).getResource());
}
Also used : Requirement(org.osgi.resource.Requirement) IdentityCapability(aQute.bnd.osgi.resource.ResourceUtils.IdentityCapability) Capability(org.osgi.resource.Capability)

Example 52 with Requirement

use of org.osgi.resource.Requirement in project bndtools by bndtools.

the class PackageDropAdapter method performDrop.

@Override
public boolean performDrop(Object data) {
    int insertionIndex = -1;
    Object target = getCurrentTarget();
    if (target != null) {
        insertionIndex = indexOf(target);
        int loc = getCurrentLocation();
        if (loc == LOCATION_ON || loc == LOCATION_AFTER)
            insertionIndex++;
    }
    List<T> newEntries = new ArrayList<T>();
    if (data instanceof IResource[]) {
        for (IResource resource : (IResource[]) data) {
            IJavaElement javaElement = JavaCore.create(resource);
            if (javaElement instanceof IPackageFragment) {
                newEntries.add(createNewEntry(javaElement.getElementName()));
            }
        }
    } else if (data instanceof IStructuredSelection) {
        Iterator<?> iterator = ((IStructuredSelection) data).iterator();
        while (iterator.hasNext()) {
            Object element = iterator.next();
            if (element instanceof IPackageFragment) {
                IPackageFragment pkg = (IPackageFragment) element;
                newEntries.add(createNewEntry(pkg.getElementName()));
            } else if (element instanceof Capability) {
                Capability cap = (Capability) element;
                String namespace = cap.getNamespace();
                if (PackageNamespace.PACKAGE_NAMESPACE.equals(namespace)) {
                    String pkgName = (String) cap.getAttributes().get(namespace);
                    newEntries.add(createNewEntry(pkgName));
                }
            } else if (element instanceof Requirement) {
                String pkgName = getPackageNameFromRequirement((Requirement) element);
                if (pkgName != null)
                    newEntries.add(createNewEntry(pkgName));
            } else if (element instanceof RequirementWrapper) {
                String pkgName = getPackageNameFromRequirement(((RequirementWrapper) element).requirement);
                if (pkgName != null)
                    newEntries.add(createNewEntry(pkgName));
            }
        }
    }
    addRows(insertionIndex, newEntries);
    return true;
}
Also used : IJavaElement(org.eclipse.jdt.core.IJavaElement) IPackageFragment(org.eclipse.jdt.core.IPackageFragment) Capability(org.osgi.resource.Capability) ArrayList(java.util.ArrayList) IStructuredSelection(org.eclipse.jface.viewers.IStructuredSelection) Requirement(org.osgi.resource.Requirement) Iterator(java.util.Iterator) RequirementWrapper(bndtools.model.resolution.RequirementWrapper) IResource(org.eclipse.core.resources.IResource)

Example 53 with Requirement

use of org.osgi.resource.Requirement in project bnd by bndtools.

the class ResourceTest method getResources.

private Set<Resource> getResources(String locations) throws MalformedURLException, URISyntaxException {
    FixedIndexedRepo repo = new FixedIndexedRepo();
    repo.setLocations(locations);
    Requirement wildcard = ResourceUtils.createWildcardRequirement();
    Collection<Capability> caps = repo.findProviders(Collections.singleton(wildcard)).get(wildcard);
    return ResourceUtils.getResources(caps);
}
Also used : Requirement(org.osgi.resource.Requirement) Capability(org.osgi.resource.Capability) FixedIndexedRepo(aQute.bnd.deployer.repository.FixedIndexedRepo)

Example 54 with Requirement

use of org.osgi.resource.Requirement in project bnd by bndtools.

the class JpmRepoTest method testSimpleResolve.

// public void testResolveProviderWithRunpath() throws Exception {
// try {
// Project provider = ws.getProject("provider");
// provider.build();
// assertTrue(provider.check());
//
// Project requirer = ws.getProject("requirer");
// requirer.build();
// assertTrue(requirer.check());
//
// BndEditModel model = new BndEditModel();
// model.setProject(requirer);
// BndrunResolveContext context = new BndrunResolveContext(model, ws, log);
//
// Resolver resolver = new ResolverImpl(new
// org.apache.felix.resolver.Logger(4), null);
//
// Map<Resource,List<Wire>> resolved = resolver.resolve(context);
// Set<Resource> resources = resolved.keySet();
// Resource resource = getResource(resources, "requirer", "0");
// assertNotNull(resource);
// }
// catch (ResolutionException e) {
// fail("Resolve failed " + e);
// }
// }
public void testSimpleResolve() {
    Repository repo = ws.getPlugin(Repository.class);
    BndEditModel model = new BndEditModel();
    model.setRunFw("org.apache.felix.framework");
    List<Requirement> requires = new ArrayList<Requirement>();
    CapReqBuilder capReq = CapReqBuilder.createBundleRequirement("org.apache.felix.gogo.shell", "[0,1)");
    requires.add(capReq.buildSyntheticRequirement());
    Map<Requirement, Collection<Capability>> shell = repo.findProviders(requires);
    assertNotNull(shell);
    assertEquals(1, shell.size());
    model.setRunRequires(requires);
    BndrunResolveContext context = new BndrunResolveContext(model, ws, log);
    Resolver resolver = new BndResolver(new org.apache.felix.resolver.Logger(4));
    try {
        Map<Resource, List<Wire>> resolved = resolver.resolve(context);
        Set<Resource> resources = resolved.keySet();
        Resource resource = getResource(resources, "org.apache.felix.gogo.runtime", "0.12");
        assertNotNull(resource);
    } catch (ResolutionException e) {
        fail("Resolve failed");
    }
}
Also used : CapReqBuilder(aQute.bnd.osgi.resource.CapReqBuilder) BndResolver(biz.aQute.resolve.BndResolver) Resolver(org.osgi.service.resolver.Resolver) ArrayList(java.util.ArrayList) Resource(org.osgi.resource.Resource) ResolutionException(org.osgi.service.resolver.ResolutionException) Requirement(org.osgi.resource.Requirement) InfoRepository(aQute.bnd.service.repository.InfoRepository) Repository(org.osgi.service.repository.Repository) BndrunResolveContext(biz.aQute.resolve.BndrunResolveContext) Collection(java.util.Collection) ArrayList(java.util.ArrayList) List(java.util.List) BndResolver(biz.aQute.resolve.BndResolver) BndEditModel(aQute.bnd.build.model.BndEditModel)

Example 55 with Requirement

use of org.osgi.resource.Requirement in project bndtools by bndtools.

the class RepositoriesView method createActions.

void createActions() {
    collapseAllAction = new Action() {

        @Override
        public void run() {
            viewer.collapseAll();
        }
    };
    collapseAllAction.setEnabled(false);
    collapseAllAction.setText("Collapse All");
    collapseAllAction.setToolTipText("Collapse All");
    collapseAllAction.setImageDescriptor(Icons.desc("collapse"));
    collapseAllAction.setDisabledImageDescriptor(Icons.desc("collapse.disabled"));
    refreshAction = new Action() {

        @Override
        public void run() {
            new WorkspaceJob("Refresh repositories") {

                @Override
                public IStatus runInWorkspace(IProgressMonitor monitor) throws CoreException {
                    if (monitor == null)
                        monitor = new NullProgressMonitor();
                    monitor.subTask("Refresh all repositories");
                    try {
                        refreshAction.setEnabled(false);
                        Central.refreshPlugins();
                    } catch (Exception e) {
                        return new Status(Status.ERROR, Plugin.PLUGIN_ID, "Failed to refresh plugins", e);
                    } finally {
                        refreshAction.setEnabled(true);
                    }
                    return Status.OK_STATUS;
                }
            }.schedule();
        }
    };
    refreshAction.setEnabled(false);
    refreshAction.setText("Refresh");
    refreshAction.setToolTipText("Refresh Repositories Tree");
    refreshAction.setImageDescriptor(Icons.desc("refresh"));
    refreshAction.setDisabledImageDescriptor(Icons.desc("refresh.disabled"));
    addBundlesAction = new Action() {

        @Override
        public void run() {
            IStructuredSelection selection = (IStructuredSelection) viewer.getSelection();
            Object element = selection.getFirstElement();
            if (element != null && element instanceof RepositoryPlugin) {
                RepositoryPlugin repo = (RepositoryPlugin) element;
                if (repo.canWrite()) {
                    AddFilesToRepositoryWizard wizard = new AddFilesToRepositoryWizard(repo, new File[0]);
                    WizardDialog dialog = new WizardDialog(getViewSite().getShell(), wizard);
                    dialog.open();
                    viewer.refresh(repo);
                }
            }
        }
    };
    addBundlesAction.setEnabled(false);
    addBundlesAction.setText("Add");
    addBundlesAction.setToolTipText("Add Bundles to Repository");
    addBundlesAction.setImageDescriptor(Icons.desc("add"));
    addBundlesAction.setDisabledImageDescriptor(Icons.desc("add.disabled"));
    advancedSearchAction = new Action("Advanced Search", Action.AS_CHECK_BOX) {

        @Override
        public void run() {
            if (advancedSearchAction.isChecked()) {
                AdvancedSearchDialog dialog = new AdvancedSearchDialog(getSite().getShell());
                if (advancedSearchState != null) {
                    try {
                        XMLMemento memento = XMLMemento.createReadRoot(new StringReader(advancedSearchState));
                        dialog.restoreState(memento);
                    } catch (Exception e) {
                        logger.logError("Failed to load dialog state", e);
                    }
                }
                if (Window.OK == dialog.open()) {
                    Requirement req = dialog.getRequirement();
                    contentProvider.setRequirementFilter(req);
                    SWTUtil.recurseEnable(false, filterPanel);
                    viewer.refresh();
                    viewer.expandToLevel(2);
                } else {
                    advancedSearchAction.setChecked(false);
                }
                try {
                    XMLMemento memento = XMLMemento.createWriteRoot("search");
                    dialog.saveState(memento);
                    StringWriter writer = new StringWriter();
                    memento.save(writer);
                    advancedSearchState = writer.toString();
                } catch (Exception e) {
                    logger.logError("Failed to save dialog state", e);
                }
            } else {
                contentProvider.setRequirementFilter(null);
                SWTUtil.recurseEnable(true, filterPanel);
                viewer.refresh();
            }
        }
    };
    advancedSearchAction.setEnabled(false);
    advancedSearchAction.setText("Advanced Search");
    advancedSearchAction.setToolTipText("Toggle Advanced Search");
    advancedSearchAction.setImageDescriptor(Icons.desc("search"));
    advancedSearchAction.setDisabledImageDescriptor(Icons.desc("search.disabled"));
    downloadAction = new Action() {

        @Override
        public void run() {
            IStructuredSelection selection = (IStructuredSelection) viewer.getSelection();
            // The set of Repos included in the selection; they will be completely downloaded.
            Set<RemoteRepositoryPlugin> repos = new IdentityHashSet<>(selectionByType(selection, RemoteRepositoryPlugin.class));
            // The set of Bundles included in the selection.
            Set<RepositoryBundle> bundles = new IdentityHashSet<RepositoryBundle>();
            for (RepositoryBundle bundle : selectionByType(selection, RepositoryBundle.class)) {
                // filter out bundles that come from already-selected repos.
                if (!repos.contains(bundle.getRepo()))
                    bundles.add(bundle);
            }
            // The set of Bundle Versions included in the selection
            Set<RepositoryBundleVersion> bundleVersions = new IdentityHashSet<RepositoryBundleVersion>();
            for (RepositoryBundleVersion bundleVersion : selectionByType(selection, RepositoryBundleVersion.class)) {
                // filter out bundles that come from already-selected repos.
                if (!repos.contains(bundleVersion.getRepo()))
                    bundleVersions.add(bundleVersion);
            }
            RepoDownloadJob downloadJob = new RepoDownloadJob(repos, bundles, bundleVersions);
            downloadJob.schedule();
        }

        private <T> List<T> selectionByType(IStructuredSelection selection, Class<T> type) {
            List<T> result = new ArrayList<T>(selection.size());
            @SuppressWarnings("unchecked") Iterator<Object> iterator = selection.iterator();
            while (iterator.hasNext()) {
                Object item = iterator.next();
                if (type.isInstance(item)) {
                    @SuppressWarnings("unchecked") T cast = (T) item;
                    result.add(cast);
                }
            }
            return result;
        }
    };
    downloadAction.setEnabled(false);
    downloadAction.setText("Download Repository Content");
    downloadAction.setImageDescriptor(Icons.desc("download"));
    downloadAction.setDisabledImageDescriptor(Icons.desc("download.disabled"));
    offlineAction = new Action("Online/Offline Mode", Action.AS_CHECK_BOX) {

        @Override
        public void run() {
            Workspace workspace = Central.getWorkspaceIfPresent();
            if (workspace != null) {
                prefs.setWorkspaceOffline(offlineAction.isChecked());
            }
        }
    };
    offlineAction.setEnabled(false);
    offlineAction.setToolTipText("Go Offline");
    offlineAction.setImageDescriptor(Icons.desc("connected"));
    offlineAction.setDisabledImageDescriptor(Icons.desc("connected.disabled"));
    viewer.addSelectionChangedListener(new ISelectionChangedListener() {

        @Override
        public void selectionChanged(SelectionChangedEvent event) {
            IStructuredSelection selection = (IStructuredSelection) event.getSelection();
            boolean enable = false;
            @SuppressWarnings("unchecked") List<Object> list = selection.toList();
            for (Object item : list) {
                if (item instanceof RemoteRepositoryPlugin) {
                    enable = true;
                    break;
                } else if (item instanceof RepositoryEntry) {
                    if (!((RepositoryEntry) item).isLocal()) {
                        enable = true;
                        break;
                    }
                }
            }
            downloadAction.setEnabled(enable);
        }
    });
}
Also used : RepositoryBundle(bndtools.model.repo.RepositoryBundle) NullProgressMonitor(org.eclipse.core.runtime.NullProgressMonitor) Action(org.eclipse.jface.action.Action) RepositoryBundleVersion(bndtools.model.repo.RepositoryBundleVersion) Set(java.util.Set) IdentityHashSet(org.bndtools.utils.collections.IdentityHashSet) AddFilesToRepositoryWizard(bndtools.wizards.workspace.AddFilesToRepositoryWizard) RemoteRepositoryPlugin(aQute.bnd.service.RemoteRepositoryPlugin) RepositoryPlugin(aQute.bnd.service.RepositoryPlugin) SelectionChangedEvent(org.eclipse.jface.viewers.SelectionChangedEvent) IStructuredSelection(org.eclipse.jface.viewers.IStructuredSelection) RepositoryEntry(bndtools.model.repo.RepositoryEntry) StringWriter(java.io.StringWriter) SWT(org.eclipse.swt.SWT) StringReader(java.io.StringReader) Iterator(java.util.Iterator) ArrayList(java.util.ArrayList) List(java.util.List) Status(org.eclipse.core.runtime.Status) IStatus(org.eclipse.core.runtime.IStatus) ISelectionChangedListener(org.eclipse.jface.viewers.ISelectionChangedListener) WorkspaceJob(org.eclipse.core.resources.WorkspaceJob) RemoteRepositoryPlugin(aQute.bnd.service.RemoteRepositoryPlugin) CoreException(org.eclipse.core.runtime.CoreException) PartInitException(org.eclipse.ui.PartInitException) IOException(java.io.IOException) Requirement(org.osgi.resource.Requirement) IProgressMonitor(org.eclipse.core.runtime.IProgressMonitor) XMLMemento(org.eclipse.ui.XMLMemento) IFile(org.eclipse.core.resources.IFile) File(java.io.File) JarFile(java.util.jar.JarFile) WizardDialog(org.eclipse.jface.wizard.WizardDialog) Workspace(aQute.bnd.build.Workspace)

Aggregations

Requirement (org.osgi.resource.Requirement)201 Capability (org.osgi.resource.Capability)103 Resource (org.osgi.resource.Resource)59 CapReqBuilder (aQute.bnd.osgi.resource.CapReqBuilder)53 ArrayList (java.util.ArrayList)51 Collection (java.util.Collection)44 BndEditModel (aQute.bnd.build.model.BndEditModel)35 HashMap (java.util.HashMap)35 IdentityCapability (aQute.bnd.osgi.resource.ResourceUtils.IdentityCapability)34 MockRegistry (test.lib.MockRegistry)32 List (java.util.List)19 Repository (org.osgi.service.repository.Repository)19 Test (org.junit.Test)18 File (java.io.File)16 Map (java.util.Map)12 Resolver (org.osgi.service.resolver.Resolver)11 LinkedHashSet (java.util.LinkedHashSet)10 LinkedList (java.util.LinkedList)10 Wire (org.osgi.resource.Wire)10 Parameters (aQute.bnd.header.Parameters)9