]> source.dussan.org Git - archiva.git/blob
e0bde549b4e82e0408e06a0a929c5ede86919ecc
[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.UnsupportedExistingLuceneIndexException;
38 import org.apache.maven.index.updater.IndexUpdater;
39 import org.slf4j.Logger;
40 import org.slf4j.LoggerFactory;
41 import org.springframework.scheduling.TaskScheduler;
42 import org.springframework.scheduling.support.CronTrigger;
43 import org.springframework.stereotype.Service;
44
45 import javax.annotation.PostConstruct;
46 import javax.inject.Inject;
47 import javax.inject.Named;
48 import java.io.File;
49 import java.io.IOException;
50 import java.util.Date;
51 import java.util.List;
52 import java.util.concurrent.CopyOnWriteArrayList;
53
54 /**
55  * @author Olivier Lamy
56  * @since 1.4
57  */
58 @Service( "downloadRemoteIndexScheduler#default" )
59 public class DefaultDownloadRemoteIndexScheduler
60     implements ConfigurationListener, DownloadRemoteIndexScheduler
61 {
62
63     private Logger log = LoggerFactory.getLogger( getClass() );
64
65     @Inject
66     @Named( value = "taskScheduler#indexDownloadRemote" )
67     private TaskScheduler taskScheduler;
68
69     @Inject
70     private ArchivaConfiguration archivaConfiguration;
71
72     @Inject
73     private WagonFactory wagonFactory;
74
75     @Inject
76     private RemoteRepositoryAdmin remoteRepositoryAdmin;
77
78     @Inject
79     private ProxyConnectorAdmin proxyConnectorAdmin;
80
81     @Inject
82     private NetworkProxyAdmin networkProxyAdmin;
83
84     @Inject
85     private PlexusSisuBridge plexusSisuBridge;
86
87     @Inject
88     private MavenIndexerUtils mavenIndexerUtils;
89
90     private NexusIndexer nexusIndexer;
91
92     private IndexUpdater indexUpdater;
93
94     // store ids about currently running remote download : updated in DownloadRemoteIndexTask
95     private List<String> runningRemoteDownloadIds = new CopyOnWriteArrayList<String>();
96
97     @PostConstruct
98     public void startup()
99         throws ArchivaException, RepositoryAdminException, PlexusSisuBridgeException, IOException,
100         UnsupportedExistingLuceneIndexException, DownloadRemoteIndexException
101     {
102         archivaConfiguration.addListener( this );
103         // TODO add indexContexts even if null
104
105         // FIXME get this from ArchivaAdministration
106         String appServerBase = System.getProperty( "appserver.base" );
107
108         nexusIndexer = plexusSisuBridge.lookup( NexusIndexer.class );
109
110         indexUpdater = plexusSisuBridge.lookup( IndexUpdater.class );
111
112         for ( RemoteRepository remoteRepository : remoteRepositoryAdmin.getRemoteRepositories() )
113         {
114             String contextKey = "remote-" + remoteRepository.getId();
115             if ( nexusIndexer.getIndexingContexts().get( contextKey ) != null )
116             {
117                 continue;
118             }
119             // create path
120             File repoDir = new File( appServerBase, "data/remotes/" + remoteRepository.getId() );
121             if ( !repoDir.exists() )
122             {
123                 repoDir.mkdirs();
124             }
125             File indexDirectory = new File( repoDir, ".index" );
126             if ( !indexDirectory.exists() )
127             {
128                 indexDirectory.mkdirs();
129             }
130             nexusIndexer.addIndexingContext( contextKey, remoteRepository.getId(), repoDir, indexDirectory,
131                                              remoteRepository.getUrl(), calculateIndexRemoteUrl( remoteRepository ),
132                                              mavenIndexerUtils.getAllIndexCreators() );
133             // TODO record jobs from configuration
134             if ( remoteRepository.isDownloadRemoteIndex() && StringUtils.isNotEmpty(
135                 remoteRepository.getCronExpression() ) )
136             {
137                 boolean fullDownload = indexDirectory.list().length == 0;
138                 scheduleDownloadRemote( remoteRepository.getId(), false, fullDownload );
139             }
140         }
141
142
143     }
144
145     public void configurationEvent( ConfigurationEvent event )
146     {
147         // TODO remove jobs and add again
148     }
149
150
151     public void scheduleDownloadRemote( String repositoryId, boolean now, boolean fullDownload )
152         throws DownloadRemoteIndexException
153     {
154         try
155         {
156             RemoteRepository remoteRepository = remoteRepositoryAdmin.getRemoteRepository( repositoryId );
157             if ( remoteRepository == null )
158             {
159                 log.warn( "ignore scheduleDownloadRemote for repo with id {} as not exists", repositoryId );
160                 return;
161             }
162             NetworkProxy networkProxy = null;
163             if ( remoteRepository.getRemoteDownloadNetworkProxyId() != null )
164             {
165                 networkProxy = networkProxyAdmin.getNetworkProxy( remoteRepository.getRemoteDownloadNetworkProxyId() );
166                 if ( networkProxy == null )
167                 {
168                     log.warn(
169                         "your remote repository is configured to download remote index trought a proxy we cannot find id:{}",
170                         remoteRepository.getRemoteDownloadNetworkProxyId() );
171                 }
172             }
173
174             //archivaConfiguration.getConfiguration().getProxyConnectorAsMap().get( "" ).get( 0 ).
175             //archivaConfiguration.getConfiguration().getNetworkProxiesAsMap()
176
177             DownloadRemoteIndexTaskRequest downloadRemoteIndexTaskRequest =
178                 new DownloadRemoteIndexTaskRequest().setRemoteRepository( remoteRepository ).setNetworkProxy(
179                     networkProxy ).setFullDownload( fullDownload ).setWagonFactory( wagonFactory ).setNexusIndexer(
180                     nexusIndexer ).setIndexUpdater( indexUpdater );
181
182             if ( now )
183             {
184                 // do it in async
185                 taskScheduler.schedule(
186                     new DownloadRemoteIndexTask( downloadRemoteIndexTaskRequest, this.runningRemoteDownloadIds ),
187                     new Date() );
188             }
189             else
190             {
191
192                 taskScheduler.schedule(
193                     new DownloadRemoteIndexTask( downloadRemoteIndexTaskRequest, this.runningRemoteDownloadIds ),
194                     new CronTrigger( remoteRepository.getCronExpression() ) );
195             }
196
197         }
198         catch ( RepositoryAdminException e )
199         {
200             log.error( e.getMessage(), e );
201             throw new DownloadRemoteIndexException( e.getMessage(), e );
202         }
203     }
204
205     protected String calculateIndexRemoteUrl( RemoteRepository remoteRepository )
206     {
207         if ( StringUtils.startsWith( remoteRepository.getRemoteIndexUrl(), "http" ) )
208         {
209             String baseUrl = remoteRepository.getRemoteIndexUrl();
210             return baseUrl.endsWith( "/" ) ? StringUtils.substringBeforeLast( baseUrl, "/" ) : baseUrl;
211         }
212         String baseUrl = StringUtils.endsWith( remoteRepository.getUrl(), "/" ) ? StringUtils.substringBeforeLast(
213             remoteRepository.getUrl(), "/" ) : remoteRepository.getUrl();
214
215         baseUrl = StringUtils.isEmpty( remoteRepository.getRemoteIndexUrl() )
216             ? baseUrl + "/.index"
217             : baseUrl + "/" + remoteRepository.getRemoteIndexUrl();
218         return baseUrl;
219
220     }
221
222     public TaskScheduler getTaskScheduler()
223     {
224         return taskScheduler;
225     }
226
227     public void setTaskScheduler( TaskScheduler taskScheduler )
228     {
229         this.taskScheduler = taskScheduler;
230     }
231 }