001/*
002 * Copyright (c) 2003-2010 The Regents of the University of California.
003 * All rights reserved.
004 *
005 * '$Author: tao $'
006 * '$Date: 2011-04-07 19:45:01 +0000 (Thu, 07 Apr 2011) $' 
007 * '$Revision: 27456 $'
008 * 
009 * Permission is hereby granted, without written agreement and without
010 * license or royalty fees, to use, copy, modify, and distribute this
011 * software and its documentation for any purpose, provided that the above
012 * copyright notice and the following two paragraphs appear in all copies
013 * of this software.
014 *
015 * IN NO EVENT SHALL THE UNIVERSITY OF CALIFORNIA BE LIABLE TO ANY PARTY
016 * FOR DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES
017 * ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN IF
018 * THE UNIVERSITY OF CALIFORNIA HAS BEEN ADVISED OF THE POSSIBILITY OF
019 * SUCH DAMAGE.
020 *
021 * THE UNIVERSITY OF CALIFORNIA SPECIFICALLY DISCLAIMS ANY WARRANTIES,
022 * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
023 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE SOFTWARE
024 * PROVIDED HEREUNDER IS ON AN "AS IS" BASIS, AND THE UNIVERSITY OF
025 * CALIFORNIA HAS NO OBLIGATION TO PROVIDE MAINTENANCE, SUPPORT, UPDATES,
026 * ENHANCEMENTS, OR MODIFICATIONS.
027 *
028 */
029
030package org.kepler.authentication;
031
032import java.util.Vector;
033
034/**
035 * ProxyRepository is used to store all of the proxies requested by the user.
036 * 
037 * @author Zhijie Guan guan@sdsc.edu
038 * 
039 */
040
041public class ProxyRepository {
042        Vector repository = new Vector();
043
044        /**
045         * This function gets the first available proxy and return it to the
046         * invoker.
047         * 
048         * @return The requested proxy, null if no such proxy exists.
049         */
050        synchronized ProxyEntity getDefaultProxy() {
051                if (repository.size() > 0) {
052                        return (ProxyEntity) repository.elementAt(0);
053                } else {
054                        return null;
055                }
056        }
057
058        /**
059         * This function returns the proxy with the specific index in the
060         * proxyRepository.
061         * 
062         * @param index
063         *            Proxy index in the proxyRepository
064         * @return The specified proxy
065         */
066        ProxyEntity getProxyAt(int index) {
067                if (index < repository.size()) {
068                        return (ProxyEntity) repository.elementAt(index);
069                } else {
070                        return null;
071                }
072        }
073
074        /**
075         * This function is used by LoginGUI to insert the new proxy into the
076         * proxyRepository
077         * 
078         * @param proxy
079         *            The new proxy
080         */
081        public synchronized void insertProxy(ProxyEntity proxy) {
082                repository.add(proxy);
083                notifyAll();
084        }
085
086        /**
087         * This function is used to search proxy with the specified user name and
088         * domain.
089         * 
090         * @param userName
091         *            The specified username
092         * @param domain
093         *            The specified domain
094         * @return Index of the proxy, -1 for not found
095         */
096        public synchronized ProxyEntity searchProxyInRepository(String userName,
097                        Domain domain) {
098                for (int i = 0; i < repository.size(); i++) {
099                        if ((((ProxyEntity) repository.elementAt(i)).getUserName() == userName)
100                                        && (((ProxyEntity) repository.elementAt(i)).getDomain()
101                                                        .equalTo(domain))) {
102                                return (ProxyEntity) repository.elementAt(i);
103                        }
104                }
105                return null;
106        }
107
108        /**
109         * This function is used to search proxy with the specified domain.
110         * 
111         * @param domain
112         *            The specified domain
113         * @return Index of the proxy, -1 for not found
114         */
115        public synchronized ProxyEntity searchProxyInRepository(Domain domain) {
116                for (int i = 0; i < repository.size(); i++) {
117                        if (((ProxyEntity) repository.elementAt(i)).getDomain().equalTo(
118                                        domain)) {
119                                return (ProxyEntity) repository.elementAt(i);
120                        }
121                }
122                return null;
123        }
124
125        public synchronized void removeProxy(ProxyEntity proxy) {
126                for (int i = 0; i < repository.size(); i++) {
127                        if (((ProxyEntity) repository.elementAt(i)).getDomain().equalTo(
128                                        proxy.getDomain())) {
129                                repository.remove(i);
130                                break;
131                        }
132                }
133        }
134        
135         /**
136   * Check if a credential with the given domain exists in this repository 
137   * @param proxyCredential the string of the credential
138   * @param domaim the given domain
139   * @return true if the credential with the given domain exists
140   */
141  public synchronized boolean proxyExists(String proxyCredential,
142                                                                                 Domain domain) {
143    boolean existing = false;
144    if(proxyCredential != null && domain != null){
145      for (int i = 0; i < repository.size(); i++) {
146        ProxyEntity proxy = (ProxyEntity) repository.elementAt(i);
147        if(proxy != null && proxy.getDomain() != null && 
148            proxy.getDomain().equals(domain) && proxy.getCredential() != null && 
149            proxy.getCredential().equals(proxyCredential)) {
150          existing = true;
151          break;
152        }      
153      }
154    }
155    return existing;
156  }
157
158
159}