KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > alfresco > repo > search > impl > lucene > fts > FullTextSearchIndexerImpl


1 /*
2  * Copyright (C) 2005 Alfresco, Inc.
3  *
4  * Licensed under the Mozilla Public License version 1.1
5  * with a permitted attribution clause. You may obtain a
6  * copy of the License at
7  *
8  * http://www.alfresco.org/legal/license.txt
9  *
10  * Unless required by applicable law or agreed to in writing,
11  * software distributed under the License is distributed on an
12  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
13  * either express or implied. See the License for the specific
14  * language governing permissions and limitations under the
15  * License.
16  */

17 package org.alfresco.repo.search.impl.lucene.fts;
18
19 import java.util.HashSet JavaDoc;
20 import java.util.LinkedHashSet JavaDoc;
21 import java.util.Set JavaDoc;
22
23 import org.alfresco.repo.search.impl.lucene.LuceneIndexer;
24 import org.alfresco.repo.search.impl.lucene.LuceneIndexerAndSearcherFactory;
25 import org.alfresco.service.cmr.repository.StoreRef;
26 import org.springframework.context.ApplicationContext;
27 import org.springframework.context.support.ClassPathXmlApplicationContext;
28
29 public class FullTextSearchIndexerImpl implements FTSIndexerAware, FullTextSearchIndexer
30 {
31     private enum State {
32         ACTIVE, PAUSING, PAUSED
33     };
34
35     private static Set JavaDoc<StoreRef> requiresIndex = new LinkedHashSet JavaDoc<StoreRef>();
36
37     private static Set JavaDoc<StoreRef> indexing = new HashSet JavaDoc<StoreRef>();
38
39     LuceneIndexerAndSearcherFactory luceneIndexerAndSearcherFactory;
40
41     private int pauseCount = 0;
42
43     private boolean paused = false;
44
45     public FullTextSearchIndexerImpl()
46     {
47         super();
48         //System.out.println("Created id is "+this);
49
}
50
51     /*
52      * (non-Javadoc)
53      *
54      * @see org.alfresco.repo.search.impl.lucene.fts.FullTextSearchIndexer#requiresIndex(org.alfresco.repo.ref.StoreRef)
55      */

56     public synchronized void requiresIndex(StoreRef storeRef)
57     {
58         requiresIndex.add(storeRef);
59     }
60
61     /*
62      * (non-Javadoc)
63      *
64      * @see org.alfresco.repo.search.impl.lucene.fts.FullTextSearchIndexer#indexCompleted(org.alfresco.repo.ref.StoreRef,
65      * int, java.lang.Exception)
66      */

67     public synchronized void indexCompleted(StoreRef storeRef, int remaining, Exception JavaDoc e)
68     {
69         try
70         {
71             indexing.remove(storeRef);
72             if ((remaining > 0) || (e != null))
73             {
74                 requiresIndex(storeRef);
75             }
76             if (e != null)
77             {
78                 throw new FTSIndexerException(e);
79             }
80         }
81         finally
82         {
83             //System.out.println("..Index Complete: id is "+this);
84
this.notifyAll();
85         }
86     }
87
88     /*
89      * (non-Javadoc)
90      *
91      * @see org.alfresco.repo.search.impl.lucene.fts.FullTextSearchIndexer#pause()
92      */

93     public synchronized void pause() throws InterruptedException JavaDoc
94     {
95         pauseCount++;
96         //System.out.println("..Waiting "+pauseCount+" id is "+this);
97
while ((indexing.size() > 0))
98         {
99             //System.out.println("Pause: Waiting with count of "+indexing.size()+" id is "+this);
100
this.wait();
101         }
102         pauseCount--;
103         if(pauseCount == 0)
104         {
105             paused = true;
106             this.notifyAll(); // only resumers
107
}
108         //System.out.println("..Remaining "+pauseCount +" paused = "+paused+" id is "+this);
109
}
110
111     /*
112      * (non-Javadoc)
113      *
114      * @see org.alfresco.repo.search.impl.lucene.fts.FullTextSearchIndexer#resume()
115      */

116     public synchronized void resume() throws InterruptedException JavaDoc
117     {
118         if(pauseCount == 0)
119         {
120             //System.out.println("Direct resume"+" id is "+this);
121
paused = false;
122         }
123         else
124         {
125             while(pauseCount > 0)
126             {
127                 //System.out.println("Reusme waiting on "+pauseCount+" id is "+this);
128
this.wait();
129             }
130             paused = false;
131         }
132     }
133
134     private synchronized boolean isPaused() throws InterruptedException JavaDoc
135     {
136         if(pauseCount == 0)
137         {
138            return paused;
139         }
140         else
141         {
142             while(pauseCount > 0)
143             {
144                 this.wait();
145             }
146             return paused;
147         }
148     }
149
150     /*
151      * (non-Javadoc)
152      *
153      * @see org.alfresco.repo.search.impl.lucene.fts.FullTextSearchIndexer#index()
154      */

155     public void index()
156     {
157         // Use the calling thread to index
158
// Parallel indexing via multiple Quartz thread initiating indexing
159

160         StoreRef toIndex = getNextRef();
161         if (toIndex != null)
162         {
163             //System.out.println("Indexing "+toIndex+" id is "+this);
164
LuceneIndexer indexer = luceneIndexerAndSearcherFactory.getIndexer(toIndex);
165             indexer.registerCallBack(this);
166             indexer.updateFullTextSearch(1000);
167         }
168         else
169         {
170             //System.out.println("Nothing to index"+" id is "+this);
171
}
172     }
173
174     private synchronized StoreRef getNextRef()
175     {
176         if (paused || (pauseCount > 0))
177         {
178             //System.out.println("Indexing suspended"+" id is "+this);
179
return null;
180         }
181
182         StoreRef nextStoreRef = null;
183
184         for (StoreRef ref : requiresIndex)
185         {
186             if (!indexing.contains(ref))
187             {
188                 nextStoreRef = ref;
189             }
190         }
191
192         if (nextStoreRef != null)
193         {
194             requiresIndex.remove(nextStoreRef);
195             indexing.add(nextStoreRef);
196         }
197
198         return nextStoreRef;
199     }
200
201     public void setLuceneIndexerAndSearcherFactory(LuceneIndexerAndSearcherFactory luceneIndexerAndSearcherFactory)
202     {
203         this.luceneIndexerAndSearcherFactory = luceneIndexerAndSearcherFactory;
204     }
205
206     public static void main(String JavaDoc[] args) throws InterruptedException JavaDoc
207     {
208         ApplicationContext ctx = new ClassPathXmlApplicationContext("classpath:alfresco/application-context.xml");
209     }
210 }
211
Popular Tags