001/*
002 *  Copyright 2016 Anyware Services
003 *
004 *  Licensed under the Apache License, Version 2.0 (the "License");
005 *  you may not use this file except in compliance with the License.
006 *  You may obtain a copy of the License at
007 *
008 *      http://www.apache.org/licenses/LICENSE-2.0
009 *
010 *  Unless required by applicable law or agreed to in writing, software
011 *  distributed under the License is distributed on an "AS IS" BASIS,
012 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 *  See the License for the specific language governing permissions and
014 *  limitations under the License.
015 */
016package org.ametys.cms.content.indexing.solr.observation;
017
018/**
019 * This helper centralize a few common helper methods
020 */
021public final class ObserverHelper
022{
023    private static int _suspended;
024    
025    private ObserverHelper()
026    {
027        // Nothing
028    }
029    
030    /**
031     * <b>Expert method</b>
032     * <br><b>This should only be used when there is only one thread working at that time (i.e. during the initialization of the application) 
033     * and that you manually do the global indexing after your long process.</b>
034     * <br>All threads will not index anymore after observed events
035     * <br>Call {@link #restartObservationForIndexation()} to listen to events again.
036     * <br>You have to call {@link #restartObservationForIndexation()} the same number of times you called before {@link #suspendObservationForIndexation()}
037     */
038    public static synchronized void suspendObservationForIndexation()
039    {
040        _suspended++;
041    }
042    
043    /**
044     * Is events observation suspended for indexation?
045     * @return true if events are not suspended, false otherwise.
046     */
047    public static synchronized boolean isNotSuspendedObservationForIndexation()
048    {
049        return _suspended == 0;
050    }
051
052    /**
053     * <b>Expert method</b>
054     * <br>Listen to events again after a call to {@link #suspendObservationForIndexation()}
055     * <br>You have to call {@link #restartObservationForIndexation()} the same number of times you called before {@link #suspendObservationForIndexation()}
056     */
057    public static synchronized void restartObservationForIndexation()
058    {
059        _suspended--;
060    }
061}