001package org.eclipse.aether;
002
003/*
004 * Licensed to the Apache Software Foundation (ASF) under one
005 * or more contributor license agreements.  See the NOTICE file
006 * distributed with this work for additional information
007 * regarding copyright ownership.  The ASF licenses this file
008 * to you under the Apache License, Version 2.0 (the
009 * "License"); you may not use this file except in compliance
010 * with the License.  You may obtain a copy of the License at
011 * 
012 *  http://www.apache.org/licenses/LICENSE-2.0
013 * 
014 * Unless required by applicable law or agreed to in writing,
015 * software distributed under the License is distributed on an
016 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
017 * KIND, either express or implied.  See the License for the
018 * specific language governing permissions and limitations
019 * under the License.
020 */
021
022import java.io.File;
023import java.util.Collections;
024import java.util.List;
025
026import org.eclipse.aether.artifact.Artifact;
027import org.eclipse.aether.metadata.Metadata;
028import org.eclipse.aether.repository.ArtifactRepository;
029
030/**
031 * An event describing an action performed by the repository system. Note that events which indicate the end of an
032 * action like {@link EventType#ARTIFACT_RESOLVED} are generally fired in both the success and the failure case. Use
033 * {@link #getException()} to check whether an event denotes success or failure.
034 * 
035 * @see RepositoryListener
036 * @see RepositoryEvent.Builder
037 */
038public final class RepositoryEvent
039{
040
041    /**
042     * The type of the repository event.
043     */
044    public enum EventType
045    {
046
047        /**
048         * @see RepositoryListener#artifactDescriptorInvalid(RepositoryEvent)
049         */
050        ARTIFACT_DESCRIPTOR_INVALID,
051
052        /**
053         * @see RepositoryListener#artifactDescriptorMissing(RepositoryEvent)
054         */
055        ARTIFACT_DESCRIPTOR_MISSING,
056
057        /**
058         * @see RepositoryListener#metadataInvalid(RepositoryEvent)
059         */
060        METADATA_INVALID,
061
062        /**
063         * @see RepositoryListener#artifactResolving(RepositoryEvent)
064         */
065        ARTIFACT_RESOLVING,
066
067        /**
068         * @see RepositoryListener#artifactResolved(RepositoryEvent)
069         */
070        ARTIFACT_RESOLVED,
071
072        /**
073         * @see RepositoryListener#metadataResolving(RepositoryEvent)
074         */
075        METADATA_RESOLVING,
076
077        /**
078         * @see RepositoryListener#metadataResolved(RepositoryEvent)
079         */
080        METADATA_RESOLVED,
081
082        /**
083         * @see RepositoryListener#artifactDownloading(RepositoryEvent)
084         */
085        ARTIFACT_DOWNLOADING,
086
087        /**
088         * @see RepositoryListener#artifactDownloaded(RepositoryEvent)
089         */
090        ARTIFACT_DOWNLOADED,
091
092        /**
093         * @see RepositoryListener#metadataDownloading(RepositoryEvent)
094         */
095        METADATA_DOWNLOADING,
096
097        /**
098         * @see RepositoryListener#metadataDownloaded(RepositoryEvent)
099         */
100        METADATA_DOWNLOADED,
101
102        /**
103         * @see RepositoryListener#artifactInstalling(RepositoryEvent)
104         */
105        ARTIFACT_INSTALLING,
106
107        /**
108         * @see RepositoryListener#artifactInstalled(RepositoryEvent)
109         */
110        ARTIFACT_INSTALLED,
111
112        /**
113         * @see RepositoryListener#metadataInstalling(RepositoryEvent)
114         */
115        METADATA_INSTALLING,
116
117        /**
118         * @see RepositoryListener#metadataInstalled(RepositoryEvent)
119         */
120        METADATA_INSTALLED,
121
122        /**
123         * @see RepositoryListener#artifactDeploying(RepositoryEvent)
124         */
125        ARTIFACT_DEPLOYING,
126
127        /**
128         * @see RepositoryListener#artifactDeployed(RepositoryEvent)
129         */
130        ARTIFACT_DEPLOYED,
131
132        /**
133         * @see RepositoryListener#metadataDeploying(RepositoryEvent)
134         */
135        METADATA_DEPLOYING,
136
137        /**
138         * @see RepositoryListener#metadataDeployed(RepositoryEvent)
139         */
140        METADATA_DEPLOYED
141
142    }
143
144    private final EventType type;
145
146    private final RepositorySystemSession session;
147
148    private final Artifact artifact;
149
150    private final Metadata metadata;
151
152    private final ArtifactRepository repository;
153
154    private final File file;
155
156    private final List<Exception> exceptions;
157
158    private final RequestTrace trace;
159
160    RepositoryEvent( Builder builder )
161    {
162        type = builder.type;
163        session = builder.session;
164        artifact = builder.artifact;
165        metadata = builder.metadata;
166        repository = builder.repository;
167        file = builder.file;
168        exceptions = builder.exceptions;
169        trace = builder.trace;
170    }
171
172    /**
173     * Gets the type of the event.
174     * 
175     * @return The type of the event, never {@code null}.
176     */
177    public EventType getType()
178    {
179        return type;
180    }
181
182    /**
183     * Gets the repository system session during which the event occurred.
184     * 
185     * @return The repository system session during which the event occurred, never {@code null}.
186     */
187    public RepositorySystemSession getSession()
188    {
189        return session;
190    }
191
192    /**
193     * Gets the artifact involved in the event (if any).
194     * 
195     * @return The involved artifact or {@code null} if none.
196     */
197    public Artifact getArtifact()
198    {
199        return artifact;
200    }
201
202    /**
203     * Gets the metadata involved in the event (if any).
204     * 
205     * @return The involved metadata or {@code null} if none.
206     */
207    public Metadata getMetadata()
208    {
209        return metadata;
210    }
211
212    /**
213     * Gets the file involved in the event (if any).
214     * 
215     * @return The involved file or {@code null} if none.
216     */
217    public File getFile()
218    {
219        return file;
220    }
221
222    /**
223     * Gets the repository involved in the event (if any).
224     * 
225     * @return The involved repository or {@code null} if none.
226     */
227    public ArtifactRepository getRepository()
228    {
229        return repository;
230    }
231
232    /**
233     * Gets the exception that caused the event (if any). As a rule of thumb, an event accompanied by an exception
234     * indicates a failure of the corresponding action. If multiple exceptions occurred, this method returns the first
235     * exception.
236     * 
237     * @return The exception or {@code null} if none.
238     */
239    public Exception getException()
240    {
241        return exceptions.isEmpty() ? null : exceptions.get( 0 );
242    }
243
244    /**
245     * Gets the exceptions that caused the event (if any). As a rule of thumb, an event accompanied by exceptions
246     * indicates a failure of the corresponding action.
247     * 
248     * @return The exceptions, never {@code null}.
249     */
250    public List<Exception> getExceptions()
251    {
252        return exceptions;
253    }
254
255    /**
256     * Gets the trace information about the request during which the event occurred.
257     * 
258     * @return The trace information or {@code null} if none.
259     */
260    public RequestTrace getTrace()
261    {
262        return trace;
263    }
264
265    @Override
266    public String toString()
267    {
268        StringBuilder buffer = new StringBuilder( 256 );
269        buffer.append( getType() );
270        if ( getArtifact() != null )
271        {
272            buffer.append( " " ).append( getArtifact() );
273        }
274        if ( getMetadata() != null )
275        {
276            buffer.append( " " ).append( getMetadata() );
277        }
278        if ( getFile() != null )
279        {
280            buffer.append( " (" ).append( getFile() ).append( ")" );
281        }
282        if ( getRepository() != null )
283        {
284            buffer.append( " @ " ).append( getRepository() );
285        }
286        return buffer.toString();
287    }
288
289    /**
290     * A builder to create events.
291     */
292    public static final class Builder
293    {
294
295        EventType type;
296
297        RepositorySystemSession session;
298
299        Artifact artifact;
300
301        Metadata metadata;
302
303        ArtifactRepository repository;
304
305        File file;
306
307        List<Exception> exceptions = Collections.emptyList();
308
309        RequestTrace trace;
310
311        /**
312         * Creates a new event builder for the specified session and event type.
313         * 
314         * @param session The repository system session, must not be {@code null}.
315         * @param type The type of the event, must not be {@code null}.
316         */
317        public Builder( RepositorySystemSession session, EventType type )
318        {
319            if ( session == null )
320            {
321                throw new IllegalArgumentException( "session not specified" );
322            }
323            this.session = session;
324            if ( type == null )
325            {
326                throw new IllegalArgumentException( "event type not specified" );
327            }
328            this.type = type;
329        }
330
331        /**
332         * Sets the artifact involved in the event.
333         * 
334         * @param artifact The involved artifact, may be {@code null}.
335         * @return This event builder for chaining, never {@code null}.
336         */
337        public Builder setArtifact( Artifact artifact )
338        {
339            this.artifact = artifact;
340            return this;
341        }
342
343        /**
344         * Sets the metadata involved in the event.
345         * 
346         * @param metadata The involved metadata, may be {@code null}.
347         * @return This event builder for chaining, never {@code null}.
348         */
349        public Builder setMetadata( Metadata metadata )
350        {
351            this.metadata = metadata;
352            return this;
353        }
354
355        /**
356         * Sets the repository involved in the event.
357         * 
358         * @param repository The involved repository, may be {@code null}.
359         * @return This event builder for chaining, never {@code null}.
360         */
361        public Builder setRepository( ArtifactRepository repository )
362        {
363            this.repository = repository;
364            return this;
365        }
366
367        /**
368         * Sets the file involved in the event.
369         * 
370         * @param file The involved file, may be {@code null}.
371         * @return This event builder for chaining, never {@code null}.
372         */
373        public Builder setFile( File file )
374        {
375            this.file = file;
376            return this;
377        }
378
379        /**
380         * Sets the exception causing the event.
381         * 
382         * @param exception The exception causing the event, may be {@code null}.
383         * @return This event builder for chaining, never {@code null}.
384         */
385        public Builder setException( Exception exception )
386        {
387            if ( exception != null )
388            {
389                this.exceptions = Collections.singletonList( exception );
390            }
391            else
392            {
393                this.exceptions = Collections.emptyList();
394            }
395            return this;
396        }
397
398        /**
399         * Sets the exceptions causing the event.
400         * 
401         * @param exceptions The exceptions causing the event, may be {@code null}.
402         * @return This event builder for chaining, never {@code null}.
403         */
404        public Builder setExceptions( List<Exception> exceptions )
405        {
406            if ( exceptions != null )
407            {
408                this.exceptions = exceptions;
409            }
410            else
411            {
412                this.exceptions = Collections.emptyList();
413            }
414            return this;
415        }
416
417        /**
418         * Sets the trace information about the request during which the event occurred.
419         * 
420         * @param trace The trace information, may be {@code null}.
421         * @return This event builder for chaining, never {@code null}.
422         */
423        public Builder setTrace( RequestTrace trace )
424        {
425            this.trace = trace;
426            return this;
427        }
428
429        /**
430         * Builds a new event from the current values of this builder. The state of the builder itself remains
431         * unchanged.
432         * 
433         * @return The event, never {@code null}.
434         */
435        public RepositoryEvent build()
436        {
437            return new RepositoryEvent( this );
438        }
439
440    }
441
442}