This project has retired. For details please refer to its Attic page.
DefaultDownloadRemoteIndexScheduler xref
View Javadoc
1   package org.apache.archiva.scheduler.indexing;
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.admin.model.RepositoryAdminException;
22  import org.apache.archiva.admin.model.beans.NetworkProxy;
23  import org.apache.archiva.admin.model.beans.RemoteRepository;
24  import org.apache.archiva.admin.model.networkproxy.NetworkProxyAdmin;
25  import org.apache.archiva.admin.model.proxyconnector.ProxyConnectorAdmin;
26  import org.apache.archiva.admin.model.remote.RemoteRepositoryAdmin;
27  import org.apache.archiva.common.ArchivaException;
28  import org.apache.archiva.common.plexusbridge.MavenIndexerUtils;
29  import org.apache.archiva.common.plexusbridge.PlexusSisuBridge;
30  import org.apache.archiva.common.plexusbridge.PlexusSisuBridgeException;
31  import org.apache.archiva.configuration.ArchivaConfiguration;
32  import org.apache.archiva.configuration.ConfigurationEvent;
33  import org.apache.archiva.configuration.ConfigurationListener;
34  import org.apache.archiva.proxy.common.WagonFactory;
35  import org.apache.commons.lang.StringUtils;
36  import org.apache.maven.index.NexusIndexer;
37  import org.apache.maven.index.context.IndexingContext;
38  import org.apache.maven.index.context.UnsupportedExistingLuceneIndexException;
39  import org.apache.maven.index.packer.IndexPacker;
40  import org.apache.maven.index.updater.IndexUpdater;
41  import org.slf4j.Logger;
42  import org.slf4j.LoggerFactory;
43  import org.springframework.scheduling.TaskScheduler;
44  import org.springframework.scheduling.support.CronTrigger;
45  import org.springframework.stereotype.Service;
46  
47  import javax.annotation.PostConstruct;
48  import javax.annotation.PreDestroy;
49  import javax.inject.Inject;
50  import javax.inject.Named;
51  import java.io.IOException;
52  import java.util.Date;
53  import java.util.List;
54  import java.util.concurrent.CopyOnWriteArrayList;
55  
56  /**
57   * @author Olivier Lamy
58   * @since 1.4-M1
59   */
60  @Service ("downloadRemoteIndexScheduler#default")
61  public class DefaultDownloadRemoteIndexScheduler
62      implements ConfigurationListener, DownloadRemoteIndexScheduler
63  {
64  
65      private Logger log = LoggerFactory.getLogger( getClass() );
66  
67      @Inject
68      @Named (value = "taskScheduler#indexDownloadRemote")
69      private TaskScheduler taskScheduler;
70  
71      @Inject
72      private ArchivaConfiguration archivaConfiguration;
73  
74      @Inject
75      private WagonFactory wagonFactory;
76  
77      @Inject
78      private RemoteRepositoryAdmin remoteRepositoryAdmin;
79  
80      @Inject
81      private ProxyConnectorAdmin proxyConnectorAdmin;
82  
83      @Inject
84      private NetworkProxyAdmin networkProxyAdmin;
85  
86      @Inject
87      private PlexusSisuBridge plexusSisuBridge;
88  
89      @Inject
90      private MavenIndexerUtils mavenIndexerUtils;
91  
92      private NexusIndexer nexusIndexer;
93  
94      private IndexUpdater indexUpdater;
95  
96      private IndexPacker indexPacker;
97  
98      // store ids about currently running remote download : updated in DownloadRemoteIndexTask
99      private List<String> runningRemoteDownloadIds = new CopyOnWriteArrayList<String>();
100 
101     @PostConstruct
102     public void startup()
103         throws ArchivaException, RepositoryAdminException, PlexusSisuBridgeException, IOException,
104         UnsupportedExistingLuceneIndexException, DownloadRemoteIndexException
105     {
106         archivaConfiguration.addListener( this );
107         // TODO add indexContexts even if null
108 
109         nexusIndexer = plexusSisuBridge.lookup( NexusIndexer.class );
110 
111         indexUpdater = plexusSisuBridge.lookup( IndexUpdater.class );
112 
113         this.indexPacker = plexusSisuBridge.lookup( IndexPacker.class );
114 
115         for ( RemoteRepository remoteRepository : remoteRepositoryAdmin.getRemoteRepositories() )
116         {
117             String contextKey = "remote-" + remoteRepository.getId();
118             IndexingContext context = nexusIndexer.getIndexingContexts().get( contextKey );
119             if ( context == null )
120             {
121                 continue;
122             }
123 
124             // TODO record jobs from configuration
125             if ( remoteRepository.isDownloadRemoteIndex() && StringUtils.isNotEmpty(
126                 remoteRepository.getCronExpression() ) )
127             {
128                 boolean fullDownload = context.getIndexDirectoryFile().list().length == 0;
129                 scheduleDownloadRemote( remoteRepository.getId(), false, fullDownload );
130             }
131         }
132 
133 
134     }
135 
136     @PreDestroy
137     public void shutdown()
138         throws RepositoryAdminException, IOException
139     {
140         for ( RemoteRepository remoteRepository : remoteRepositoryAdmin.getRemoteRepositories() )
141         {
142             String contextKey = "remote-" + remoteRepository.getId();
143             IndexingContext context = nexusIndexer.getIndexingContexts().get( contextKey );
144             if ( context == null )
145             {
146                 continue;
147             }
148             nexusIndexer.removeIndexingContext( context, false );
149         }
150     }
151 
152     @Override
153     public void configurationEvent( ConfigurationEvent event )
154     {
155         // TODO remove jobs and add again
156     }
157 
158 
159     @Override
160     public void scheduleDownloadRemote( String repositoryId, boolean now, boolean fullDownload )
161         throws DownloadRemoteIndexException
162     {
163         try
164         {
165             RemoteRepository remoteRepository = remoteRepositoryAdmin.getRemoteRepository( repositoryId );
166             if ( remoteRepository == null )
167             {
168                 log.warn( "ignore scheduleDownloadRemote for repo with id {} as not exists", repositoryId );
169                 return;
170             }
171             NetworkProxy networkProxy = null;
172             if ( StringUtils.isNotBlank( remoteRepository.getRemoteDownloadNetworkProxyId() ) )
173             {
174                 networkProxy = networkProxyAdmin.getNetworkProxy( remoteRepository.getRemoteDownloadNetworkProxyId() );
175                 if ( networkProxy == null )
176                 {
177                     log.warn(
178                         "your remote repository is configured to download remote index trought a proxy we cannot find id:{}",
179                         remoteRepository.getRemoteDownloadNetworkProxyId() );
180                 }
181             }
182 
183             DownloadRemoteIndexTaskRequest downloadRemoteIndexTaskRequest =
184                 new DownloadRemoteIndexTaskRequest().setRemoteRepository( remoteRepository ).setNetworkProxy(
185                     networkProxy ).setFullDownload( fullDownload ).setWagonFactory(
186                     wagonFactory ).setRemoteRepositoryAdmin( remoteRepositoryAdmin ).setIndexUpdater(
187                     indexUpdater ).setIndexPacker( this.indexPacker );
188 
189             if ( now )
190             {
191                 log.info( "schedule download remote index for repository {}", remoteRepository.getId() );
192                 // do it now
193                 taskScheduler.schedule(
194                     new DownloadRemoteIndexTask( downloadRemoteIndexTaskRequest, this.runningRemoteDownloadIds ),
195                     new Date() );
196             }
197             else
198             {
199                 log.info( "schedule download remote index for repository {} with cron expression {}",
200                           remoteRepository.getId(), remoteRepository.getCronExpression() );
201                 try
202                 {
203                     CronTrigger cronTrigger = new CronTrigger( remoteRepository.getCronExpression() );
204                     taskScheduler.schedule(
205                         new DownloadRemoteIndexTask( downloadRemoteIndexTaskRequest, this.runningRemoteDownloadIds ),
206                         cronTrigger );
207                 }
208                 catch ( IllegalArgumentException e )
209                 {
210                     log.warn( "Unable to schedule remote index download: {}", e.getLocalizedMessage() );
211                 }
212 
213                 if ( remoteRepository.isDownloadRemoteIndexOnStartup() )
214                 {
215                     log.info(
216                         "remote repository {} configured with downloadRemoteIndexOnStartup schedule now a download",
217                         remoteRepository.getId() );
218                     taskScheduler.schedule(
219                         new DownloadRemoteIndexTask( downloadRemoteIndexTaskRequest, this.runningRemoteDownloadIds ),
220                         new Date() );
221                 }
222             }
223 
224         }
225         catch ( RepositoryAdminException e )
226         {
227             log.error( e.getMessage(), e );
228             throw new DownloadRemoteIndexException( e.getMessage(), e );
229         }
230     }
231 
232     public TaskScheduler getTaskScheduler()
233     {
234         return taskScheduler;
235     }
236 
237     public void setTaskScheduler( TaskScheduler taskScheduler )
238     {
239         this.taskScheduler = taskScheduler;
240     }
241 
242     @Override
243     public List<String> getRunningRemoteDownloadIds()
244     {
245         return runningRemoteDownloadIds;
246     }
247 }