001/**
002 * Copyright 2014 DuraSpace, Inc.
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 */
016package org.modeshape.jcr;
017
018import org.fcrepo.kernel.exception.RepositoryRuntimeException;
019import org.modeshape.jcr.bus.ChangeBus;
020import org.slf4j.Logger;
021
022import javax.jcr.RepositoryException;
023import javax.jcr.Session;
024
025import static org.slf4j.LoggerFactory.getLogger;
026
027/**
028 * @author cabeer
029 * @since 10/15/14
030 */
031public class SessionUtils {
032
033    /**
034     * No public constructor for utility classes
035     */
036    private SessionUtils() {
037
038    }
039
040    private static final Logger LOGGER = getLogger(SessionUtils.class);
041
042    /**
043     * Until MODE-2343 is fixed, we need to manually dispose of the ObservationManager listener.
044     *
045     * @param session
046     */
047    public static void unregisterObservationManager(final Session session) {
048        try {
049            getChangeBus(session).unregister(getObservationManager(session));
050        } catch (final RepositoryException e) {
051            LOGGER.info("Unable to dispose observation manager: {}", e);
052            throw new RepositoryRuntimeException(e);
053        }
054    }
055
056    private static JcrObservationManager getObservationManager(final Session session) throws RepositoryException {
057        return (JcrObservationManager) session.getWorkspace().getObservationManager();
058    }
059
060    private static ChangeBus getChangeBus(final Session session) {
061        return ((JcrRepository)session.getRepository()).changeBus();
062    }
063}