001/*
002 * ModeShape (http://www.modeshape.org)
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.jdbc.delegate;
017
018import javax.jcr.Credentials;
019import javax.jcr.LoginException;
020import javax.jcr.NoSuchWorkspaceException;
021import javax.jcr.Repository;
022import javax.jcr.RepositoryException;
023import javax.jcr.Session;
024
025/**
026
027 */
028public class LocalSession {
029
030    private static LocalSession instance = new LocalSession();
031
032    public static LocalSession getLocalSessionInstance() {
033        return instance;
034    }
035
036    private ThreadLocal<LocalSession> tlocal = new ThreadLocal<LocalSession>() {
037        @Override
038        protected LocalSession initialValue() {
039            LocalSession ls = new LocalSession();
040            LocalRepositoryDelegate.TRANSACTION_IDS.add(ls);
041            return ls;
042        }
043    };
044
045    private Session session;
046
047    private void setSession( Session localSession ) {
048        session = localSession;
049    }
050
051    public LocalSession getLocalSession() {
052        return tlocal.get();
053    }
054
055    public LocalSession getLocalSession( final Repository repository,
056                                         final ConnectionInfo connInfo )
057        throws LoginException, NoSuchWorkspaceException, RepositoryException {
058        LocalSession lsession = tlocal.get();
059
060        Credentials credentials = connInfo.getCredentials();
061        String workspaceName = connInfo.getWorkspaceName();
062        Session session = null;
063        if (workspaceName != null) {
064            session = credentials != null ? repository.login(credentials, workspaceName) : repository.login(workspaceName);
065        } else {
066            session = credentials != null ? repository.login(credentials) : repository.login();
067        }
068        // this shouldn't happen, but in testing it did occur only because of
069        // the repository not being setup correctly
070        assert session != null;
071
072        lsession.setSession(session);
073
074        return lsession;
075    }
076
077    public Session getSession() {
078        return session;
079    }
080
081    public void remove() {
082        tlocal.remove();
083        LocalRepositoryDelegate.TRANSACTION_IDS.remove(this);
084        session.logout();
085    }
086
087    @Override
088    public String toString() {
089        StringBuilder sb = new StringBuilder("Session:");
090        sb.append(session.toString());
091        return sb.toString();
092    }
093
094}