File D:\maven-qcr-plugin-testsunitaires\qcr4java\jenkins-master\target\site\qc\maven-plugin\src\main\java\hudson\maven\reporters\MavenAbstractArtifactRecord.java.html

1 /*
2 * The MIT License
3 *
4 * Copyright (c) 2004-2009, Sun Microsystems, Inc., Kohsuke Kawaguchi, Seiji Sogabe
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a copy
7 * of this software and associated documentation files (the "Software"), to deal
8 * in the Software without restriction, including without limitation the rights
9 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 * copies of the Software, and to permit persons to whom the Software is
11 * furnished to do so, subject to the following conditions:
12 *
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22 * THE SOFTWARE.
23 */
24 package hudson.maven.reporters;
25
26 import hudson.console.AnnotatedLargeText;
27 import hudson.maven.MavenEmbedder;
28 import hudson.maven.MavenEmbedderException;
29 import hudson.maven.MavenUtil;
30 import hudson.maven.RedeployPublisher.WrappedArtifactRepository;
31 import hudson.model.AbstractBuild;
32 import hudson.model.AbstractProject;
33 import hudson.model.Api;
34 import hudson.model.BallColor;
35 import hudson.model.BuildBadgeAction;
36 import hudson.model.Result;
37 import hudson.model.TaskAction;
38 import hudson.model.TaskListener;
39 import hudson.model.TaskThread;
40 import hudson.model.TaskThread.ListenerAndText;
41 import hudson.security.ACL;
42 import hudson.security.Permission;
43 import hudson.util.Iterators;
44 import hudson.widgets.HistoryWidget;
45 import hudson.widgets.HistoryWidget.Adapter;
46
47 import java.io.File;
48 import java.io.IOException;
49 import java.nio.charset.Charset;
50 import java.util.Calendar;
51 import java.util.GregorianCalendar;
52 import java.util.concurrent.CopyOnWriteArrayList;
53
54 import javax.servlet.ServletException;
55
56 import org.apache.maven.artifact.deployer.ArtifactDeploymentException;
57 import org.apache.maven.artifact.repository.ArtifactRepository;
58 import org.apache.maven.artifact.repository.ArtifactRepositoryFactory;
59 import org.apache.maven.artifact.repository.layout.ArtifactRepositoryLayout;
60 import org.codehaus.plexus.component.repository.exception.ComponentLookupException;
61 import org.kohsuke.stapler.HttpRedirect;
62 import org.kohsuke.stapler.HttpResponse;
63 import org.kohsuke.stapler.QueryParameter;
64 import org.kohsuke.stapler.StaplerRequest;
65 import org.kohsuke.stapler.StaplerResponse;
66 import org.kohsuke.stapler.export.Exported;
67 import org.kohsuke.stapler.export.ExportedBean;
68
69 /**
70 * UI to redeploy artifacts after the fact.
71 *
72 * <p>
73 * There are two types &mdash; one for the module, the other for the whole project.
74 * The semantics specific to these cases are defined in subtypes.
75 *
76 * @author Kohsuke Kawaguchi
77 */
78 public abstract class MavenAbstractArtifactRecord<T extends AbstractBuild<?,?>> extends TaskAction implements BuildBadgeAction {
79 @ExportedBean
80 public final class Record {
81 /**
82 * Repository URL that artifacts were deployed.
83 */
84 @Exported
85 public final String url;
86
87 /**
88 * Log file name. Relative to {@link AbstractBuild#getRootDir()}.
89 */
90 private final String fileName;
91
92 /**
93 * Status of this record.
94 */
95 private Result result;
96
97 private final Calendar timeStamp;
98
99 public Record(String url, String fileName) {
100 this.url = url;
101 this.fileName = fileName;
102 timeStamp = new GregorianCalendar();
103 }
104
105 /**
106 * Returns the log of this deployment record.
107 */
108 public AnnotatedLargeText<Record> getLog() {
109 return new AnnotatedLargeText<Record>(new File(getBuild().getRootDir(),fileName), Charset.defaultCharset(), true, this);
110 }
111
112 /**
113 * Result of the deployment. During the build, this value is null.
114 */
115 @Exported
116 public Result getResult() {
117 return result;
118 }
119
120 @Exported
121 public int getNumber() {
122 return records.indexOf(this);
123 }
124
125 @Exported
126 public boolean isBuilding() {
127 return result==null;
128 }
129
130 @Exported
131 public Calendar getTimestamp() {
132 return (Calendar) timeStamp.clone();
133 }
134
135 @Exported
136 public String getBuildStatusUrl() {
137 return getIconColor().getImage();
138 }
139
140 @Exported
141 public BallColor getIconColor() {
142 if(result==null)
143 return BallColor.GREY_ANIME;
144 else
145 return result.color;
146 }
147
148 // TODO: Eventually provide a better UI
149 public void doIndex(StaplerResponse rsp) throws IOException {
150 rsp.setContentType("text/plain;charset=UTF-8");
151 getLog().writeLogTo(0,rsp.getWriter());
152 }
153 }
154
155 /**
156 * Records of a deployment.
157 */
158 @Exported
159 public final CopyOnWriteArrayList<Record> records = new CopyOnWriteArrayList<Record>();
160
161 /**
162 * Gets the parent build object to which this record is registered.
163 */
164 public abstract T getBuild();
165
166 protected ACL getACL() {
167 return getBuild().getACL();
168 }
169
170 public final String getIconFileName() {
171 return "redo.gif";
172 }
173
174 public final String getDisplayName() {
175 return Messages.MavenAbstractArtifactRecord_Displayname();
176 }
177
178 public final String getUrlName() {
179 return "redeploy";
180 }
181
182 protected Permission getPermission() {
183 return REDEPLOY;
184 }
185
186 public Api getApi() {
187 return new Api(this);
188 }
189
190 public boolean hasBadge() {
191 if (records != null) {
192 for (final Record record : records) {
193 if (Result.SUCCESS.equals(record.result))
194 return true;
195 }
196 }
197 return false;
198 }
199
200 public HistoryWidgetImpl getHistoryWidget() {
201 return new HistoryWidgetImpl();
202 }
203
204 public Object getDynamic(String token, StaplerRequest req, StaplerResponse rsp) {
205 return records.get(Integer.valueOf(token));
206 }
207
208 /**
209 * Performs a redeployment.
210 */
211 public final HttpResponse doRedeploy(
212 @QueryParameter("_.id") final String id,
213 @QueryParameter("_.url") final String repositoryUrl,
214 @QueryParameter("_.uniqueVersion") final boolean uniqueVersion) throws ServletException, IOException {
215 getACL().checkPermission(REDEPLOY);
216
217 File logFile = new File(getBuild().getRootDir(),"maven-deployment."+records.size()+".log");
218 final Record record = new Record(repositoryUrl, logFile.getName());
219 records.add(record);
220
221 new TaskThread(this,ListenerAndText.forFile(logFile,this)) {
222 protected void perform(TaskListener listener) throws Exception {
223 try {
224 MavenEmbedder embedder = MavenUtil.createEmbedder(listener,getBuild());
225 ArtifactRepositoryLayout layout =
226 embedder.lookup( ArtifactRepositoryLayout.class,"default");
227 ArtifactRepositoryFactory factory =
228 (ArtifactRepositoryFactory) embedder.lookup(ArtifactRepositoryFactory.ROLE);
229
230 ArtifactRepository repository = factory.createDeploymentArtifactRepository(
231 id, repositoryUrl, layout, uniqueVersion);
232 WrappedArtifactRepository repo = new WrappedArtifactRepository(repository, uniqueVersion);
233 deploy(embedder,repo,listener);
234
235 record.result = Result.SUCCESS;
236 } finally {
237 if(record.result==null)
238 record.result = Result.FAILURE;
239 // persist the record
240 getBuild().save();
241 }
242 }
243 }.start();
244
245 return HttpRedirect.DOT;
246 }
247
248 /**
249 * Deploys the artifacts to the specified {@link ArtifactRepository}.
250 *
251 * @param embedder
252 * This component hosts all the Maven components we need to do the work.
253 * @param deploymentRepository
254 * The remote repository to deploy to.
255 * @param listener
256 * The status and error goes to this listener.
257 */
258 public abstract void deploy(MavenEmbedder embedder, ArtifactRepository deploymentRepository, TaskListener listener) throws MavenEmbedderException, IOException, ComponentLookupException, ArtifactDeploymentException;
259
260 private final class HistoryWidgetImpl extends HistoryWidget<MavenAbstractArtifactRecord,Record> {
261 private HistoryWidgetImpl() {
262 super(MavenAbstractArtifactRecord.this, Iterators.reverse(records), ADAPTER);
263 }
264
265 public String getDisplayName() {
266 return Messages.HistoryWidgetImpl_Displayname();
267 }
268 }
269
270 private static final Adapter<MavenAbstractArtifactRecord<?>.Record> ADAPTER = new Adapter<MavenAbstractArtifactRecord<?>.Record>() {
271 public int compare(MavenAbstractArtifactRecord<?>.Record record, String key) {
272 return record.getNumber()-Integer.parseInt(key);
273 }
274
275 public String getKey(MavenAbstractArtifactRecord<?>.Record record) {
276 return String.valueOf(record.getNumber());
277 }
278
279 public boolean isBuilding(MavenAbstractArtifactRecord<?>.Record record) {
280 return record.isBuilding();
281 }
282
283 public String getNextKey(String key) {
284 return String.valueOf(Integer.parseInt(key)+1);
285 }
286 };
287
288
289 /**
290 * Permission for redeploying artifacts.
291 */
292 public static final Permission REDEPLOY = AbstractProject.BUILD;
293
294 /**
295 * Debug probe for HUDSON-1461.
296 */
297 public static boolean debug = Boolean.getBoolean(MavenArtifactRecord.class.getName()+".debug");
298 }
299