001// Copyright 2013 The Apache Software Foundation
002//
003// Licensed under the Apache License, Version 2.0 (the "License");
004// you may not use this file except in compliance with the License.
005// You may obtain a copy of the License at
006//
007// http://www.apache.org/licenses/LICENSE-2.0
008//
009// Unless required by applicable law or agreed to in writing, software
010// distributed under the License is distributed on an "AS IS" BASIS,
011// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
012// See the License for the specific language governing permissions and
013// limitations under the License.
014
015package org.apache.tapestry5.internal.services;
016
017import org.apache.tapestry5.alerts.AlertManager;
018import org.apache.tapestry5.commons.util.CollectionFactory;
019import org.apache.tapestry5.http.TapestryHttpSymbolConstants;
020import org.apache.tapestry5.ioc.annotations.Symbol;
021
022import java.util.List;
023
024public class ReloadHelperImpl implements ReloadHelper
025{
026    private final AlertManager alertManager;
027
028    private final boolean productionMode;
029
030    private final List<Runnable> callbacks = CollectionFactory.newThreadSafeList();
031
032    public ReloadHelperImpl(AlertManager alertManager, @Symbol(TapestryHttpSymbolConstants.PRODUCTION_MODE) boolean productionMode)
033    {
034        this.alertManager = alertManager;
035        this.productionMode = productionMode;
036    }
037
038    public void forceReload()
039    {
040        if (productionMode)
041        {
042            alertManager.error("Can not force a reload in production mode.");
043            return;
044        }
045
046        for (Runnable c : callbacks)
047        {
048            c.run();
049        }
050
051        alertManager.info("Component classes, templates, and messages reloaded.");
052    }
053
054    public void addReloadCallback(Runnable callback)
055    {
056        assert callback != null;
057
058        callbacks.add(callback);
059    }
060}