Skip to content

Package: JobContextWrapper

JobContextWrapper

nameinstructionbranchcomplexitylinemethod
JobContextWrapper(JobContext)
M: 6 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 3 C: 0
0%
M: 1 C: 0
0%
getBatchStatus()
M: 4 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 1 C: 0
0%
M: 1 C: 0
0%
getEnqueue()
M: 8 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 2 C: 0
0%
M: 1 C: 0
0%
getExecutionId()
M: 4 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 1 C: 0
0%
M: 1 C: 0
0%
getExitStatus()
M: 4 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 1 C: 0
0%
M: 1 C: 0
0%
getFromStepIndex()
M: 13 C: 0
0%
M: 2 C: 0
0%
M: 2 C: 0
0%
M: 2 C: 0
0%
M: 1 C: 0
0%
getInstanceId()
M: 4 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 1 C: 0
0%
M: 1 C: 0
0%
getJobId()
M: 12 C: 0
0%
M: 2 C: 0
0%
M: 2 C: 0
0%
M: 2 C: 0
0%
M: 1 C: 0
0%
getJobLogger()
M: 22 C: 0
0%
M: 2 C: 0
0%
M: 2 C: 0
0%
M: 5 C: 0
0%
M: 1 C: 0
0%
getJobName()
M: 4 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 1 C: 0
0%
M: 1 C: 0
0%
getJobStepPropertiesOverrides()
M: 18 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 4 C: 0
0%
M: 1 C: 0
0%
getJobTransientUserData()
M: 17 C: 0
0%
M: 2 C: 0
0%
M: 2 C: 0
0%
M: 4 C: 0
0%
M: 1 C: 0
0%
getKapuaExecutionId()
M: 6 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 1 C: 0
0%
M: 1 C: 0
0%
getProperties()
M: 4 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 1 C: 0
0%
M: 1 C: 0
0%
getResetStepIndex()
M: 8 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 2 C: 0
0%
M: 1 C: 0
0%
getResumedJobExecutionId()
M: 13 C: 0
0%
M: 2 C: 0
0%
M: 2 C: 0
0%
M: 2 C: 0
0%
M: 1 C: 0
0%
getScopeId()
M: 12 C: 0
0%
M: 2 C: 0
0%
M: 2 C: 0
0%
M: 2 C: 0
0%
M: 1 C: 0
0%
getTargetSublist()
M: 18 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 4 C: 0
0%
M: 1 C: 0
0%
getTransientUserData()
M: 4 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 1 C: 0
0%
M: 1 C: 0
0%
setExitStatus(String)
M: 5 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 2 C: 0
0%
M: 1 C: 0
0%
setJobTransientUserData(JobTransientUserData)
M: 4 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 2 C: 0
0%
M: 1 C: 0
0%
setKapuaExecutionId(KapuaId)
M: 7 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 2 C: 0
0%
M: 1 C: 0
0%
setTransientUserData(Object)
M: 5 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 2 C: 0
0%
M: 1 C: 0
0%
static {...}
M: 4 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 1 C: 0
0%
M: 1 C: 0
0%

Coverage

1: /*******************************************************************************
2: * Copyright (c) 2017, 2022 Eurotech and/or its affiliates and others
3: *
4: * This program and the accompanying materials are made
5: * available under the terms of the Eclipse Public License 2.0
6: * which is available at https://www.eclipse.org/legal/epl-2.0/
7: *
8: * SPDX-License-Identifier: EPL-2.0
9: *
10: * Contributors:
11: * Eurotech - initial API and implementation
12: *******************************************************************************/
13: package org.eclipse.kapua.job.engine.commons.wrappers;
14:
15: import com.google.common.base.Strings;
16: import org.eclipse.kapua.commons.model.id.KapuaEid;
17: import org.eclipse.kapua.commons.util.xml.XmlUtil;
18: import org.eclipse.kapua.job.engine.commons.exception.ReadJobPropertyException;
19: import org.eclipse.kapua.job.engine.commons.logger.JobLogger;
20: import org.eclipse.kapua.job.engine.commons.model.JobStepPropertiesOverrides;
21: import org.eclipse.kapua.job.engine.commons.model.JobTargetSublist;
22: import org.eclipse.kapua.job.engine.commons.model.JobTransientUserData;
23: import org.eclipse.kapua.model.id.KapuaId;
24: import org.eclipse.kapua.service.job.execution.JobExecution;
25: import org.slf4j.Logger;
26: import org.slf4j.LoggerFactory;
27: import org.xml.sax.SAXException;
28:
29: import javax.batch.runtime.BatchStatus;
30: import javax.batch.runtime.context.JobContext;
31: import javax.xml.bind.JAXBException;
32: import java.util.Properties;
33:
34: /**
35: * {@link JobContextWrapper} wraps the {@link JobContext} and offers utility methods around it.
36: *
37: * @since 1.0.0
38: */
39: public class JobContextWrapper {
40:
41: private static final Logger LOG = LoggerFactory.getLogger(JobContextWrapper.class);
42:
43: private JobContext jobContext;
44:
45: /**
46: * Constructor from the {@code inject}ed {@link JobContext}.
47: * <p>
48: * Wraps the given {@link JobContext}
49: *
50: * @param jobContext The {@link JobContext} to wrap.
51: * @since 1.1.0
52: */
53: public JobContextWrapper(JobContext jobContext) {
54: this.jobContext = jobContext;
55: }
56:
57: /**
58: * Gets the scope {@link KapuaId} of the {@link org.eclipse.kapua.service.job.execution.JobExecution}.
59: *
60: * @return The current scope {@link KapuaId} of the {@link org.eclipse.kapua.service.job.execution.JobExecution}.
61: * @since 1.0.0
62: */
63: public KapuaId getScopeId() {
64: String scopeIdString = getProperties().getProperty(JobContextPropertyNames.JOB_SCOPE_ID);
65:• return scopeIdString != null ? KapuaEid.parseCompactId(scopeIdString) : null;
66: }
67:
68: /**
69: * Gets the {@link org.eclipse.kapua.service.job.Job} {@link KapuaId} of the {@link org.eclipse.kapua.service.job.execution.JobExecution}.
70: *
71: * @return The current {@link org.eclipse.kapua.service.job.Job} {@link KapuaId} of the {@link org.eclipse.kapua.service.job.execution.JobExecution}.
72: * @since 1.0.0
73: */
74: public KapuaId getJobId() {
75: String jobIdString = getProperties().getProperty(JobContextPropertyNames.JOB_ID);
76:• return jobIdString != null ? KapuaEid.parseCompactId(jobIdString) : null;
77: }
78:
79: /**
80: * Gets the {@link JobTargetSublist} of the {@link org.eclipse.kapua.service.job.execution.JobExecution}.
81: *
82: * @return The current {@link JobTargetSublist} of the {@link org.eclipse.kapua.service.job.execution.JobExecution}.
83: * @since 1.0.0
84: */
85: public JobTargetSublist getTargetSublist() {
86: String jobTargetSublistString = getProperties().getProperty(JobContextPropertyNames.JOB_TARGET_SUBLIST);
87:
88: try {
89: return XmlUtil.unmarshal(jobTargetSublistString, JobTargetSublist.class);
90: } catch (JAXBException | SAXException e) {
91: throw new ReadJobPropertyException(e, JobContextPropertyNames.JOB_TARGET_SUBLIST, jobTargetSublistString);
92: }
93: }
94:
95: /**
96: * Gets the {@link JobStepPropertiesOverrides} of the {@link org.eclipse.kapua.service.job.execution.JobExecution}.
97: *
98: * @return The current {@link JobStepPropertiesOverrides} of the {@link org.eclipse.kapua.service.job.execution.JobExecution}.
99: * @since 2.0.0
100: */
101: public JobStepPropertiesOverrides getJobStepPropertiesOverrides() {
102: String jobStepPropertiesOverrides = getProperties().getProperty(JobContextPropertyNames.JOB_STEP_PROPERTIES_OVERRIDES);
103:
104: try {
105: return XmlUtil.unmarshal(jobStepPropertiesOverrides, JobStepPropertiesOverrides.class);
106: } catch (JAXBException | SAXException e) {
107: throw new ReadJobPropertyException(e, JobContextPropertyNames.JOB_STEP_PROPERTIES_OVERRIDES, jobStepPropertiesOverrides);
108: }
109: }
110:
111: /**
112: * Gets the {@link JobExecution} {@link KapuaId} to resume
113: *
114: * @return The {@link JobExecution} {@link KapuaId} to resume
115: * @since 1.1.0
116: */
117: public KapuaId getResumedJobExecutionId() {
118: String resumedKapuaExecutionIdString = getProperties().getProperty(JobContextPropertyNames.RESUMED_KAPUA_EXECUTION_ID);
119:• return Strings.isNullOrEmpty(resumedKapuaExecutionIdString) ? null : KapuaEid.parseCompactId(resumedKapuaExecutionIdString);
120: }
121:
122: /**
123: * Gets whether or not the {@link org.eclipse.kapua.service.job.targets.JobTarget}s needs to be reset to the given {@link #getFromStepIndex()}.
124: *
125: * @return {@code true} if the {@link org.eclipse.kapua.service.job.targets.JobTarget}s needs to be reset to the given {@link #getFromStepIndex()}, {@code false} otherwise.
126: * @since 1.1.0
127: */
128: public boolean getResetStepIndex() {
129: String resetFromIndexString = getProperties().getProperty(JobContextPropertyNames.RESET_STEP_INDEX);
130: return Boolean.parseBoolean(resetFromIndexString);
131: }
132:
133: /**
134: * Gets the start step index of the {@link org.eclipse.kapua.service.job.execution.JobExecution}.
135: *
136: * @return The start step index of the {@link org.eclipse.kapua.service.job.execution.JobExecution}.
137: * @since 1.0.0
138: */
139: public Integer getFromStepIndex() {
140: String fromStepIndexString = getProperties().getProperty(JobContextPropertyNames.JOB_STEP_FROM_INDEX);
141:• return Strings.isNullOrEmpty(fromStepIndexString) ? null : Integer.valueOf(fromStepIndexString);
142: }
143:
144: /**
145: * Gets whether or not this {@link JobExecution} should be enqueued or not.
146: *
147: * @return {@code true} if this {@link JobExecution} should be enqueued, {@code false} otherwise.
148: * @since 1.1.0
149: */
150: public boolean getEnqueue() {
151: String enqueueString = getProperties().getProperty(JobContextPropertyNames.ENQUEUE);
152: return Boolean.parseBoolean(enqueueString);
153: }
154:
155: /**
156: * Gets the {@link JobTransientUserData}.
157: * <p>
158: * If does not exists at the moment, it will instantiate a default {@link JobTransientUserData} and return it.
159: *
160: * @return The {@link JobTransientUserData}.
161: * @since 1.1.0
162: */
163: public <J extends JobTransientUserData> J getJobTransientUserData() {
164:• if (getTransientUserData() == null) {
165: LOG.warn("No JobTransientData has been defined. Using the default one: {}", JobTransientUserData.class.getName());
166: setTransientUserData(new JobTransientUserData());
167: }
168:
169: return (J) getTransientUserData();
170: }
171:
172: /**
173: * Sets the {@link JobTransientUserData}
174: *
175: * @param jobTransientUserData The {@link JobTransientUserData}.
176: * @since 1.1.0
177: */
178: public <J extends JobTransientUserData> void setJobTransientUserData(J jobTransientUserData) {
179: setTransientUserData(jobTransientUserData);
180: }
181:
182: /**
183: * Gets the {@link JobLogger}.
184: * <p>
185: * If it does not exist, it instantiates a new one.
186: *
187: * @return The {@link JobLogger}.
188: * @since 1.1.0
189: */
190: public JobLogger getJobLogger() {
191:
192: JobLogger jobLogger = getJobTransientUserData().getJobLogger();
193:
194:• if (jobLogger == null) {
195: jobLogger = new JobLogger(getScopeId(), getJobId(), getJobName());
196: getJobTransientUserData().setJobLogger(jobLogger);
197: }
198:
199: return jobLogger;
200: }
201:
202: /**
203: * @return {@link JobContext#getJobName()}.
204: * @see JobContext#getJobName
205: * @since 1.0.0
206: */
207: public String getJobName() {
208: return jobContext.getJobName();
209: }
210:
211: /**
212: * @return {@link JobContext#getTransientUserData()}.
213: * @see JobContext#getTransientUserData().
214: * @since 1.0.0
215: */
216: private Object getTransientUserData() {
217: return jobContext.getTransientUserData();
218: }
219:
220: /**
221: * @param data {@link JobContext#setTransientUserData(Object)}.
222: * @see JobContext#setTransientUserData(Object).
223: * @since 1.0.0
224: */
225: private void setTransientUserData(Object data) {
226: jobContext.setTransientUserData(data);
227: }
228:
229: /**
230: * @return {@link JobContext#getInstanceId()}.
231: * @see JobContext#getInstanceId
232: * @since 1.0.0
233: */
234: public long getInstanceId() {
235: return jobContext.getInstanceId();
236: }
237:
238: /**
239: * @return {@link JobContext#getExecutionId()}.
240: * @see JobContext#getExecutionId
241: * @since 1.0.0
242: */
243: public long getExecutionId() {
244: return jobContext.getExecutionId();
245: }
246:
247: /**
248: * @return {@link JobContext#getProperties()}.
249: * @see JobContext#getProperties
250: * @since 1.0.0
251: */
252: public Properties getProperties() {
253: return jobContext.getProperties();
254: }
255:
256: /**
257: * @return {@link JobContext#getBatchStatus()}.
258: * @see JobContext#getBatchStatus
259: * @since 1.0.0
260: */
261: public BatchStatus getBatchStatus() {
262: return jobContext.getBatchStatus();
263: }
264:
265: /**
266: * @return {@link JobContext#getExitStatus()}.
267: * @see JobContext#getExitStatus
268: * @since 1.0.0
269: */
270: public String getExitStatus() {
271: return jobContext.getExitStatus();
272: }
273:
274: /**
275: * @param status {@link JobContext#setExitStatus(String)}.
276: * @see JobContext#setExitStatus(String)
277: * @since 1.0.0
278: */
279: public void setExitStatus(String status) {
280: jobContext.setExitStatus(status);
281: }
282:
283: /**
284: * Gets the current {@link JobExecution#getId()}.
285: *
286: * @return The current {@link JobExecution#getId()}.
287: * @since 1.0.0
288: */
289: public KapuaId getKapuaExecutionId() {
290: return (KapuaId) getProperties().get(JobContextPropertyNames.KAPUA_EXECUTION_ID);
291: }
292:
293: /**
294: * Sets the current {@link JobExecution#getId()}.
295: *
296: * @param kapuaExecutionId The current {@link JobExecution#getId()}.
297: * @since 1.0.0
298: */
299: public void setKapuaExecutionId(KapuaId kapuaExecutionId) {
300: getProperties().put(JobContextPropertyNames.KAPUA_EXECUTION_ID, kapuaExecutionId);
301: }
302: }