Search in sources :

Example 31 with BndEditModel

use of aQute.bnd.build.model.BndEditModel in project bndtools by bndtools.

the class EnableSubBundlesOperation method run.

@Override
public void run(final IProgressMonitor monitor) throws CoreException {
    IResource container = workspace.getRoot().findMember(containerPath);
    if (container == null || !container.exists())
        throw newCoreException("Container path does not exist", null);
    // Create new project model
    BndEditModel newBundleModel;
    try {
        newBundleModel = new BndEditModel(Central.getWorkspace());
    } catch (Exception e) {
        System.err.println("Unable to create BndEditModel with Workspace, defaulting to without Workspace");
        newBundleModel = new BndEditModel();
    }
    // Load project file and model
    IProject project = container.getProject();
    IFile projectFile = project.getFile(Project.BNDFILE);
    BndEditModel projectModel;
    final Document projectDocument;
    try {
        if (projectFile.exists()) {
            byte[] bytes = FileUtils.readFully(projectFile.getContents());
            projectDocument = new Document(new String(bytes, projectFile.getCharset()));
        } else {
            projectDocument = new Document("");
        }
        try {
            projectModel = new BndEditModel(Central.getWorkspace());
        } catch (Exception e) {
            System.err.println("Unable to create BndEditModel with Workspace, defaulting to without Workspace");
            projectModel = new BndEditModel();
        }
        projectModel.loadFrom(projectDocument);
    } catch (IOException e) {
        throw new CoreException(new Status(IStatus.ERROR, Plugin.PLUGIN_ID, 0, e.getMessage(), e));
    }
    // Sub-bundles are meaningless outside of Bndtools
    if (project.hasNature(BndtoolsConstants.NATURE_ID)) {
        // Check if we need to enable sub-bundles on the project file
        boolean enableSubs;
        List<String> subBndFiles = projectModel.getSubBndFiles();
        List<String> availableHeaders = calculateProjectOnlyHeaders(projectModel.getAllPropertyNames());
        Collection<String> bundleSpecificHeaders = calculateBundleSpecificHeaders(availableHeaders);
        if (subBndFiles == null || subBndFiles.isEmpty()) {
            final EnableSubBundlesDialog subBundlesDialog = new EnableSubBundlesDialog(parentShell, availableHeaders, bundleSpecificHeaders);
            if (subBundlesDialog.open() != Window.OK) {
                monitor.setCanceled(true);
                return;
            }
            enableSubs = subBundlesDialog.isEnableSubBundles();
            bundleSpecificHeaders = subBundlesDialog.getSelectedProperties();
        } else {
            enableSubs = false;
        }
        // Enable subs and copy entries from project model to new bundle model
        if (enableSubs) {
            projectModel.setSubBndFiles(Arrays.asList(new String[] { "*.bnd" }));
            for (String propertyName : bundleSpecificHeaders) {
                Object value = projectModel.genericGet(propertyName);
                projectModel.genericSet(propertyName, null);
                newBundleModel.genericSet(propertyName, value);
            }
            // Save the project model
            projectModel.saveChangesTo(projectDocument);
            FileUtils.writeFully(projectDocument.get(), projectFile, false);
        }
    }
    // Generate the new bundle model
    Document newBundleDocument = new Document("");
    newBundleModel.saveChangesTo(newBundleDocument);
    try {
        newBundleInputStream = new ByteArrayInputStream(newBundleDocument.get().getBytes("UTF-8"));
    } catch (UnsupportedEncodingException e) {
        newBundleInputStream = null;
    }
}
Also used : IStatus(org.eclipse.core.runtime.IStatus) Status(org.eclipse.core.runtime.Status) IFile(org.eclipse.core.resources.IFile) UnsupportedEncodingException(java.io.UnsupportedEncodingException) IOException(java.io.IOException) Document(aQute.bnd.properties.Document) CoreException(org.eclipse.core.runtime.CoreException) IOException(java.io.IOException) UnsupportedEncodingException(java.io.UnsupportedEncodingException) IProject(org.eclipse.core.resources.IProject) CoreException(org.eclipse.core.runtime.CoreException) ByteArrayInputStream(java.io.ByteArrayInputStream) BndEditModel(aQute.bnd.build.model.BndEditModel) IResource(org.eclipse.core.resources.IResource)

Example 32 with BndEditModel

use of aQute.bnd.build.model.BndEditModel in project bnd by bndtools.

the class BndrunResolveContextTest method testResolvePreferences.

public static void testResolvePreferences() {
    MockRegistry registry = new MockRegistry();
    registry.addPlugin(createRepo(IO.getFile("testdata/repo4/index.xml")));
    BndEditModel runModel = new BndEditModel();
    runModel.setRunFw("org.apache.felix.framework");
    runModel.genericSet("-resolve.preferences", "x.1");
    Requirement requirement = new CapReqBuilder("x").buildSyntheticRequirement();
    BndrunResolveContext context = new BndrunResolveContext(runModel, registry, log);
    List<Capability> providers = context.findProviders(requirement);
    assertEquals(3, providers.size());
    assertEquals(IO.getFile("testdata/repo4/x.1.jar").toURI(), findContentURI(providers.get(0).getResource()));
    assertEquals(IO.getFile("testdata/repo4/x.3.jar").toURI(), findContentURI(providers.get(1).getResource()));
    assertEquals(IO.getFile("testdata/repo4/x.2.jar").toURI(), findContentURI(providers.get(2).getResource()));
}
Also used : CapReqBuilder(aQute.bnd.osgi.resource.CapReqBuilder) Requirement(org.osgi.resource.Requirement) Capability(org.osgi.resource.Capability) IdentityCapability(aQute.bnd.osgi.resource.ResourceUtils.IdentityCapability) MockRegistry(test.lib.MockRegistry) BndEditModel(aQute.bnd.build.model.BndEditModel)

Example 33 with BndEditModel

use of aQute.bnd.build.model.BndEditModel in project bnd by bndtools.

the class BndrunResolveContextTest method testResolveProvidedCapabilitiesWithDistro.

public static void testResolveProvidedCapabilitiesWithDistro() throws Exception {
    MockRegistry registry = new MockRegistry();
    registry.addPlugin(createRepo(IO.getFile("testdata/repo3.index.xml")));
    BndEditModel model = new BndEditModel();
    model.genericSet("-runprovidedcapabilities", "osgi.service;objectClass=foo.bar.FooBarService;effective:=active");
    model.setDistro(Arrays.asList("testdata/distro.jar;version=file"));
    List<Requirement> requires = new ArrayList<Requirement>();
    List<Capability> caps = CapReqBuilder.getCapabilitiesFrom(new Parameters("osgi.service;objectClass=foo.bar.FooBarService;effective:=active"));
    Requirement req = CapReqBuilder.createRequirementFromCapability(caps.get(0)).buildSyntheticRequirement();
    requires.add(req);
    model.setRunRequires(requires);
    BndrunResolveContext context = new BndrunResolveContext(model, registry, log);
    context.setLevel(0);
    context.init();
    Resolver resolver = new BndResolver(new ResolverLogger(4));
    resolver.resolve(context);
}
Also used : Requirement(org.osgi.resource.Requirement) Parameters(aQute.bnd.header.Parameters) Capability(org.osgi.resource.Capability) IdentityCapability(aQute.bnd.osgi.resource.ResourceUtils.IdentityCapability) Resolver(org.osgi.service.resolver.Resolver) MockRegistry(test.lib.MockRegistry) ArrayList(java.util.ArrayList) BndEditModel(aQute.bnd.build.model.BndEditModel)

Example 34 with BndEditModel

use of aQute.bnd.build.model.BndEditModel in project bnd by bndtools.

the class BndrunResolveContextTest method testSimpleBlacklist.

/**
	 * Test the blacklist. We reject any resources that matches a specific
	 * requirements
	 */
public static void testSimpleBlacklist() {
    MockRegistry registry = new MockRegistry();
    registry.addPlugin(createRepo(IO.getFile("testdata/repo1.index.xml"), "Repository1"));
    BndEditModel model = new BndEditModel();
    Requirement blacklist = new CapReqBuilder("osgi.wiring.package").addDirective("filter", "(osgi.wiring.package=org.apache.felix.gogo.api)").buildSyntheticRequirement();
    model.setRunBlacklist(Arrays.asList(blacklist));
    BndrunResolveContext context = new BndrunResolveContext(model, registry, log);
    //
    // This one is ok in testSimple
    // but should fail because we blacklisted it
    //
    Requirement req = new CapReqBuilder("osgi.wiring.package").addDirective("filter", "(osgi.wiring.package=org.apache.felix.gogo.api)").buildSyntheticRequirement();
    List<Capability> providers = context.findProviders(req);
    assertEquals(0, providers.size());
}
Also used : CapReqBuilder(aQute.bnd.osgi.resource.CapReqBuilder) Requirement(org.osgi.resource.Requirement) Capability(org.osgi.resource.Capability) IdentityCapability(aQute.bnd.osgi.resource.ResourceUtils.IdentityCapability) MockRegistry(test.lib.MockRegistry) BndEditModel(aQute.bnd.build.model.BndEditModel)

Example 35 with BndEditModel

use of aQute.bnd.build.model.BndEditModel in project bnd by bndtools.

the class BndrunResolveContextTest method testResolverHookFiltersResultWithBlacklist.

public static void testResolverHookFiltersResultWithBlacklist() {
    MockRegistry registry = new MockRegistry();
    registry.addPlugin(createRepo(new File("testdata/osgi.cmpn-4.3.0.index.xml")));
    registry.addPlugin(createRepo(new File("testdata/org.apache.felix.framework-4.0.2.index.xml")));
    // Add a hook that removes all capabilities from resource with id
    // "osgi.cmpn"
    HashMap<String, String> blacklistProp = new HashMap<String, String>();
    BndEditModel runModel = new BndEditModel();
    ArrayList<Requirement> blacklistlist = new ArrayList<Requirement>();
    blacklistlist.add(CapReqBuilder.createSimpleRequirement("osgi.identity", "osgi.cmpn", "4.3.0").buildSyntheticRequirement());
    runModel.setRunBlacklist(blacklistlist);
    runModel.setRunFw("org.apache.felix.framework");
    Requirement requirement = new CapReqBuilder("osgi.wiring.package").addDirective("filter", "(&(osgi.wiring.package=org.osgi.util.tracker)(version>=1.5)(!(version>=1.6)))").buildSyntheticRequirement();
    BndrunResolveContext context = new BndrunResolveContext(runModel, registry, log);
    List<Capability> providers = context.findProviders(requirement);
    assertEquals(1, providers.size());
    assertEquals(new File("testdata/org.apache.felix.framework-4.0.2.jar").toURI(), findContentURI(providers.get(0).getResource()));
}
Also used : CapReqBuilder(aQute.bnd.osgi.resource.CapReqBuilder) Capability(org.osgi.resource.Capability) IdentityCapability(aQute.bnd.osgi.resource.ResourceUtils.IdentityCapability) HashMap(java.util.HashMap) MockRegistry(test.lib.MockRegistry) ArrayList(java.util.ArrayList) Requirement(org.osgi.resource.Requirement) File(java.io.File) BndEditModel(aQute.bnd.build.model.BndEditModel)

Aggregations

BndEditModel (aQute.bnd.build.model.BndEditModel)55 Requirement (org.osgi.resource.Requirement)35 MockRegistry (test.lib.MockRegistry)34 CapReqBuilder (aQute.bnd.osgi.resource.CapReqBuilder)32 IdentityCapability (aQute.bnd.osgi.resource.ResourceUtils.IdentityCapability)25 Capability (org.osgi.resource.Capability)25 File (java.io.File)15 Resource (org.osgi.resource.Resource)15 ArrayList (java.util.ArrayList)12 Document (aQute.bnd.properties.Document)8 List (java.util.List)7 Workspace (aQute.bnd.build.Workspace)6 Resolver (org.osgi.service.resolver.Resolver)6 ExportedPackage (aQute.bnd.build.model.clauses.ExportedPackage)5 VersionedClause (aQute.bnd.build.model.clauses.VersionedClause)5 IOException (java.io.IOException)5 HashMap (java.util.HashMap)5 IFile (org.eclipse.core.resources.IFile)4 CoreException (org.eclipse.core.runtime.CoreException)4 ResolutionException (org.osgi.service.resolver.ResolutionException)4