]> source.dussan.org Git - archiva.git/commitdiff
[MRM-749]
authorMaria Odea B. Ching <oching@apache.org>
Thu, 22 Jan 2009 09:23:01 +0000 (09:23 +0000)
committerMaria Odea B. Ching <oching@apache.org>
Thu, 22 Jan 2009 09:23:01 +0000 (09:23 +0000)
o re-packaged NexusIndexerConsumer and index discovered artifact using Nexus' IndexerEngine
o added test for NexusIndexerConsumer

git-svn-id: https://svn.apache.org/repos/asf/archiva/branches/archiva-nexus-indexer@736595 13f79535-47bb-0310-9956-ffa450edef68

archiva-modules/archiva-base/archiva-consumers/archiva-lucene-consumers/src/main/java/org/apache/archiva/consumers/lucene/NexusIndexerConsumer.java [new file with mode: 0644]
archiva-modules/archiva-base/archiva-consumers/archiva-lucene-consumers/src/main/java/org/apache/maven/archiva/consumers/lucene/NexusIndexerConsumer.java [deleted file]
archiva-modules/archiva-base/archiva-consumers/archiva-lucene-consumers/src/main/resources/META-INF/spring-context.xml
archiva-modules/archiva-base/archiva-consumers/archiva-lucene-consumers/src/test/java/org/apache/archiva/consumers/lucene/NexusIndexerConsumerTest.java [new file with mode: 0644]

diff --git a/archiva-modules/archiva-base/archiva-consumers/archiva-lucene-consumers/src/main/java/org/apache/archiva/consumers/lucene/NexusIndexerConsumer.java b/archiva-modules/archiva-base/archiva-consumers/archiva-lucene-consumers/src/main/java/org/apache/archiva/consumers/lucene/NexusIndexerConsumer.java
new file mode 100644 (file)
index 0000000..ce8f6b1
--- /dev/null
@@ -0,0 +1,168 @@
+package org.apache.archiva.consumers.lucene;
+
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *  http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+import java.io.File;
+import java.io.IOException;
+import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.Date;
+import java.util.List;
+import org.apache.maven.archiva.configuration.ManagedRepositoryConfiguration;
+import org.apache.maven.archiva.consumers.AbstractMonitoredConsumer;
+import org.apache.maven.archiva.consumers.ConsumerException;
+import org.apache.maven.archiva.consumers.KnownRepositoryContentConsumer;
+import org.apache.maven.archiva.repository.content.ManagedDefaultRepositoryContent;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+import org.sonatype.nexus.index.ArtifactContext;
+import org.sonatype.nexus.index.ArtifactContextProducer;
+import org.sonatype.nexus.index.DefaultArtifactContextProducer;
+import org.sonatype.nexus.index.NexusIndexer;
+import org.sonatype.nexus.index.context.IndexingContext;
+import org.sonatype.nexus.index.context.UnsupportedExistingLuceneIndexException;
+import org.sonatype.nexus.index.creator.IndexerEngine;
+import org.sonatype.nexus.index.packer.IndexPacker;
+
+/**
+ * Consumer for indexing the repository to provide search and IDE integration features.
+ */
+public class NexusIndexerConsumer
+    extends AbstractMonitoredConsumer
+    implements KnownRepositoryContentConsumer
+{
+    private static final Logger log = LoggerFactory.getLogger( NexusIndexerConsumer.class );
+
+    private final NexusIndexer indexer;
+
+    private final ArtifactContextProducer artifactContextProducer;
+
+    private final IndexPacker indexPacker;
+
+    private ManagedRepositoryConfiguration repository;
+
+    private ManagedDefaultRepositoryContent repositoryContent;
+
+    private IndexingContext context;
+
+    private File managedRepository;
+    
+    private IndexerEngine indexerEngine;
+
+    public NexusIndexerConsumer( NexusIndexer indexer, IndexPacker indexPacker, IndexerEngine indexerEngine )
+    {
+        this.indexer = indexer;
+        this.indexPacker = indexPacker;
+        this.indexerEngine = indexerEngine;
+        this.artifactContextProducer = new DefaultArtifactContextProducer();
+    }
+
+    public String getDescription()
+    {
+        return "Indexes the repository to provide search and IDE integration features";
+    }
+
+    public String getId()
+    {
+        return "index-content";
+    }
+
+    public boolean isPermanent()
+    {
+        return false;
+    }
+
+    public void beginScan( ManagedRepositoryConfiguration repository, Date whenGathered )
+        throws ConsumerException
+    {
+        this.repository = repository;
+        managedRepository = new File( repository.getLocation() );
+        File indexDirectory = new File( managedRepository, ".indexer" );
+
+        repositoryContent = new ManagedDefaultRepositoryContent();
+        repositoryContent.setRepository( repository );
+
+        synchronized ( indexer )
+        {
+            try
+            {
+                context =
+                    indexer.addIndexingContext( repository.getId(), repository.getId(), managedRepository,
+                                                indexDirectory, null, null, NexusIndexer.FULL_INDEX );
+                context.setSearchable( repository.isScanned() );
+                
+                indexerEngine.beginIndexing( context );
+            }
+            catch ( UnsupportedExistingLuceneIndexException e )
+            {
+                log.error( "Could not create index at " + indexDirectory.getAbsoluteFile(), e );
+            }
+            catch ( IOException e )
+            {
+                log.error( "Could not create index at " + indexDirectory.getAbsoluteFile(), e );
+            }
+        }
+    }
+    
+    public void processFile( String path )
+        throws ConsumerException
+    {
+        File artifactFile = new File( managedRepository, path );
+    
+        ArtifactContext artifactContext = artifactContextProducer.getArtifactContext( context, artifactFile );
+        if ( artifactContext != null )
+        {
+            try
+            {
+                indexer.artifactDiscovered( artifactContext, context );
+             
+                indexerEngine.index( context, artifactContext );
+            }
+            catch ( IOException e )
+            {
+                throw new ConsumerException( e.getMessage(), e );
+            }
+        }
+    }
+
+    public void completeScan()
+    {
+        final File indexLocation = new File( managedRepository, ".index" );
+        try
+        {
+            indexPacker.packIndex( context, indexLocation );
+            indexerEngine.endIndexing( context );
+        }
+        catch ( IOException e )
+        {
+            log.error( "Could not pack index" + indexLocation.getAbsolutePath(), e );
+        }
+    }
+
+    public List<String> getExcludes()
+    {
+        return new ArrayList<String>();
+    }
+
+    public List<String> getIncludes()
+    {
+        return Arrays.asList( "**/*" );
+    }
+}
diff --git a/archiva-modules/archiva-base/archiva-consumers/archiva-lucene-consumers/src/main/java/org/apache/maven/archiva/consumers/lucene/NexusIndexerConsumer.java b/archiva-modules/archiva-base/archiva-consumers/archiva-lucene-consumers/src/main/java/org/apache/maven/archiva/consumers/lucene/NexusIndexerConsumer.java
deleted file mode 100644 (file)
index 6664ee4..0000000
+++ /dev/null
@@ -1,155 +0,0 @@
-package org.apache.maven.archiva.consumers.lucene;
-
-/*
- * Licensed to the Apache Software Foundation (ASF) under one
- * or more contributor license agreements.  See the NOTICE file
- * distributed with this work for additional information
- * regarding copyright ownership.  The ASF licenses this file
- * to you under the Apache License, Version 2.0 (the
- * "License"); you may not use this file except in compliance
- * with the License.  You may obtain a copy of the License at
- *
- *  http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing,
- * software distributed under the License is distributed on an
- * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
- * KIND, either express or implied.  See the License for the
- * specific language governing permissions and limitations
- * under the License.
- */
-
-import java.io.File;
-import java.io.IOException;
-import java.util.ArrayList;
-import java.util.Arrays;
-import java.util.Date;
-import java.util.List;
-import org.apache.maven.archiva.configuration.ManagedRepositoryConfiguration;
-import org.apache.maven.archiva.consumers.AbstractMonitoredConsumer;
-import org.apache.maven.archiva.consumers.ConsumerException;
-import org.apache.maven.archiva.consumers.KnownRepositoryContentConsumer;
-import org.apache.maven.archiva.repository.content.ManagedDefaultRepositoryContent;
-import org.apache.maven.archiva.repository.layout.LayoutException;
-import org.slf4j.Logger;
-import org.slf4j.LoggerFactory;
-import org.sonatype.nexus.index.ArtifactContext;
-import org.sonatype.nexus.index.ArtifactContextProducer;
-import org.sonatype.nexus.index.DefaultArtifactContextProducer;
-import org.sonatype.nexus.index.NexusIndexer;
-import org.sonatype.nexus.index.context.IndexingContext;
-import org.sonatype.nexus.index.context.UnsupportedExistingLuceneIndexException;
-import org.sonatype.nexus.index.packer.IndexPacker;
-
-public class NexusIndexerConsumer 
-    extends AbstractMonitoredConsumer
-    implements KnownRepositoryContentConsumer
-{
-    private static final Logger log = LoggerFactory.getLogger(NexusIndexerConsumer.class);
-
-    private final NexusIndexer indexer;
-
-    private final ArtifactContextProducer artifactContextProducer;
-
-    private final IndexPacker indexPacker;
-
-    private ManagedRepositoryConfiguration repository;
-
-    private ManagedDefaultRepositoryContent repositoryContent;
-
-    private IndexingContext context;
-
-    private File managedRepository;
-
-    public NexusIndexerConsumer(NexusIndexer indexer, IndexPacker indexPacker)
-    {
-        this.indexer = indexer;
-        this.indexPacker = indexPacker;
-        this.artifactContextProducer = new DefaultArtifactContextProducer();
-    }
-
-    public String getDescription()
-    {
-        return "Indexes the repository to provide search and IDE integration features";
-    }
-
-    public String getId()
-    {
-        return "index-content";
-    }
-
-    public boolean isPermanent()
-    {
-        return false;
-    }
-
-    public void beginScan(ManagedRepositoryConfiguration repository, Date whenGathered)
-        throws ConsumerException
-    {
-        this.repository = repository;
-        managedRepository = new File(repository.getLocation());
-        File indexDirectory = new File(managedRepository, ".indexer");
-
-        repositoryContent = new ManagedDefaultRepositoryContent();
-        repositoryContent.setRepository(repository);
-
-        synchronized (indexer)
-        {
-            try
-            {
-                context = indexer.addIndexingContext(repository.getId(), repository.getId(), managedRepository, indexDirectory, null, null, NexusIndexer.FULL_INDEX);
-                context.setSearchable(repository.isScanned());
-            }
-            catch (UnsupportedExistingLuceneIndexException e)
-            {
-                log.error("Could not create index at " + indexDirectory.getAbsoluteFile(), e);
-            }
-            catch (IOException e)
-            {
-                log.error("Could not create index at " + indexDirectory.getAbsoluteFile(), e);
-            }
-        }
-    }
-
-    public void completeScan()
-    {
-        final File indexLocation = new File(managedRepository, ".index");
-        try
-        {
-            indexPacker.packIndex(context, indexLocation);
-        }
-        catch (IOException e)
-        {
-            log.error("Could not pack index" + indexLocation.getAbsolutePath(), e );
-        }
-    }
-
-    public List<String> getExcludes()
-    {
-        return new ArrayList<String>();
-    }
-
-    public List<String> getIncludes()
-    {
-        return Arrays.asList("**/*");
-    }
-
-    public void processFile(String path) 
-        throws ConsumerException
-    {
-        File artifactFile = new File(managedRepository, path);
-
-        ArtifactContext artifactContext = artifactContextProducer.getArtifactContext(context, artifactFile);
-        if (artifactContext != null)
-        {
-            try
-            {
-                indexer.artifactDiscovered(artifactContext, context);
-            }
-            catch (IOException e)
-            {
-                throw new ConsumerException(e.getMessage(), e);
-            }
-        }
-    }
-}
index 66905fa0437a6fdc080cf35031ed5e11b3cf0631..ff7dac5e24f78956ca9626d179725f24f81c7234 100644 (file)
@@ -3,8 +3,9 @@
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd">
 
-    <bean id="indexerConsumer" class="org.apache.maven.archiva.consumers.lucene.NexusIndexerConsumer">
+    <bean id="indexerConsumer" class="org.apache.archiva.consumers.lucene.NexusIndexerConsumer">
         <constructor-arg ref="nexusIndexer"/>
         <constructor-arg ref="indexPacker"/>
+        <constructor-arg ref="indexerEngine"/>
     </bean>
 </beans>
\ No newline at end of file
diff --git a/archiva-modules/archiva-base/archiva-consumers/archiva-lucene-consumers/src/test/java/org/apache/archiva/consumers/lucene/NexusIndexerConsumerTest.java b/archiva-modules/archiva-base/archiva-consumers/archiva-lucene-consumers/src/test/java/org/apache/archiva/consumers/lucene/NexusIndexerConsumerTest.java
new file mode 100644 (file)
index 0000000..4adb402
--- /dev/null
@@ -0,0 +1,140 @@
+package org.apache.archiva.consumers.lucene;
+
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *  http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+import java.io.File;
+import java.util.Calendar;
+import java.util.Date;
+import java.util.Set;
+
+import org.apache.commons.io.FileUtils;
+import org.apache.lucene.search.BooleanQuery;
+import org.apache.lucene.search.BooleanClause.Occur;
+import org.apache.maven.archiva.configuration.ManagedRepositoryConfiguration;
+import org.apache.maven.archiva.consumers.KnownRepositoryContentConsumer;
+import org.codehaus.plexus.spring.PlexusInSpringTestCase;
+import org.sonatype.nexus.index.ArtifactInfo;
+import org.sonatype.nexus.index.FlatSearchRequest;
+import org.sonatype.nexus.index.FlatSearchResponse;
+import org.sonatype.nexus.index.NexusIndexer;
+import org.sonatype.nexus.index.creator.IndexerEngine;
+import org.sonatype.nexus.index.packer.IndexPacker;
+
+public class NexusIndexerConsumerTest
+    extends PlexusInSpringTestCase
+{
+    private KnownRepositoryContentConsumer nexusIndexerConsumer;
+        
+    private ManagedRepositoryConfiguration repositoryConfig;
+
+    private NexusIndexer nexusIndexer;
+
+    private IndexPacker indexPacker;
+
+    private IndexerEngine indexerEngine;
+    
+    @Override
+    protected void setUp() 
+        throws Exception
+    {
+        super.setUp();
+        
+        nexusIndexer = ( NexusIndexer ) lookup( NexusIndexer.class );
+        
+        indexPacker = ( IndexPacker ) lookup( IndexPacker.class );
+        
+        indexerEngine = ( IndexerEngine ) lookup( IndexerEngine.class );
+        
+        nexusIndexerConsumer = new NexusIndexerConsumer( nexusIndexer, indexPacker, indexerEngine );
+                
+        repositoryConfig = new ManagedRepositoryConfiguration();
+        repositoryConfig.setId( "test-repo" );
+        repositoryConfig.setLocation( getBasedir() + "/target/test-classes/test-repo" );
+        repositoryConfig.setLayout( "default" );
+        repositoryConfig.setName( "Test Repository" );
+        repositoryConfig.setScanned( true );
+        repositoryConfig.setSnapshots( false );
+        repositoryConfig.setReleases( true );
+    }
+    
+    @Override
+    protected void tearDown()
+        throws Exception
+    {
+        // delete created index in the repository
+        File indexDir = new File( repositoryConfig.getLocation(), ".indexer" );
+        FileUtils.deleteDirectory( indexDir );
+        assertFalse( indexDir.exists() );
+        
+        indexDir = new File( repositoryConfig.getLocation(), ".index" );
+        FileUtils.deleteDirectory( indexDir );
+        assertFalse( indexDir.exists() );
+        
+        super.tearDown();
+    }
+    
+    public void testIndexerIndexArtifact()
+        throws Exception
+    {        
+        // begin scan
+        Date now = Calendar.getInstance().getTime();
+        nexusIndexerConsumer.beginScan( repositoryConfig, now );
+        
+        // process file
+        nexusIndexerConsumer.processFile( "org/apache/archiva/archiva-index-methods-jar-test/1.0/archiva-index-methods-jar-test-1.0.jar" );
+        
+        // end scan
+        nexusIndexerConsumer.completeScan();
+        
+        // search!
+        BooleanQuery q = new BooleanQuery();        
+        q.add( nexusIndexer.constructQuery( ArtifactInfo.GROUP_ID, "org.apache.archiva" ), Occur.SHOULD );
+        q.add( nexusIndexer.constructQuery( ArtifactInfo.ARTIFACT_ID, "archiva-index-methods-jar-test" ), Occur.SHOULD );
+        
+        FlatSearchRequest request = new FlatSearchRequest( q );
+        FlatSearchResponse response = nexusIndexer.searchFlat( request );
+        
+        assertTrue( new File( repositoryConfig.getLocation(), ".indexer" ).exists() );
+        assertTrue( new File( repositoryConfig.getLocation(), ".index" ).exists() );
+        assertEquals( 1, response.getTotalHits() );
+        
+        Set<ArtifactInfo> results = response.getResults();
+        
+        ArtifactInfo artifactInfo = (ArtifactInfo) results.iterator().next();
+        assertEquals( "org.apache.archiva", artifactInfo.groupId );
+        assertEquals( "archiva-index-methods-jar-test", artifactInfo.artifactId );
+        assertEquals( "test-repo", artifactInfo.repository );        
+    }
+    
+    /*public void testIndexerIndexPom()
+        throws Exception
+    {        
+        // begin scan
+        Date now = Calendar.getInstance().getTime();
+        nexusIndexerConsumer.beginScan( repositoryConfig, now );
+        
+        // process file
+        //nexusIndexerConsumer.processFile(  )
+        
+        // end scan
+        
+        // search!
+    }*/
+}