001/*
002 * (C) Copyright 2006-2014 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 boolean aboutToHandleEvent(Event event) {
036        return true;
037    }
038
039    @Override
040    public void handleEvent(Event event) {
041        final String id = event.getId();
042        if (ReloadService.RELOAD_REPOSITORIES_ID.equals(id) || ReloadService.FLUSH_EVENT_ID.equals(id)) {
043            reloadRepositories();
044        }
045    }
046
047    protected static void closeRepositories() {
048        Framework.getService(RepositoryService.class).shutdown();
049    }
050
051    protected static void flushJCAPool() {
052        try {
053            Class<?> nuxeoContainerClass = Class.forName("org.nuxeo.runtime.jtajca.NuxeoContainer");
054            if (nuxeoContainerClass != null) {
055                nuxeoContainerClass.getMethod("resetConnectionManager").invoke(null);
056            }
057        } catch (ClassNotFoundException e) {
058            // no container
059            log.debug(e, e);
060        } catch (ReflectiveOperationException e) {
061            throw new RuntimeException(e);
062        }
063    }
064
065    /**
066     * Reload core repositories.
067     */
068    protected static void reloadRepositories() {
069        RepositoryReloader.closeRepositories();
070    }
071}