001/*
002 * Copyright (c) 2006-2011 Nuxeo SA (http://nuxeo.com/) and others.
003 *
004 * All rights reserved. This program and the accompanying materials
005 * are made available under the terms of the Eclipse Public License v1.0
006 * which accompanies this distribution, and is available at
007 * http://www.eclipse.org/legal/epl-v10.html
008 *
009 * Contributors:
010 *     bstefanescu
011 *
012 * $Id$
013 */
014
015package org.nuxeo.ecm.core.api.local;
016
017import java.security.Principal;
018import java.util.LinkedList;
019
020import javax.security.auth.Subject;
021
022/**
023 * @author <a href="mailto:bs@nuxeo.com">Bogdan Stefanescu</a>
024 */
025public class LoginStack {
026
027    public static LoginStack synchronizedStack() {
028        return new Sync();
029    }
030
031    protected final LinkedList<Entry> stack = new LinkedList<Entry>();
032
033    public void clear() {
034        stack.clear();
035    }
036
037    public void push(Principal principal, Object credential, Subject subject) {
038        stack.add(new Entry(principal, credential, subject));
039    }
040
041    public Entry pop() {
042        if (stack.isEmpty()) {
043            return null;
044        }
045        return stack.removeLast();
046    }
047
048    public Entry peek() {
049        if (stack.isEmpty()) {
050            return null;
051        }
052        return stack.getLast();
053    }
054
055    public boolean isEmpty() {
056        return stack.isEmpty();
057    }
058
059    public int size() {
060        return stack.size();
061    }
062
063    public Entry get(int index) {
064        return stack.get(index);
065    }
066
067    public Entry remove(int index) {
068        return stack.remove(index);
069    }
070
071    public Entry[] toArray() {
072        return stack.toArray(new Entry[stack.size()]);
073    }
074
075    public static class Entry {
076        protected final Principal principal;
077
078        protected final Object credential;
079
080        protected final Subject subject;
081
082        public Entry(Principal principal, Object credential, Subject subject) {
083            this.principal = principal;
084            this.credential = credential;
085            this.subject = subject;
086        }
087
088        public Principal getPrincipal() {
089            return principal;
090        }
091
092        public Object getCredential() {
093            return credential;
094        }
095
096        public Subject getSubject() {
097            return subject;
098        }
099    }
100
101    public static class Sync extends LoginStack {
102
103        @Override
104        public synchronized void clear() {
105            stack.clear();
106        }
107
108        @Override
109        public synchronized void push(Principal principal, Object credential, Subject subject) {
110            stack.add(new Entry(principal, credential, subject));
111        }
112
113        @Override
114        public synchronized Entry pop() {
115            if (stack.isEmpty()) {
116                return null;
117            }
118            return stack.removeLast();
119        }
120
121        @Override
122        public synchronized Entry peek() {
123            if (stack.isEmpty()) {
124                return null;
125            }
126            return stack.getLast();
127        }
128
129        @Override
130        public synchronized boolean isEmpty() {
131            return stack.isEmpty();
132        }
133
134        @Override
135        public synchronized int size() {
136            return stack.size();
137        }
138
139        @Override
140        public synchronized Entry get(int index) {
141            return stack.get(index);
142        }
143
144        @Override
145        public synchronized Entry remove(int index) {
146            return stack.remove(index);
147        }
148
149        @Override
150        public synchronized Entry[] toArray() {
151            return stack.toArray(new Entry[stack.size()]);
152        }
153
154    }
155
156}