]> source.dussan.org Git - archiva.git/blob
79ba90b25bc572f9301a9fb7f3b2c2201f155d1c
[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.IOException;
21 import java.util.Collection;
22
23 import org.apache.lucene.analysis.Analyzer;
24 import org.apache.lucene.analysis.standard.StandardAnalyzer;
25 import org.apache.lucene.index.IndexReader;
26 import org.apache.lucene.index.IndexWriter;
27
28 /**
29  *
30  * @author Edwin Punzalan
31  */
32 public abstract class AbstractRepositoryIndexer
33     implements RepositoryIndexer
34 {
35     protected String indexPath;
36     protected boolean indexOpen;
37     protected IndexReader indexReader;
38     protected IndexWriter indexWriter;
39     
40     public void optimize()
41         throws RepositoryIndexerException
42     {
43         if ( !isOpen() )
44         {
45             throw new RepositoryIndexerException( "Unable to optimize index on a closed index" );
46         }
47
48         try
49         {
50             indexWriter.optimize();
51         }
52         catch ( IOException ioe )
53         {
54             throw new RepositoryIndexerException( "Failed to optimize index", ioe );
55         }
56     }
57
58     public boolean isOpen()
59     {
60         return indexOpen;
61     }
62     
63     public void close() 
64         throws RepositoryIndexerException
65     {
66         if ( indexOpen )
67         {
68             try
69             {
70                 if ( indexWriter != null )
71                 {
72                     indexWriter.close();
73                     indexWriter = null;
74                 }
75
76                 if ( indexReader != null )
77                 {
78                     indexReader.close();
79                     indexReader = null;
80                 }
81                 
82                 indexOpen = false;
83             }
84             catch ( Exception e )
85             {
86                 throw new RepositoryIndexerException( e );
87             }
88         }
89     }
90
91     public void open()
92         throws RepositoryIndexerException
93     {
94         try
95         {
96             if ( !indexOpen )
97             {
98                 validateIndex();
99             }
100         }
101         catch ( Exception e )
102         {
103             throw new RepositoryIndexerException( e );
104         }
105     }
106
107
108     protected void getIndexWriter()
109         throws IOException
110     {
111         if ( indexWriter == null )
112         {
113             indexWriter = new IndexWriter( indexPath, getAnalyzer(), true );
114         }
115     }
116
117     protected void getIndexReader()
118         throws IOException
119     {
120         if ( indexReader == null )
121         {
122             indexReader = IndexReader.open( indexPath );
123         }
124     }
125
126     protected Analyzer getAnalyzer()
127     {
128         return new StandardAnalyzer();
129     }
130
131     protected void validateIndex()
132         throws RepositoryIndexerException
133     {
134         indexOpen = true;
135         if ( true ) return;
136         try
137         {
138             getIndexReader();
139             Collection fields = indexReader.getFieldNames();
140             String[] indexFields = getIndexFields();
141             for( int idx=0; idx<indexFields.length; idx++ )
142             {
143                 if ( !fields.contains( indexFields[ idx ] ) )
144                 {
145                     throw new RepositoryIndexerException( "The Field " + indexFields[ idx ] + " does not exist in " +
146                             "index path " + indexPath + "." );
147                 }
148             }
149             indexOpen = true;
150         }
151         catch ( IOException e )
152         {
153             throw new RepositoryIndexerException( e );
154         }
155     }
156 }