001// --------------------------------------------------------------------------------
002// Copyright 2002-2024 Echo Three, LLC
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
017package com.echothree.util.server.persistence;
018
019import org.apache.commons.logging.Log;
020import org.apache.commons.logging.LogFactory;
021
022public class ThreadSession {
023    
024    private static Log log = LogFactory.getLog(ThreadSession.class);
025    private static final ThreadLocal<Session> sessions = new ThreadLocal<>();
026
027    private ThreadSession() {}
028
029    public static Session currentSession() {
030        Session session = sessions.get();
031        
032        if(session == null) {
033            session = SessionFactory.getInstance().getSession();
034            sessions.set(session);
035            
036            if(PersistenceDebugFlags.LogThreads) {
037                log.info("Created Session for Thread " + Thread.currentThread().getName());
038            }
039        }
040        
041        return session;
042    }
043
044    public static void pushSessionEntityCache() {
045        currentSession().pushSessionEntityCache();
046    }
047
048    public static void popSessionEntityCache() {
049        currentSession().popSessionEntityCache();
050    }
051
052    static class PreservedSession {
053        private Session session;
054
055        private PreservedSession(Session session) {
056            this.session = session;
057        }
058    }
059
060    // Utilize via ThreadUtils
061    static PreservedSession preserve() {
062        Session session = sessions.get();
063
064        if(session != null) {
065            sessions.remove();
066
067            if(PersistenceDebugFlags.LogThreads) {
068                log.info("Preserved Session for Thread " + Thread.currentThread().getName());
069            }
070        }
071
072        return new PreservedSession(session);
073    }
074
075    // Utilize via ThreadUtils
076    static void restore(PreservedSession preservedSession) {
077        var session = preservedSession.session;
078
079        if(session != null) {
080            sessions.set(session);
081
082            if(PersistenceDebugFlags.LogThreads) {
083                log.info("Restored Session for Thread " + Thread.currentThread().getName());
084            }
085        }
086    }
087
088    // Utilize via ThreadUtils
089    static void close() {
090        Session session = sessions.get();
091        
092        if(session != null) {
093            session.close();
094            sessions.remove();
095            
096            if(PersistenceDebugFlags.LogThreads) {
097                log.info("Closed Session for Thread " + Thread.currentThread().getName());
098            }
099        }
100    }
101    
102}