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 org.apache.wiki.WikiBackgroundThread;
022import org.apache.wiki.api.core.Context;
023import org.apache.wiki.api.core.Engine;
024import org.apache.wiki.api.core.Page;
025import org.apache.wiki.pages.PageManager;
026import org.apache.wiki.search.SearchManager;
027import org.apache.wiki.ui.admin.SimpleAdminBean;
028import org.apache.wiki.ui.progress.ProgressItem;
029import org.apache.wiki.ui.progress.ProgressManager;
030
031import javax.management.NotCompliantMBeanException;
032import java.util.Collection;
033
034
035/**
036 * The SearchManagerBean is a simple AdminBean interface to the SearchManager.  It currently can be used to force 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    private static final String[] METHODS = { "reload" };
044
045    // private static Logger log = LogManager.getLogger( SearchManagerBean.class );
046
047    private WikiBackgroundThread m_updater;
048
049    public SearchManagerBean( final Engine engine ) throws NotCompliantMBeanException {
050        super();
051        initialize( engine );
052    }
053
054    @Override
055    public String[] getAttributeNames()
056    {
057        return new String[0];
058    }
059
060    @Override
061    public String[] getMethodNames()
062    {
063        return METHODS;
064    }
065
066    @Override
067    public String getTitle()
068    {
069        return "Search manager";
070    }
071
072    /**
073     *  Starts a background thread which goes through all the pages and adds them to the reindex queue.
074     *  <p>
075     *  This method prevents itself from being called twice.
076     */
077    public synchronized void reload() {
078        if( m_updater == null ) {
079            m_updater = new WikiBackgroundThread( m_engine, 0 ) {
080
081                int m_count;
082                int m_max;
083
084                @Override
085                public void startupTask() throws Exception {
086                    super.startupTask();
087
088                    setName( "Reindexer started" );
089                }
090
091                @Override
092                public void backgroundTask() throws Exception {
093                    final Collection< Page > allPages = m_engine.getManager( PageManager.class ).getAllPages();
094
095                    final SearchManager mgr = m_engine.getManager( SearchManager.class );
096                    m_max = allPages.size();
097
098                    final ProgressItem pi = new ProgressItem() {
099
100                        @Override
101                        public int getProgress() {
102                            return 100 * m_count / m_max;
103                        }
104                    };
105                    m_engine.getManager( ProgressManager.class ).startProgress( pi, PROGRESS_ID );
106
107                    for( final Page page : allPages ) {
108                        mgr.reindexPage( page );
109                        m_count++;
110                    }
111
112                    m_engine.getManager( ProgressManager.class ).stopProgress( PROGRESS_ID );
113                    shutdown();
114                    m_updater = null;
115                }
116
117            };
118
119            m_updater.start();
120        }
121    }
122
123    @Override
124    public int getType() {
125        return CORE;
126    }
127
128    @Override
129    public String doGet( final Context context ) {
130        if( m_updater != null ) {
131            return "Update already in progress ("+ context.getEngine().getManager( ProgressManager.class ).getProgress(PROGRESS_ID)+ "%)";
132        }
133
134        return "<input type='submit' id='searchmanagerbean-reload' name='searchmanagerbean-reload' value='Force index reload'/>"+
135               "<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>";
136    }
137
138    @Override
139    public String doPost( final Context context ) {
140        final String val = context.getHttpParameter( "searchmanagerbean-reload" );
141        if( val != null ) {
142            reload();
143            context.getWikiSession().addMessage( "Started reload of all indexed pages..." );
144            return "";
145        }
146
147        return doGet( context );
148    }
149
150}