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 *     Florent Guillaume
011 */
012package org.nuxeo.ecm.core.storage.sql.coremodel;
013
014import org.apache.commons.logging.Log;
015import org.apache.commons.logging.LogFactory;
016import org.nuxeo.ecm.core.api.NuxeoException;
017import org.nuxeo.ecm.core.model.Repository;
018import org.nuxeo.ecm.core.model.Session;
019import org.nuxeo.ecm.core.storage.sql.RepositoryDescriptor;
020import org.nuxeo.ecm.core.storage.sql.RepositoryImpl;
021
022/**
023 * This is the {@link Session} factory when the repository is used outside of a datasource.
024 * <p>
025 * (When repositories are looked up through JNDI, the class org.nuxeo.ecm.core.storage.sql.ra.ConnectionFactoryImpl is
026 * used instead of this one.) [suppressed link for solving cycle dependencies in eclipse]
027 * <p>
028 * This class is constructed by {@link SQLRepositoryFactory}.
029 *
030 * @author Florent Guillaume
031 */
032public class SQLRepository implements Repository {
033
034    private static final Log log = LogFactory.getLog(SQLRepository.class);
035
036    public final RepositoryImpl repository;
037
038    private final String name;
039
040    public SQLRepository(RepositoryDescriptor descriptor) {
041        repository = new RepositoryImpl(descriptor);
042        name = descriptor.name;
043    }
044
045    /*
046     * ----- org.nuxeo.ecm.core.model.Repository -----
047     */
048
049    @Override
050    public String getName() {
051        return name;
052    }
053
054    /*
055     * Called by LocalSession.createSession
056     */
057    @Override
058    public Session getSession() {
059        return new SQLSession(repository.getConnection(), this);
060    }
061
062    @Override
063    public void shutdown() {
064        try {
065            repository.close();
066        } catch (NuxeoException e) {
067            log.error("Cannot close repository", e);
068        }
069    }
070
071    @Override
072    public int getActiveSessionsCount() {
073        return repository.getActiveSessionsCount();
074    }
075
076    @Override
077    public void markReferencedBinaries() {
078        repository.markReferencedBinaries();
079    }
080
081}