]> source.dussan.org Git - archiva.git/commitdiff
add a convenient method to create index in repository admin service for remote repos
authorOlivier Lamy <olamy@apache.org>
Wed, 2 Nov 2011 22:30:45 +0000 (22:30 +0000)
committerOlivier Lamy <olamy@apache.org>
Wed, 2 Nov 2011 22:30:45 +0000 (22:30 +0000)
git-svn-id: https://svn.apache.org/repos/asf/archiva/trunk@1196838 13f79535-47bb-0310-9956-ffa450edef68

archiva-modules/archiva-base/archiva-repository-admin/archiva-repository-admin-api/src/main/java/org/apache/archiva/admin/model/remote/RemoteRepositoryAdmin.java
archiva-modules/archiva-base/archiva-repository-admin/archiva-repository-admin-default/src/main/java/org/apache/archiva/admin/repository/managed/DefaultManagedRepositoryAdmin.java
archiva-modules/archiva-base/archiva-repository-admin/archiva-repository-admin-default/src/main/java/org/apache/archiva/admin/repository/remote/DefaultRemoteRepositoryAdmin.java

index 887ad925b75f127e3982a04254bcec7d8414b253..da841f4015f37c0eed14d40b64e63bd3733d6def 100644 (file)
@@ -22,6 +22,7 @@ package org.apache.archiva.admin.model.remote;
 import org.apache.archiva.admin.model.AuditInformation;
 import org.apache.archiva.admin.model.RepositoryAdminException;
 import org.apache.archiva.admin.model.beans.RemoteRepository;
+import org.apache.maven.index.context.IndexingContext;
 
 import java.util.List;
 import java.util.Map;
@@ -49,4 +50,13 @@ public interface RemoteRepositoryAdmin
 
     Map<String, RemoteRepository> getRemoteRepositoriesAsMap()
         throws RepositoryAdminException;
+
+    /**
+     * @param repository
+     * @return
+     * @throws RepositoryAdminException
+     * @since 1.4-M2
+     */
+    IndexingContext createIndexContext( RemoteRepository repository )
+        throws RepositoryAdminException;
 }
index 5348630a20c09f28dda697828319eff36ce5d586..9f23674907be8e414ab77da0815f4051d0657dbf 100644 (file)
@@ -173,7 +173,7 @@ public class DefaultManagedRepositoryAdmin
 
         getRepositoryCommonValidator().basicValidation( managedRepository, false );
         triggerAuditEvent( managedRepository.getId(), null, AuditEvent.ADD_MANAGED_REPO, auditInformation );
-        return
+        Boolean res =
             addManagedRepository( managedRepository.getId(), managedRepository.getLayout(), managedRepository.getName(),
                                   managedRepository.getLocation(), managedRepository.isBlockRedeployments(),
                                   managedRepository.isReleases(), managedRepository.isSnapshots(), needStageRepo,
@@ -182,6 +182,9 @@ public class DefaultManagedRepositoryAdmin
                                   managedRepository.isDeleteReleasedSnapshots(), auditInformation,
                                   getArchivaConfiguration().getConfiguration() ) != null;
 
+        createIndexContext( managedRepository );
+        return res;
+
     }
 
     private ManagedRepositoryConfiguration addManagedRepository( String repoId, String layout, String name,
@@ -486,7 +489,7 @@ public class DefaultManagedRepositoryAdmin
         {
             repositorySession.close();
         }
-
+        createIndexContext( managedRepository );
         return true;
     }
 
@@ -512,7 +515,7 @@ public class DefaultManagedRepositoryAdmin
         }
 
         configuration.addManagedRepository( repository );
-
+        
     }
 
     public IndexingContext createIndexContext( ManagedRepository repository )
index 36350af2d7af2d629d9ae65c50025035f960386b..4c703c5cc9605968c2f759f4cd438a174729f6c0 100644 (file)
@@ -24,12 +24,24 @@ import org.apache.archiva.admin.model.beans.RemoteRepository;
 import org.apache.archiva.admin.model.remote.RemoteRepositoryAdmin;
 import org.apache.archiva.admin.repository.AbstractRepositoryAdmin;
 import org.apache.archiva.audit.AuditEvent;
+import org.apache.archiva.common.plexusbridge.MavenIndexerUtils;
+import org.apache.archiva.common.plexusbridge.PlexusSisuBridge;
+import org.apache.archiva.common.plexusbridge.PlexusSisuBridgeException;
 import org.apache.archiva.configuration.Configuration;
 import org.apache.archiva.configuration.ProxyConnectorConfiguration;
 import org.apache.archiva.configuration.RemoteRepositoryConfiguration;
 import org.apache.commons.lang.StringUtils;
+import org.apache.maven.index.NexusIndexer;
+import org.apache.maven.index.context.IndexCreator;
+import org.apache.maven.index.context.IndexingContext;
+import org.apache.maven.index.context.UnsupportedExistingLuceneIndexException;
 import org.springframework.stereotype.Service;
 
+import javax.annotation.PostConstruct;
+import javax.inject.Inject;
+import java.io.File;
+import java.io.IOException;
+import java.net.MalformedURLException;
 import java.util.ArrayList;
 import java.util.HashMap;
 import java.util.List;
@@ -45,6 +57,22 @@ public class DefaultRemoteRepositoryAdmin
     implements RemoteRepositoryAdmin
 {
 
+    @Inject
+    private PlexusSisuBridge plexusSisuBridge;
+
+    @Inject
+    private MavenIndexerUtils mavenIndexerUtils;
+
+    @PostConstruct
+    private void initialize()
+        throws RepositoryAdminException
+    {
+        for ( RemoteRepository remoteRepository : getRemoteRepositories() )
+        {
+            createIndexContext( remoteRepository );
+        }
+    }
+
 
     public List<RemoteRepository> getRemoteRepositories()
         throws RepositoryAdminException
@@ -184,6 +212,75 @@ public class DefaultRemoteRepositoryAdmin
         return map;
     }
 
+    public IndexingContext createIndexContext( RemoteRepository remoteRepository )
+        throws RepositoryAdminException
+    {
+        try
+        {
+            // FIXME get this from ArchivaAdministration
+            String appServerBase = System.getProperty( "appserver.base" );
+
+            List<? extends IndexCreator> indexCreators = mavenIndexerUtils.getAllIndexCreators();
+            NexusIndexer nexusIndexer = plexusSisuBridge.lookup( NexusIndexer.class );
+
+            String contextKey = "remote-" + remoteRepository.getId();
+            IndexingContext indexingContext = nexusIndexer.getIndexingContexts().get( contextKey );
+            if ( indexingContext != null )
+            {
+                return indexingContext;
+            }
+            // create path
+            File repoDir = new File( appServerBase, "data/remotes/" + remoteRepository.getId() );
+            if ( !repoDir.exists() )
+            {
+                repoDir.mkdirs();
+            }
+            File indexDirectory = new File( repoDir, ".index" );
+            if ( !indexDirectory.exists() )
+            {
+                indexDirectory.mkdirs();
+            }
+            return nexusIndexer.addIndexingContext( contextKey, remoteRepository.getId(), repoDir, indexDirectory,
+                                                    remoteRepository.getUrl(),
+                                                    calculateIndexRemoteUrl( remoteRepository ),
+                                                    mavenIndexerUtils.getAllIndexCreators() );
+        }
+        catch ( MalformedURLException e )
+        {
+            throw new RepositoryAdminException( e.getMessage(), e );
+        }
+        catch ( IOException e )
+        {
+            throw new RepositoryAdminException( e.getMessage(), e );
+        }
+        catch ( PlexusSisuBridgeException e )
+        {
+            throw new RepositoryAdminException( e.getMessage(), e );
+        }
+        catch ( UnsupportedExistingLuceneIndexException e )
+        {
+            throw new RepositoryAdminException( e.getMessage(), e );
+        }
+
+    }
+
+    protected String calculateIndexRemoteUrl( RemoteRepository remoteRepository )
+    {
+        if ( StringUtils.startsWith( remoteRepository.getRemoteIndexUrl(), "http" ) )
+        {
+            String baseUrl = remoteRepository.getRemoteIndexUrl();
+            return baseUrl.endsWith( "/" ) ? StringUtils.substringBeforeLast( baseUrl, "/" ) : baseUrl;
+        }
+        String baseUrl = StringUtils.endsWith( remoteRepository.getUrl(), "/" ) ? StringUtils.substringBeforeLast(
+            remoteRepository.getUrl(), "/" ) : remoteRepository.getUrl();
+
+        baseUrl = StringUtils.isEmpty( remoteRepository.getRemoteIndexUrl() )
+            ? baseUrl + "/.index"
+            : baseUrl + "/" + remoteRepository.getRemoteIndexUrl();
+        return baseUrl;
+
+    }
+
     private RemoteRepositoryConfiguration getRemoteRepositoryConfiguration( RemoteRepository remoteRepository )
     {
         RemoteRepositoryConfiguration remoteRepositoryConfiguration = new RemoteRepositoryConfiguration();