001    /**
002     *
003     * Copyright 2003-2004 The Apache Software Foundation
004     *
005     *  Licensed under the Apache License, Version 2.0 (the "License");
006     *  you may not use this file except in compliance with the License.
007     *  You may obtain a copy of the License at
008     *
009     *     http://www.apache.org/licenses/LICENSE-2.0
010     *
011     *  Unless required by applicable law or agreed to in writing, software
012     *  distributed under the License is distributed on an "AS IS" BASIS,
013     *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014     *  See the License for the specific language governing permissions and
015     *  limitations under the License.
016     */
017    package org.apache.geronimo.security.jaas;
018    
019    import java.io.Externalizable;
020    import java.io.Serializable;
021    import java.rmi.Remote;
022    import java.util.HashMap;
023    import java.util.HashSet;
024    import java.util.Iterator;
025    import java.util.Map;
026    import java.util.Set;
027    import javax.security.auth.Subject;
028    
029    import org.apache.geronimo.security.jaas.server.JaasLoginModuleConfiguration;
030    
031    
032    /**
033     * Helper class the computes the login result across a number of separate
034     * login modules.
035     *
036     * @version $Rev: 355877 $ $Date: 2005-12-10 18:48:27 -0800 (Sat, 10 Dec 2005) $
037     */
038    public class LoginUtils {
039        public static void copyPrincipals(Subject to, Subject from) {
040            to.getPrincipals().addAll(from.getPrincipals());
041        }
042    
043        public static Map getSerializableCopy(Map from) {
044            Map to = new HashMap();
045            for (Iterator it = from.keySet().iterator(); it.hasNext();) {
046                String key = (String) it.next();
047                Object value = from.get(key);
048                if (value instanceof Serializable || value instanceof Externalizable || value instanceof Remote) {
049                    to.put(key, value);
050                }
051            }
052            return to;
053        }
054    
055        public static Set getSerializableCopy(Set from) {
056            Set to = new HashSet();
057            for (Iterator it = from.iterator(); it.hasNext();) {
058                Object value = it.next();
059                if (value instanceof Serializable || value instanceof Externalizable || value instanceof Remote) {
060                    to.add(value);
061                }
062            }
063            return to;
064        }
065    
066        /**
067         * Strips out stuff that isn't serializable so this can be safely passed to
068         * a remote server.
069         */
070        public static JaasLoginModuleConfiguration getSerializableCopy(JaasLoginModuleConfiguration config) {
071            return new JaasLoginModuleConfiguration(config.getLoginModuleClassName(),
072                    config.getFlag(),
073                    LoginUtils.getSerializableCopy(config.getOptions()),
074                    config.isServerSide(),
075                    config.getLoginDomainName(),
076                    config.isWrapPrincipals(),
077                    null);
078        }
079    }