This project has retired. For details please refer to its Attic page.
DefaultDownloadRemoteIndexScheduler xref
View Javadoc
1   package org.apache.archiva.scheduler.indexing.maven;
2   /*
3    * Licensed to the Apache Software Foundation (ASF) under one
4    * or more contributor license agreements.  See the NOTICE file
5    * distributed with this work for additional information
6    * regarding copyright ownership.  The ASF licenses this file
7    * to you under the Apache License, Version 2.0 (the
8    * "License"); you may not use this file except in compliance
9    * with the License.  You may obtain a copy of the License at
10   *
11   *   http://www.apache.org/licenses/LICENSE-2.0
12   *
13   * Unless required by applicable law or agreed to in writing,
14   * software distributed under the License is distributed on an
15   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16   * KIND, either express or implied.  See the License for the
17   * specific language governing permissions and limitations
18   * under the License.
19   */
20  
21  import org.apache.archiva.proxy.ProxyRegistry;
22  import org.apache.archiva.proxy.model.NetworkProxy;
23  import org.apache.archiva.scheduler.indexing.DownloadRemoteIndexException;
24  import org.apache.archiva.scheduler.indexing.DownloadRemoteIndexScheduler;
25  import org.apache.archiva.admin.model.RepositoryAdminException;
26  import org.apache.archiva.configuration.ArchivaConfiguration;
27  import org.apache.archiva.configuration.ConfigurationEvent;
28  import org.apache.archiva.configuration.ConfigurationListener;
29  import org.apache.archiva.indexer.UnsupportedBaseContextException;
30  import org.apache.archiva.proxy.maven.WagonFactory;
31  import org.apache.archiva.repository.RepositoryRegistry;
32  import org.apache.archiva.repository.features.RemoteIndexFeature;
33  import org.apache.commons.lang3.StringUtils;
34  import org.apache.maven.index.context.IndexingContext;
35  import org.apache.maven.index.packer.IndexPacker;
36  import org.apache.maven.index.updater.IndexUpdater;
37  import org.slf4j.Logger;
38  import org.slf4j.LoggerFactory;
39  import org.springframework.scheduling.TaskScheduler;
40  import org.springframework.scheduling.support.CronTrigger;
41  import org.springframework.stereotype.Service;
42  
43  import javax.annotation.PostConstruct;
44  import javax.inject.Inject;
45  import javax.inject.Named;
46  import java.util.Date;
47  import java.util.List;
48  import java.util.concurrent.CopyOnWriteArrayList;
49  
50  /**
51   * @author Olivier Lamy
52   * @since 1.4-M1
53   */
54  @Service( "downloadRemoteIndexScheduler#default" )
55  public class DefaultDownloadRemoteIndexScheduler
56      implements ConfigurationListener, DownloadRemoteIndexScheduler
57  {
58  
59      private Logger log = LoggerFactory.getLogger( getClass() );
60  
61      @Inject
62      @Named( value = "taskScheduler#indexDownloadRemote" )
63      private TaskScheduler taskScheduler;
64  
65      @Inject
66      RepositoryRegistry repositoryRegistry;
67  
68      @Inject
69      private ArchivaConfiguration archivaConfiguration;
70  
71      @Inject
72      private WagonFactory wagonFactory;
73  
74      @Inject
75      private IndexUpdater indexUpdater;
76  
77      @Inject
78      private IndexPacker indexPacker;
79  
80      @Inject
81      private ProxyRegistry proxyRegistry;
82  
83      // store ids about currently running remote download : updated in DownloadRemoteIndexTask
84      private List<String> runningRemoteDownloadIds = new CopyOnWriteArrayList<String>();
85  
86      @PostConstruct
87      public void startup()
88              throws
89          DownloadRemoteIndexException, UnsupportedBaseContextException {
90          archivaConfiguration.addListener( this );
91          // TODO add indexContexts even if null
92  
93          for ( org.apache.archiva.repository.RemoteRepository remoteRepository : repositoryRegistry.getRemoteRepositories() )
94          {
95              String contextKey = "remote-" + remoteRepository.getId();
96              IndexingContext context = remoteRepository.getIndexingContext().getBaseContext(IndexingContext.class);
97              if ( context == null )
98              {
99                  continue;
100             }
101             RemoteIndexFeature rif = remoteRepository.getFeature(RemoteIndexFeature.class).get();
102 
103 
104             // TODO record jobs from configuration
105             if ( rif.isDownloadRemoteIndex() && StringUtils.isNotEmpty(
106                 remoteRepository.getSchedulingDefinition() ) )
107             {
108                 boolean fullDownload = context.getIndexDirectoryFile().list().length == 0;
109                 scheduleDownloadRemote( remoteRepository.getId(), false, fullDownload );
110             }
111         }
112 
113 
114     }
115 
116     @Override
117     public void configurationEvent( ConfigurationEvent event )
118     {
119         // TODO remove jobs and add again
120     }
121 
122 
123     @Override
124     public void scheduleDownloadRemote( String repositoryId, boolean now, boolean fullDownload )
125         throws DownloadRemoteIndexException
126     {
127         org.apache.archiva.repository.RemoteRepository remoteRepo = repositoryRegistry.getRemoteRepository(repositoryId);
128 
129         if ( remoteRepo == null )
130         {
131             log.warn( "ignore scheduleDownloadRemote for repo with id {} as not exists", repositoryId );
132             return;
133         }
134         if (!remoteRepo.supportsFeature(RemoteIndexFeature.class)) {
135             log.warn("ignore scheduleDownloadRemote for repo with id {}. Does not support remote index.", repositoryId);
136             return;
137         }
138         RemoteIndexFeature rif = remoteRepo.getFeature(RemoteIndexFeature.class).get();
139         NetworkProxy networkProxy = null;
140         if ( StringUtils.isNotBlank( rif.getProxyId() ) )
141         {
142             networkProxy = proxyRegistry.getNetworkProxy( rif.getProxyId() );
143             if ( networkProxy == null )
144             {
145                 log.warn(
146                     "your remote repository is configured to download remote index trought a proxy we cannot find id:{}",
147                     rif.getProxyId() );
148             }
149         }
150 
151         DownloadRemoteIndexTaskRequestuest.html#DownloadRemoteIndexTaskRequest">DownloadRemoteIndexTaskRequest downloadRemoteIndexTaskRequest = new DownloadRemoteIndexTaskRequest() //
152             .setRemoteRepository( remoteRepo ) //
153             .setNetworkProxy( networkProxy ) //
154             .setFullDownload( fullDownload ) //
155             .setWagonFactory( wagonFactory ) //
156             .setIndexUpdater( indexUpdater ) //
157             .setIndexPacker( this.indexPacker );
158 
159         if ( now )
160         {
161             log.info( "schedule download remote index for repository {}", remoteRepo.getId() );
162             // do it now
163             taskScheduler.schedule(
164                 new DownloadRemoteIndexTask( downloadRemoteIndexTaskRequest, this.runningRemoteDownloadIds ),
165                 new Date() );
166         }
167         else
168         {
169             log.info( "schedule download remote index for repository {} with cron expression {}",
170                       remoteRepo.getId(), remoteRepo.getSchedulingDefinition());
171             try
172             {
173                 CronTrigger cronTrigger = new CronTrigger( remoteRepo.getSchedulingDefinition());
174                 taskScheduler.schedule(
175                     new DownloadRemoteIndexTask( downloadRemoteIndexTaskRequest, this.runningRemoteDownloadIds ),
176                     cronTrigger );
177             }
178             catch ( IllegalArgumentException e )
179             {
180                 log.warn( "Unable to schedule remote index download: {}", e.getLocalizedMessage() );
181             }
182 
183             if ( rif.isDownloadRemoteIndexOnStartup() )
184             {
185                 log.info(
186                     "remote repository {} configured with downloadRemoteIndexOnStartup schedule now a download",
187                     remoteRepo.getId() );
188                 taskScheduler.schedule(
189                     new DownloadRemoteIndexTask( downloadRemoteIndexTaskRequest, this.runningRemoteDownloadIds ),
190                     new Date() );
191             }
192         }
193 
194     }
195 
196     public TaskScheduler getTaskScheduler()
197     {
198         return taskScheduler;
199     }
200 
201     public void setTaskScheduler( TaskScheduler taskScheduler )
202     {
203         this.taskScheduler = taskScheduler;
204     }
205 
206     @Override
207     public List<String> getRunningRemoteDownloadIds()
208     {
209         return runningRemoteDownloadIds;
210     }
211 }