Search in sources :

Example 11 with DistributionQueue

use of org.apache.sling.distribution.queue.DistributionQueue in project sling by apache.

the class AgentDistributionPackageExporter method getPackage.

public DistributionPackage getPackage(@Nonnull ResourceResolver resourceResolver, @Nonnull String distributionPackageId) {
    try {
        log.debug("getting package from queue {}", queueName);
        DistributionQueue queue = getQueueOrThrow(queueName);
        DistributionQueueEntry entry = queue.getItem(distributionPackageId);
        DistributionPackage distributionPackage;
        if (entry != null) {
            DistributionQueueItem queueItem = entry.getItem();
            DistributionPackageInfo info = DistributionPackageUtils.fromQueueItem(queueItem);
            DistributionPackageBuilder packageBuilder = packageBuilderProvider.getPackageBuilder(info.getType());
            if (packageBuilder != null) {
                distributionPackage = packageBuilder.getPackage(resourceResolver, queueItem.getPackageId());
                distributionPackage.getInfo().putAll(info);
                log.debug("item {} fetched from the queue", info);
                if (distributionPackage != null) {
                    return new AgentDistributionPackage(distributionPackage, queue, entry.getId());
                }
            } else {
                log.warn("cannot find package builder with type {}", info.getType());
            }
        }
    } catch (Exception ex) {
        log.error("Error exporting package", ex);
    }
    return null;
}
Also used : DistributionPackage(org.apache.sling.distribution.packaging.DistributionPackage) SimpleDistributionPackage(org.apache.sling.distribution.packaging.impl.SimpleDistributionPackage) DistributionPackageInfo(org.apache.sling.distribution.packaging.DistributionPackageInfo) DistributionQueue(org.apache.sling.distribution.queue.DistributionQueue) DistributionQueueEntry(org.apache.sling.distribution.queue.DistributionQueueEntry) DistributionPackageBuilder(org.apache.sling.distribution.packaging.DistributionPackageBuilder) DistributionQueueItem(org.apache.sling.distribution.queue.DistributionQueueItem) DistributionException(org.apache.sling.distribution.common.DistributionException)

Example 12 with DistributionQueue

use of org.apache.sling.distribution.queue.DistributionQueue in project sling by apache.

the class ExtendedDistributionServiceResourceProvider method getQueueProperties.

private Map<String, Object> getQueueProperties(DistributionAgent agent, SimplePathInfo queueInfo) {
    if (queueInfo.isRoot()) {
        Map<String, Object> result = new HashMap<String, Object>();
        List<String> nameList = new ArrayList<String>();
        for (String name : agent.getQueueNames()) {
            nameList.add(name);
        }
        result.put(ITEMS, nameList.toArray(new String[nameList.size()]));
        result.put(SLING_RESOURCE_TYPE, DistributionResourceTypes.AGENT_QUEUE_LIST_RESOURCE_TYPE);
        return result;
    } else if (queueInfo.isMain()) {
        String queueName = queueInfo.getMainResourceName();
        Map<String, Object> result = new HashMap<String, Object>();
        DistributionQueue queue = agent.getQueue(queueName);
        if (queue != null) {
            DistributionQueueStatus queueStatus = queue.getStatus();
            result.put(SLING_RESOURCE_TYPE, DistributionResourceTypes.AGENT_QUEUE_RESOURCE_TYPE);
            result.put("state", queueStatus.getState().name());
            result.put("empty", queueStatus.isEmpty());
            result.put("itemsCount", queueStatus.getItemsCount());
            if (queueName.startsWith(ErrorQueueDispatchingStrategy.ERROR_PREFIX)) {
                String retryQueue = queueName.replace(ErrorQueueDispatchingStrategy.ERROR_PREFIX, "");
                result.put("retryQueue", retryQueue);
            }
            List<String> nameList = new ArrayList<String>();
            DistributionQueueEntry entry = queue.getHead();
            if (entry != null) {
                nameList.add(entry.getId());
            }
            result.put(ITEMS, nameList.toArray(new String[nameList.size()]));
            result.put(INTERNAL_ITEMS_ITERATOR, new QueueItemsIterator(queue));
            result.put(INTERNAL_ADAPTABLE, queue);
        }
        return result;
    } else if (queueInfo.isChild()) {
        String queueName = queueInfo.getMainResourceName();
        Map<String, Object> result = new HashMap<String, Object>();
        DistributionQueue queue = agent.getQueue(queueName);
        if (queue != null) {
            String itemId = queueInfo.getChildResourceName();
            DistributionQueueEntry entry = queue.getItem(itemId);
            result = getItemProperties(entry);
        }
        return result;
    }
    return null;
}
Also used : DistributionQueueStatus(org.apache.sling.distribution.queue.DistributionQueueStatus) HashMap(java.util.HashMap) DistributionQueue(org.apache.sling.distribution.queue.DistributionQueue) DistributionQueueEntry(org.apache.sling.distribution.queue.DistributionQueueEntry) ArrayList(java.util.ArrayList) ArrayList(java.util.ArrayList) List(java.util.List) HashMap(java.util.HashMap) Map(java.util.Map)

Example 13 with DistributionQueue

use of org.apache.sling.distribution.queue.DistributionQueue in project sling by apache.

the class SimpleDistributionAgentTest method testGetNonExistingNamedQueue.

@Test
public void testGetNonExistingNamedQueue() throws Exception {
    String name = "sample-agent";
    DistributionPackageImporter packageImporter = mock(DistributionPackageImporter.class);
    DistributionPackageExporter packageExporter = mock(DistributionPackageExporter.class);
    DistributionRequestAuthorizationStrategy packageExporterStrategy = mock(DistributionRequestAuthorizationStrategy.class);
    DistributionQueueProvider queueProvider = mock(DistributionQueueProvider.class);
    DistributionQueueDispatchingStrategy distributionHandler = mock(DistributionQueueDispatchingStrategy.class);
    DistributionEventFactory distributionEventFactory = mock(DistributionEventFactory.class);
    ResourceResolverFactory resolverFactory = mock(ResourceResolverFactory.class);
    SimpleDistributionAgent agent = new SimpleDistributionAgent(name, false, null, "serviceName", packageImporter, packageExporter, packageExporterStrategy, queueProvider, distributionHandler, null, distributionEventFactory, resolverFactory, mock(SlingRepository.class), mock(DefaultDistributionLog.class), null, null, 0);
    DistributionQueue queue = mock(DistributionQueue.class);
    when(queueProvider.getQueue("priority")).thenReturn(queue);
    assertNull(agent.getQueue("weird"));
}
Also used : SlingRepository(org.apache.sling.jcr.api.SlingRepository) DistributionQueueDispatchingStrategy(org.apache.sling.distribution.queue.impl.DistributionQueueDispatchingStrategy) ResourceResolverFactory(org.apache.sling.api.resource.ResourceResolverFactory) SimpleDistributionQueue(org.apache.sling.distribution.queue.impl.simple.SimpleDistributionQueue) DistributionQueue(org.apache.sling.distribution.queue.DistributionQueue) DistributionQueueProvider(org.apache.sling.distribution.queue.DistributionQueueProvider) DistributionPackageImporter(org.apache.sling.distribution.packaging.DistributionPackageImporter) DefaultDistributionLog(org.apache.sling.distribution.log.impl.DefaultDistributionLog) DistributionPackageExporter(org.apache.sling.distribution.packaging.DistributionPackageExporter) DistributionEventFactory(org.apache.sling.distribution.event.impl.DistributionEventFactory) Test(org.junit.Test)

Example 14 with DistributionQueue

use of org.apache.sling.distribution.queue.DistributionQueue in project sling by apache.

the class SimpleDistributionAgentTest method testGetExistingNamedQueue.

@Test
public void testGetExistingNamedQueue() throws Exception {
    String name = "sample-agent";
    DistributionPackageImporter packageImporter = mock(DistributionPackageImporter.class);
    DistributionPackageExporter packageExporter = mock(DistributionPackageExporter.class);
    DistributionRequestAuthorizationStrategy authorizationStrategy = mock(DistributionRequestAuthorizationStrategy.class);
    DistributionQueueProvider queueProvider = mock(DistributionQueueProvider.class);
    DistributionQueueDispatchingStrategy dispatchingStrategy = mock(DistributionQueueDispatchingStrategy.class);
    when(dispatchingStrategy.getQueueNames()).thenReturn(Arrays.asList("priority"));
    DistributionEventFactory distributionEventFactory = mock(DistributionEventFactory.class);
    ResourceResolverFactory resolverFactory = mock(ResourceResolverFactory.class);
    SimpleDistributionAgent agent = new SimpleDistributionAgent(name, false, null, "serviceName", packageImporter, packageExporter, authorizationStrategy, queueProvider, dispatchingStrategy, null, distributionEventFactory, resolverFactory, mock(SlingRepository.class), mock(DefaultDistributionLog.class), null, null, 0);
    DistributionQueue queue = mock(DistributionQueue.class);
    when(queueProvider.getQueue("priority")).thenReturn(queue);
    assertNotNull(agent.getQueue("priority"));
}
Also used : SlingRepository(org.apache.sling.jcr.api.SlingRepository) DistributionQueueDispatchingStrategy(org.apache.sling.distribution.queue.impl.DistributionQueueDispatchingStrategy) ResourceResolverFactory(org.apache.sling.api.resource.ResourceResolverFactory) SimpleDistributionQueue(org.apache.sling.distribution.queue.impl.simple.SimpleDistributionQueue) DistributionQueue(org.apache.sling.distribution.queue.DistributionQueue) DistributionQueueProvider(org.apache.sling.distribution.queue.DistributionQueueProvider) DistributionPackageImporter(org.apache.sling.distribution.packaging.DistributionPackageImporter) DefaultDistributionLog(org.apache.sling.distribution.log.impl.DefaultDistributionLog) DistributionPackageExporter(org.apache.sling.distribution.packaging.DistributionPackageExporter) DistributionEventFactory(org.apache.sling.distribution.event.impl.DistributionEventFactory) Test(org.junit.Test)

Example 15 with DistributionQueue

use of org.apache.sling.distribution.queue.DistributionQueue in project sling by apache.

the class DistributionQueueHealthCheckTest method testWithNoItemInTheQueue.

@Test
public void testWithNoItemInTheQueue() throws Exception {
    DistributionQueueHealthCheck distributionQueueHealthCheck = new DistributionQueueHealthCheck();
    distributionQueueHealthCheck.activate(Collections.<String, Object>emptyMap());
    DistributionQueue queue = mock(DistributionQueue.class);
    when(queue.getHead()).thenReturn(null);
    DistributionAgent distributionAgent = mock(DistributionAgent.class);
    List<String> queues = new ArrayList<String>();
    queues.add("queueName");
    when(distributionAgent.getQueueNames()).thenReturn(queues);
    when(distributionAgent.getQueue(anyString())).thenReturn(queue);
    distributionQueueHealthCheck.bindDistributionAgent(distributionAgent);
    Result result = distributionQueueHealthCheck.execute();
    assertNotNull(result);
    assertTrue(result.isOk());
}
Also used : DistributionAgent(org.apache.sling.distribution.agent.DistributionAgent) DistributionQueue(org.apache.sling.distribution.queue.DistributionQueue) ArrayList(java.util.ArrayList) Matchers.anyString(org.mockito.Matchers.anyString) Result(org.apache.sling.hc.api.Result) Test(org.junit.Test)

Aggregations

DistributionQueue (org.apache.sling.distribution.queue.DistributionQueue)40 Test (org.junit.Test)25 DistributionQueueItem (org.apache.sling.distribution.queue.DistributionQueueItem)20 DistributionQueueEntry (org.apache.sling.distribution.queue.DistributionQueueEntry)18 DistributionQueueItemStatus (org.apache.sling.distribution.queue.DistributionQueueItemStatus)14 HashMap (java.util.HashMap)8 DistributionQueueProvider (org.apache.sling.distribution.queue.DistributionQueueProvider)8 DistributionPackage (org.apache.sling.distribution.packaging.DistributionPackage)7 DistributionPackageInfo (org.apache.sling.distribution.packaging.DistributionPackageInfo)7 ArrayList (java.util.ArrayList)6 File (java.io.File)5 Map (java.util.Map)5 DistributionAgent (org.apache.sling.distribution.agent.DistributionAgent)5 Matchers.anyString (org.mockito.Matchers.anyString)5 DistributionException (org.apache.sling.distribution.common.DistributionException)4 SharedDistributionPackage (org.apache.sling.distribution.packaging.impl.SharedDistributionPackage)4 DistributionQueueStatus (org.apache.sling.distribution.queue.DistributionQueueStatus)4 Result (org.apache.sling.hc.api.Result)4 LinkedList (java.util.LinkedList)3 Scheduler (org.apache.sling.commons.scheduler.Scheduler)3