1 /*******************************************************************************
2 * Copyright (c) 2010, 2011 Sonatype, Inc.
3 * All rights reserved. This program and the accompanying materials
4 * are made available under the terms of the Eclipse Public License v1.0
5 * which accompanies this distribution, and is available at
6 * http://www.eclipse.org/legal/epl-v10.html
9 * Sonatype, Inc. - initial API and implementation
10 *******************************************************************************/
11 package org
.eclipse
.aether
;
14 import java
.util
.Collections
;
15 import java
.util
.List
;
17 import org
.eclipse
.aether
.artifact
.Artifact
;
18 import org
.eclipse
.aether
.metadata
.Metadata
;
19 import org
.eclipse
.aether
.repository
.ArtifactRepository
;
22 * An event describing an action performed by the repository system. Note that events which indicate the end of an
23 * action like {@link EventType#ARTIFACT_RESOLVED} are generally fired in both the success and the failure case. Use
24 * {@link #getException()} to check whether an event denotes success or failure.
26 * @see RepositoryListener
27 * @see RepositoryEvent.Builder
29 public final class RepositoryEvent
33 * The type of the repository event.
39 * @see RepositoryListener#artifactDescriptorInvalid(RepositoryEvent)
41 ARTIFACT_DESCRIPTOR_INVALID
,
44 * @see RepositoryListener#artifactDescriptorMissing(RepositoryEvent)
46 ARTIFACT_DESCRIPTOR_MISSING
,
49 * @see RepositoryListener#metadataInvalid(RepositoryEvent)
54 * @see RepositoryListener#artifactResolving(RepositoryEvent)
59 * @see RepositoryListener#artifactResolved(RepositoryEvent)
64 * @see RepositoryListener#metadataResolving(RepositoryEvent)
69 * @see RepositoryListener#metadataResolved(RepositoryEvent)
74 * @see RepositoryListener#artifactDownloading(RepositoryEvent)
79 * @see RepositoryListener#artifactDownloaded(RepositoryEvent)
84 * @see RepositoryListener#metadataDownloading(RepositoryEvent)
89 * @see RepositoryListener#metadataDownloaded(RepositoryEvent)
94 * @see RepositoryListener#artifactInstalling(RepositoryEvent)
99 * @see RepositoryListener#artifactInstalled(RepositoryEvent)
104 * @see RepositoryListener#metadataInstalling(RepositoryEvent)
109 * @see RepositoryListener#metadataInstalled(RepositoryEvent)
114 * @see RepositoryListener#artifactDeploying(RepositoryEvent)
119 * @see RepositoryListener#artifactDeployed(RepositoryEvent)
124 * @see RepositoryListener#metadataDeploying(RepositoryEvent)
129 * @see RepositoryListener#metadataDeployed(RepositoryEvent)
135 private final EventType type
;
137 private final RepositorySystemSession session
;
139 private final Artifact artifact
;
141 private final Metadata metadata
;
143 private final ArtifactRepository repository
;
145 private final File file
;
147 private final List
<Exception
> exceptions
;
149 private final RequestTrace trace
;
151 RepositoryEvent( Builder builder
)
154 session
= builder
.session
;
155 artifact
= builder
.artifact
;
156 metadata
= builder
.metadata
;
157 repository
= builder
.repository
;
159 exceptions
= builder
.exceptions
;
160 trace
= builder
.trace
;
164 * Gets the type of the event.
166 * @return The type of the event, never {@code null}.
168 public EventType
getType()
174 * Gets the repository system session during which the event occurred.
176 * @return The repository system session during which the event occurred, never {@code null}.
178 public RepositorySystemSession
getSession()
184 * Gets the artifact involved in the event (if any).
186 * @return The involved artifact or {@code null} if none.
188 public Artifact
getArtifact()
194 * Gets the metadata involved in the event (if any).
196 * @return The involved metadata or {@code null} if none.
198 public Metadata
getMetadata()
204 * Gets the file involved in the event (if any).
206 * @return The involved file or {@code null} if none.
208 public File
getFile()
214 * Gets the repository involved in the event (if any).
216 * @return The involved repository or {@code null} if none.
218 public ArtifactRepository
getRepository()
224 * Gets the exception that caused the event (if any). As a rule of thumb, an event accompanied by an exception
225 * indicates a failure of the corresponding action. If multiple exceptions occurred, this method returns the first
228 * @return The exception or {@code null} if none.
230 public Exception
getException()
232 return exceptions
.isEmpty() ?
null : exceptions
.get( 0 );
236 * Gets the exceptions that caused the event (if any). As a rule of thumb, an event accompanied by exceptions
237 * indicates a failure of the corresponding action.
239 * @return The exceptions, never {@code null}.
241 public List
<Exception
> getExceptions()
247 * Gets the trace information about the request during which the event occurred.
249 * @return The trace information or {@code null} if none.
251 public RequestTrace
getTrace()
257 public String
toString()
259 StringBuilder buffer
= new StringBuilder( 256 );
260 buffer
.append( getType() );
261 if ( getArtifact() != null )
263 buffer
.append( " " ).append( getArtifact() );
265 if ( getMetadata() != null )
267 buffer
.append( " " ).append( getMetadata() );
269 if ( getFile() != null )
271 buffer
.append( " (" ).append( getFile() ).append( ")" );
273 if ( getRepository() != null )
275 buffer
.append( " @ " ).append( getRepository() );
277 return buffer
.toString();
281 * A builder to create events.
283 public static final class Builder
288 RepositorySystemSession session
;
294 ArtifactRepository repository
;
298 List
<Exception
> exceptions
= Collections
.emptyList();
303 * Creates a new event builder for the specified session and event type.
305 * @param session The repository system session, must not be {@code null}.
306 * @param type The type of the event, must not be {@code null}.
308 public Builder( RepositorySystemSession session
, EventType type
)
310 if ( session
== null )
312 throw new IllegalArgumentException( "session not specified" );
314 this.session
= session
;
317 throw new IllegalArgumentException( "event type not specified" );
323 * Sets the artifact involved in the event.
325 * @param artifact The involved artifact, may be {@code null}.
326 * @return This event builder for chaining, never {@code null}.
328 public Builder
setArtifact( Artifact artifact
)
330 this.artifact
= artifact
;
335 * Sets the metadata involved in the event.
337 * @param metadata The involved metadata, may be {@code null}.
338 * @return This event builder for chaining, never {@code null}.
340 public Builder
setMetadata( Metadata metadata
)
342 this.metadata
= metadata
;
347 * Sets the repository involved in the event.
349 * @param repository The involved repository, may be {@code null}.
350 * @return This event builder for chaining, never {@code null}.
352 public Builder
setRepository( ArtifactRepository repository
)
354 this.repository
= repository
;
359 * Sets the file involved in the event.
361 * @param file The involved file, may be {@code null}.
362 * @return This event builder for chaining, never {@code null}.
364 public Builder
setFile( File file
)
371 * Sets the exception causing the event.
373 * @param exception The exception causing the event, may be {@code null}.
374 * @return This event builder for chaining, never {@code null}.
376 public Builder
setException( Exception exception
)
378 if ( exception
!= null )
380 this.exceptions
= Collections
.singletonList( exception
);
384 this.exceptions
= Collections
.emptyList();
390 * Sets the exceptions causing the event.
392 * @param exceptions The exceptions causing the event, may be {@code null}.
393 * @return This event builder for chaining, never {@code null}.
395 public Builder
setExceptions( List
<Exception
> exceptions
)
397 if ( exceptions
!= null )
399 this.exceptions
= exceptions
;
403 this.exceptions
= Collections
.emptyList();
409 * Sets the trace information about the request during which the event occurred.
411 * @param trace The trace information, may be {@code null}.
412 * @return This event builder for chaining, never {@code null}.
414 public Builder
setTrace( RequestTrace trace
)
421 * Builds a new event from the current values of this builder. The state of the builder itself remains
424 * @return The event, never {@code null}.
426 public RepositoryEvent
build()
428 return new RepositoryEvent( this );