Search in sources :

Example 1 with ResourceTransformer

use of org.apache.sling.installer.api.tasks.ResourceTransformer in project sling by apache.

the class OsgiInstallerImpl method transformResources.

/**
     * Invoke the transformers on the resources.
     */
private void transformResources() {
    boolean changed = false;
    final List<ServiceReference> serviceRefs = this.transformerTracker.getSortedServiceReferences();
    if (serviceRefs.size() > 0) {
        synchronized (this.resourcesLock) {
            // Walk the list of unknown resources and invoke all transformers
            int index = 0;
            final List<RegisteredResource> unknownList = this.persistentList.getUntransformedResources();
            while (index < unknownList.size()) {
                final RegisteredResource resource = unknownList.get(index);
                for (final ServiceReference reference : serviceRefs) {
                    final Long id = (Long) reference.getProperty(Constants.SERVICE_ID);
                    // check if this transformer has already been invoked for the resource
                    final String transformers = (String) ((RegisteredResourceImpl) resource).getAttribute(ResourceTransformer.class.getName());
                    if (id == null || (transformers != null && transformers.contains(":" + id + ':'))) {
                        continue;
                    }
                    final ResourceTransformer transformer = (ResourceTransformer) this.transformerTracker.getService(reference);
                    if (transformer != null) {
                        try {
                            final TransformationResult[] result = transformer.transform(resource);
                            final String newTransformers = (transformers == null ? ":" + id + ':' : transformers + id + ':');
                            ((RegisteredResourceImpl) resource).setAttribute(ResourceTransformer.class.getName(), newTransformers);
                            if (logger.isDebugEnabled()) {
                                logger.debug("Invoked transformer {} on {} : {}", new Object[] { transformer, resource, Arrays.toString(result) });
                            }
                            if (result != null && result.length > 0) {
                                this.persistentList.transform(resource, result);
                                changed = true;
                                index--;
                                break;
                            }
                        } catch (final Throwable t) {
                            logger.error("Uncaught exception during resource transformation!", t);
                        }
                    }
                }
                index++;
            }
        }
        if (changed) {
            this.persistentList.save();
            printResources("Transformed");
        }
    }
}
Also used : RegisteredResource(org.apache.sling.installer.api.tasks.RegisteredResource) ServiceReference(org.osgi.framework.ServiceReference) AtomicLong(java.util.concurrent.atomic.AtomicLong) ResourceTransformer(org.apache.sling.installer.api.tasks.ResourceTransformer) TransformationResult(org.apache.sling.installer.api.tasks.TransformationResult)

Example 2 with ResourceTransformer

use of org.apache.sling.installer.api.tasks.ResourceTransformer in project sling by apache.

the class UpdateHandlerTest method setUp.

@Before
public void setUp() {
    setupInstaller();
    serviceRegistrations.clear();
    final Dictionary<String, Object> props = new Hashtable<String, Object>();
    props.put(Constants.SERVICE_RANKING, 1000);
    serviceRegistrations.add(this.bundleContext.registerService(ResourceTransformer.class, new ResourceTransformer() {

        public TransformationResult[] transform(final RegisteredResource resource) {
            final int lastDot = resource.getURL().lastIndexOf('.');
            final int lastSlash = resource.getURL().lastIndexOf('/');
            if (resource.getURL().substring(lastDot + 1).equals(TYPE)) {
                final String id = resource.getURL().substring(lastSlash + 1, lastDot);
                final TransformationResult tr = new TransformationResult();
                tr.setId(id);
                tr.setResourceType(TYPE);
                return new TransformationResult[] { tr };
            }
            return null;
        }
    }, props));
    serviceRegistrations.add(this.bundleContext.registerService(InstallTaskFactory.class, new InstallTaskFactory() {

        public InstallTask createTask(final TaskResourceGroup toActivate) {
            final TaskResource tr = toActivate.getActiveResource();
            if (tr != null && tr.getEntityId().startsWith(TYPE)) {
                if (tr.getState() == ResourceState.INSTALL) {
                    installed.put(tr.getEntityId(), tr.getDictionary());
                    return new ChangeStateTask(toActivate, ResourceState.INSTALLED);
                } else {
                    installed.remove(tr.getEntityId());
                    return new ChangeStateTask(toActivate, ResourceState.UNINSTALLED);
                }
            }
            return null;
        }
    }, props));
}
Also used : InstallTaskFactory(org.apache.sling.installer.api.tasks.InstallTaskFactory) TaskResource(org.apache.sling.installer.api.tasks.TaskResource) Hashtable(java.util.Hashtable) RegisteredResource(org.apache.sling.installer.api.tasks.RegisteredResource) TaskResourceGroup(org.apache.sling.installer.api.tasks.TaskResourceGroup) ChangeStateTask(org.apache.sling.installer.api.tasks.ChangeStateTask) ResourceTransformer(org.apache.sling.installer.api.tasks.ResourceTransformer) TransformationResult(org.apache.sling.installer.api.tasks.TransformationResult) Before(org.junit.Before)

Example 3 with ResourceTransformer

use of org.apache.sling.installer.api.tasks.ResourceTransformer in project sling by apache.

the class ConfigInstallTest method testDeferredConfigRemove.

@Test
public void testDeferredConfigRemove() throws Exception {
    final AtomicInteger transformerCount = new AtomicInteger();
    final ServiceTracker<ResourceTransformer, ResourceTransformer> st = new ServiceTracker<ResourceTransformer, ResourceTransformer>(bundleContext, ResourceTransformer.class, new ServiceTrackerCustomizer<ResourceTransformer, ResourceTransformer>() {

        @Override
        public void removedService(ServiceReference<ResourceTransformer> reference, ResourceTransformer service) {
            bundleContext.ungetService(reference);
            transformerCount.decrementAndGet();
        }

        @Override
        public void modifiedService(ServiceReference<ResourceTransformer> reference, ResourceTransformer service) {
        }

        @Override
        public ResourceTransformer addingService(ServiceReference<ResourceTransformer> reference) {
            transformerCount.incrementAndGet();
            return bundleContext.getService(reference);
        }
    });
    st.open();
    final AtomicInteger expectedCount = new AtomicInteger();
    final Condition cond = new Condition() {

        @Override
        boolean isTrue() throws Exception {
            return transformerCount.get() == expectedCount.get();
        }

        @Override
        String additionalInfo() {
            return "Resource transformer count is " + String.valueOf(transformerCount.get()) + ", should be " + String.valueOf(expectedCount.get());
        }
    };
    // get config admin bundle and wait for service
    final Bundle configAdmin = this.getConfigAdminBundle();
    assertNotNull("ConfigAdmin bundle must be found", configAdmin);
    waitForConfigAdmin(true);
    // when everything is up and running, we have two factories
    expectedCount.set(2);
    waitForCondition(null, cond);
    // check that configuration is not available
    final String cfgPid = getClass().getSimpleName() + ".deferred." + uniqueID();
    assertNull("Config " + cfgPid + " must not be found before test", findConfiguration(cfgPid));
    // create and install new configuration object, verify
    final Dictionary<String, Object> cfgData = new Hashtable<String, Object>();
    cfgData.put("foo", "bar");
    final InstallableResource[] rsrc = getInstallableResource(cfgPid, cfgData);
    installer.updateResources(URL_SCHEME, rsrc, null);
    waitForConfiguration("Config must be installed before stopping ConfigurationAdmin", cfgPid, true);
    this.waitForResource(URL_SCHEME + ":" + rsrc[0].getId(), ResourceState.INSTALLED);
    // Configuration uninstalls must be deferred if ConfigAdmin service is stopped
    configAdmin.stop();
    waitForConfigAdmin(false);
    // only bundle transformer
    expectedCount.set(1);
    waitForCondition(null, cond);
    // remove configuration
    installationEvents = 0;
    installer.updateResources(URL_SCHEME, null, new String[] { rsrc[0].getId() });
    waitForInstallationEvents(2);
    this.waitForResource(URL_SCHEME + ":" + rsrc[0].getId(), ResourceState.UNINSTALL);
    configAdmin.start();
    waitForConfigAdmin(true);
    // when everything is up and running, we have two transformers again
    expectedCount.set(2);
    waitForCondition(null, cond);
    waitForConfiguration("Config must be removed once ConfigurationAdmin restarts", cfgPid, false);
    st.close();
}
Also used : ServiceTracker(org.osgi.util.tracker.ServiceTracker) Bundle(org.osgi.framework.Bundle) Hashtable(java.util.Hashtable) InstallableResource(org.apache.sling.installer.api.InstallableResource) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) ResourceTransformer(org.apache.sling.installer.api.tasks.ResourceTransformer) Test(org.junit.Test)

Aggregations

ResourceTransformer (org.apache.sling.installer.api.tasks.ResourceTransformer)3 Hashtable (java.util.Hashtable)2 RegisteredResource (org.apache.sling.installer.api.tasks.RegisteredResource)2 TransformationResult (org.apache.sling.installer.api.tasks.TransformationResult)2 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)1 AtomicLong (java.util.concurrent.atomic.AtomicLong)1 InstallableResource (org.apache.sling.installer.api.InstallableResource)1 ChangeStateTask (org.apache.sling.installer.api.tasks.ChangeStateTask)1 InstallTaskFactory (org.apache.sling.installer.api.tasks.InstallTaskFactory)1 TaskResource (org.apache.sling.installer.api.tasks.TaskResource)1 TaskResourceGroup (org.apache.sling.installer.api.tasks.TaskResourceGroup)1 Before (org.junit.Before)1 Test (org.junit.Test)1 Bundle (org.osgi.framework.Bundle)1 ServiceReference (org.osgi.framework.ServiceReference)1 ServiceTracker (org.osgi.util.tracker.ServiceTracker)1