1. Project Clover database Sat Feb 2 2019 06:45:20 CET
  2. Package org.xwiki.extension.job.internal

File InstallJob.java

 

Coverage histogram

../../../../../img/srcFileCovDistChart10.png
0% of files have more coverage

Code metrics

10
40
5
1
189
101
12
0.3
8
5
2.4

Classes

Class Line # Actions
InstallJob 53 40 0% 12 0
1.0100%
 

Contributing tests

This file is covered by 75 tests. .

Source view

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.extension.job.internal;
21   
22    import java.util.Collection;
23    import java.util.List;
24   
25    import javax.inject.Inject;
26    import javax.inject.Named;
27   
28    import org.xwiki.component.annotation.Component;
29    import org.xwiki.context.Execution;
30    import org.xwiki.context.ExecutionContext;
31    import org.xwiki.extension.Extension;
32    import org.xwiki.extension.InstallException;
33    import org.xwiki.extension.job.InstallRequest;
34    import org.xwiki.extension.job.plan.ExtensionPlan;
35    import org.xwiki.extension.job.plan.ExtensionPlanAction;
36    import org.xwiki.extension.job.plan.ExtensionPlanAction.Action;
37    import org.xwiki.extension.repository.LocalExtensionRepositoryException;
38    import org.xwiki.job.DefaultJobStatus;
39    import org.xwiki.job.Job;
40    import org.xwiki.job.Request;
41    import org.xwiki.logging.marker.TranslationMarker;
42   
43    /**
44    * Extension installation related task.
45    * <p>
46    * This task generates related events.
47    *
48    * @version $Id: f3fefae59a6ba6106dfda34b14432796f2f44804 $
49    * @since 4.0M1
50    */
51    @Component
52    @Named(InstallJob.JOBTYPE)
 
53    public class InstallJob extends AbstractExtensionJob<InstallRequest, DefaultJobStatus<InstallRequest>>
54    {
55    /**
56    * The id of the job.
57    */
58    public static final String JOBTYPE = "install";
59   
60    private static final TranslationMarker LOG_DOWNLOADING = new TranslationMarker("extension.log.job.downloading");
61   
62    /**
63    * Used to generate the install plan.
64    */
65    @Inject
66    @Named(InstallPlanJob.JOBTYPE)
67    private Job installPlanJob;
68   
69    /**
70    * Used to access the execution context.
71    */
72    @Inject
73    private Execution execution;
74   
 
75  703 toggle @Override
76    public String getType()
77    {
78  703 return JOBTYPE;
79    }
80   
 
81  142 toggle @Override
82    protected InstallRequest castRequest(Request request)
83    {
84  142 InstallRequest installRequest;
85  142 if (request instanceof InstallRequest) {
86  139 installRequest = (InstallRequest) request;
87    } else {
88  3 installRequest = new InstallRequest(request);
89    }
90   
91  142 return installRequest;
92    }
93   
 
94  142 toggle @Override
95    protected void runInternal() throws Exception
96    {
97  142 this.progressManager.pushLevelProgress(3, this);
98   
99  142 ExecutionContext context = this.execution.getContext();
100   
101  142 try {
102  142 this.progressManager.startStep(this);
103   
104    // Create the plan
105   
106  142 InstallRequest planRequest = new InstallRequest(getRequest());
107  142 planRequest.setId((List<String>) null);
108   
109  142 planRequest.setVerbose(getRequest().isVerbose());
110   
111  142 this.installPlanJob.initialize(planRequest);
112  142 this.installPlanJob.run();
113   
114  142 ExtensionPlan plan = (ExtensionPlan) this.installPlanJob.getStatus();
115   
116  142 if (plan.getError() != null) {
117  9 throw new InstallException("Failed to create install plan", plan.getError());
118    }
119   
120  133 this.progressManager.endStep(this);
121   
122  133 this.progressManager.startStep(this);
123   
124    // Put the plan in context
125    // TODO: use a stack ?
126  133 context.setProperty(CONTEXTKEY_PLAN, plan);
127   
128    // Apply the plan
129   
130  133 Collection<ExtensionPlanAction> actions = plan.getActions();
131   
132    // Download all extensions
133   
134  133 this.progressManager.pushLevelProgress(actions.size(), actions);
135   
136  133 try {
137  133 for (ExtensionPlanAction action : actions) {
138  764 this.progressManager.startStep(actions);
139   
140  764 store(action);
141   
142  764 this.progressManager.endStep(actions);
143    }
144    } finally {
145  133 this.progressManager.popLevelProgress(actions);
146    }
147   
148  133 this.progressManager.endStep(this);
149   
150  133 this.progressManager.startStep(this);
151   
152    // Install all extensions
153   
154  133 applyActions(actions);
155    } finally {
156  142 this.progressManager.popLevelProgress(this);
157   
158    // Clean context
159  142 context.removeProperty(CONTEXTKEY_PLAN);
160    }
161    }
162   
163    /**
164    * @param action the action containing the extension to download
165    * @throws LocalExtensionRepositoryException failed to store extension
166    */
 
167  764 toggle private void store(ExtensionPlanAction action) throws LocalExtensionRepositoryException
168    {
169  764 if (action.getAction() == Action.INSTALL || action.getAction() == Action.UPGRADE
170    || action.getAction() == Action.DOWNGRADE) {
171  425 storeExtension(action.getExtension());
172    }
173    }
174   
175    /**
176    * @param extension the extension to store
177    * @throws LocalExtensionRepositoryException failed to store extension
178    */
 
179  425 toggle private void storeExtension(Extension extension) throws LocalExtensionRepositoryException
180    {
181  425 if (!this.localExtensionRepository.exists(extension.getId())) {
182  259 if (getRequest().isVerbose()) {
183  119 this.logger.info(LOG_DOWNLOADING, "Downloading extension [{}]", extension.getId());
184    }
185   
186  259 this.localExtensionRepository.storeExtension(extension);
187    }
188    }
189    }