You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@sling.apache.org by to...@apache.org on 2016/08/18 14:29:47 UTC
svn commit: r1756793 - in
/sling/trunk/contrib/extensions/distribution/core/src:
main/java/org/apache/sling/distribution/agent/impl/
test/java/org/apache/sling/distribution/agent/impl/
Author: tommaso
Date: Thu Aug 18 14:29:47 2016
New Revision: 1756793
URL: http://svn.apache.org/viewvc?rev=1756793&view=rev
Log:
SLING-5977 - moved SimpleDistributionAgent remaining inner class out, added tests
Added:
sling/trunk/contrib/extensions/distribution/core/src/main/java/org/apache/sling/distribution/agent/impl/TriggerAgentRequestHandler.java (with props)
sling/trunk/contrib/extensions/distribution/core/src/test/java/org/apache/sling/distribution/agent/impl/TriggerAgentRequestHandlerTest.java (with props)
Modified:
sling/trunk/contrib/extensions/distribution/core/src/main/java/org/apache/sling/distribution/agent/impl/SimpleDistributionAgent.java
Modified: sling/trunk/contrib/extensions/distribution/core/src/main/java/org/apache/sling/distribution/agent/impl/SimpleDistributionAgent.java
URL: http://svn.apache.org/viewvc/sling/trunk/contrib/extensions/distribution/core/src/main/java/org/apache/sling/distribution/agent/impl/SimpleDistributionAgent.java?rev=1756793&r1=1756792&r2=1756793&view=diff
==============================================================================
--- sling/trunk/contrib/extensions/distribution/core/src/main/java/org/apache/sling/distribution/agent/impl/SimpleDistributionAgent.java (original)
+++ sling/trunk/contrib/extensions/distribution/core/src/main/java/org/apache/sling/distribution/agent/impl/SimpleDistributionAgent.java Thu Aug 18 14:29:47 2016
@@ -19,7 +19,6 @@
package org.apache.sling.distribution.agent.impl;
import javax.annotation.Nonnull;
-import javax.annotation.Nullable;
import java.util.Arrays;
import java.util.List;
import java.util.Set;
@@ -52,7 +51,6 @@ import org.apache.sling.distribution.que
import org.apache.sling.distribution.queue.DistributionQueueState;
import org.apache.sling.distribution.queue.impl.DistributionQueueDispatchingStrategy;
import org.apache.sling.distribution.queue.impl.SimpleAgentDistributionQueue;
-import org.apache.sling.distribution.trigger.DistributionRequestHandler;
import org.apache.sling.distribution.trigger.DistributionTrigger;
import org.apache.sling.distribution.util.impl.DistributionUtils;
import org.apache.sling.jcr.api.SlingRepository;
@@ -72,7 +70,7 @@ public class SimpleDistributionAgent imp
private final DefaultDistributionLog log;
private final DistributionEventFactory distributionEventFactory;
private final DistributionQueueProcessor queueProcessor;
- private AgentBasedRequestHandler agentBasedRequestHandler;
+ private TriggerAgentRequestHandler agentBasedRequestHandler;
private final String name;
private final boolean queueProcessingEnabled;
@@ -292,7 +290,7 @@ public class SimpleDistributionAgent imp
log.info("enabling agent");
// register triggers if any
- agentBasedRequestHandler = new AgentBasedRequestHandler(this);
+ agentBasedRequestHandler = new TriggerAgentRequestHandler(this, agentAuthenticationInfo, log, active);
if (!isPassive()) {
try {
@@ -411,44 +409,4 @@ public class SimpleDistributionAgent imp
return true;
}
- public class AgentBasedRequestHandler implements DistributionRequestHandler {
- private final DistributionAgent agent;
-
- public AgentBasedRequestHandler(DistributionAgent agent) {
- this.agent = agent;
- }
-
- public void handle(@Nullable ResourceResolver resourceResolver, @Nonnull DistributionRequest request) {
-
- if (!active) {
- log.warn("skipping agent handler as agent is disabled");
- return;
- }
-
- if (resourceResolver != null) {
- try {
- agent.execute(resourceResolver, request);
- } catch (Throwable t) {
- log.error("Error executing handler {}", request, t);
- }
- } else {
- ResourceResolver agentResourceResolver = null;
-
- try {
- agentResourceResolver = DistributionUtils.getResourceResolver(null, agentAuthenticationInfo.getAgentService(),
- agentAuthenticationInfo.getSlingRepository(), agentAuthenticationInfo.getSubServiceName(),
- agentAuthenticationInfo.getResourceResolverFactory());
-
- agent.execute(agentResourceResolver, request);
- } catch (Throwable e) {
- log.error("Error executing handler {}", request, e);
- } finally {
- DistributionUtils.ungetResourceResolver(agentResourceResolver);
- }
- }
-
- }
- }
-
-
-}
+}
\ No newline at end of file
Added: sling/trunk/contrib/extensions/distribution/core/src/main/java/org/apache/sling/distribution/agent/impl/TriggerAgentRequestHandler.java
URL: http://svn.apache.org/viewvc/sling/trunk/contrib/extensions/distribution/core/src/main/java/org/apache/sling/distribution/agent/impl/TriggerAgentRequestHandler.java?rev=1756793&view=auto
==============================================================================
--- sling/trunk/contrib/extensions/distribution/core/src/main/java/org/apache/sling/distribution/agent/impl/TriggerAgentRequestHandler.java (added)
+++ sling/trunk/contrib/extensions/distribution/core/src/main/java/org/apache/sling/distribution/agent/impl/TriggerAgentRequestHandler.java Thu Aug 18 14:29:47 2016
@@ -0,0 +1,82 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.sling.distribution.agent.impl;
+
+import javax.annotation.Nonnull;
+import javax.annotation.Nullable;
+
+import org.apache.sling.api.resource.ResourceResolver;
+import org.apache.sling.distribution.DistributionRequest;
+import org.apache.sling.distribution.agent.DistributionAgent;
+import org.apache.sling.distribution.log.impl.DefaultDistributionLog;
+import org.apache.sling.distribution.trigger.DistributionRequestHandler;
+import org.apache.sling.distribution.util.impl.DistributionUtils;
+
+/**
+ * A {@link DistributionRequestHandler} to trigger an agent.
+ */
+class TriggerAgentRequestHandler implements DistributionRequestHandler {
+ private final SimpleDistributionAgentAuthenticationInfo authenticationInfo;
+ private final DefaultDistributionLog log;
+ private final boolean active;
+ private SimpleDistributionAgent simpleDistributionAgent;
+ private final DistributionAgent agent;
+
+ public TriggerAgentRequestHandler(@Nonnull DistributionAgent agent,
+ @Nonnull SimpleDistributionAgentAuthenticationInfo authenticationInfo,
+ @Nonnull DefaultDistributionLog log,
+ boolean active) {
+ this.authenticationInfo = authenticationInfo;
+ this.log = log;
+ this.active = active;
+ this.simpleDistributionAgent = simpleDistributionAgent;
+ this.agent = agent;
+ }
+
+ public void handle(@Nullable ResourceResolver resourceResolver, @Nonnull DistributionRequest request) {
+
+ if (!active) {
+ log.warn("skipping agent handler as agent is disabled");
+ return;
+ }
+
+ if (resourceResolver != null) {
+ try {
+ agent.execute(resourceResolver, request);
+ } catch (Throwable t) {
+ log.error("Error executing handler {}", request, t);
+ }
+ } else {
+ ResourceResolver agentResourceResolver = null;
+
+ try {
+ agentResourceResolver = DistributionUtils.getResourceResolver(null, authenticationInfo.getAgentService(),
+ authenticationInfo.getSlingRepository(), authenticationInfo.getSubServiceName(),
+ authenticationInfo.getResourceResolverFactory());
+
+ agent.execute(agentResourceResolver, request);
+ } catch (Throwable e) {
+ log.error("Error executing handler {}", request, e);
+ } finally {
+ DistributionUtils.ungetResourceResolver(agentResourceResolver);
+ }
+ }
+
+ }
+}
Propchange: sling/trunk/contrib/extensions/distribution/core/src/main/java/org/apache/sling/distribution/agent/impl/TriggerAgentRequestHandler.java
------------------------------------------------------------------------------
svn:eol-style = native
Added: sling/trunk/contrib/extensions/distribution/core/src/test/java/org/apache/sling/distribution/agent/impl/TriggerAgentRequestHandlerTest.java
URL: http://svn.apache.org/viewvc/sling/trunk/contrib/extensions/distribution/core/src/test/java/org/apache/sling/distribution/agent/impl/TriggerAgentRequestHandlerTest.java?rev=1756793&view=auto
==============================================================================
--- sling/trunk/contrib/extensions/distribution/core/src/test/java/org/apache/sling/distribution/agent/impl/TriggerAgentRequestHandlerTest.java (added)
+++ sling/trunk/contrib/extensions/distribution/core/src/test/java/org/apache/sling/distribution/agent/impl/TriggerAgentRequestHandlerTest.java Thu Aug 18 14:29:47 2016
@@ -0,0 +1,58 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.sling.distribution.agent.impl;
+
+import org.apache.sling.api.resource.ResourceResolver;
+import org.apache.sling.distribution.DistributionRequest;
+import org.apache.sling.distribution.agent.DistributionAgent;
+import org.apache.sling.distribution.log.impl.DefaultDistributionLog;
+import org.junit.Test;
+
+import static org.junit.Assert.*;
+import static org.mockito.Mockito.mock;
+
+/**
+ * Tests for {@link TriggerAgentRequestHandler}
+ */
+public class TriggerAgentRequestHandlerTest {
+
+ @Test
+ public void testHandleActive() throws Exception {
+ DistributionAgent agent = mock(DistributionAgent.class);
+ SimpleDistributionAgentAuthenticationInfo authenticationInfo = mock(SimpleDistributionAgentAuthenticationInfo.class);
+ DefaultDistributionLog log = mock(DefaultDistributionLog.class);
+ TriggerAgentRequestHandler triggerAgentRequestHandler = new TriggerAgentRequestHandler(agent,
+ authenticationInfo, log, true);
+ ResourceResolver resourceResolver = mock(ResourceResolver.class);
+ DistributionRequest request = mock(DistributionRequest.class);
+ triggerAgentRequestHandler.handle(resourceResolver, request);
+ }
+
+ @Test
+ public void testHandlePassive() throws Exception {
+ DistributionAgent agent = mock(DistributionAgent.class);
+ SimpleDistributionAgentAuthenticationInfo authenticationInfo = mock(SimpleDistributionAgentAuthenticationInfo.class);
+ DefaultDistributionLog log = mock(DefaultDistributionLog.class);
+ TriggerAgentRequestHandler triggerAgentRequestHandler = new TriggerAgentRequestHandler(agent,
+ authenticationInfo, log, false);
+ ResourceResolver resourceResolver = mock(ResourceResolver.class);
+ DistributionRequest request = mock(DistributionRequest.class);
+ triggerAgentRequestHandler.handle(resourceResolver, request);
+ }
+}
\ No newline at end of file
Propchange: sling/trunk/contrib/extensions/distribution/core/src/test/java/org/apache/sling/distribution/agent/impl/TriggerAgentRequestHandlerTest.java
------------------------------------------------------------------------------
svn:eol-style = native