You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

DefaultDownloadRemoteIndexScheduler.java 7.7KB

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