/
UninstallPlanJob.java
269 lines (238 loc) · 10.6 KB
/
UninstallPlanJob.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
/*
* See the NOTICE file distributed with this work for additional
* information regarding copyright ownership.
*
* This is free software; you can redistribute it and/or modify it
* under the terms of the GNU Lesser General Public License as
* published by the Free Software Foundation; either version 2.1 of
* the License, or (at your option) any later version.
*
* This software is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this software; if not, write to the Free
* Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
* 02110-1301 USA, or see the FSF site: http://www.fsf.org.
*/
package org.xwiki.extension.job.internal;
import java.text.MessageFormat;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.Map;
import javax.inject.Inject;
import javax.inject.Named;
import org.xwiki.component.annotation.Component;
import org.xwiki.extension.ExtensionId;
import org.xwiki.extension.LocalExtension;
import org.xwiki.extension.ResolveException;
import org.xwiki.extension.UninstallException;
import org.xwiki.extension.job.UninstallRequest;
import org.xwiki.extension.job.plan.ExtensionPlanAction.Action;
import org.xwiki.extension.job.plan.ExtensionPlanNode;
import org.xwiki.extension.job.plan.internal.DefaultExtensionPlan;
import org.xwiki.extension.job.plan.internal.DefaultExtensionPlanAction;
import org.xwiki.extension.job.plan.internal.DefaultExtensionPlanNode;
import org.xwiki.extension.repository.LocalExtensionRepository;
import org.xwiki.job.Request;
import org.xwiki.job.internal.DefaultJobStatus;
/**
* Create an Extension uninstallation plan.
*
* @version $Id$
* @since 4.0M1
*/
@Component
@Named(UninstallPlanJob.JOBTYPE)
public class UninstallPlanJob extends AbstractExtensionJob<UninstallRequest>
{
/**
* The id of the job.
*/
public static final String JOBTYPE = "uninstallplan";
/**
* Error message used in exception throw when trying to uninstall an extension which is not installed.
*/
private static final String EXCEPTION_NOTINSTALLED = "Extension [{0}] is not installed";
/**
* Error message used in exception throw when trying to uninstall an extension which is not installed.
*/
private static final String EXCEPTION_NOTINSTALLEDNAMESPACE = EXCEPTION_NOTINSTALLED + " from namespace [{1}]";
/**
* Used to manipulate local repository.
*/
@Inject
private LocalExtensionRepository localExtensionRepository;
/**
* The install plan.
*/
private List<ExtensionPlanNode> extensionTree = new ArrayList<ExtensionPlanNode>();
@Override
public String getType()
{
return JOBTYPE;
}
@Override
protected DefaultJobStatus<UninstallRequest> createNewStatus(UninstallRequest request)
{
return new DefaultExtensionPlan<UninstallRequest>(request, getId(), this.observationManager,
this.loggerManager, this.extensionTree);
}
@Override
protected UninstallRequest castRequest(Request request)
{
UninstallRequest uninstallRequest;
if (request instanceof UninstallRequest) {
uninstallRequest = (UninstallRequest) request;
} else {
uninstallRequest = new UninstallRequest(request);
}
return uninstallRequest;
}
@Override
protected void start() throws Exception
{
Collection<ExtensionId> extensions = getRequest().getExtensions();
notifyPushLevelProgress(extensions.size());
try {
for (ExtensionId extensionId : extensions) {
if (extensionId.getVersion() != null) {
LocalExtension localExtension = (LocalExtension) this.localExtensionRepository.resolve(extensionId);
if (getRequest().hasNamespaces()) {
uninstallExtension(localExtension, getRequest().getNamespaces(), this.extensionTree);
} else if (localExtension.getNamespaces() != null) {
// Duplicate the namespace list to avoid ConcurrentModificationException
uninstallExtension(localExtension, new ArrayList<String>(localExtension.getNamespaces()),
this.extensionTree);
} else {
uninstallExtension(localExtension, (String) null, this.extensionTree);
}
} else {
if (getRequest().hasNamespaces()) {
uninstallExtension(extensionId.getId(), getRequest().getNamespaces(), this.extensionTree);
} else {
uninstallExtension(extensionId.getId(), (String) null, this.extensionTree);
}
}
notifyStepPropress();
}
} finally {
notifyPopLevelProgress();
}
}
/**
* @param extensionId the identifier of the extension to uninstall
* @param namespaces the namespaces from where to uninstall the extension
* @param parentBranch the children of the parent {@link DefaultExtensionPlanNode}
* @throws UninstallException error when trying to uninstall provided extensions
*/
private void uninstallExtension(String extensionId, Collection<String> namespaces,
List<ExtensionPlanNode> parentBranch) throws UninstallException
{
notifyPushLevelProgress(namespaces.size());
try {
for (String namespace : namespaces) {
uninstallExtension(extensionId, namespace, parentBranch);
notifyStepPropress();
}
} finally {
notifyPopLevelProgress();
}
}
/**
* @param extensionId the identifier of the extension to uninstall
* @param namespace the namespace from where to uninstall the extension
* @param parentBranch the children of the parent {@link DefaultExtensionPlanNode}
* @throws UninstallException error when trying to uninstall provided extension
*/
private void uninstallExtension(String extensionId, String namespace, List<ExtensionPlanNode> parentBranch)
throws UninstallException
{
LocalExtension localExtension = this.localExtensionRepository.getInstalledExtension(extensionId, namespace);
if (localExtension == null) {
throw new UninstallException(MessageFormat.format(EXCEPTION_NOTINSTALLED, extensionId));
}
try {
uninstallExtension(localExtension, namespace, parentBranch);
} catch (Exception e) {
throw new UninstallException("Failed to uninstall extension", e);
}
}
/**
* @param localExtension the extension to uninstall
* @param namespaces the namespaces from where to uninstall the extension
* @param parentBranch the children of the parent {@link DefaultExtensionPlanNode}
* @throws UninstallException error when trying to uninstall provided extension
*/
private void uninstallExtension(LocalExtension localExtension, Collection<String> namespaces,
List<ExtensionPlanNode> parentBranch) throws UninstallException
{
for (String namespace : namespaces) {
uninstallExtension(localExtension, namespace, parentBranch);
}
}
/**
* @param extensions the local extensions to uninstall
* @param namespace the namespaces from where to uninstall the extensions
* @param parentBranch the children of the parent {@link DefaultExtensionPlanNode}
* @throws UninstallException error when trying to uninstall provided extensions
*/
private void uninstallExtensions(Collection<LocalExtension> extensions, String namespace,
List<ExtensionPlanNode> parentBranch) throws UninstallException
{
for (LocalExtension backardDependency : extensions) {
uninstallExtension(backardDependency, namespace, parentBranch);
}
}
/**
* @param localExtension the extension to uninstall
* @param namespace the namespace from where to uninstall the extension
* @param parentBranch the children of the parent {@link ExtensionPlanNode}
* @throws UninstallException error when trying to uninstall provided extension
*/
private void uninstallExtension(LocalExtension localExtension, String namespace,
List<ExtensionPlanNode> parentBranch) throws UninstallException
{
if (!localExtension.isInstalled()) {
throw new UninstallException(MessageFormat.format(EXCEPTION_NOTINSTALLED, localExtension, namespace));
} else if (namespace != null
&& (localExtension.getNamespaces() == null || !localExtension.getNamespaces().contains(namespace))) {
throw new UninstallException(MessageFormat.format(EXCEPTION_NOTINSTALLEDNAMESPACE, localExtension,
namespace));
}
// Log progression
if (namespace != null) {
this.logger.info("Resolving extension [{}] from namespace [{}]", localExtension, namespace);
} else {
this.logger.info("Resolving extension [{}]", localExtension);
}
notifyPushLevelProgress(2);
try {
// Uninstall backward dependencies
List<ExtensionPlanNode> children = new ArrayList<ExtensionPlanNode>();
try {
if (namespace != null) {
uninstallExtensions(this.localExtensionRepository.getBackwardDependencies(localExtension.getId()
.getId(), namespace), namespace, children);
} else {
for (Map.Entry<String, Collection<LocalExtension>> entry : this.localExtensionRepository
.getBackwardDependencies(localExtension.getId()).entrySet()) {
uninstallExtensions(entry.getValue(), entry.getKey(), children);
}
}
} catch (ResolveException e) {
throw new UninstallException("Failed to resolve backward dependencies of extension [" + localExtension
+ "]", e);
}
notifyStepPropress();
DefaultExtensionPlanAction action =
new DefaultExtensionPlanAction(localExtension, null, Action.UNINSTALL, namespace, false);
parentBranch.add(new DefaultExtensionPlanNode(action, children, null));
} finally {
notifyPopLevelProgress();
}
}
}