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;
030import org.apache.wiki.util.Synchronizer;
031
032import javax.management.NotCompliantMBeanException;
033import java.util.Collection;
034import java.util.concurrent.locks.ReentrantLock;
035
036
037/**
038 * The SearchManagerBean is a simple AdminBean interface to the SearchManager.  It currently can be used to force a reload of all of the pages.
039 *
040 *  @since 2.6
041 */
042public class SearchManagerBean extends SimpleAdminBean {
043
044    private static final String PROGRESS_ID = "searchmanagerbean.reindexer";
045    private static final String[] METHODS = { "reload" };
046
047    // private static Logger log = LogManager.getLogger( SearchManagerBean.class );
048
049    private WikiBackgroundThread m_updater;
050
051    /**
052     * A lock used to ensure thread safety when accessing shared resources.
053     * This lock provides more flexibility and capabilities than the intrinsic locking mechanism,
054     * such as the ability to attempt to acquire a lock with a timeout, or to interrupt a thread
055     * waiting to acquire a lock.
056     *
057     * @see java.util.concurrent.locks.ReentrantLock
058     */
059    private final ReentrantLock lock = new ReentrantLock();
060
061    public SearchManagerBean( final Engine engine ) throws NotCompliantMBeanException {
062        super();
063        initialize( engine );
064    }
065
066    @Override
067    public String[] getAttributeNames()
068    {
069        return new String[0];
070    }
071
072    @Override
073    public String[] getMethodNames()
074    {
075        return METHODS;
076    }
077
078    @Override
079    public String getTitle()
080    {
081        return "Search manager";
082    }
083
084    /**
085     *  Starts a background thread which goes through all the pages and adds them to the reindex queue.
086     *  <p>
087     *  This method prevents itself from being called twice.
088     */
089    public void reload() {
090        Synchronizer.synchronize(lock, () -> {
091            if( m_updater == null ) {
092                m_updater = new WikiBackgroundThread( m_engine, 0 ) {
093
094                    int m_count;
095                    int m_max;
096
097                    @Override
098                    public void startupTask() throws Exception {
099                        super.startupTask();
100
101                        setName( "Reindexer started" );
102                    }
103
104                    @Override
105                    public void backgroundTask() throws Exception {
106                        final Collection< Page > allPages = m_engine.getManager( PageManager.class ).getAllPages();
107
108                        final SearchManager mgr = m_engine.getManager( SearchManager.class );
109                        m_max = allPages.size();
110
111                        final ProgressItem pi = new ProgressItem() {
112
113                            @Override
114                            public int getProgress() {
115                                return 100 * m_count / m_max;
116                            }
117                        };
118                        m_engine.getManager( ProgressManager.class ).startProgress( pi, PROGRESS_ID );
119
120                        for( final Page page : allPages ) {
121                            mgr.reindexPage( page );
122                            m_count++;
123                        }
124
125                        m_engine.getManager( ProgressManager.class ).stopProgress( PROGRESS_ID );
126                        shutdown();
127                        m_updater = null;
128                    }
129
130                };
131
132                m_updater.start();
133            }
134        });
135    }
136
137    @Override
138    public int getType() {
139        return CORE;
140    }
141
142    @Override
143    public String doGet( final Context context ) {
144        if( m_updater != null ) {
145            return "Update already in progress ("+ context.getEngine().getManager( ProgressManager.class ).getProgress(PROGRESS_ID)+ "%)";
146        }
147
148        return "<input type='submit' id='searchmanagerbean-reload' name='searchmanagerbean-reload' value='Force index reload'/>"+
149               "<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>";
150    }
151
152    @Override
153    public String doPost( final Context context ) {
154        final String val = context.getHttpParameter( "searchmanagerbean-reload" );
155        if( val != null ) {
156            reload();
157            context.getWikiSession().addMessage( "Started reload of all indexed pages..." );
158            return "";
159        }
160
161        return doGet( context );
162    }
163
164}