View Javadoc
1   package org.eclipse.aether.internal.impl;
2   
3   /*
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   *  http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing,
15   * software distributed under the License is distributed on an
16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17   * KIND, either express or implied.  See the License for the
18   * specific language governing permissions and limitations
19   * under the License.
20   */
21  
22  import java.util.ArrayList;
23  import java.util.Collection;
24  import static java.util.Objects.requireNonNull;
25  import java.util.Set;
26  
27  import javax.inject.Inject;
28  import javax.inject.Named;
29  import javax.inject.Singleton;
30  
31  import org.eclipse.aether.RepositoryEvent;
32  import org.eclipse.aether.RepositoryListener;
33  import org.eclipse.aether.impl.RepositoryEventDispatcher;
34  import org.eclipse.aether.spi.locator.Service;
35  import org.eclipse.aether.spi.locator.ServiceLocator;
36  import org.slf4j.Logger;
37  import org.slf4j.LoggerFactory;
38  
39  /**
40   */
41  @Singleton
42  @Named
43  public class DefaultRepositoryEventDispatcher
44      implements RepositoryEventDispatcher, Service
45  {
46  
47      private static final Logger LOGGER = LoggerFactory.getLogger( DefaultRepositoryEventDispatcher.class );
48  
49      private Collection<RepositoryListener> listeners = new ArrayList<>();
50  
51      public DefaultRepositoryEventDispatcher()
52      {
53          // enables no-arg constructor
54      }
55  
56      @Inject
57      DefaultRepositoryEventDispatcher( Set<RepositoryListener> listeners )
58      {
59          setRepositoryListeners( listeners );
60      }
61  
62      public DefaultRepositoryEventDispatcher addRepositoryListener( RepositoryListener listener )
63      {
64          this.listeners.add( requireNonNull( listener, "repository listener cannot be null" ) );
65          return this;
66      }
67  
68      public DefaultRepositoryEventDispatcher setRepositoryListeners( Collection<RepositoryListener> listeners )
69      {
70          if ( listeners == null )
71          {
72              this.listeners = new ArrayList<>();
73          }
74          else
75          {
76              this.listeners = listeners;
77          }
78          return this;
79      }
80  
81      public void initService( ServiceLocator locator )
82      {
83          setRepositoryListeners( locator.getServices( RepositoryListener.class ) );
84      }
85  
86      public void dispatch( RepositoryEvent event )
87      {
88          requireNonNull( event, "event cannot be null" );
89          if ( !listeners.isEmpty() )
90          {
91              for ( RepositoryListener listener : listeners )
92              {
93                  dispatch( event, listener );
94              }
95          }
96  
97          RepositoryListener listener = event.getSession().getRepositoryListener();
98  
99          if ( listener != null )
100         {
101             dispatch( event, listener );
102         }
103     }
104 
105     private void dispatch( RepositoryEvent event, RepositoryListener listener )
106     {
107         try
108         {
109             switch ( event.getType() )
110             {
111                 case ARTIFACT_DEPLOYED:
112                     listener.artifactDeployed( event );
113                     break;
114                 case ARTIFACT_DEPLOYING:
115                     listener.artifactDeploying( event );
116                     break;
117                 case ARTIFACT_DESCRIPTOR_INVALID:
118                     listener.artifactDescriptorInvalid( event );
119                     break;
120                 case ARTIFACT_DESCRIPTOR_MISSING:
121                     listener.artifactDescriptorMissing( event );
122                     break;
123                 case ARTIFACT_DOWNLOADED:
124                     listener.artifactDownloaded( event );
125                     break;
126                 case ARTIFACT_DOWNLOADING:
127                     listener.artifactDownloading( event );
128                     break;
129                 case ARTIFACT_INSTALLED:
130                     listener.artifactInstalled( event );
131                     break;
132                 case ARTIFACT_INSTALLING:
133                     listener.artifactInstalling( event );
134                     break;
135                 case ARTIFACT_RESOLVED:
136                     listener.artifactResolved( event );
137                     break;
138                 case ARTIFACT_RESOLVING:
139                     listener.artifactResolving( event );
140                     break;
141                 case METADATA_DEPLOYED:
142                     listener.metadataDeployed( event );
143                     break;
144                 case METADATA_DEPLOYING:
145                     listener.metadataDeploying( event );
146                     break;
147                 case METADATA_DOWNLOADED:
148                     listener.metadataDownloaded( event );
149                     break;
150                 case METADATA_DOWNLOADING:
151                     listener.metadataDownloading( event );
152                     break;
153                 case METADATA_INSTALLED:
154                     listener.metadataInstalled( event );
155                     break;
156                 case METADATA_INSTALLING:
157                     listener.metadataInstalling( event );
158                     break;
159                 case METADATA_INVALID:
160                     listener.metadataInvalid( event );
161                     break;
162                 case METADATA_RESOLVED:
163                     listener.metadataResolved( event );
164                     break;
165                 case METADATA_RESOLVING:
166                     listener.metadataResolving( event );
167                     break;
168                 default:
169                     throw new IllegalStateException( "unknown repository event type " + event.getType() );
170             }
171         }
172         catch ( Exception | LinkageError e )
173         {
174             logError( e, listener );
175         }
176     }
177 
178     private void logError( Throwable e, Object listener )
179     {
180         LOGGER.warn( "Failed to dispatch repository event to {}", listener.getClass().getCanonicalName(), e );
181     }
182 
183 }