001/*
002 * (C) Copyright 2012-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 *     Mariana Cedica
018 */
019package org.nuxeo.ecm.platform.routing.core.listener;
020
021import org.apache.commons.logging.Log;
022import org.apache.commons.logging.LogFactory;
023import org.nuxeo.ecm.core.api.NuxeoException;
024import org.nuxeo.ecm.core.api.UnrestrictedSessionRunner;
025import org.nuxeo.ecm.core.api.repository.RepositoryManager;
026import org.nuxeo.ecm.platform.routing.api.DocumentRoutingService;
027import org.nuxeo.runtime.api.Framework;
028import org.nuxeo.runtime.reload.ReloadEventNames;
029import org.nuxeo.runtime.services.event.Event;
030import org.nuxeo.runtime.services.event.EventListener;
031import org.nuxeo.runtime.transaction.TransactionHelper;
032
033/**
034 * Event listener that reloads the contributed route models.
035 *
036 * @since 5.6
037 */
038public class RouteModelsReloader implements EventListener {
039
040    private static final Log log = LogFactory.getLog(RouteModelsReloader.class);
041
042    @Override
043    public void handleEvent(Event event) {
044        if (!Framework.isDevModeSet()) {
045            log.info("Do not flush the directory caches: dev mode is not set");
046            return;
047        }
048        if (!ReloadEventNames.RELOAD_EVENT_ID.equals(event.getId())) {
049            return;
050        }
051        try {
052            RepositoryManager rm = Framework.getService(RepositoryManager.class);
053            // Transaction management
054            final boolean txStarted = !TransactionHelper.isTransactionActive() && TransactionHelper.startTransaction();
055            boolean txSucceed = false;
056            try {
057                new UnrestrictedSessionRunner(rm.getDefaultRepositoryName()) {
058                    @Override
059                    public void run() {
060                        DocumentRoutingService service = Framework.getLocalService(DocumentRoutingService.class);
061                        service.importAllRouteModels(session);
062                    }
063                }.runUnrestricted();
064                txSucceed = true;
065            } finally {
066                if (txStarted) {
067                    if (!txSucceed) {
068                        TransactionHelper.setTransactionRollbackOnly();
069                        log.warn("Rollbacking import of route models");
070                    }
071                    TransactionHelper.commitOrRollbackTransaction();
072                }
073            }
074        } catch (NuxeoException e) {
075            log.error("Error while reloading the route models", e);
076        }
077    }
078}