001/*
002 * (C) Copyright 2012 Nuxeo SA (http://nuxeo.com/) and contributors.
003 *
004 * All rights reserved. This program and the accompanying materials
005 * are made available under the terms of the GNU Lesser General Public License
006 * (LGPL) version 2.1 which accompanies this distribution, and is available at
007 * http://www.gnu.org/licenses/lgpl.html
008 *
009 * This library is distributed in the hope that it will be useful,
010 * but WITHOUT ANY WARRANTY; without even the implied warranty of
011 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
012 * Lesser General Public License for more details.
013 *
014 * Contributors:
015 *     Mariana Cedica
016 */
017package org.nuxeo.ecm.platform.routing.core.listener;
018
019import org.apache.commons.logging.Log;
020import org.apache.commons.logging.LogFactory;
021import org.nuxeo.ecm.core.api.NuxeoException;
022import org.nuxeo.ecm.core.api.UnrestrictedSessionRunner;
023import org.nuxeo.ecm.core.api.repository.RepositoryManager;
024import org.nuxeo.ecm.platform.routing.api.DocumentRoutingService;
025import org.nuxeo.runtime.api.Framework;
026import org.nuxeo.runtime.reload.ReloadEventNames;
027import org.nuxeo.runtime.services.event.Event;
028import org.nuxeo.runtime.services.event.EventListener;
029import org.nuxeo.runtime.transaction.TransactionHelper;
030
031/**
032 * Event listener that reloads the contributed route models.
033 *
034 * @since 5.6
035 */
036public class RouteModelsReloader implements EventListener {
037
038    private static final Log log = LogFactory.getLog(RouteModelsReloader.class);
039
040    @Override
041    public boolean aboutToHandleEvent(Event event) {
042        return false;
043    }
044
045    @Override
046    public void handleEvent(Event event) {
047        if (!Framework.isDevModeSet()) {
048            log.info("Do not flush the directory caches: dev mode is not set");
049            return;
050        }
051        if (!ReloadEventNames.RELOAD_EVENT_ID.equals(event.getId())) {
052            return;
053        }
054        try {
055            RepositoryManager rm = Framework.getService(RepositoryManager.class);
056            // Transaction management
057            final boolean txStarted = !TransactionHelper.isTransactionActive() && TransactionHelper.startTransaction();
058            boolean txSucceed = false;
059            try {
060                new UnrestrictedSessionRunner(rm.getDefaultRepositoryName()) {
061                    @Override
062                    public void run() {
063                        DocumentRoutingService service = Framework.getLocalService(DocumentRoutingService.class);
064                        service.importAllRouteModels(session);
065                    }
066                }.runUnrestricted();
067                txSucceed = true;
068            } finally {
069                if (txStarted) {
070                    if (!txSucceed) {
071                        TransactionHelper.setTransactionRollbackOnly();
072                        log.warn("Rollbacking import of route models");
073                    }
074                    TransactionHelper.commitOrRollbackTransaction();
075                }
076            }
077        } catch (NuxeoException e) {
078            log.error("Error while reloading the route models", e);
079        }
080    }
081}