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            if (currentSession != null) {
096                refreshSession(currentSession);
097            }
098        }
099    }
100
101    protected void refreshSession(CoreSession session) throws OperationException {
102        if (session != null && !session.isStateSharedByAllThreadSessions()) {
103            // this will indirectly process refresh the session
104            session.save();
105        }
106    }
107
108    /**
109     * Remove the stacked logins if any. This is called when chain execution is done.
110     */
111    protected void clear() throws OperationException {
112        if (!stack.isEmpty()) {
113            for (int i = stack.size() - 1; i > -1; i--) {
114                stack.get(i).dispose();
115            }
116            stack.clear();
117            currentSession = originalSession;
118            if (currentSession != null) {
119                refreshSession(currentSession);
120            }
121            stack.clear();
122        }
123    }
124
125    public static class Entry {
126        public LoginContext lc;
127
128        public CoreSession session;
129
130        public Entry(LoginContext lc) {
131            this(lc, null);
132        }
133
134        public Entry(LoginContext lc, CoreSession session) {
135            this.lc = lc;
136            this.session = session;
137        }
138
139        public final boolean hasSession() {
140            return session != null;
141        }
142
143        public final void dispose() throws OperationException {
144            try {
145                if (session != null) {
146                    try {
147                        session.save();
148                    } finally {
149                        session.close();
150                    }
151                }
152            } finally {
153                try {
154                    session = null;
155                    lc.logout();
156                    lc = null;
157                } catch (LoginException e) {
158                    throw new OperationException(e);
159                }
160            }
161        }
162    }
163
164}