001/*
002 * Copyright (c) 2006-2014 Nuxeo SA (http://nuxeo.com/) and others.
003 *
004 * All rights reserved. This program and the accompanying materials
005 * are made available under the terms of the Eclipse Public License v1.0
006 * which accompanies this distribution, and is available at
007 * http://www.eclipse.org/legal/epl-v10.html
008 *
009 * Contributors:
010 *     Bogdan Stefanescu
011 *     Florent Guillaume
012 */
013package org.nuxeo.ecm.core.storage;
014
015import org.apache.commons.logging.Log;
016import org.apache.commons.logging.LogFactory;
017import org.nuxeo.ecm.core.repository.RepositoryService;
018import org.nuxeo.runtime.api.Framework;
019import org.nuxeo.runtime.reload.ReloadService;
020import org.nuxeo.runtime.services.event.Event;
021import org.nuxeo.runtime.services.event.EventListener;
022
023public class RepositoryReloader implements EventListener {
024
025    private static Log log = LogFactory.getLog(RepositoryReloader.class);
026
027    @Override
028    public boolean aboutToHandleEvent(Event event) {
029        return true;
030    }
031
032    @Override
033    public void handleEvent(Event event) {
034        final String id = event.getId();
035        if (ReloadService.RELOAD_REPOSITORIES_ID.equals(id) || ReloadService.FLUSH_EVENT_ID.equals(id)) {
036            reloadRepositories();
037        }
038    }
039
040    protected static void closeRepositories() {
041        Framework.getService(RepositoryService.class).shutdown();
042    }
043
044    protected static void flushJCAPool() {
045        try {
046            Class<?> nuxeoContainerClass = Class.forName("org.nuxeo.runtime.jtajca.NuxeoContainer");
047            if (nuxeoContainerClass != null) {
048                nuxeoContainerClass.getMethod("resetConnectionManager").invoke(null);
049            }
050        } catch (ClassNotFoundException e) {
051            // no container
052            log.debug(e, e);
053        } catch (ReflectiveOperationException e) {
054            throw new RuntimeException(e);
055        }
056    }
057
058    /**
059     * Reload core repositories.
060     */
061    protected static void reloadRepositories() {
062        RepositoryReloader.closeRepositories();
063    }
064}