001/*
002 * (C) Copyright 2006-2011 Nuxeo SA (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 *     bstefanescu
018 */
019package org.nuxeo.ecm.automation;
020
021import java.util.ArrayList;
022import java.util.List;
023
024import javax.security.auth.login.LoginContext;
025import javax.security.auth.login.LoginException;
026
027import org.apache.commons.logging.Log;
028import org.apache.commons.logging.LogFactory;
029import org.nuxeo.ecm.core.api.CloseableCoreSession;
030import org.nuxeo.ecm.core.api.CoreInstance;
031import org.nuxeo.ecm.core.api.CoreSession;
032
033/**
034 * @author <a href="mailto:bs@nuxeo.com">Bogdan Stefanescu</a>
035 */
036public class LoginStack {
037
038    protected List<Entry> stack = new ArrayList<>();
039
040    protected CoreSession originalSession;
041
042    protected CoreSession currentSession;
043
044    public LoginStack(CoreSession session) {
045        setSession(session);
046    }
047
048    public void setSession(CoreSession session) {
049        originalSession = session;
050        currentSession = session;
051    }
052
053    /**
054     * Get the current session
055     *
056     * @return
057     */
058    public CoreSession getSession() {
059        return currentSession;
060    }
061
062    public void push(LoginContext lc) {
063        Entry entry = new Entry(lc);
064        String repositoryName;
065        if (originalSession != null) {
066            repositoryName = originalSession.getRepositoryName();
067        } else {
068            repositoryName = null; // default repository
069        }
070        entry.session = CoreInstance.openCoreSession(repositoryName);
071        currentSession = entry.session;
072        stack.add(entry);
073    }
074
075    public Entry peek() {
076        if (!stack.isEmpty()) {
077            return stack.get(stack.size() - 1);
078        }
079        return null;
080    }
081
082    /**
083     * Remove the current login context from the stack.
084     * <p>
085     * If no login context in in the stack nothing is done. If the login context has an associated CoreSession the
086     * session will be destroyed and the previous session is restored as the active session of the operation context.
087     */
088    public void pop() {
089        if (!stack.isEmpty()) {
090            Entry entry = stack.remove(stack.size() - 1);
091            entry.dispose();
092            entry = peek();
093            if (entry != null) {
094                currentSession = entry.session;
095            } else {
096                currentSession = originalSession;
097            }
098        }
099    }
100
101    /**
102     * Remove the stacked logins if any. This is called when chain execution is done.
103     */
104    protected void clear() {
105        if (!stack.isEmpty()) {
106            for (int i = stack.size() - 1; i > -1; i--) {
107                stack.get(i).dispose();
108            }
109            stack.clear();
110            currentSession = originalSession;
111            stack.clear();
112        }
113    }
114
115    public static class Entry {
116
117        private static final Log log = LogFactory.getLog(Entry.class);
118
119        public LoginContext lc;
120
121        public CloseableCoreSession session;
122
123        public Entry(LoginContext lc) {
124            this.lc = lc;
125        }
126
127        public final boolean hasSession() {
128            return session != null;
129        }
130
131        public final void dispose() {
132            try {
133                if (session != null) {
134                    try {
135                        session.save();
136                    } finally {
137                        session.close();
138                    }
139                }
140            } finally {
141                try {
142                    session = null;
143                    lc.logout();
144                    lc = null;
145                } catch (LoginException e) {
146                    log.error(e); // don't rethrow inside finally
147                }
148            }
149        }
150    }
151
152}