KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > alfresco > repo > node > index > FtsIndexRecoveryComponent


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.node.index;
18
19 import java.util.ArrayList JavaDoc;
20 import java.util.List JavaDoc;
21
22 import org.alfresco.repo.search.impl.lucene.fts.FullTextSearchIndexer;
23 import org.alfresco.repo.transaction.TransactionUtil;
24 import org.alfresco.repo.transaction.TransactionUtil.TransactionWork;
25 import org.alfresco.service.cmr.repository.NodeService;
26 import org.alfresco.service.cmr.repository.StoreRef;
27 import org.alfresco.service.cmr.search.SearchService;
28 import org.alfresco.service.transaction.TransactionService;
29 import org.apache.commons.logging.Log;
30 import org.apache.commons.logging.LogFactory;
31
32 /**
33  * Ensures that the FTS indexing picks up on any outstanding documents that
34  * require indexing.
35  * <p>
36  * FTS indexing is a background process. It is therefore possible that
37  * certain documents don't get indexed when the server shuts down.
38  *
39  * @author Derek Hulley
40  */

41 public class FtsIndexRecoveryComponent implements IndexRecovery
42 {
43     private static Log logger = LogFactory.getLog(FtsIndexRecoveryComponent.class);
44     
45     /** provides transactions to atomically index each missed transaction */
46     private TransactionService transactionService;
47     /** the FTS indexer that we will prompt to pick up on any un-indexed text */
48     private FullTextSearchIndexer ftsIndexer;
49     /** the component providing searches of the indexed nodes */
50     private SearchService searcher;
51     /** the component giving direct access to <b>node</b> instances */
52     private NodeService nodeService;
53     /** the workspaces to reindex */
54     private List JavaDoc<StoreRef> storeRefs;
55     
56     public FtsIndexRecoveryComponent()
57     {
58         this.storeRefs = new ArrayList JavaDoc<StoreRef>(2);
59     }
60     
61     /**
62      * @param transactionService provide transactions to index each missed transaction
63      */

64     public void setTransactionService(TransactionService transactionService)
65     {
66         this.transactionService = transactionService;
67     }
68
69     /**
70      * @param ftsIndexer the FTS background indexer
71      */

72     public void setFtsIndexer(FullTextSearchIndexer ftsIndexer)
73     {
74         this.ftsIndexer = ftsIndexer;
75     }
76
77     /**
78      * @param nodeService provides information about nodes for indexing
79      */

80     public void setNodeService(NodeService nodeService)
81     {
82         this.nodeService = nodeService;
83     }
84     
85     /**
86      * Set the workspaces that need reindexing
87      *
88      * @param storeRefStrings a list of strings representing store references
89      */

90     public void setStores(List JavaDoc<String JavaDoc> storeRefStrings)
91     {
92         storeRefs.clear();
93         for (String JavaDoc storeRefStr : storeRefStrings)
94         {
95             StoreRef storeRef = new StoreRef(storeRefStr);
96             storeRefs.add(storeRef);
97         }
98     }
99     
100     /**
101      * Ensures that the FTS indexing is activated for any outstanding full text searches.
102      */

103     public void reindex()
104     {
105         TransactionWork<Object JavaDoc> reindexWork = new TransactionWork<Object JavaDoc>()
106         {
107             public Object JavaDoc doWork()
108             {
109                 // reindex each store
110
for (StoreRef storeRef : storeRefs)
111                 {
112                     // check if the store exists
113
if (!nodeService.exists(storeRef))
114                     {
115                         // store does not exist
116
if (logger.isDebugEnabled())
117                         {
118                             logger.debug("Skipping reindex of non-existent store: " + storeRef);
119                         }
120                         continue;
121                     }
122                     
123                     // prompt FTS to reindex the store
124
ftsIndexer.requiresIndex(storeRef);
125                 }
126                 // done
127
return null;
128             }
129         };
130         TransactionUtil.executeInUserTransaction(transactionService, reindexWork);
131         // done
132
if (logger.isDebugEnabled())
133         {
134             logger.debug("Prompted FTS index on stores: " + storeRefs);
135         }
136     }
137 }
Popular Tags