/
PushJobDelegate.java
278 lines (238 loc) · 11.7 KB
/
PushJobDelegate.java
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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
/*
* 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.syncope.core.provisioning.java.pushpull;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.concurrent.ConcurrentHashMap;
import java.util.stream.Collectors;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.tuple.MutablePair;
import org.apache.syncope.common.lib.SyncopeConstants;
import org.apache.syncope.common.lib.types.ConflictResolutionAction;
import org.apache.syncope.core.persistence.api.dao.AnyDAO;
import org.apache.syncope.core.persistence.api.dao.AnySearchDAO;
import org.apache.syncope.core.persistence.api.dao.RealmDAO;
import org.apache.syncope.core.persistence.api.dao.search.SearchCond;
import org.apache.syncope.core.persistence.api.entity.Any;
import org.apache.syncope.core.persistence.api.entity.AnyUtilsFactory;
import org.apache.syncope.core.persistence.api.entity.Implementation;
import org.apache.syncope.core.persistence.api.entity.Realm;
import org.apache.syncope.core.persistence.api.entity.anyobject.AnyObject;
import org.apache.syncope.core.persistence.api.entity.group.Group;
import org.apache.syncope.core.persistence.api.entity.resource.ExternalResource;
import org.apache.syncope.core.persistence.api.entity.resource.Provision;
import org.apache.syncope.core.persistence.api.entity.task.PushTask;
import org.apache.syncope.core.persistence.api.entity.task.PushTaskAnyFilter;
import org.apache.syncope.core.persistence.api.entity.user.User;
import org.apache.syncope.core.persistence.api.search.SearchCondConverter;
import org.apache.syncope.core.persistence.api.search.SearchCondVisitor;
import org.apache.syncope.core.provisioning.api.Connector;
import org.apache.syncope.core.provisioning.api.ProvisionSorter;
import org.apache.syncope.core.provisioning.api.pushpull.AnyObjectPushResultHandler;
import org.apache.syncope.core.provisioning.api.pushpull.GroupPushResultHandler;
import org.apache.syncope.core.provisioning.api.pushpull.ProvisioningProfile;
import org.apache.syncope.core.provisioning.api.pushpull.PushActions;
import org.apache.syncope.core.provisioning.api.pushpull.RealmPushResultHandler;
import org.apache.syncope.core.provisioning.api.pushpull.SyncopePushResultHandler;
import org.apache.syncope.core.provisioning.api.pushpull.UserPushResultHandler;
import org.apache.syncope.core.spring.ApplicationContextProvider;
import org.apache.syncope.core.spring.implementation.ImplementationManager;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.support.AbstractBeanDefinition;
public class PushJobDelegate extends AbstractProvisioningJobDelegate<PushTask> {
/**
* Search DAO.
*/
@Autowired
protected AnySearchDAO searchDAO;
@Autowired
protected RealmDAO realmDAO;
@Autowired
protected AnyUtilsFactory anyUtilsFactory;
@Autowired
protected SearchCondVisitor searchCondVisitor;
protected ProvisioningProfile<PushTask, PushActions> profile;
protected final Map<String, MutablePair<Integer, String>> handled = new ConcurrentHashMap<>();
protected final Map<String, PushActions> perContextActions = new ConcurrentHashMap<>();
protected void reportHandled(final String anyType, final String key) {
MutablePair<Integer, String> pair = handled.get(anyType);
if (pair == null) {
pair = MutablePair.of(0, null);
handled.put(anyType, pair);
}
pair.setLeft(pair.getLeft() + 1);
pair.setRight(key);
if (!handled.isEmpty()) {
StringBuilder builder = new StringBuilder("Processed:\n");
handled.forEach((k, v) -> builder.append(' ').append(v.getLeft()).append('\t').
append(k).
append(" / latest: ").append(v.getRight()).
append('\n'));
setStatus(builder.toString());
}
}
protected void doHandle(
final List<? extends Any<?>> anys,
final SyncopePushResultHandler handler,
final ExternalResource resource)
throws JobExecutionException {
for (int i = 0; i < anys.size() && !interrupt; i++) {
try {
handler.handle(anys.get(i).getKey());
reportHandled(
anys.get(i).getType().getKey(),
(anys.get(i) instanceof User
? ((User) anys.get(i)).getUsername()
: anys.get(i) instanceof Group
? ((Group) anys.get(i)).getName()
: ((AnyObject) anys.get(i)).getName()));
} catch (Exception e) {
LOG.warn("Failure pushing '{}' on '{}'", anys.get(i), resource, e);
throw new JobExecutionException("While pushing " + anys.get(i) + " on " + resource, e);
}
}
}
protected RealmPushResultHandler buildRealmHandler() {
return (RealmPushResultHandler) ApplicationContextProvider.getBeanFactory().
createBean(DefaultRealmPushResultHandler.class, AbstractBeanDefinition.AUTOWIRE_BY_NAME, false);
}
protected AnyObjectPushResultHandler buildAnyObjectHandler() {
return (AnyObjectPushResultHandler) ApplicationContextProvider.getBeanFactory().
createBean(DefaultAnyObjectPushResultHandler.class, AbstractBeanDefinition.AUTOWIRE_BY_NAME, false);
}
protected UserPushResultHandler buildUserHandler() {
return (UserPushResultHandler) ApplicationContextProvider.getBeanFactory().
createBean(DefaultUserPushResultHandler.class, AbstractBeanDefinition.AUTOWIRE_BY_NAME, false);
}
protected GroupPushResultHandler buildGroupHandler() {
return (GroupPushResultHandler) ApplicationContextProvider.getBeanFactory().
createBean(DefaultGroupPushResultHandler.class, AbstractBeanDefinition.AUTOWIRE_BY_NAME, false);
}
protected List<PushActions> getPushActions(final List<? extends Implementation> impls) {
List<PushActions> result = new ArrayList<>();
impls.forEach(impl -> {
try {
result.add(ImplementationManager.build(
impl,
() -> perContextActions.get(impl.getKey()),
instance -> perContextActions.put(impl.getKey(), instance)));
} catch (Exception e) {
LOG.warn("While building {}", impl, e);
}
});
return result;
}
@Override
protected String doExecuteProvisioning(
final PushTask pushTask,
final Connector connector,
final boolean dryRun,
final JobExecutionContext context) throws JobExecutionException {
LOG.debug("Executing push on {}", pushTask.getResource());
profile = new ProvisioningProfile<>(connector, pushTask);
profile.getActions().addAll(getPushActions(pushTask.getActions()));
profile.setDryRun(dryRun);
profile.setConflictResolutionAction(pushTask.getResource().getPushPolicy() == null
? ConflictResolutionAction.IGNORE
: pushTask.getResource().getPushPolicy().getConflictResolutionAction());
if (!profile.isDryRun()) {
for (PushActions action : profile.getActions()) {
action.beforeAll(profile);
}
}
setStatus("Initialization completed");
// First realms...
if (pushTask.getResource().getOrgUnit() != null) {
setStatus("Pushing realms");
RealmPushResultHandler handler = buildRealmHandler();
handler.setProfile(profile);
for (Realm realm : realmDAO.findDescendants(profile.getTask().getSourceRealm())) {
// Never push the root realm
if (realm.getParent() != null) {
try {
handler.handle(realm.getKey());
reportHandled(SyncopeConstants.REALM_ANYTYPE, realm.getName());
} catch (Exception e) {
LOG.warn("Failure pushing '{}' on '{}'", realm, pushTask.getResource(), e);
throw new JobExecutionException("While pushing " + realm + " on " + pushTask.getResource(), e);
}
}
}
}
// ...then provisions for any types
ProvisionSorter provisionSorter = getProvisionSorter(pushTask);
for (Provision provision : pushTask.getResource().getProvisions().stream().
filter(provision -> provision.getMapping() != null).sorted(provisionSorter).
collect(Collectors.toList())) {
setStatus("Pushing " + provision.getAnyType());
AnyDAO<?> anyDAO = anyUtilsFactory.getInstance(provision.getAnyType().getKind()).dao();
SyncopePushResultHandler handler;
switch (provision.getAnyType().getKind()) {
case USER:
handler = buildUserHandler();
break;
case GROUP:
handler = buildGroupHandler();
break;
case ANY_OBJECT:
default:
handler = buildAnyObjectHandler();
}
handler.setProfile(profile);
Optional<? extends PushTaskAnyFilter> anyFilter = pushTask.getFilter(provision.getAnyType());
String filter = anyFilter.isPresent()
? anyFilter.get().getFIQLCond()
: null;
SearchCond cond = StringUtils.isBlank(filter)
? anyDAO.getAllMatchingCond()
: SearchCondConverter.convert(searchCondVisitor, filter);
int count = searchDAO.count(
Collections.singleton(profile.getTask().getSourceRealm().getFullPath()),
cond,
provision.getAnyType().getKind());
for (int page = 1; page <= (count / AnyDAO.DEFAULT_PAGE_SIZE) + 1 && !interrupt; page++) {
List<? extends Any<?>> anys = searchDAO.search(
Collections.singleton(profile.getTask().getSourceRealm().getFullPath()),
cond,
page,
AnyDAO.DEFAULT_PAGE_SIZE,
Collections.emptyList(),
provision.getAnyType().getKind());
doHandle(anys, handler, pushTask.getResource());
}
}
if (!profile.isDryRun() && !interrupt) {
for (PushActions action : profile.getActions()) {
action.afterAll(profile);
}
}
if (interrupt) {
interrupted = true;
}
setStatus("Push done");
String result = createReport(profile.getResults(), pushTask.getResource(), dryRun);
LOG.debug("Push result: {}", result);
return result;
}
}