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