Search in sources :

Example 1 with Resource

use of org.apache.sling.installer.api.info.Resource in project sling by apache.

the class OsgiInstallerTestBase method waitForResource.

protected Resource waitForResource(final String url, final ResourceState state) {
    final long start = System.currentTimeMillis();
    final long end = start + WAIT_FOR_CHANGE_TIMEOUT;
    do {
        final InstallationState is = this.infoProvider.getInstallationState();
        for (final ResourceGroup rg : (state == ResourceState.INSTALL || state == ResourceState.UNINSTALL ? is.getActiveResources() : is.getInstalledResources())) {
            for (final Resource rsrc : rg.getResources()) {
                if (url.equals(rsrc.getURL())) {
                    if (rsrc.getState() == state) {
                        return rsrc;
                    }
                }
            }
        }
        sleep(50);
    } while (System.currentTimeMillis() < end);
    fail("Resource " + url + " not found with state " + state + " : " + this.infoProvider.getInstallationState());
    return null;
}
Also used : InstallationState(org.apache.sling.installer.api.info.InstallationState) Resource(org.apache.sling.installer.api.info.Resource) InstallableResource(org.apache.sling.installer.api.InstallableResource) ResourceGroup(org.apache.sling.installer.api.info.ResourceGroup)

Example 2 with Resource

use of org.apache.sling.installer.api.info.Resource in project sling by apache.

the class OsgiInstallerHealthCheck method evaluateGroup.

/**
     * @param group
     *            the resource group to evaluate
     * @param hcLog
     *            the log to fill during the health check
     * @return the type of resources in this group ("bundle" or "config") or empty string, if the group was not
     *         considered by this health check
     */
private String evaluateGroup(ResourceGroup group, FormattingResultLog hcLog) {
    Resource invalidResource = null;
    String resourceType = "";
    // go through all resources within the given group
    for (Resource resource : group.getResources()) {
        // check for the correct type
        resourceType = resource.getType();
        switch(resourceType) {
            case InstallableResource.TYPE_CONFIG:
                if (!checkConfigurations) {
                    LOG.debug("Skip resource '{}', configuration checks are disabled", resource.getEntityId());
                    return "";
                }
                break;
            case InstallableResource.TYPE_BUNDLE:
                if (!checkBundles) {
                    LOG.debug("Skip resource '{}', bundle checks are disabled", resource.getEntityId());
                    return "";
                }
                break;
            default:
                LOG.debug("Skip resource '{}' as it is neither a bundle nor a configuration but a {}", resource.getEntityId(), resourceType);
                return "";
        }
        if (StringUtils.startsWithAny(resource.getURL(), urlPrefixes)) {
            switch(resource.getState()) {
                // means a considered resource was found and it is invalid
                case IGNORED:
                // still the other resources need to be evaluated
                case INSTALL:
                    if (invalidResource == null) {
                        invalidResource = resource;
                    }
                    break;
                default:
                    // no need to evaluate other resources from this group
                    return resourceType;
            }
        } else {
            LOG.debug("Skipping resource '{}' as its URL is not starting with any of these prefixes'{}'", resource, StringUtils.join(urlPrefixes, ","));
        }
    }
    if (invalidResource != null) {
        if (resourceType.equals(InstallableResource.TYPE_CONFIG)) {
            hcLog.critical("The installer state of the OSGi configuration resource '{}' is {}, config might have been manually overwritten!", invalidResource, invalidResource.getState());
        } else {
            hcLog.critical("The installer state of the OSGi bundle resource '{}' is {}, probably because a later or the same version of that bundle is already installed!", invalidResource, invalidResource.getState());
        }
        return resourceType;
    } else {
        // do not count this group, as only non-considered resources have been in there
        return "";
    }
}
Also used : Resource(org.apache.sling.installer.api.info.Resource) InstallableResource(org.apache.sling.installer.api.InstallableResource)

Example 3 with Resource

use of org.apache.sling.installer.api.info.Resource 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 4 with Resource

use of org.apache.sling.installer.api.info.Resource in project sling by apache.

the class ServerSideInstallerTest method noDuplicates.

@Test
public void noDuplicates() {
    final InstallationState is = ip.getInstallationState();
    String output = "";
    final List<ResourceGroup> resources = is.getInstalledResources();
    for (final ResourceGroup group : resources) {
        if (group.getResources().size() > 1) {
            boolean first = true;
            for (final Resource rsrc : group.getResources()) {
                if (ignore(rsrc.getEntityId())) {
                    continue;
                }
                if (first) {
                    output += "Duplicate resources for '" + rsrc.getEntityId() + "' : ";
                    first = false;
                } else {
                    output += ", ";
                }
                output += rsrc.getURL();
            }
            if (!output.isEmpty()) {
                output += "\n";
            }
        }
    }
    if (output.length() > 0) {
        fail(output);
    }
}
Also used : InstallationState(org.apache.sling.installer.api.info.InstallationState) Resource(org.apache.sling.installer.api.info.Resource) ResourceGroup(org.apache.sling.installer.api.info.ResourceGroup) Test(org.junit.Test)

Example 5 with Resource

use of org.apache.sling.installer.api.info.Resource in project sling by apache.

the class OsgiInstallerWebConsolePlugin method printConfiguration.

/**
     * Method for the configuration printer.
     */
public void printConfiguration(final PrintWriter pw, final String mode) {
    if (!"zip".equals(mode) && !"txt".equals(mode)) {
        return;
    }
    pw.println("Apache Sling OSGi Installer");
    pw.println("===========================");
    final InstallationState state = this.installer.getInstallationState();
    pw.println("Active Resources");
    pw.println("----------------");
    String rt = null;
    for (final ResourceGroup group : state.getActiveResources()) {
        final Resource toActivate = group.getResources().get(0);
        if (!toActivate.getType().equals(rt)) {
            pw.printf("%s:%n", getType(toActivate));
            rt = toActivate.getType();
        }
        pw.printf("- %s: %s, %s, %s%n", getEntityId(toActivate, group.getAlias()), getInfo(toActivate), getURL(toActivate), toActivate.getState());
    }
    pw.println();
    pw.println("Processed Resources");
    pw.println("-------------------");
    rt = null;
    for (final ResourceGroup group : state.getInstalledResources()) {
        final Collection<Resource> resources = group.getResources();
        if (resources.size() > 0) {
            final Iterator<Resource> iter = resources.iterator();
            final Resource first = iter.next();
            if (!first.getType().equals(rt)) {
                pw.printf("%s:%n", getType(first));
                rt = first.getType();
            }
            pw.printf("* %s: %s, %s, %s%n", getEntityId(first, group.getAlias()), getInfo(first), getURL(first), getState(first));
            if (first.getAttribute(TaskResource.ATTR_INSTALL_EXCLUDED) != null) {
                pw.printf("  : %s", first.getAttribute(TaskResource.ATTR_INSTALL_EXCLUDED));
            }
            if (first.getAttribute(TaskResource.ATTR_INSTALL_INFO) != null) {
                pw.printf("  : %s", first.getAttribute(TaskResource.ATTR_INSTALL_INFO));
            }
            while (iter.hasNext()) {
                final Resource resource = iter.next();
                pw.printf("  - %s, %s, %s%n", getInfo(resource), getURL(resource), resource.getState());
            }
        }
    }
    pw.println();
    pw.println("Untransformed Resources");
    pw.println("-----------------------");
    rt = null;
    for (final RegisteredResource registeredResource : state.getUntransformedResources()) {
        if (!registeredResource.getType().equals(rt)) {
            pw.printf("%s:%n", getType(registeredResource));
            rt = registeredResource.getType();
        }
        pw.printf("- %s, %s%n", getInfo(registeredResource), registeredResource.getURL());
    }
}
Also used : InstallationState(org.apache.sling.installer.api.info.InstallationState) RegisteredResource(org.apache.sling.installer.api.tasks.RegisteredResource) TaskResource(org.apache.sling.installer.api.tasks.TaskResource) RegisteredResource(org.apache.sling.installer.api.tasks.RegisteredResource) Resource(org.apache.sling.installer.api.info.Resource) InstallableResource(org.apache.sling.installer.api.InstallableResource) ResourceGroup(org.apache.sling.installer.api.info.ResourceGroup)

Aggregations

Resource (org.apache.sling.installer.api.info.Resource)6 InstallableResource (org.apache.sling.installer.api.InstallableResource)5 InstallationState (org.apache.sling.installer.api.info.InstallationState)5 ResourceGroup (org.apache.sling.installer.api.info.ResourceGroup)5 RegisteredResource (org.apache.sling.installer.api.tasks.RegisteredResource)3 TaskResource (org.apache.sling.installer.api.tasks.TaskResource)3 IOException (java.io.IOException)1 InputStream (java.io.InputStream)1 PrintWriter (java.io.PrintWriter)1 ArrayList (java.util.ArrayList)1 Dictionary (java.util.Dictionary)1 LinkedList (java.util.LinkedList)1 List (java.util.List)1 CheckForNull (javax.annotation.CheckForNull)1 ResourceState (org.apache.sling.installer.api.tasks.ResourceState)1 TaskResourceGroup (org.apache.sling.installer.api.tasks.TaskResourceGroup)1 Test (org.junit.Test)1 Version (org.osgi.framework.Version)1