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;
}
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;
}
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"));
}
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"));
}
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());
}
Aggregations