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

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