Search in sources :

Example 51 with Version

use of org.osgi.framework.Version in project bndtools by bndtools.

the class BundleSorter method compare.

@Override
public int compare(Viewer viewer, Object e1, Object e2) {
    Resource r1 = (Resource) e1;
    Resource r2 = (Resource) e2;
    Capability id1 = ResourceUtils.getIdentityCapability(r1);
    Capability id2 = ResourceUtils.getIdentityCapability(r2);
    String name1 = ResourceUtils.getIdentity(id1);
    if (name1 == null) {
        name1 = "";
    }
    String name2 = ResourceUtils.getIdentity(id2);
    if (name2 == null) {
        name2 = "";
    }
    int ret = name1.compareTo(name2);
    if (ret != 0) {
        return ret;
    }
    Version ver1 = ResourceUtils.getVersion(id1);
    if (ver1 == null) {
        ver1 = Version.emptyVersion;
    }
    Version ver2 = ResourceUtils.getVersion(id2);
    if (ver2 == null) {
        ver2 = Version.emptyVersion;
    }
    // This comparison is the reverse of what you would expect
    return ver2.compareTo(ver1);
}
Also used : Capability(org.osgi.resource.Capability) Version(org.osgi.framework.Version) Resource(org.osgi.resource.Resource)

Example 52 with Version

use of org.osgi.framework.Version in project bnd by bndtools.

the class BundleAnalyzer method doPlainJarIdentity.

private void doPlainJarIdentity(Resource resource, List<? super Capability> caps) {
    String name = (String) resource.getProperties().get(Resource.NAME);
    if (name.toLowerCase().endsWith(SUFFIX_JAR))
        name = name.substring(0, name.length() - SUFFIX_JAR.length());
    Version version = null;
    int dashIndex = name.lastIndexOf('-');
    if (dashIndex > 0) {
        try {
            String versionStr = name.substring(dashIndex + 1);
            version = new Version(versionStr);
            name = name.substring(0, dashIndex);
        } catch (Exception e) {
            version = null;
        }
    }
    Builder builder = new Builder().setNamespace(Namespaces.NS_IDENTITY).addAttribute(Namespaces.NS_IDENTITY, name).addAttribute(Namespaces.ATTR_IDENTITY_TYPE, Namespaces.RESOURCE_TYPE_PLAIN_JAR);
    if (version != null)
        builder.addAttribute(Namespaces.ATTR_VERSION, version);
    caps.add(builder.buildCapability());
}
Also used : Version(org.osgi.framework.Version) Builder(org.osgi.service.indexer.Builder) URISyntaxException(java.net.URISyntaxException) IOException(java.io.IOException) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException)

Example 53 with Version

use of org.osgi.framework.Version in project bnd by bndtools.

the class BundleAnalyzer method doBundleIdentity.

private void doBundleIdentity(Resource resource, MimeType mimeType, List<? super Capability> caps) throws Exception {
    Manifest manifest = resource.getManifest();
    if (manifest == null)
        throw new IllegalArgumentException("Missing bundle manifest.");
    String type;
    switch(mimeType) {
        case Bundle:
            type = Namespaces.RESOURCE_TYPE_BUNDLE;
            break;
        case Fragment:
            type = Namespaces.RESOURCE_TYPE_FRAGMENT;
            break;
        default:
            type = Namespaces.RESOURCE_TYPE_PLAIN_JAR;
            break;
    }
    SymbolicName bsn = Util.getSymbolicName(resource);
    boolean singleton = Boolean.TRUE.toString().equalsIgnoreCase(bsn.getAttributes().get(Constants.SINGLETON_DIRECTIVE + ":"));
    Version version = Util.getVersion(resource);
    Builder builder = new Builder().setNamespace(Namespaces.NS_IDENTITY).addAttribute(Namespaces.NS_IDENTITY, bsn.getName()).addAttribute(Namespaces.ATTR_IDENTITY_TYPE, type).addAttribute(Namespaces.ATTR_VERSION, version);
    if (singleton)
        builder.addDirective(Namespaces.DIRECTIVE_SINGLETON, Boolean.TRUE.toString());
    caps.add(builder.buildCapability());
}
Also used : Version(org.osgi.framework.Version) Builder(org.osgi.service.indexer.Builder) Manifest(java.util.jar.Manifest) SymbolicName(org.osgi.service.indexer.impl.types.SymbolicName)

Example 54 with Version

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

the class OsgiInstallerImpl method getInstallationState.

/**
     * @see org.apache.sling.installer.api.info.InfoProvider#getInstallationState()
     */
@Override
public InstallationState getInstallationState() {
    synchronized (this.resourcesLock) {
        final InstallationState state = new InstallationState() {

            private final List<ResourceGroup> activeResources = new ArrayList<>();

            private final List<ResourceGroup> installedResources = new ArrayList<>();

            private final List<RegisteredResource> untransformedResources = new ArrayList<>();

            @Override
            public List<ResourceGroup> getActiveResources() {
                return activeResources;
            }

            @Override
            public List<ResourceGroup> getInstalledResources() {
                return installedResources;
            }

            @Override
            public List<RegisteredResource> getUntransformedResources() {
                return untransformedResources;
            }

            @Override
            public String toString() {
                return "InstallationState[active resources: " + this.activeResources + ", installed resources: " + this.installedResources + ", untransformed resources: " + this.untransformedResources + "]";
            }
        };
        for (final String entityId : this.persistentList.getEntityIds()) {
            if (!this.persistentList.isSpecialEntityId(entityId)) {
                final EntityResourceList group = this.persistentList.getEntityResourceList(entityId);
                final String alias = group.getAlias();
                final List<Resource> resources = new ArrayList<>();
                boolean first = true;
                boolean isActive = false;
                for (final TaskResource tr : group.getResources()) {
                    final ResourceState resourceState = tr.getState();
                    if (first) {
                        if (resourceState == ResourceState.INSTALL || resourceState == ResourceState.UNINSTALL) {
                            isActive = true;
                        }
                        first = false;
                    }
                    resources.add(new Resource() {

                        @Override
                        public String getScheme() {
                            return tr.getScheme();
                        }

                        @Override
                        public String getURL() {
                            return tr.getURL();
                        }

                        @Override
                        public String getType() {
                            return tr.getType();
                        }

                        @Override
                        public InputStream getInputStream() throws IOException {
                            return tr.getInputStream();
                        }

                        @Override
                        public Dictionary<String, Object> getDictionary() {
                            return tr.getDictionary();
                        }

                        @Override
                        public String getDigest() {
                            return tr.getDigest();
                        }

                        @Override
                        public int getPriority() {
                            return tr.getPriority();
                        }

                        @Override
                        public String getEntityId() {
                            return tr.getEntityId();
                        }

                        @Override
                        public ResourceState getState() {
                            return resourceState;
                        }

                        @Override
                        public Version getVersion() {
                            return tr.getVersion();
                        }

                        @Override
                        public long getLastChange() {
                            return ((RegisteredResourceImpl) tr).getLastChange();
                        }

                        @Override
                        public Object getAttribute(final String key) {
                            return tr.getAttribute(key);
                        }

                        @Override
                        @CheckForNull
                        public String getError() {
                            return tr.getError();
                        }

                        @Override
                        public String toString() {
                            return "resource[entityId=" + getEntityId() + ", scheme=" + getScheme() + ", url=" + getURL() + ", type=" + getType() + ", error=" + getError() + ", state=" + getState() + ", version=" + getVersion() + ", lastChange=" + getLastChange() + ", priority=" + getPriority() + ", digest=" + getDigest() + "]";
                        }
                    });
                }
                final ResourceGroup rg = new ResourceGroup() {

                    @Override
                    public List<Resource> getResources() {
                        return resources;
                    }

                    @Override
                    public String getAlias() {
                        return alias;
                    }

                    @Override
                    public String toString() {
                        return "group[" + resources + "]";
                    }
                };
                if (isActive) {
                    state.getActiveResources().add(rg);
                } else {
                    state.getInstalledResources().add(rg);
                }
            }
        }
        Collections.sort(state.getActiveResources(), COMPARATOR);
        Collections.sort(state.getInstalledResources(), COMPARATOR);
        state.getUntransformedResources().addAll(this.persistentList.getUntransformedResources());
        return state;
    }
}
Also used : Dictionary(java.util.Dictionary) TaskResource(org.apache.sling.installer.api.tasks.TaskResource) InputStream(java.io.InputStream) RegisteredResource(org.apache.sling.installer.api.tasks.RegisteredResource) ArrayList(java.util.ArrayList) TaskResource(org.apache.sling.installer.api.tasks.TaskResource) Resource(org.apache.sling.installer.api.info.Resource) RegisteredResource(org.apache.sling.installer.api.tasks.RegisteredResource) InstallableResource(org.apache.sling.installer.api.InstallableResource) IOException(java.io.IOException) InstallationState(org.apache.sling.installer.api.info.InstallationState) Version(org.osgi.framework.Version) CheckForNull(javax.annotation.CheckForNull) List(java.util.List) ArrayList(java.util.ArrayList) LinkedList(java.util.LinkedList) ResourceState(org.apache.sling.installer.api.tasks.ResourceState) TaskResourceGroup(org.apache.sling.installer.api.tasks.TaskResourceGroup) ResourceGroup(org.apache.sling.installer.api.info.ResourceGroup)

Example 55 with Version

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

the class RegisteredResourceImpl method isSameResource.

/**
     * Compare two resources where we know that they point to the same entity and have the
     * same URL!
     */
public static boolean isSameResource(final RegisteredResourceImpl a, final RegisteredResourceImpl b) {
    // check if the artifacts have a version
    final Version va = a.getVersion();
    final Version vb = b.getVersion();
    if (va != null && vb != null) {
        // Compare version
        if (!vb.equals(va)) {
            return false;
        }
        final boolean isSnapshot = va.toString().contains("SNAPSHOT");
        if (!isSnapshot) {
            return true;
        }
    }
    // we just compare the digest
    return a.getDigest().equals(b.getDigest());
}
Also used : Version(org.osgi.framework.Version)

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