001/*
002 * (C) Copyright 2006-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 *     Bogdan Stefanescu
018 *     Florent Guillaume
019 */
020package org.nuxeo.ecm.core.storage;
021
022import org.apache.commons.logging.Log;
023import org.apache.commons.logging.LogFactory;
024import org.nuxeo.ecm.core.repository.RepositoryService;
025import org.nuxeo.runtime.api.Framework;
026import org.nuxeo.runtime.reload.ReloadService;
027import org.nuxeo.runtime.services.event.Event;
028import org.nuxeo.runtime.services.event.EventListener;
029
030public class RepositoryReloader implements EventListener {
031
032    private static Log log = LogFactory.getLog(RepositoryReloader.class);
033
034    @Override
035    public void handleEvent(Event event) {
036        final String id = event.getId();
037        if (ReloadService.RELOAD_REPOSITORIES_ID.equals(id) || ReloadService.FLUSH_EVENT_ID.equals(id)) {
038            reloadRepositories();
039        }
040    }
041
042    protected static void closeRepositories() {
043        Framework.getService(RepositoryService.class).shutdown();
044    }
045
046    protected static void flushJCAPool() {
047        try {
048            Class<?> nuxeoContainerClass = Class.forName("org.nuxeo.runtime.jtajca.NuxeoContainer");
049            if (nuxeoContainerClass != null) {
050                nuxeoContainerClass.getMethod("resetConnectionManager").invoke(null);
051            }
052        } catch (ClassNotFoundException e) {
053            // no container
054            log.debug(e, e);
055        } catch (ReflectiveOperationException e) {
056            throw new RuntimeException(e);
057        }
058    }
059
060    /**
061     * Reload core repositories.
062     */
063    protected static void reloadRepositories() {
064        RepositoryReloader.closeRepositories();
065    }
066}