Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

XCOMMONS-164: Make possible to access extension plan jobs by id

  • Loading branch information...
commit 5c0b728d4f775f1f46b2bc9054309cc54605a0fd 1 parent d07b9b1
Thomas Mortagne tmortagne authored
24 ...wiki-commons-extension-api/src/main/java/org/xwiki/extension/job/plan/internal/DefaultExtensionPlan.java
@@ -30,7 +30,7 @@
30 30 import org.xwiki.extension.job.plan.ExtensionPlan;
31 31 import org.xwiki.extension.job.plan.ExtensionPlanAction;
32 32 import org.xwiki.extension.job.plan.ExtensionPlanNode;
33   -import org.xwiki.job.internal.DefaultJobStatus;
  33 +import org.xwiki.job.internal.AbstractJobStatus;
34 34 import org.xwiki.logging.LoggerManager;
35 35 import org.xwiki.observation.ObservationManager;
36 36
@@ -41,25 +41,19 @@
41 41 * @version $Id$
42 42 * @since 4.0M1
43 43 */
44   -// TODO: not really serializable
45   -public class DefaultExtensionPlan<R extends ExtensionRequest> extends DefaultJobStatus<R> implements ExtensionPlan
  44 +public class DefaultExtensionPlan<R extends ExtensionRequest> extends AbstractJobStatus<R> implements ExtensionPlan
46 45 {
47 46 /**
48   - * Serialization identifier.
49   - */
50   - private static final long serialVersionUID = 1L;
51   -
52   - /**
53 47 * @see #getTree()
54 48 */
55   - // TODO: find a way to serialize
  49 + // TODO: find a way to serialize before making DefaultExtensionPlan Serializable (the main issue is the Extension
  50 + // objects in the nodes)
56 51 private transient List<ExtensionPlanNode> tree = new ArrayList<ExtensionPlanNode>();
57 52
58 53 /**
59 54 * @see #getActions()
60 55 */
61   - // TODO: find a way to serialize
62   - private transient Set<ExtensionPlanAction> actions;
  56 + private transient Set<ExtensionPlanAction> actionsCache;
63 57
64 58 /**
65 59 * @param request the request provided when started the job
@@ -105,12 +99,12 @@ private void fillExtensionActions(Set<ExtensionPlanAction> extensions, Collectio
105 99
106 100 return extensions;
107 101 } else {
108   - if (this.actions == null) {
109   - this.actions = new LinkedHashSet<ExtensionPlanAction>();
110   - fillExtensionActions(this.actions, this.tree);
  102 + if (this.actionsCache == null) {
  103 + this.actionsCache = new LinkedHashSet<ExtensionPlanAction>();
  104 + fillExtensionActions(this.actionsCache, this.tree);
111 105 }
112 106
113   - return Collections.unmodifiableCollection(this.actions);
  107 + return Collections.unmodifiableCollection(this.actionsCache);
114 108 }
115 109 }
116 110 }
15 xwiki-commons-core/xwiki-commons-job/pom.xml
@@ -87,4 +87,19 @@
87 87 <scope>test</scope>
88 88 </dependency>
89 89 </dependencies>
  90 +
  91 + <build>
  92 + <plugins>
  93 + <plugin>
  94 + <!-- Apply the Checkstyle configurations defined in the top level pom.xml file -->
  95 + <groupId>org.apache.maven.plugins</groupId>
  96 + <artifactId>maven-checkstyle-plugin</artifactId>
  97 + <configuration>
  98 + <excludes>
  99 + **/AbstractJob.java
  100 + </excludes>
  101 + </configuration>
  102 + </plugin>
  103 + </plugins>
  104 + </build>
90 105 </project>
4 xwiki-commons-core/xwiki-commons-job/src/main/java/org/xwiki/job/AbstractJob.java
@@ -19,6 +19,7 @@
19 19 */
20 20 package org.xwiki.job;
21 21
  22 +import java.io.Serializable;
22 23 import java.util.Date;
23 24 import java.util.concurrent.TimeUnit;
24 25 import java.util.concurrent.locks.Condition;
@@ -37,6 +38,7 @@
37 38 import org.xwiki.job.event.status.PopLevelProgressEvent;
38 39 import org.xwiki.job.event.status.PushLevelProgressEvent;
39 40 import org.xwiki.job.event.status.StepProgressEvent;
  41 +import org.xwiki.job.internal.AbstractJobStatus;
40 42 import org.xwiki.job.internal.DefaultJobStatus;
41 43 import org.xwiki.job.internal.JobStatusStorage;
42 44 import org.xwiki.logging.LoggerManager;
@@ -96,7 +98,7 @@
96 98 /**
97 99 * @see #getStatus()
98 100 */
99   - protected DefaultJobStatus<R> status;
  101 + protected AbstractJobStatus<R> status;
100 102
101 103 /**
102 104 * Main lock guarding all access.
257 xwiki-commons-core/xwiki-commons-job/src/main/java/org/xwiki/job/internal/AbstractJobStatus.java
... ... @@ -0,0 +1,257 @@
  1 +/*
  2 + * See the NOTICE file distributed with this work for additional
  3 + * information regarding copyright ownership.
  4 + *
  5 + * This is free software; you can redistribute it and/or modify it
  6 + * under the terms of the GNU Lesser General Public License as
  7 + * published by the Free Software Foundation; either version 2.1 of
  8 + * the License, or (at your option) any later version.
  9 + *
  10 + * This software is distributed in the hope that it will be useful,
  11 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  13 + * Lesser General Public License for more details.
  14 + *
  15 + * You should have received a copy of the GNU Lesser General Public
  16 + * License along with this software; if not, write to the Free
  17 + * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
  18 + * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
  19 + */
  20 +package org.xwiki.job.internal;
  21 +
  22 +import java.util.ArrayList;
  23 +import java.util.Date;
  24 +import java.util.List;
  25 +import java.util.concurrent.locks.Condition;
  26 +import java.util.concurrent.locks.ReentrantLock;
  27 +
  28 +import org.xwiki.job.Request;
  29 +import org.xwiki.job.event.status.JobProgress;
  30 +import org.xwiki.job.event.status.JobStatus;
  31 +import org.xwiki.logging.LogLevel;
  32 +import org.xwiki.logging.LogQueue;
  33 +import org.xwiki.logging.LoggerManager;
  34 +import org.xwiki.logging.event.LogEvent;
  35 +import org.xwiki.logging.event.LogQueueListener;
  36 +import org.xwiki.observation.ObservationManager;
  37 +
  38 +/**
  39 + * Base implementation of {@link JobStatus}.
  40 + *
  41 + * @param <R>
  42 + * @version $Id$
  43 + * @since 4.0M1
  44 + */
  45 +public abstract class AbstractJobStatus<R extends Request> implements JobStatus
  46 +{
  47 + /**
  48 + * Used register itself to receive logging and progress related events.
  49 + */
  50 + private transient ObservationManager observationManager;
  51 +
  52 + /**
  53 + * Used to isolate job related log.
  54 + */
  55 + private transient LoggerManager loggerManager;
  56 +
  57 + /**
  58 + * The unique id of the job.
  59 + */
  60 + private String id;
  61 +
  62 + /**
  63 + * General state of the job.
  64 + */
  65 + private State state = State.NONE;
  66 +
  67 + /**
  68 + * Request provided when starting the job.
  69 + */
  70 + private R request;
  71 +
  72 + /**
  73 + * Log sent during job execution.
  74 + */
  75 + private LogQueue logs = new LogQueue();
  76 +
  77 + /**
  78 + * @see #getStartDate()
  79 + */
  80 + private Date startDate;
  81 +
  82 + /**
  83 + * @see #getEndDate()
  84 + */
  85 + private Date endDate;
  86 +
  87 + /**
  88 + * Used to lock #ask().
  89 + */
  90 + private final transient ReentrantLock askLock = new ReentrantLock();
  91 +
  92 + /**
  93 + * Condition for waiting answer.
  94 + */
  95 + private final transient Condition answered = this.askLock.newCondition();
  96 +
  97 + /**
  98 + * The question.
  99 + */
  100 + private transient volatile Object question;
  101 +
  102 + /**
  103 + * Take care of progress related events to produce a progression information usually used in a progress bar.
  104 + */
  105 + private transient DefaultJobProgress progress;
  106 +
  107 + /**
  108 + * @param request the request provided when started the job
  109 + * @param id the unique id of the job
  110 + * @param observationManager the observation manager component
  111 + * @param loggerManager the logger manager component
  112 + */
  113 + public AbstractJobStatus(R request, String id, ObservationManager observationManager, LoggerManager loggerManager)
  114 + {
  115 + this.request = request;
  116 + this.observationManager = observationManager;
  117 + this.loggerManager = loggerManager;
  118 + this.id = id;
  119 +
  120 + this.progress = new DefaultJobProgress(this.id);
  121 + }
  122 +
  123 + /**
  124 + * Start listening to events.
  125 + */
  126 + public void startListening()
  127 + {
  128 + // Register progress listener
  129 + this.observationManager.addListener(this.progress);
  130 +
  131 + // Isolate log for the job status
  132 + this.loggerManager.pushLogListener(new LogQueueListener(LogQueueListener.class.getName() + '_' + this.id,
  133 + this.logs));
  134 + }
  135 +
  136 + /**
  137 + * Stop listening to events.
  138 + */
  139 + public void stopListening()
  140 + {
  141 + this.loggerManager.popLogListener();
  142 + this.observationManager.removeListener(this.progress.getName());
  143 + }
  144 +
  145 + // JobStatus
  146 +
  147 + @Override
  148 + public State getState()
  149 + {
  150 + return this.state;
  151 + }
  152 +
  153 + /**
  154 + * @param state the general state of the job
  155 + */
  156 + public void setState(State state)
  157 + {
  158 + this.state = state;
  159 + }
  160 +
  161 + @Override
  162 + public R getRequest()
  163 + {
  164 + return this.request;
  165 + }
  166 +
  167 + @Override
  168 + public LogQueue getLog()
  169 + {
  170 + return this.logs;
  171 + }
  172 +
  173 + @Override
  174 + public List<LogEvent> getLog(LogLevel level)
  175 + {
  176 + List<LogEvent> levelLogs = new ArrayList<LogEvent>();
  177 +
  178 + for (LogEvent log : this.logs) {
  179 + if (log.getLevel() == level) {
  180 + levelLogs.add(log);
  181 + }
  182 + }
  183 +
  184 + return levelLogs;
  185 + }
  186 +
  187 + @Override
  188 + public JobProgress getProgress()
  189 + {
  190 + return this.progress;
  191 + }
  192 +
  193 + @Override
  194 + public void ask(Object question) throws InterruptedException
  195 + {
  196 + this.question = question;
  197 +
  198 + this.askLock.lockInterruptibly();
  199 +
  200 + try {
  201 + // Wait for the answer
  202 + this.state = State.WAITING;
  203 + this.answered.await();
  204 + this.state = State.RUNNING;
  205 + } finally {
  206 + this.askLock.unlock();
  207 + }
  208 + }
  209 +
  210 + @Override
  211 + public Object getQuestion()
  212 + {
  213 + return this.question;
  214 + }
  215 +
  216 + @Override
  217 + public void answered()
  218 + {
  219 + this.askLock.lock();
  220 +
  221 + this.question = null;
  222 +
  223 + try {
  224 + this.answered.signal();
  225 + } finally {
  226 + this.askLock.unlock();
  227 + }
  228 + }
  229 +
  230 + @Override
  231 + public Date getStartDate()
  232 + {
  233 + return this.startDate;
  234 + }
  235 +
  236 + /**
  237 + * @param startDate the date and time when the job has been started
  238 + */
  239 + public void setStartDate(Date startDate)
  240 + {
  241 + this.startDate = startDate;
  242 + }
  243 +
  244 + @Override
  245 + public Date getEndDate()
  246 + {
  247 + return this.endDate;
  248 + }
  249 +
  250 + /**
  251 + * @param endDate the date and time when the job finished
  252 + */
  253 + public void setEndDate(Date endDate)
  254 + {
  255 + this.endDate = endDate;
  256 + }
  257 +}
217 xwiki-commons-core/xwiki-commons-job/src/main/java/org/xwiki/job/internal/DefaultJobStatus.java
@@ -20,30 +20,19 @@
20 20 package org.xwiki.job.internal;
21 21
22 22 import java.io.Serializable;
23   -import java.util.ArrayList;
24   -import java.util.Date;
25   -import java.util.List;
26   -import java.util.concurrent.locks.Condition;
27   -import java.util.concurrent.locks.ReentrantLock;
28 23
29 24 import org.xwiki.job.Request;
30   -import org.xwiki.job.event.status.JobProgress;
31   -import org.xwiki.job.event.status.JobStatus;
32   -import org.xwiki.logging.LogLevel;
33   -import org.xwiki.logging.LogQueue;
34 25 import org.xwiki.logging.LoggerManager;
35   -import org.xwiki.logging.event.LogEvent;
36   -import org.xwiki.logging.event.LogQueueListener;
37 26 import org.xwiki.observation.ObservationManager;
38 27
39 28 /**
40   - * Default implementation of {@link JobStatus}.
  29 + * Default implementation of {@link org.xwiki.job.event.status.JobStatus}.
41 30 *
42 31 * @param <R>
43 32 * @version $Id$
44 33 * @since 4.0M1
45 34 */
46   -public class DefaultJobStatus<R extends Request> implements JobStatus, Serializable
  35 +public class DefaultJobStatus<R extends Request> extends AbstractJobStatus<R> implements Serializable
47 36 {
48 37 /**
49 38 * Serialization identifier.
@@ -51,66 +40,6 @@
51 40 private static final long serialVersionUID = 1L;
52 41
53 42 /**
54   - * Used register itself to receive logging and progress related events.
55   - */
56   - private transient ObservationManager observationManager;
57   -
58   - /**
59   - * Used to isolate job related log.
60   - */
61   - private transient LoggerManager loggerManager;
62   -
63   - /**
64   - * The unique id of the job.
65   - */
66   - private String id;
67   -
68   - /**
69   - * General state of the job.
70   - */
71   - private State state = State.NONE;
72   -
73   - /**
74   - * Request provided when starting the job.
75   - */
76   - private R request;
77   -
78   - /**
79   - * Log sent during job execution.
80   - */
81   - private LogQueue logs = new LogQueue();
82   -
83   - /**
84   - * @see #getStartDate()
85   - */
86   - private Date startDate;
87   -
88   - /**
89   - * @see #getEndDate()
90   - */
91   - private Date endDate;
92   -
93   - /**
94   - * Used to lock #ask().
95   - */
96   - private final transient ReentrantLock askLock = new ReentrantLock();
97   -
98   - /**
99   - * Condition for waiting answer.
100   - */
101   - private final transient Condition answered = this.askLock.newCondition();
102   -
103   - /**
104   - * The question.
105   - */
106   - private transient volatile Object question;
107   -
108   - /**
109   - * Take care of progress related events to produce a progression information usually used in a progress bar.
110   - */
111   - private transient DefaultJobProgress progress;
112   -
113   - /**
114 43 * @param request the request provided when started the job
115 44 * @param id the unique id of the job
116 45 * @param observationManager the observation manager component
@@ -118,146 +47,6 @@
118 47 */
119 48 public DefaultJobStatus(R request, String id, ObservationManager observationManager, LoggerManager loggerManager)
120 49 {
121   - this.request = request;
122   - this.observationManager = observationManager;
123   - this.loggerManager = loggerManager;
124   - this.id = id;
125   -
126   - this.progress = new DefaultJobProgress(this.id);
127   - }
128   -
129   - /**
130   - * Start listening to events.
131   - */
132   - public void startListening()
133   - {
134   - // Register progress listener
135   - this.observationManager.addListener(this.progress);
136   -
137   - // Isolate log for the job status
138   - this.loggerManager.pushLogListener(new LogQueueListener(LogQueueListener.class.getName() + '_' + this.id,
139   - this.logs));
140   - }
141   -
142   - /**
143   - * Stop listening to events.
144   - */
145   - public void stopListening()
146   - {
147   - this.loggerManager.popLogListener();
148   - this.observationManager.removeListener(this.progress.getName());
149   - }
150   -
151   - // JobStatus
152   -
153   - @Override
154   - public State getState()
155   - {
156   - return this.state;
157   - }
158   -
159   - /**
160   - * @param state the general state of the job
161   - */
162   - public void setState(State state)
163   - {
164   - this.state = state;
165   - }
166   -
167   - @Override
168   - public R getRequest()
169   - {
170   - return this.request;
171   - }
172   -
173   - @Override
174   - public LogQueue getLog()
175   - {
176   - return this.logs;
177   - }
178   -
179   - @Override
180   - public List<LogEvent> getLog(LogLevel level)
181   - {
182   - List<LogEvent> levelLogs = new ArrayList<LogEvent>();
183   -
184   - for (LogEvent log : this.logs) {
185   - if (log.getLevel() == level) {
186   - levelLogs.add(log);
187   - }
188   - }
189   -
190   - return levelLogs;
191   - }
192   -
193   - @Override
194   - public JobProgress getProgress()
195   - {
196   - return this.progress;
197   - }
198   -
199   - @Override
200   - public void ask(Object question) throws InterruptedException
201   - {
202   - this.question = question;
203   -
204   - this.askLock.lockInterruptibly();
205   -
206   - try {
207   - // Wait for the answer
208   - this.state = State.WAITING;
209   - this.answered.await();
210   - this.state = State.RUNNING;
211   - } finally {
212   - this.askLock.unlock();
213   - }
214   - }
215   -
216   - @Override
217   - public Object getQuestion()
218   - {
219   - return this.question;
220   - }
221   -
222   - @Override
223   - public void answered()
224   - {
225   - this.askLock.lock();
226   -
227   - this.question = null;
228   -
229   - try {
230   - this.answered.signal();
231   - } finally {
232   - this.askLock.unlock();
233   - }
234   - }
235   -
236   - @Override
237   - public Date getStartDate()
238   - {
239   - return this.startDate;
240   - }
241   -
242   - /**
243   - * @param startDate the date and time when the job has been started
244   - */
245   - public void setStartDate(Date startDate)
246   - {
247   - this.startDate = startDate;
248   - }
249   -
250   - @Override
251   - public Date getEndDate()
252   - {
253   - return this.endDate;
254   - }
255   -
256   - /**
257   - * @param endDate the date and time when the job finished
258   - */
259   - public void setEndDate(Date endDate)
260   - {
261   - this.endDate = endDate;
  50 + super(request, id, observationManager, loggerManager);
262 51 }
263 52 }
12 xwiki-commons-core/xwiki-commons-job/src/main/java/org/xwiki/job/internal/DefaultJobStatusStorage.java
@@ -23,6 +23,7 @@
23 23 import java.io.FileOutputStream;
24 24 import java.io.IOException;
25 25 import java.io.OutputStreamWriter;
  26 +import java.io.Serializable;
26 27 import java.io.UnsupportedEncodingException;
27 28 import java.net.URLEncoder;
28 29 import java.util.Map;
@@ -192,10 +193,13 @@ public void store(JobStatus status)
192 193 {
193 194 this.jobs.put(status.getRequest().getId(), status);
194 195
195   - try {
196   - saveJobStatus(status);
197   - } catch (Exception e) {
198   - this.logger.warn("Failed to save job status [{}]", status, e);
  196 + // On store Serializable job status on file system
  197 + if (status instanceof Serializable) {
  198 + try {
  199 + saveJobStatus(status);
  200 + } catch (Exception e) {
  201 + this.logger.warn("Failed to save job status [{}]", status, e);
  202 + }
199 203 }
200 204 }
201 205

0 comments on commit 5c0b728

Please sign in to comment.
Something went wrong with that request. Please try again.