001/*
002 * (C) Copyright 2006-2020 Nuxeo (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 */
020
021package org.nuxeo.ecm.core.api.local;
022
023import org.nuxeo.ecm.core.api.AbstractSession;
024import org.nuxeo.ecm.core.api.CloseableCoreSession;
025import org.nuxeo.ecm.core.api.CoreSession;
026import org.nuxeo.ecm.core.api.NuxeoPrincipal;
027import org.nuxeo.ecm.core.model.Session;
028import org.nuxeo.ecm.core.repository.RepositoryService;
029import org.nuxeo.runtime.api.Framework;
030
031/**
032 * Local Session: implementation of {@link CoreSession} beyond {@link AbstractSession}, dealing with low-level stuff.
033 */
034public class LocalSession extends AbstractSession implements CloseableCoreSession {
035
036    private static final long serialVersionUID = 1L;
037
038    protected String repositoryName;
039
040    protected NuxeoPrincipal principal;
041
042    public LocalSession(String repositoryName, NuxeoPrincipal principal) {
043        this.repositoryName = repositoryName;
044        this.principal = principal;
045    }
046
047    @Override
048    public String getRepositoryName() {
049        return repositoryName;
050    }
051
052    @Override
053    public String getSessionId() {
054        return toString();
055    }
056
057    @Override
058    public String toString() {
059        return repositoryName + "/" + principal;
060    }
061
062    @Override
063    public Session<?> getSession() {
064        RepositoryService repositoryService = Framework.getService(RepositoryService.class);
065        return repositoryService.getSession(repositoryName);
066    }
067
068    @Override
069    public void close() {
070        // nothing (the session holds no resources to close)
071    }
072
073    @Override
074    public void destroy() {
075        // nothing (deprecated)
076    }
077
078    @Override
079    public NuxeoPrincipal getPrincipal() {
080        return principal;
081    }
082
083    @Override
084    public boolean isStateSharedByAllThreadSessions() {
085        // by design we always share state when in the same thread
086        return true;
087    }
088
089}