ReverseReplicator.java
6.19 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
/*
* Decompiled with CFR 0_118.
*
* Could not load the following classes:
* javax.jcr.Credentials
* javax.jcr.RepositoryException
* javax.jcr.Session
* javax.jcr.SimpleCredentials
* org.apache.felix.scr.annotations.Component
* org.apache.felix.scr.annotations.Properties
* org.apache.felix.scr.annotations.Property
* org.apache.felix.scr.annotations.Reference
* org.apache.felix.scr.annotations.Service
* org.apache.sling.jcr.api.SlingRepository
* org.slf4j.Logger
* org.slf4j.LoggerFactory
*/
package com.day.cq.replication.impl;
import com.day.cq.replication.Agent;
import com.day.cq.replication.AgentConfig;
import com.day.cq.replication.AgentManager;
import com.day.cq.replication.ReplicationAction;
import com.day.cq.replication.ReplicationActionType;
import com.day.cq.replication.ReplicationException;
import com.day.cq.replication.ReplicationLog;
import com.day.cq.replication.impl.ReverseReplicationHandler;
import java.util.Collection;
import java.util.LinkedList;
import java.util.Map;
import javax.jcr.Credentials;
import javax.jcr.RepositoryException;
import javax.jcr.Session;
import javax.jcr.SimpleCredentials;
import org.apache.felix.scr.annotations.Component;
import org.apache.felix.scr.annotations.Properties;
import org.apache.felix.scr.annotations.Property;
import org.apache.felix.scr.annotations.Reference;
import org.apache.felix.scr.annotations.Service;
import org.apache.sling.jcr.api.SlingRepository;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@Component(metatype=1)
@Service(value={Runnable.class})
@Properties(value={@Property(name="scheduler.period", longValue={30}, label="%reverse.scheduler.period.name", description="%reverse.scheduler.period.description"), @Property(name="scheduler.concurrent", boolValue={0}, propertyPrivate=1)})
public class ReverseReplicator
implements Runnable {
private final Logger logger;
@Reference
private SlingRepository repository;
@Reference
private AgentManager agentMgr;
@Reference
private ReverseReplicationHandler agentReverseReplicationHandler;
public ReverseReplicator() {
this.logger = LoggerFactory.getLogger(this.getClass());
this.repository = null;
this.agentMgr = null;
}
@Override
public void run() {
try {
if (this.isMaster()) {
this.logger.debug("Running reverse replication, since this is the cluster master...");
this.poll();
} else {
this.logger.debug("Skipping reverse replication, since this is a cluster slave.");
}
}
catch (ReplicationException e) {
String msg = "Error during poll.";
this.logger.warn(msg, (Throwable)e);
}
}
private boolean isMaster() {
String value = this.repository.getDescriptor("crx.cluster.master");
return value == null || "true".equals(value);
}
/*
* WARNING - Removed try catching itself - possible behaviour change.
*/
public void poll() throws ReplicationException {
LinkedList<Agent> agents = new LinkedList<Agent>();
for (Agent agent : this.agentMgr.getAgents().values()) {
if (!agent.isValid() || !agent.isEnabled() || !agent.getConfiguration().usedForReverseReplication()) continue;
agents.add(agent);
}
if (agents.isEmpty()) {
return;
}
ReplicationAction action = new ReplicationAction(ReplicationActionType.INTERNAL_POLL, "", 0, "", null);
Session session = null;
try {
session = this.repository.loginService("replicationService", null);
for (Agent agent2 : agents) {
Session agentSession = session;
ReplicationLog log = agent2.getLog();
String userId = agent2.getConfiguration().getAgentUserId();
if (userId != null) {
try {
agentSession = this.repository.impersonateFromService("replicationService", (Credentials)new SimpleCredentials(userId, new char[0]), null);
log.info("Using user %s for building content.", userId);
}
catch (RepositoryException e) {
log.error("Error while impersonating to user '%s'. using system session.", userId);
}
}
try {
this.agentReverseReplicationHandler.poll(agent2, agentSession, action);
continue;
}
catch (Exception e) {
log.error("Error while polling agent %s: %s", agent2.getId(), e.toString());
this.logger.error("Error while poling agent " + agent2.getId(), (Throwable)e);
continue;
}
finally {
if (agentSession == session) continue;
agentSession.logout();
continue;
}
}
}
catch (RepositoryException e) {
throw new ReplicationException("Error while accessing repository", (Exception)e);
}
finally {
if (session != null) {
session.logout();
}
}
}
protected void bindRepository(SlingRepository slingRepository) {
this.repository = slingRepository;
}
protected void unbindRepository(SlingRepository slingRepository) {
if (this.repository == slingRepository) {
this.repository = null;
}
}
protected void bindAgentMgr(AgentManager agentManager) {
this.agentMgr = agentManager;
}
protected void unbindAgentMgr(AgentManager agentManager) {
if (this.agentMgr == agentManager) {
this.agentMgr = null;
}
}
protected void bindAgentReverseReplicationHandler(ReverseReplicationHandler reverseReplicationHandler) {
this.agentReverseReplicationHandler = reverseReplicationHandler;
}
protected void unbindAgentReverseReplicationHandler(ReverseReplicationHandler reverseReplicationHandler) {
if (this.agentReverseReplicationHandler == reverseReplicationHandler) {
this.agentReverseReplicationHandler = null;
}
}
}