/
JobServiceImpl.java
executable file
·270 lines (228 loc) · 10.1 KB
/
JobServiceImpl.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
/*******************************************************************************
* Copyright (c) 2017, 2022 Eurotech and/or its affiliates and others
*
* This program and the accompanying materials are made
* available under the terms of the Eclipse Public License 2.0
* which is available at https://www.eclipse.org/legal/epl-2.0/
*
* SPDX-License-Identifier: EPL-2.0
*
* Contributors:
* Eurotech - initial API and implementation
*******************************************************************************/
package org.eclipse.kapua.service.job.internal;
import org.eclipse.kapua.KapuaDuplicateNameException;
import org.eclipse.kapua.KapuaEntityNotFoundException;
import org.eclipse.kapua.KapuaException;
import org.eclipse.kapua.commons.configuration.AbstractKapuaConfigurableResourceLimitedService;
import org.eclipse.kapua.commons.security.KapuaSecurityUtils;
import org.eclipse.kapua.commons.util.ArgumentValidator;
import org.eclipse.kapua.job.engine.JobEngineService;
import org.eclipse.kapua.locator.KapuaLocator;
import org.eclipse.kapua.locator.KapuaProvider;
import org.eclipse.kapua.model.KapuaEntityAttributes;
import org.eclipse.kapua.model.KapuaNamedEntityAttributes;
import org.eclipse.kapua.model.domain.Actions;
import org.eclipse.kapua.model.id.KapuaId;
import org.eclipse.kapua.model.query.KapuaQuery;
import org.eclipse.kapua.model.query.predicate.AndPredicate;
import org.eclipse.kapua.model.query.predicate.AttributePredicate.Operator;
import org.eclipse.kapua.service.authorization.AuthorizationService;
import org.eclipse.kapua.service.authorization.permission.PermissionFactory;
import org.eclipse.kapua.service.job.Job;
import org.eclipse.kapua.service.job.JobCreator;
import org.eclipse.kapua.service.job.JobDomains;
import org.eclipse.kapua.service.job.JobFactory;
import org.eclipse.kapua.service.job.JobListResult;
import org.eclipse.kapua.service.job.JobQuery;
import org.eclipse.kapua.service.job.JobService;
import org.eclipse.kapua.service.scheduler.trigger.Trigger;
import org.eclipse.kapua.service.scheduler.trigger.TriggerAttributes;
import org.eclipse.kapua.service.scheduler.trigger.TriggerFactory;
import org.eclipse.kapua.service.scheduler.trigger.TriggerListResult;
import org.eclipse.kapua.service.scheduler.trigger.TriggerQuery;
import org.eclipse.kapua.service.scheduler.trigger.TriggerService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import javax.inject.Inject;
/**
* {@link JobService} implementation
*
* @since 1.0.0
*/
@KapuaProvider
public class JobServiceImpl extends AbstractKapuaConfigurableResourceLimitedService<Job, JobCreator, JobService, JobListResult, JobQuery, JobFactory> implements JobService {
private static final Logger LOG = LoggerFactory.getLogger(JobServiceImpl.class);
private static final KapuaLocator LOCATOR = KapuaLocator.getInstance();
@Inject
private AuthorizationService authorizationService;
@Inject
private PermissionFactory permissionFactory;
private final JobEngineService jobEngineService = LOCATOR.getService(JobEngineService.class);
@Inject
private TriggerService triggerService;
@Inject
private TriggerFactory triggerFactory;
public JobServiceImpl() {
super(JobService.class.getName(), JobDomains.JOB_DOMAIN, JobEntityManagerFactory.getInstance(), JobService.class, JobFactory.class);
}
@Override
public Job create(JobCreator creator) throws KapuaException {
//
// Argument validation
ArgumentValidator.notNull(creator, "jobCreator");
ArgumentValidator.notNull(creator.getScopeId(), "jobCreator.scopeId");
ArgumentValidator.validateJobName(creator.getName(), "jobCreator.name");
//
// Check access
authorizationService.checkPermission(permissionFactory.newPermission(JobDomains.JOB_DOMAIN, Actions.write, creator.getScopeId()));
//
// Check entity limit
checkAllowedEntities(creator.getScopeId(), "Jobs");
//
// Check duplicate name
JobQuery query = new JobQueryImpl(creator.getScopeId());
query.setPredicate(query.attributePredicate(KapuaNamedEntityAttributes.NAME, creator.getName()));
if (count(query) > 0) {
throw new KapuaDuplicateNameException(creator.getName());
}
//
// Do create
return entityManagerSession.doTransactedAction(em -> JobDAO.create(em, creator));
}
@Override
public Job update(Job job) throws KapuaException {
//
// Argument Validation
ArgumentValidator.notNull(job, "job");
ArgumentValidator.notNull(job.getScopeId(), "job.scopeId");
ArgumentValidator.validateEntityName(job.getName(), "job.name");
//
// Check access
authorizationService.checkPermission(permissionFactory.newPermission(JobDomains.JOB_DOMAIN, Actions.write, job.getScopeId()));
//
// Check existence
if (find(job.getScopeId(), job.getId()) == null) {
throw new KapuaEntityNotFoundException(Job.TYPE, job.getId());
}
//
// Check duplicate name
JobQuery query = new JobQueryImpl(job.getScopeId());
query.setPredicate(
query.andPredicate(
query.attributePredicate(KapuaNamedEntityAttributes.NAME, job.getName()),
query.attributePredicate(KapuaEntityAttributes.ENTITY_ID, job.getId(), Operator.NOT_EQUAL)
)
);
if (count(query) > 0) {
throw new KapuaDuplicateNameException(job.getName());
}
//
// Do update
return entityManagerSession.doTransactedAction(em -> JobDAO.update(em, job));
}
@Override
public Job find(KapuaId scopeId, KapuaId jobId) throws KapuaException {
//
// Argument Validation
ArgumentValidator.notNull(scopeId, "scopeId");
ArgumentValidator.notNull(jobId, KapuaEntityAttributes.ENTITY_ID);
//
// Check Access
authorizationService.checkPermission(permissionFactory.newPermission(JobDomains.JOB_DOMAIN, Actions.read, scopeId));
//
// Do find
return entityManagerSession.doAction(em -> JobDAO.find(em, scopeId, jobId));
}
@Override
public JobListResult query(KapuaQuery query) throws KapuaException {
//
// Argument Validation
ArgumentValidator.notNull(query, "query");
//
// Check Access
authorizationService.checkPermission(permissionFactory.newPermission(JobDomains.JOB_DOMAIN, Actions.read, query.getScopeId()));
//
// Do query
return entityManagerSession.doAction(em -> JobDAO.query(em, query));
}
@Override
public long count(KapuaQuery query) throws KapuaException {
//
// Argument Validation
ArgumentValidator.notNull(query, "query");
//
// Check Access
authorizationService.checkPermission(permissionFactory.newPermission(JobDomains.JOB_DOMAIN, Actions.read, query.getScopeId()));
//
// Do query
return entityManagerSession.doAction(em -> JobDAO.count(em, query));
}
@Override
public void delete(KapuaId scopeId, KapuaId jobId) throws KapuaException {
deleteInternal(scopeId, jobId, false);
}
@Override
public void deleteForced(KapuaId scopeId, KapuaId jobId) throws KapuaException {
deleteInternal(scopeId, jobId, true);
}
//
// Private methods
//
/**
* Deletes the {@link Job} like {@link #delete(KapuaId, KapuaId)}.
* <p>
* If {@code forced} is {@code true} {@link org.eclipse.kapua.service.authorization.permission.Permission} checked will be {@code job:delete:null},
* and when invoking {@link JobEngineService#cleanJobData(KapuaId, KapuaId)} any exception is logged and ignored.
*
* @param scopeId The {@link KapuaId} scopeId of the {@link Job}.
* @param jobId The {@link KapuaId} of the {@link Job}.
* @param forced Whether or not the {@link Job} must be forcibly deleted.
* @throws KapuaException In case something bad happens.
* @since 1.1.0
*/
private void deleteInternal(KapuaId scopeId, KapuaId jobId, boolean forced) throws KapuaException {
//
// Argument Validation
ArgumentValidator.notNull(scopeId, "scopeId");
ArgumentValidator.notNull(jobId, KapuaEntityAttributes.ENTITY_ID);
//
// Check Access
authorizationService.checkPermission(permissionFactory.newPermission(JobDomains.JOB_DOMAIN, Actions.delete, forced ? null : scopeId));
//
// Check existence
if (find(scopeId, jobId) == null) {
throw new KapuaEntityNotFoundException(Job.TYPE, jobId);
}
//
// Find all the triggers that are associated with this job
TriggerQuery query = triggerFactory.newQuery(scopeId);
AndPredicate andPredicate = query.andPredicate(
query.attributePredicate(TriggerAttributes.TRIGGER_PROPERTIES_NAME, "jobId"),
query.attributePredicate(TriggerAttributes.TRIGGER_PROPERTIES_VALUE, jobId.toCompactId()),
query.attributePredicate(TriggerAttributes.TRIGGER_PROPERTIES_TYPE, KapuaId.class.getName())
);
query.setPredicate(andPredicate);
//
// Query for and delete all the triggers that are associated with this job
KapuaSecurityUtils.doPrivileged(() -> {
TriggerListResult triggers = triggerService.query(query);
for (Trigger trig : triggers.getItems()) {
triggerService.delete(trig.getScopeId(), trig.getId());
}
});
//
// Do delete
try {
KapuaSecurityUtils.doPrivileged(() -> jobEngineService.cleanJobData(scopeId, jobId));
} catch (Exception e) {
if (forced) {
LOG.warn("Error while cleaning Job data. Ignoring exception since delete is forced! Error: {}", e.getMessage());
LOG.debug("Error while cleaning Job data. Ignoring exception since delete is forced!", e);
} else {
throw e;
}
}
entityManagerSession.doTransactedAction(em -> JobDAO.delete(em, scopeId, jobId));
}
}