Search in sources :

Example 31 with Version

use of org.osgi.framework.Version in project karaf by apache.

the class XmlRepository method addResource.

@Override
protected void addResource(Resource resource) {
    List<Capability> identities = resource.getCapabilities(IDENTITY_NAMESPACE);
    if (identities.isEmpty()) {
        throw new IllegalStateException("Invalid resource: a capability with 'osgi.identity' namespace is required");
    } else if (identities.size() > 1) {
        throw new IllegalStateException("Invalid resource: multiple 'osgi.identity' capabilities found");
    }
    Capability identity = identities.get(0);
    Object name = identity.getAttributes().get(IDENTITY_NAMESPACE);
    Object type = identity.getAttributes().get(CAPABILITY_TYPE_ATTRIBUTE);
    Object vers = identity.getAttributes().get(CAPABILITY_VERSION_ATTRIBUTE);
    if (!String.class.isInstance(name) || !String.class.isInstance(type) || !Version.class.isInstance(vers)) {
        throw new IllegalStateException("Invalid osgi.identity capability: " + identity);
    }
    if (!hasResource((String) type, (String) name, (Version) vers)) {
        super.addResource(resource);
    }
}
Also used : Capability(org.osgi.resource.Capability) Version(org.osgi.framework.Version)

Example 32 with Version

use of org.osgi.framework.Version in project karaf by apache.

the class ObrCommandSupport method selectNewestVersion.

public Resource selectNewestVersion(Resource[] resources) {
    int idx = -1;
    Version v = null;
    for (int i = 0; (resources != null) && (i < resources.length); i++) {
        if (i == 0) {
            idx = 0;
            v = resources[i].getVersion();
        } else {
            Version vtmp = resources[i].getVersion();
            if (vtmp.compareTo(v) > 0) {
                idx = i;
                v = vtmp;
            }
        }
    }
    return (idx < 0) ? null : resources[idx];
}
Also used : Version(org.osgi.framework.Version)

Example 33 with Version

use of org.osgi.framework.Version in project sling by apache.

the class BundleBundleVersionInfoTest method testSnapshot.

@Test
public void testSnapshot() {
    final String name = "some.bundle";
    final Version version = new Version("1.0.4.SNAPSHOT");
    final long lastMod = 0;
    final Bundle b = getMockBundle(name, version, lastMod);
    BundleVersionInfo<?> vi = new BundleBundleVersionInfo(b);
    assertEquals("Symbolic name matches", name, vi.getBundleSymbolicName());
    assertEquals("Version matches", version, vi.getVersion());
    assertTrue("isBundle", vi.isBundle());
    assertTrue("Bundle is a snapshot", vi.isSnapshot());
    assertEquals("Last-Modified matches", BundleVersionInfo.BND_LAST_MODIFIED_MISSING, vi.getBundleLastModified());
    assertTrue("Bundle is stored as source", vi.getSource() == b);
}
Also used : Version(org.osgi.framework.Version) Bundle(org.osgi.framework.Bundle) Test(org.junit.Test)

Example 34 with Version

use of org.osgi.framework.Version in project sling by apache.

the class ClassLoadingTest method testLoading_SLING_6258.

@Test
public void testLoading_SLING_6258() throws Exception {
    final BundleContext bundleContext = this.context.mock(BundleContext.class);
    final PackageAdmin packageAdmin = this.context.mock(PackageAdmin.class);
    final ExportedPackage ep1 = this.context.mock(ExportedPackage.class, "ep1");
    final ExportedPackage ep2 = this.context.mock(ExportedPackage.class, "ep2");
    final ExportedPackage ep3 = this.context.mock(ExportedPackage.class, "ep3");
    final Bundle bundle1 = this.context.mock(Bundle.class, "bundle1");
    final Bundle bundle2 = this.context.mock(Bundle.class, "bundle2");
    final Bundle bundle3 = this.context.mock(Bundle.class, "bundle3");
    final ClassNotFoundException cnfe = new ClassNotFoundException();
    this.context.checking(new Expectations() {

        {
            allowing(bundleContext).createFilter(with(any(String.class)));
            will(returnValue(null));
            allowing(bundleContext).getServiceReferences(with(any(String.class)), with((String) null));
            will(returnValue(null));
            allowing(bundleContext).addServiceListener(with(any(ServiceListener.class)), with(any(String.class)));
            allowing(bundleContext).removeServiceListener(with(any(ServiceListener.class)));
            allowing(packageAdmin).getExportedPackages(with("org.apache.sling.test"));
            will(returnValue(new ExportedPackage[] { ep1, ep2 }));
            allowing(packageAdmin).getExportedPackages(with("org.apache.sling.test3"));
            will(returnValue(new ExportedPackage[] { ep3 }));
            allowing(ep1).getExportingBundle();
            will(returnValue(bundle1));
            allowing(ep1).isRemovalPending();
            will(returnValue(false));
            allowing(ep2).getExportingBundle();
            will(returnValue(bundle2));
            allowing(ep2).isRemovalPending();
            will(returnValue(false));
            allowing(ep3).getExportingBundle();
            will(returnValue(bundle3));
            allowing(ep3).isRemovalPending();
            will(returnValue(false));
            allowing(bundle1).getBundleId();
            will(returnValue(1L));
            allowing(bundle1).getState();
            will(returnValue(Bundle.ACTIVE));
            allowing(bundle1).getVersion();
            will(returnValue(new Version("1.0.0")));
            allowing(bundle1).getSymbolicName();
            will(returnValue("org.apache.sling.test1"));
            allowing(bundle2).getBundleId();
            will(returnValue(2L));
            allowing(bundle2).getState();
            will(returnValue(Bundle.ACTIVE));
            allowing(bundle2).getVersion();
            will(returnValue(new Version("2.0.0")));
            allowing(bundle2).getSymbolicName();
            will(returnValue("org.apache.sling.test2"));
            allowing(bundle3).getBundleId();
            will(returnValue(3L));
            allowing(bundle3).getState();
            will(returnValue(Bundle.ACTIVE));
            allowing(bundle3).getVersion();
            will(returnValue(new Version("1.2.3")));
            allowing(bundle3).getSymbolicName();
            will(returnValue("org.apache.sling.test3"));
            allowing(bundle1).loadClass("org.apache.sling.test.T1");
            will(throwException(cnfe));
            allowing(bundle1).getResource("org/apache/sling/test/T3.class");
            will(returnValue(new URL("jar:file:/ws/org.apache.sling.test1.jar!/org/apache/sling/test/T3.class")));
            allowing(bundle2).loadClass("org.apache.sling.test.T1");
            will(returnValue(ArrayList.class));
            allowing(bundle2).loadClass("org.apache.sling.test.T2");
            will(throwException(new ClassNotFoundException()));
            allowing(bundle2).getResource("org/apache/sling/test/T3.class");
            will(returnValue(null));
            allowing(bundle2).getResources("org/apache/sling/test/T3.class");
            will(returnValue(null));
            allowing(bundle3).getResource("org/apache/sling/test3/T4.class");
            will(returnValue(new URL("jar:file:/ws/org.apache.sling.test3.jar!/org/apache/sling/test3/T4.class")));
            allowing(bundle3).getResources("org/apache/sling/test3/T4.class");
            will(returnValue(Collections.enumeration(new ArrayList<URL>() {

                {
                    add(new URL("jar:file:/ws/org.apache.sling.test3.jar!/org/apache/sling/test3/T4.class"));
                }
            })));
        }
    });
    DynamicClassLoaderManagerImpl manager = new DynamicClassLoaderManagerImpl(bundleContext, packageAdmin, null, new DynamicClassLoaderManagerFactory(bundleContext, packageAdmin));
    final ClassLoader cl = manager.getDynamicClassLoader();
    Assert.assertEquals(ArrayList.class, cl.loadClass("org.apache.sling.test.T1"));
    try {
        cl.loadClass("org.apache.sling.test.T2");
    } catch (Exception e) {
        Assert.assertEquals(ClassNotFoundException.class, e.getClass());
    }
    Assert.assertNull(cl.getResource("org/apache/sling/test/T3.class"));
    Assert.assertFalse(cl.getResources("org/apache/sling/test/T3.class").hasMoreElements());
    Assert.assertEquals("jar:file:/ws/org.apache.sling.test3.jar!/org/apache/sling/test3/T4.class", cl.getResource("org/apache/sling/test3/T4.class").toString());
    Enumeration<URL> resourceURLs = cl.getResources("org/apache/sling/test3/T4.class");
    int count = 0;
    URL lastURL = new URL("https://sling.apache.org");
    while (resourceURLs.hasMoreElements()) {
        count++;
        lastURL = resourceURLs.nextElement();
    }
    Assert.assertEquals(1, count);
    Assert.assertEquals("jar:file:/ws/org.apache.sling.test3.jar!/org/apache/sling/test3/T4.class", lastURL.toString());
}
Also used : Expectations(org.jmock.Expectations) ServiceListener(org.osgi.framework.ServiceListener) Bundle(org.osgi.framework.Bundle) ArrayList(java.util.ArrayList) URL(java.net.URL) PackageAdmin(org.osgi.service.packageadmin.PackageAdmin) Version(org.osgi.framework.Version) ExportedPackage(org.osgi.service.packageadmin.ExportedPackage) BundleContext(org.osgi.framework.BundleContext) Test(org.junit.Test)

Example 35 with Version

use of org.osgi.framework.Version in project aries by apache.

the class BundleRequiredExecutionEnvironmentHeaderTest method testExecutionEnvironment2.

@Test
public void testExecutionEnvironment2() {
    String name = "bar";
    Version version = Version.parseVersion("2.0.0.qualifier");
    ExecutionEnvironment ee = new ExecutionEnvironment(name, version);
    assertExecutionEnvironmentName(ee, name);
    assertExecutionEnvironmentVersion(ee, version);
}
Also used : ExecutionEnvironment(org.apache.aries.subsystem.core.archive.BundleRequiredExecutionEnvironmentHeader.Clause.ExecutionEnvironment) Version(org.osgi.framework.Version) Test(org.junit.Test)

Aggregations

Version (org.osgi.framework.Version)222 Test (org.junit.Test)35 ArrayList (java.util.ArrayList)28 Bundle (org.osgi.framework.Bundle)27 File (java.io.File)23 HashMap (java.util.HashMap)21 Capability (org.osgi.resource.Capability)20 IOException (java.io.IOException)19 BundleException (org.osgi.framework.BundleException)15 Resource (org.osgi.resource.Resource)14 InputStream (java.io.InputStream)12 Map (java.util.Map)11 Manifest (java.util.jar.Manifest)11 List (java.util.List)10 URL (java.net.URL)8 HashSet (java.util.HashSet)8 Content (org.apache.aries.application.Content)8 AriesApplication (org.apache.aries.application.management.AriesApplication)8 VersionRange (org.apache.felix.utils.version.VersionRange)8 BundleRevision (org.osgi.framework.wiring.BundleRevision)8