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