001/*
002 * (C) Copyright 2012 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 boolean aboutToHandleEvent(Event event) {
044        return false;
045    }
046
047    @Override
048    public void handleEvent(Event event) {
049        if (!Framework.isDevModeSet()) {
050            log.info("Do not flush the directory caches: dev mode is not set");
051            return;
052        }
053        if (!ReloadEventNames.RELOAD_EVENT_ID.equals(event.getId())) {
054            return;
055        }
056        try {
057            RepositoryManager rm = Framework.getService(RepositoryManager.class);
058            // Transaction management
059            final boolean txStarted = !TransactionHelper.isTransactionActive() && TransactionHelper.startTransaction();
060            boolean txSucceed = false;
061            try {
062                new UnrestrictedSessionRunner(rm.getDefaultRepositoryName()) {
063                    @Override
064                    public void run() {
065                        DocumentRoutingService service = Framework.getLocalService(DocumentRoutingService.class);
066                        service.importAllRouteModels(session);
067                    }
068                }.runUnrestricted();
069                txSucceed = true;
070            } finally {
071                if (txStarted) {
072                    if (!txSucceed) {
073                        TransactionHelper.setTransactionRollbackOnly();
074                        log.warn("Rollbacking import of route models");
075                    }
076                    TransactionHelper.commitOrRollbackTransaction();
077                }
078            }
079        } catch (NuxeoException e) {
080            log.error("Error while reloading the route models", e);
081        }
082    }
083}