]> source.dussan.org Git - archiva.git/commitdiff
PR: MRM-39
authorEdwin L. Punzalan <epunzalan@apache.org>
Fri, 23 Dec 2005 07:06:14 +0000 (07:06 +0000)
committerEdwin L. Punzalan <epunzalan@apache.org>
Fri, 23 Dec 2005 07:06:14 +0000 (07:06 +0000)
Submitted by: Maria Odea Ching

ArtifactIndexSearcher classes.  Also, added more unit test coverage

git-svn-id: https://svn.apache.org/repos/asf/maven/repository-manager/trunk@358756 13f79535-47bb-0310-9956-ffa450edef68

maven-repository-indexer/src/main/java/org/apache/maven/repository/indexing/AbstractRepositoryIndexer.java
maven-repository-indexer/src/main/java/org/apache/maven/repository/indexing/ArtifactRepositoryIndexAnalyzer.java
maven-repository-indexer/src/main/java/org/apache/maven/repository/indexing/ArtifactRepositoryIndexer.java
maven-repository-indexer/src/main/java/org/apache/maven/repository/indexing/ArtifactRepositorySearcher.java [new file with mode: 0644]
maven-repository-indexer/src/main/java/org/apache/maven/repository/indexing/RepositorySearcher.java [new file with mode: 0644]
maven-repository-indexer/src/test/java/org/apache/maven/repository/indexing/ArtifactRepositoryIndexingTest.java

index d71a5040d90841ebd9ce1e7f9d983ff3bd955543..d71ab2232256c3454af8f7f078934e781ac5b1a3 100644 (file)
@@ -64,28 +64,25 @@ public abstract class AbstractRepositoryIndexer
     public void close() 
         throws RepositoryIndexerException
     {
-        if ( indexOpen )
+        try
         {
-            try
+            if ( indexWriter != null )
             {
-                if ( indexWriter != null )
-                {
-                    indexWriter.close();
-                    indexWriter = null;
-                }
-
-                if ( indexReader != null )
-                {
-                    indexReader.close();
-                    indexReader = null;
-                }
-                
-                indexOpen = false;
+                indexWriter.close();
+                indexWriter = null;
             }
-            catch ( Exception e )
+
+            if ( indexReader != null )
             {
-                throw new RepositoryIndexerException( e );
+                indexReader.close();
+                indexReader = null;
             }
+
+            indexOpen = false;
+        }
+        catch ( Exception e )
+        {
+            throw new RepositoryIndexerException( e );
         }
     }
 
@@ -94,10 +91,7 @@ public abstract class AbstractRepositoryIndexer
     {
         try
         {
-            if ( !indexOpen )
-            {
-                validateIndex();
-            }
+            validateIndex();
         }
         catch ( Exception e )
         {
@@ -123,7 +117,7 @@ public abstract class AbstractRepositoryIndexer
             indexReader = IndexReader.open( indexPath );
         }
     }
-
+    
     protected Analyzer getAnalyzer()
     {
         return new ArtifactRepositoryIndexAnalyzer( new SimpleAnalyzer() );
index 1be1c172167ca152c1c87296b52d008f7685ba11..48196b71d0ef756e8f8e948c4e9088c3636a09b3 100644 (file)
@@ -18,6 +18,8 @@ package org.apache.maven.repository.indexing;
  */
 
 import java.io.Reader;
+import java.util.HashMap;
+import java.util.Map;
 import org.apache.lucene.analysis.Analyzer;
 import org.apache.lucene.analysis.CharTokenizer;
 import org.apache.lucene.analysis.TokenStream;
@@ -51,7 +53,7 @@ public class ArtifactRepositoryIndexAnalyzer
 
         return tokenStream;
     }
-
+    
     private class VersionTokenizer
         extends CharTokenizer
     {
@@ -60,14 +62,15 @@ public class ArtifactRepositoryIndexAnalyzer
             super( reader );
         }
 
-        protected boolean isTokenChar(char param)
+        protected boolean isTokenChar( char param )
         {
             boolean token;
 
             switch( param )
             {
-                case '.': token = false; break;
-                case '-': token = false; break;
+                case '.': 
+                case '-': 
+                    token = false; break;
                 default:
                     token = true;
             }
index 82708f8fc44d62165b1ff01158f9d500e5b1c386..9ae03f2ac5cfa60d1906f20b0f794111c82d54f0 100644 (file)
@@ -101,7 +101,6 @@ public class ArtifactRepositoryIndexer
         {
             getIndexWriter();
 
-            initBuffers();
             processArtifactContents( artifact.getFile() );
             
             //@todo should some of these fields be Keyword instead of Text ?
@@ -176,6 +175,7 @@ public class ArtifactRepositoryIndexer
     private void processArtifactContents( File artifact )
         throws IOException, ZipException
     {
+        initBuffers();
         ZipFile jar = new ZipFile( artifact );
         for ( Enumeration entries = jar.entries(); entries.hasMoreElements(); )
         {
diff --git a/maven-repository-indexer/src/main/java/org/apache/maven/repository/indexing/ArtifactRepositorySearcher.java b/maven-repository-indexer/src/main/java/org/apache/maven/repository/indexing/ArtifactRepositorySearcher.java
new file mode 100644 (file)
index 0000000..0ea85de
--- /dev/null
@@ -0,0 +1,178 @@
+package org.apache.maven.repository.indexing;
+
+/*
+ * Copyright 2001-2005 The Apache Software Foundation.
+ *
+ * Licensed 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.List;
+
+import org.apache.lucene.analysis.Analyzer;
+import org.apache.lucene.analysis.PerFieldAnalyzerWrapper;
+import org.apache.lucene.analysis.SimpleAnalyzer;
+import org.apache.lucene.analysis.StopAnalyzer;
+import org.apache.lucene.analysis.standard.StandardAnalyzer;
+import org.apache.lucene.document.Document;
+import org.apache.lucene.queryParser.ParseException;
+import org.apache.lucene.queryParser.QueryParser;
+import org.apache.lucene.search.Hits;
+import org.apache.lucene.search.IndexSearcher;
+import org.apache.lucene.search.Query;
+import org.apache.maven.artifact.Artifact;
+import org.apache.maven.artifact.DefaultArtifact;
+import org.apache.maven.artifact.factory.ArtifactFactory;
+import org.apache.maven.artifact.factory.DefaultArtifactFactory;
+import org.apache.maven.artifact.handler.ArtifactHandler;
+import org.apache.maven.artifact.handler.DefaultArtifactHandler;
+import org.apache.maven.artifact.repository.ArtifactRepository;
+import org.apache.maven.artifact.versioning.VersionRange;
+
+/**
+ * This class searches the index for existing artifacts that contains the
+ * specified query string.
+ * 
+ * @author Maria Odea Ching
+ */
+public class ArtifactRepositorySearcher implements RepositorySearcher {
+
+       private IndexSearcher searcher;
+       private ArtifactRepository repository;
+       private ArtifactFactory factory;
+       private static final String NAME = "name";
+       private static final String GROUPID = "groupId";
+       private static final String ARTIFACTID = "artifactId";
+       private static final String VERSION = "version";
+       private static final String JAR_TYPE = "jar";
+       private static final String XML_TYPE = "xml";
+       private static final String POM_TYPE = "pom";
+       
+       /**
+        * Constructor
+        * 
+        * @param indexPath
+        * @param repository
+        */
+       public ArtifactRepositorySearcher(String indexPath,
+                       ArtifactRepository repository) {
+
+               this.repository = repository;
+               factory = new DefaultArtifactFactory();
+
+               try {
+                       searcher = new IndexSearcher(indexPath);
+               } catch (IOException ie) {
+                       ie.printStackTrace();
+               }
+       }
+       
+       protected Analyzer getAnalyzer()
+    {
+        //PerFieldAnalyzerWrapper wrapper = new PerFieldAnalyzerWrapper(new SimpleAnalyzer());
+        //wrapper.addAnalyzer(VERSION, new StandardAnalyzer());
+        
+               //return wrapper;
+        return new ArtifactRepositoryIndexAnalyzer(new SimpleAnalyzer());
+    }
+
+       /**
+        * Search the artifact that contains the query string in the specified
+        * search field.
+        * 
+        * @param queryString
+        * @param searchField
+        * @return
+        */
+       public List searchArtifact(String queryString, String searchField) {
+
+               
+               QueryParser parser = new QueryParser(searchField,
+                               getAnalyzer());
+               Query qry = null;
+               List artifactList = new ArrayList();
+
+               try {
+                       qry = parser.parse(queryString);
+               } catch (ParseException pe) {
+                       pe.printStackTrace();
+                       return artifactList;
+               }
+
+               try {
+                       Hits hits = searcher.search(qry);
+                        //System.out.println("HITS SIZE --> " + hits.length());
+
+                       for (int i = 0; i < hits.length(); i++) {
+                               Document doc = hits.doc(i);
+                               // System.out.println("===========================");
+                               // System.out.println("NAME :: " + (String) doc.get(NAME));
+                               // System.out.println("GROUP ID :: " + (String)
+                               // doc.get(GROUPID));
+                               // System.out.println("ARTIFACT ID :: " + (String)
+                               // doc.get(ARTIFACTID));
+                               //System.out.println("VERSION :: " + (String)
+                               // doc.get(VERSION));
+                               // System.out.println("SHA! :: " + (String) doc.get(SHA1));
+                               // System.out.println("MD5 :: " + (String) doc.get(MD5));
+                               // System.out.println("CLASSES :: " + (String)
+                               // doc.get(CLASSES));
+                               // System.out.println("PACKAGES :: " + (String)
+                               // doc.get(PACKAGES));
+                               // System.out.println("FILES :: " + (String) doc.get(FILES));
+                               // System.out.println("===========================");
+
+                               String name = (String) doc.get(NAME);
+                               String type = "";
+                               if ((name.substring(name.length() - 3).toLowerCase())
+                                               .equals(JAR_TYPE))
+                                       type = JAR_TYPE;
+                               else if ((name.substring(name.length() - 3).toLowerCase())
+                                               .equals(XML_TYPE)
+                                               || (name.substring(name.length() - 3).toLowerCase())
+                                                               .equals(POM_TYPE))
+                                       type = POM_TYPE;
+
+                               if (!type.equals("") && type != null) {
+                                       ArtifactHandler handler = new DefaultArtifactHandler(type);
+                                       VersionRange version = VersionRange
+                                                       .createFromVersion((String) doc.get(VERSION));
+
+                                       Artifact artifact = new DefaultArtifact((String) doc
+                                                       .get(GROUPID), (String) doc.get(ARTIFACTID),
+                                                       version, "compile", type, "", handler);
+
+                                       /*
+                                        * Artifact artifact = factory.createArtifact((String)
+                                        * doc.get(GROUPID), (String) doc.get(ARTIFACTID), (String)
+                                        * doc.get(VERSION), "", type);
+                                        */
+                                       artifact.setRepository(repository);
+                                       artifact.setFile(new File(repository.getBasedir() + "/"
+                                                       + (String) doc.get(NAME)));
+
+                                       artifactList.add(artifact);
+                               }
+                       }
+
+               } catch (IOException ie) {
+                       ie.printStackTrace();
+                       return artifactList;
+               }
+
+               return artifactList;
+       }
+}
diff --git a/maven-repository-indexer/src/main/java/org/apache/maven/repository/indexing/RepositorySearcher.java b/maven-repository-indexer/src/main/java/org/apache/maven/repository/indexing/RepositorySearcher.java
new file mode 100644 (file)
index 0000000..3d41b37
--- /dev/null
@@ -0,0 +1,39 @@
+package org.apache.maven.repository.indexing;
+
+/*
+ * Copyright 2001-2005 The Apache Software Foundation.
+ *
+ * Licensed 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.util.List;
+
+/**
+ * @author Maria Odea Ching
+ */
+public interface RepositorySearcher {
+
+       String ROLE = RepositoryIndexer.class.getName();
+       
+       /**
+        * Search the artifact that contains the query string in the specified
+        * search field.
+        * 
+        * @param queryString
+        * @param searchField
+        * @return
+        */
+       public List searchArtifact(String queryString, String searchField);
+       
+}
index 1bb2ddc4d8f1e0d41f5810d4d577ee5cbf7624de..88100f3de040e70c6aefe40947832b5295f86bfc 100644 (file)
@@ -18,6 +18,9 @@ package org.apache.maven.repository.indexing;
  */
 
 import java.io.File;
+import java.util.Iterator;
+import java.util.List;
+
 import org.apache.maven.artifact.Artifact;
 import org.apache.maven.artifact.factory.ArtifactFactory;
 import org.apache.maven.artifact.repository.ArtifactRepository;
@@ -29,7 +32,7 @@ import org.codehaus.plexus.PlexusTestCase;
 
 /**
  *
- * @author Edwin Punzalan
+ * @author Edwin Punzalan/Maria Odea Ching
  */
 public class ArtifactRepositoryIndexingTest
     extends PlexusTestCase
@@ -38,6 +41,15 @@ public class ArtifactRepositoryIndexingTest
     protected ArtifactFactory artifactFactory;
     protected ArtifactRepository repository;
     protected String indexPath;
+    private RepositorySearcher repoSearcher;
+    private static final String GROUPID = "groupId";
+    private static final String ARTIFACTID = "artifactId";
+       private static final String VERSION = "version";
+       private static final String SHA1 = "sha1";
+    private static final String MD5 = "md5";
+    private static final String CLASSES = "classes";
+    private static final String PACKAGES = "packages";
+    private static final String FILES = "files";
 
     protected void setUp()
         throws Exception
@@ -55,36 +67,112 @@ public class ArtifactRepositoryIndexingTest
         repository = repoFactory.createArtifactRepository( "test", repoDir, layout, null, null );
         indexer = (ArtifactRepositoryIndexer) factory.getArtifactRepositoryIndexer( indexPath, repository );
         artifactFactory = (ArtifactFactory) lookup( ArtifactFactory.ROLE );
+        
+        repoSearcher = new ArtifactRepositorySearcher(indexPath, repository);
     }
-
+    
     public void testIndex()
         throws Exception
     {
-        Artifact artifact = getArtifact( "test", "test-artifactId", "1.0" );
+        Artifact artifact = getArtifact( "org.apache.maven", "maven-artifact", "2.0.1" );
+        artifact.setFile( new File( repository.getBasedir(), repository.pathOf( artifact ) ) );
+        indexer.addArtifactIndex( artifact );
+        
+        artifact = getArtifact( "org.apache.maven", "maven-model", "2.0" );
         artifact.setFile( new File( repository.getBasedir(), repository.pathOf( artifact ) ) );
         indexer.addArtifactIndex( artifact );
-        //indexer.optimize();
+        
+        indexer.optimize();
+        indexer.close();
+
+        indexer.open();
+        artifact = getArtifact( "test", "test-artifactId", "1.0" );
+        artifact.setFile( new File( repository.getBasedir(), repository.pathOf( artifact ) ) );
+        indexer.addObjectIndex( artifact );
         indexer.close();
     }
+  
+    public void testSearch() throws Exception{
+       
+        //test the search GROUPID
+        List artifacts = repoSearcher.searchArtifact("test", GROUPID);
+        assertEquals( 1, artifacts.size() );
+                        
+        artifacts = repoSearcher.searchArtifact("test", ARTIFACTID);
+        assertEquals( 1, artifacts.size() );
+                
+        artifacts = repoSearcher.searchArtifact("1.0", VERSION);
+        assertEquals( 1, artifacts.size() );
+        
+        artifacts = repoSearcher.searchArtifact("App", CLASSES);
+        assertEquals( 1, artifacts.size() );
+        
+        artifacts = repoSearcher.searchArtifact("groupId", PACKAGES);
+        assertEquals( 1, artifacts.size() );
+        
+        artifacts = repoSearcher.searchArtifact("pom.xml", FILES);
+        assertEquals( 3, artifacts.size() );
+
+        for( Iterator iter = artifacts.iterator(); iter.hasNext(); )
+        {
+               Artifact artifact = (Artifact) iter.next();
+               File f = artifact.getFile();
+            assertNotNull( f );
+               assertTrue( f.exists() );
+        }
+        
+        //search org.apache.maven jars
+        artifacts = repoSearcher.searchArtifact("org.apache.maven", GROUPID);
+        assertEquals( 2, artifacts.size() );
+        
+        artifacts = repoSearcher.searchArtifact("maven-artifact", ARTIFACTID);
+        assertEquals( 1, artifacts.size() );
+                
+        artifacts = repoSearcher.searchArtifact("2", VERSION);
+        assertEquals( 2, artifacts.size() );
+   
+    }
 
     public void testIndexerExceptions()
         throws Exception
     {
-        //test closed index
+        indexer.close();
+        Artifact artifact = getArtifact( "test", "test-artifactId", "1.0" );
+        artifact.setFile( new File( repository.getBasedir(), repository.pathOf( artifact ) ) );
+
         try
         {
-            indexer.close();
-            Artifact artifact = getArtifact( "test", "test-artifactId", "1.0" );
-            artifact.setFile( new File( repository.getBasedir(), repository.pathOf( artifact ) ) );
             indexer.addArtifactIndex( artifact );
-            fail( "Must throw exception on closed index." );
+            fail( "Must throw exception on add index with closed index." );
+        }
+        catch( RepositoryIndexerException e )
+        {
+            //expected
+        }
+        
+        try
+        {
+            indexer.optimize();
+            fail( "Must throw exception on optimize index with closed index." );
+        }
+        catch( RepositoryIndexerException e )
+        {
+            //expected
+        }
+        
+        indexer.open();
+        
+        try
+        {
+            indexer.addObjectIndex( "should fail" );
+            fail( "Must throw exception on add non-Artifact object." );
         }
         catch( RepositoryIndexerException e )
         {
             //expected
         }
     }
-
+    
     protected Artifact getArtifact( String groupId, String artifactId, String version )
     {
         return artifactFactory.createBuildArtifact( groupId, artifactId, version, "jar" );