1. Project Clover database Tue Dec 20 2016 21:24:09 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
36
5
1
181
97
12
0.33
7.2
5
2.4

Classes

Class Line # Actions
InstallJob 53 36 0% 12 3
0.941176594.1%
 

Contributing tests

This file is covered by 74 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: e793e5828898f9a29f68f6ddd5c6613463e76418 $
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  688 toggle @Override
76    public String getType()
77    {
78  688 return JOBTYPE;
79    }
80   
 
81  134 toggle @Override
82    protected InstallRequest castRequest(Request request)
83    {
84  134 InstallRequest installRequest;
85  134 if (request instanceof InstallRequest) {
86  134 installRequest = (InstallRequest) request;
87    } else {
88  0 installRequest = new InstallRequest(request);
89    }
90   
91  134 return installRequest;
92    }
93   
 
94  134 toggle @Override
95    protected void runInternal() throws Exception
96    {
97  134 this.progressManager.pushLevelProgress(3, this);
98   
99  134 ExecutionContext context = this.execution.getContext();
100   
101  134 try {
102  134 this.progressManager.startStep(this);
103   
104    // Create the plan
105   
106  134 InstallRequest planRequest = new InstallRequest(getRequest());
107  134 planRequest.setId((List<String>) null);
108   
109  134 this.installPlanJob.initialize(planRequest);
110  134 this.installPlanJob.run();
111   
112  134 ExtensionPlan plan = (ExtensionPlan) this.installPlanJob.getStatus();
113   
114  134 if (plan.getError() != null) {
115  7 throw new InstallException("Failed to create install plan", plan.getError());
116    }
117   
118  127 this.progressManager.startStep(this);
119   
120    // Put the plan in context
121    // TODO: use a stack ?
122  127 context.setProperty(CONTEXTKEY_PLAN, plan);
123   
124    // Apply the plan
125   
126  127 Collection<ExtensionPlanAction> actions = plan.getActions();
127   
128    // Download all extensions
129   
130  127 this.progressManager.pushLevelProgress(actions.size(), this);
131   
132  127 try {
133  127 for (ExtensionPlanAction action : actions) {
134  217 this.progressManager.startStep(this);
135   
136  217 store(action);
137    }
138    } finally {
139  127 this.progressManager.popLevelProgress(this);
140    }
141   
142  127 this.progressManager.startStep(this);
143   
144    // Install all extensions
145   
146  127 applyActions(actions);
147    } finally {
148  134 this.progressManager.popLevelProgress(this);
149   
150    // Clean context
151  134 context.removeProperty(CONTEXTKEY_PLAN);
152    }
153    }
154   
155    /**
156    * @param action the action containing the extension to download
157    * @throws LocalExtensionRepositoryException failed to store extension
158    */
 
159  217 toggle private void store(ExtensionPlanAction action) throws LocalExtensionRepositoryException
160    {
161  217 if (action.getAction() == Action.INSTALL || action.getAction() == Action.UPGRADE
162    || action.getAction() == Action.DOWNGRADE) {
163  181 storeExtension(action.getExtension());
164    }
165    }
166   
167    /**
168    * @param extension the extension to store
169    * @throws LocalExtensionRepositoryException failed to store extension
170    */
 
171  181 toggle private void storeExtension(Extension extension) throws LocalExtensionRepositoryException
172    {
173  181 if (!this.localExtensionRepository.exists(extension.getId())) {
174  136 if (getRequest().isVerbose()) {
175  136 this.logger.info(LOG_DOWNLOADING, "Downloading extension [{}]", extension.getId());
176    }
177   
178  136 this.localExtensionRepository.storeExtension(extension);
179    }
180    }
181    }