001/*
002 * (C) Copyright 2006-2016 Nuxeo SA (http://nuxeo.com/) and others.
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 *
016 * Contributors:
017 *     Nuxeo - initial API and implementation
018 */
019package org.nuxeo.ecm.platform.web.common.admin;
020
021import static org.nuxeo.ecm.core.management.api.AdministrativeStatusManager.ACTIVATED_EVENT;
022import static org.nuxeo.ecm.core.management.api.AdministrativeStatusManager.GLOBAL_INSTANCE_AVAILABILITY;
023import static org.nuxeo.ecm.core.management.api.AdministrativeStatusManager.PASSIVATED_EVENT;
024
025import org.nuxeo.ecm.core.management.api.AdministrativeStatus;
026import org.nuxeo.ecm.core.management.api.AdministrativeStatusManager;
027import org.nuxeo.ecm.core.management.api.GlobalAdministrativeStatusManager;
028import org.nuxeo.ecm.platform.api.login.RestrictedLoginHelper;
029import org.nuxeo.runtime.api.Framework;
030import org.nuxeo.runtime.services.event.Event;
031import org.nuxeo.runtime.services.event.EventListener;
032
033/**
034 * Listen for {@link AdministrativeStatus} changes and set the necessary flag in {@link AdminStatusHelper} so that web
035 * infrastructure can directly use the Helper.
036 *
037 * @author tiry
038 */
039public class AdministrativeStatusListener implements EventListener {
040
041    public static final String ADM_MESSAGE_SERVICE = "org.nuxeo.ecm.administrator.message";
042
043    protected static String localInstanceId;
044
045    protected static String getLocalInstanceId() {
046        if (localInstanceId == null) {
047            GlobalAdministrativeStatusManager gasm = Framework.getService(GlobalAdministrativeStatusManager.class);
048            localInstanceId = gasm.getLocalNuxeoInstanceIdentifier();
049        }
050        return localInstanceId;
051    }
052
053    @Override
054    public void handleEvent(Event event) {
055        String eventId = event.getId();
056        String instanceId = (String) event.getSource();
057        String serviceId = (String) event.getData();
058
059        if (!getLocalInstanceId().equals(instanceId)) {
060            return;
061        }
062
063        AdministrativeStatusManager asm = Framework.getService(AdministrativeStatusManager.class);
064
065        if (serviceId.equals(GLOBAL_INSTANCE_AVAILABILITY)) {
066            if (eventId.equals(ACTIVATED_EVENT)) {
067                AdminStatusHelper.instanceInMaintenanceMode = false;
068                RestrictedLoginHelper.setRestrictedModeActivated(false);
069            }
070            if (eventId.equals(PASSIVATED_EVENT)) {
071                AdminStatusHelper.instanceInMaintenanceMode = true;
072                RestrictedLoginHelper.setRestrictedModeActivated(true);
073            }
074
075            AdminStatusHelper.maintenanceMessage = asm.getStatus(GLOBAL_INSTANCE_AVAILABILITY).getMessage();
076        }
077
078        if (serviceId.equals(ADM_MESSAGE_SERVICE)) {
079            if (eventId.equals(ACTIVATED_EVENT)) {
080                AdminStatusHelper.adminMessageActivated = true;
081            }
082            if (eventId.equals(PASSIVATED_EVENT)) {
083                AdminStatusHelper.adminMessageActivated = false;
084            }
085            AdministrativeStatus status = asm.getStatus(ADM_MESSAGE_SERVICE);
086            AdminStatusHelper.adminMessage = status.getMessage();
087            AdminStatusHelper.adminMessageModificationDate = status.getModificationDate();
088
089        }
090    }
091
092}