001/* 002 Licensed to the Apache Software Foundation (ASF) under one 003 or more contributor license agreements. See the NOTICE file 004 distributed with this work for additional information 005 regarding copyright ownership. The ASF licenses this file 006 to you under the Apache License, Version 2.0 (the 007 "License"); you may not use this file except in compliance 008 with the License. You may obtain a copy of the License at 009 010 http://www.apache.org/licenses/LICENSE-2.0 011 012 Unless required by applicable law or agreed to in writing, 013 software distributed under the License is distributed on an 014 "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 015 KIND, either express or implied. See the License for the 016 specific language governing permissions and limitations 017 under the License. 018 */ 019package org.apache.wiki.ui.admin.beans; 020 021import java.util.Collection; 022 023import javax.management.NotCompliantMBeanException; 024 025import org.apache.wiki.WikiBackgroundThread; 026import org.apache.wiki.WikiContext; 027import org.apache.wiki.WikiEngine; 028import org.apache.wiki.WikiPage; 029import org.apache.wiki.search.SearchManager; 030import org.apache.wiki.ui.admin.SimpleAdminBean; 031import org.apache.wiki.ui.progress.ProgressItem; 032 033/** 034 * The SearchManagerBean is a simple AdminBean interface 035 * to the SearchManager. It currently can be used to force 036 * a reload of all of the pages. 037 * 038 * @since 2.6 039 */ 040public class SearchManagerBean extends SimpleAdminBean 041{ 042 private static final String PROGRESS_ID = "searchmanagerbean.reindexer"; 043 044 private static final String[] METHODS = { "reload" }; 045 046 // private static Logger log = Logger.getLogger( SearchManagerBean.class ); 047 048 private WikiBackgroundThread m_updater; 049 050 public SearchManagerBean(WikiEngine engine) throws NotCompliantMBeanException 051 { 052 super(); 053 initialize(engine); 054 } 055 056 public String[] getAttributeNames() 057 { 058 return new String[0]; 059 } 060 061 public String[] getMethodNames() 062 { 063 return METHODS; 064 } 065 066 public String getTitle() 067 { 068 return "Search manager"; 069 } 070 071 /** 072 * Starts a background thread which goes through all the pages and adds them 073 * to the reindex queue. 074 * <p> 075 * This method prevents itself from being called twice. 076 */ 077 public synchronized void reload() 078 { 079 if( m_updater == null ) 080 { 081 m_updater = new WikiBackgroundThread(m_engine, 0) { 082 int m_count; 083 int m_max; 084 085 public void startupTask() throws Exception 086 { 087 super.startupTask(); 088 089 setName("Reindexer started"); 090 } 091 092 @SuppressWarnings("unchecked") 093 public void backgroundTask() throws Exception 094 { 095 Collection<WikiPage> allPages = m_engine.getPageManager().getAllPages(); 096 097 SearchManager mgr = m_engine.getSearchManager(); 098 m_max = allPages.size(); 099 100 ProgressItem pi = new ProgressItem() { 101 public int getProgress() 102 { 103 return 100 * m_count / m_max; 104 } 105 }; 106 107 m_engine.getProgressManager().startProgress( pi, PROGRESS_ID ); 108 109 for( WikiPage page : allPages ) 110 { 111 mgr.reindexPage(page); 112 m_count++; 113 } 114 115 m_engine.getProgressManager().stopProgress( PROGRESS_ID ); 116 shutdown(); 117 m_updater = null; 118 } 119 120 }; 121 122 m_updater.start(); 123 } 124 } 125 126 public int getType() 127 { 128 return CORE; 129 } 130 131 public String doGet(WikiContext context) 132 { 133 if( m_updater != null ) 134 { 135 return "Update already in progress ("+ 136 context.getEngine().getProgressManager().getProgress(PROGRESS_ID)+ 137 "%)"; 138 } 139 140 return "<input type='submit' id='searchmanagerbean-reload' name='searchmanagerbean-reload' value='Force index reload'/>"+ 141 "<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>"; 142 } 143 144 public String doPost(WikiContext context) 145 { 146 String val = context.getHttpParameter("searchmanagerbean-reload"); 147 148 if( val != null ) 149 { 150 reload(); 151 152 context.getWikiSession().addMessage( "Started reload of all indexed pages..." ); 153 154 return ""; 155 } 156 157 return doGet(context); 158 } 159 160}