]> source.dussan.org Git - archiva.git/blob
2ebaf41b514977e8ad9bde009c558eee8efd89c6
[archiva.git] /
1 package org.apache.maven.repository.indexing;
2
3 /*
4  * Copyright 2001-2005 The Apache Software Foundation.
5  *
6  * Licensed under the Apache License, Version 2.0 (the "License");
7  * you may not use this file except in compliance with the License.
8  * You may obtain a copy of the License at
9  *
10  *      http://www.apache.org/licenses/LICENSE-2.0
11  
12  *
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  */
19
20 import java.io.File;
21 import java.io.IOException;
22 import java.util.Collection;
23
24 import org.apache.lucene.analysis.Analyzer;
25 import org.apache.lucene.analysis.SimpleAnalyzer;
26 import org.apache.lucene.index.IndexReader;
27 import org.apache.lucene.index.IndexWriter;
28
29 /**
30  * Abstract class for RepositoryIndexers
31  *
32  * @author Edwin Punzalan
33  */
34 public abstract class AbstractRepositoryIndexer
35     implements RepositoryIndexer
36 {
37     protected String indexPath;
38     protected boolean indexOpen;
39     protected IndexReader indexReader;
40     protected IndexWriter indexWriter;
41     
42     /**
43      * method to encapsulate the optimize() method for lucene
44      */
45     public void optimize()
46         throws RepositoryIndexerException
47     {
48         if ( !isOpen() )
49         {
50             throw new RepositoryIndexerException( "Unable to optimize index on a closed index" );
51         }
52
53         try
54         {
55             indexWriter.optimize();
56         }
57         catch ( IOException ioe )
58         {
59             throw new RepositoryIndexerException( "Failed to optimize index", ioe );
60         }
61     }
62
63     /**
64      * method used to query the index status
65      *
66      * @param true if the index is open.
67      */
68     public boolean isOpen()
69     {
70         return indexOpen;
71     }
72     
73     /**
74      * method used to close all open streams to the index directory
75      */
76     public void close() 
77         throws RepositoryIndexerException
78     {
79         try
80         {
81             if ( indexWriter != null )
82             {
83                 indexWriter.close();
84                 indexWriter = null;
85             }
86
87             if ( indexReader != null )
88             {
89                 indexReader.close();
90                 indexReader = null;
91             }
92
93             indexOpen = false;
94         }
95         catch ( Exception e )
96         {
97             throw new RepositoryIndexerException( e );
98         }
99     }
100
101     /**
102      * method for opening the index directory for indexing operations
103      */
104     public void open()
105         throws RepositoryIndexerException
106     {
107         try
108         {
109             validateIndex();
110         }
111         catch ( Exception e )
112         {
113             throw new RepositoryIndexerException( e );
114         }
115     }
116
117     protected void getIndexWriter()
118         throws IOException
119     {
120         if ( indexWriter == null )
121         {
122             indexWriter = new IndexWriter( indexPath, getAnalyzer(), false );
123         }
124     }
125
126     protected void getIndexReader()
127         throws IOException
128     {
129         if ( indexReader == null )
130         {
131             indexReader = IndexReader.open( indexPath );
132         }
133     }
134     
135     protected Analyzer getAnalyzer()
136     {
137         return new ArtifactRepositoryIndexAnalyzer( new SimpleAnalyzer() );
138     }
139
140     /**
141      * method for validating an index directory
142      *
143      * @throws RepositoryIndexerException if the given indexPath is not valid for this type of RepositoryIndexer
144      */
145     protected void validateIndex()
146         throws RepositoryIndexerException
147     {
148         File indexDir = new File( indexPath );
149         if ( indexDir.exists() )
150         {
151             if ( indexDir.isDirectory() )
152             {
153                 if ( indexDir.listFiles().length > 1 )
154                 {
155                     try
156                     {
157                         getIndexReader();
158                         Collection fields = indexReader.getFieldNames();
159                         String[] indexFields = getIndexFields();
160                         for( int idx=0; idx<indexFields.length; idx++ )
161                         {
162                             if ( !fields.contains( indexFields[ idx ] ) )
163                             {
164                                 throw new RepositoryIndexerException( "The Field " + indexFields[ idx ] + " does not exist in " +
165                                         "index path " + indexPath + "." );
166                             }
167                         }
168                     }
169                     catch ( IOException e )
170                     {
171                         throw new RepositoryIndexerException( e );
172                     }
173                 }
174                 else
175                 {
176                     System.out.println( "Skipping validation of an empty index in: " + indexDir.getAbsolutePath() );
177                 }
178             }
179             else
180             {
181                 throw new RepositoryIndexerException( "Specified index path is not a directory: " + 
182                                                       indexDir.getAbsolutePath() );
183             }
184         }
185         else
186         {
187             try
188             {
189                 indexWriter = new IndexWriter( indexPath, getAnalyzer(), true );
190                 System.out.println( "New index directory created in: " + indexDir.getAbsolutePath() );
191             }
192             catch( Exception e )
193             {
194                 throw new RepositoryIndexerException( e );
195             }
196         }
197
198         indexOpen = true;
199     }
200 }