001: package com.ecyrd.jspwiki.ui.admin.beans;
002:
003: import java.util.Collection;
004: import java.util.Iterator;
005:
006: import javax.management.NotCompliantMBeanException;
007:
008: import org.apache.log4j.Logger;
009:
010: import com.ecyrd.jspwiki.WikiContext;
011: import com.ecyrd.jspwiki.WikiEngine;
012: import com.ecyrd.jspwiki.WikiPage;
013: import com.ecyrd.jspwiki.search.SearchManager;
014: import com.ecyrd.jspwiki.ui.admin.SimpleAdminBean;
015: import com.ecyrd.jspwiki.ui.progress.ProgressItem;
016: import com.ecyrd.jspwiki.util.WikiBackgroundThread;
017:
018: /**
019: * The SearchManagerBean is a simple AdminBean interface
020: * to the SearchManager. It currently can be used to force
021: * a reload of all of the pages.
022: *
023: * @since 2.6
024: */
025: public class SearchManagerBean extends SimpleAdminBean {
026: private static final String PROGRESS_ID = "searchmanagerbean.reindexer";
027:
028: public static final String[] METHODS = { "reload" };
029:
030: private static Logger log = Logger
031: .getLogger(SearchManagerBean.class);
032:
033: private WikiBackgroundThread m_updater;
034:
035: public SearchManagerBean(WikiEngine engine)
036: throws NotCompliantMBeanException {
037: super ();
038: initialize(engine);
039: }
040:
041: public String[] getAttributeNames() {
042: return new String[0];
043: }
044:
045: public String[] getMethodNames() {
046: return METHODS;
047: }
048:
049: public String getTitle() {
050: return "Search manager";
051: }
052:
053: /**
054: * Starts a background thread which goes through all the pages and adds them
055: * to the reindex queue.
056: * <p>
057: * This method prevents itself from being called twice.
058: */
059: public synchronized void reload() {
060: if (m_updater == null) {
061: m_updater = new WikiBackgroundThread(m_engine, 0) {
062: int m_count;
063: int m_max;
064:
065: public void startupTask() throws Exception {
066: super .startupTask();
067:
068: setName("Reindexer started");
069: }
070:
071: public void backgroundTask() throws Exception {
072: Collection allPages = m_engine.getPageManager()
073: .getAllPages();
074:
075: SearchManager mgr = m_engine.getSearchManager();
076: m_max = allPages.size();
077:
078: ProgressItem pi = new ProgressItem() {
079: public int getProgress() {
080: return 100 * m_count / m_max;
081: }
082: };
083:
084: m_engine.getProgressManager().startProgress(pi,
085: PROGRESS_ID);
086:
087: for (Iterator i = allPages.iterator(); i.hasNext();) {
088: WikiPage page = (WikiPage) i.next();
089:
090: mgr.reindexPage(page);
091: m_count++;
092: }
093:
094: m_engine.getProgressManager().stopProgress(
095: PROGRESS_ID);
096: shutdown();
097: m_updater = null;
098: }
099:
100: };
101:
102: m_updater.start();
103: }
104: }
105:
106: public int getType() {
107: return CORE;
108: }
109:
110: public String doGet(WikiContext context) {
111: if (m_updater != null) {
112: return "Update already in progress ("
113: + context.getEngine().getProgressManager()
114: .getProgress(PROGRESS_ID) + "%)";
115: }
116:
117: return "<input type='submit' id='searchmanagerbean-reload' name='searchmanagerbean-reload' value='Force index reload'/>"
118: + "<div class='description'>Forces JSPWiki search engine to reindex all pages. Use this if you think some pages are not being found even if they should.</div>";
119: }
120:
121: public String doPost(WikiContext context) {
122: String val = context
123: .getHttpParameter("searchmanagerbean-reload");
124:
125: if (val != null) {
126: reload();
127:
128: context.getWikiSession().addMessage(
129: "Started reload of all indexed pages...");
130:
131: return "";
132: }
133:
134: return doGet(context);
135: }
136:
137: }
|