]> source.dussan.org Git - archiva.git/blob
b0be98c0ec8342a7c384614607faf2b69e8073d5
[archiva.git] /
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.updater.IndexUpdater;
40 import org.slf4j.Logger;
41 import org.slf4j.LoggerFactory;
42 import org.springframework.scheduling.TaskScheduler;
43 import org.springframework.scheduling.support.CronTrigger;
44 import org.springframework.stereotype.Service;
45
46 import javax.annotation.PostConstruct;
47 import javax.annotation.PreDestroy;
48 import javax.inject.Inject;
49 import javax.inject.Named;
50 import java.io.File;
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
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     // store ids about currently running remote download : updated in DownloadRemoteIndexTask
97     private List<String> runningRemoteDownloadIds = new CopyOnWriteArrayList<String>();
98
99     @PostConstruct
100     public void startup()
101         throws ArchivaException, RepositoryAdminException, PlexusSisuBridgeException, IOException,
102         UnsupportedExistingLuceneIndexException, DownloadRemoteIndexException
103     {
104         archivaConfiguration.addListener( this );
105         // TODO add indexContexts even if null
106
107         // FIXME get this from ArchivaAdministration
108         String appServerBase = System.getProperty( "appserver.base" );
109
110         nexusIndexer = plexusSisuBridge.lookup( NexusIndexer.class );
111
112         indexUpdater = plexusSisuBridge.lookup( IndexUpdater.class );
113
114         for ( RemoteRepository remoteRepository : remoteRepositoryAdmin.getRemoteRepositories() )
115         {
116             String contextKey = "remote-" + remoteRepository.getId();
117             if ( nexusIndexer.getIndexingContexts().get( contextKey ) != null )
118             {
119                 continue;
120             }
121             // create path
122             File repoDir = new File( appServerBase, "data/remotes/" + remoteRepository.getId() );
123             if ( !repoDir.exists() )
124             {
125                 repoDir.mkdirs();
126             }
127             File indexDirectory = new File( repoDir, ".index" );
128             if ( !indexDirectory.exists() )
129             {
130                 indexDirectory.mkdirs();
131             }
132             nexusIndexer.addIndexingContext( contextKey, remoteRepository.getId(), repoDir, indexDirectory,
133                                              remoteRepository.getUrl(), calculateIndexRemoteUrl( remoteRepository ),
134                                              mavenIndexerUtils.getAllIndexCreators() );
135             // TODO record jobs from configuration
136             if ( remoteRepository.isDownloadRemoteIndex() && StringUtils.isNotEmpty(
137                 remoteRepository.getCronExpression() ) )
138             {
139                 boolean fullDownload = indexDirectory.list().length == 0;
140                 scheduleDownloadRemote( remoteRepository.getId(), false, fullDownload );
141             }
142         }
143
144
145     }
146
147     @PreDestroy
148     private void shutdown()
149         throws RepositoryAdminException, IOException
150     {
151         for ( RemoteRepository remoteRepository : remoteRepositoryAdmin.getRemoteRepositories() )
152         {
153             String contextKey = "remote-" + remoteRepository.getId();
154             IndexingContext context = nexusIndexer.getIndexingContexts().get( contextKey );
155             if ( context == null )
156             {
157                 continue;
158             }
159             nexusIndexer.removeIndexingContext( context, false );
160         }
161     }
162
163     public void configurationEvent( ConfigurationEvent event )
164     {
165         // TODO remove jobs and add again
166     }
167
168
169     public void scheduleDownloadRemote( String repositoryId, boolean now, boolean fullDownload )
170         throws DownloadRemoteIndexException
171     {
172         try
173         {
174             RemoteRepository remoteRepository = remoteRepositoryAdmin.getRemoteRepository( repositoryId );
175             if ( remoteRepository == null )
176             {
177                 log.warn( "ignore scheduleDownloadRemote for repo with id {} as not exists", repositoryId );
178                 return;
179             }
180             NetworkProxy networkProxy = null;
181             if ( remoteRepository.getRemoteDownloadNetworkProxyId() != null )
182             {
183                 networkProxy = networkProxyAdmin.getNetworkProxy( remoteRepository.getRemoteDownloadNetworkProxyId() );
184                 if ( networkProxy == null )
185                 {
186                     log.warn(
187                         "your remote repository is configured to download remote index trought a proxy we cannot find id:{}",
188                         remoteRepository.getRemoteDownloadNetworkProxyId() );
189                 }
190             }
191
192             //archivaConfiguration.getConfiguration().getProxyConnectorAsMap().get( "" ).get( 0 ).
193             //archivaConfiguration.getConfiguration().getNetworkProxiesAsMap()
194
195             DownloadRemoteIndexTaskRequest downloadRemoteIndexTaskRequest =
196                 new DownloadRemoteIndexTaskRequest().setRemoteRepository( remoteRepository ).setNetworkProxy(
197                     networkProxy ).setFullDownload( fullDownload ).setWagonFactory( wagonFactory ).setNexusIndexer(
198                     nexusIndexer ).setIndexUpdater( indexUpdater );
199
200             if ( now )
201             {
202                 // do it in async
203                 taskScheduler.schedule(
204                     new DownloadRemoteIndexTask( downloadRemoteIndexTaskRequest, this.runningRemoteDownloadIds ),
205                     new Date() );
206             }
207             else
208             {
209
210                 taskScheduler.schedule(
211                     new DownloadRemoteIndexTask( downloadRemoteIndexTaskRequest, this.runningRemoteDownloadIds ),
212                     new CronTrigger( remoteRepository.getCronExpression() ) );
213             }
214
215         }
216         catch ( RepositoryAdminException e )
217         {
218             log.error( e.getMessage(), e );
219             throw new DownloadRemoteIndexException( e.getMessage(), e );
220         }
221     }
222
223     protected String calculateIndexRemoteUrl( RemoteRepository remoteRepository )
224     {
225         if ( StringUtils.startsWith( remoteRepository.getRemoteIndexUrl(), "http" ) )
226         {
227             String baseUrl = remoteRepository.getRemoteIndexUrl();
228             return baseUrl.endsWith( "/" ) ? StringUtils.substringBeforeLast( baseUrl, "/" ) : baseUrl;
229         }
230         String baseUrl = StringUtils.endsWith( remoteRepository.getUrl(), "/" ) ? StringUtils.substringBeforeLast(
231             remoteRepository.getUrl(), "/" ) : remoteRepository.getUrl();
232
233         baseUrl = StringUtils.isEmpty( remoteRepository.getRemoteIndexUrl() )
234             ? baseUrl + "/.index"
235             : baseUrl + "/" + remoteRepository.getRemoteIndexUrl();
236         return baseUrl;
237
238     }
239
240     public TaskScheduler getTaskScheduler()
241     {
242         return taskScheduler;
243     }
244
245     public void setTaskScheduler( TaskScheduler taskScheduler )
246     {
247         this.taskScheduler = taskScheduler;
248     }
249 }