001    /**
002     * Licensed to the Apache Software Foundation (ASF) under one or more
003     * contributor license agreements.  See the NOTICE file distributed with
004     * this work for additional information regarding copyright ownership.
005     * The ASF licenses this file to You under the Apache License, Version 2.0
006     * (the "License"); you may not use this file except in compliance with
007     * the License.  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.corba.security.config.tss;
018    
019    import java.io.Serializable;
020    import java.util.ArrayList;
021    import java.util.Iterator;
022    
023    import javax.security.auth.Subject;
024    
025    import org.omg.CORBA.Any;
026    import org.omg.CORBA.ORB;
027    import org.omg.CSI.EstablishContext;
028    import org.omg.CSIIOP.CompoundSecMech;
029    import org.omg.CSIIOP.CompoundSecMechList;
030    import org.omg.CSIIOP.CompoundSecMechListHelper;
031    import org.omg.CSIIOP.TAG_CSI_SEC_MECH_LIST;
032    import org.omg.IOP.Codec;
033    import org.omg.IOP.TaggedComponent;
034    
035    import org.apache.geronimo.corba.security.SASException;
036    
037    
038    /**
039     * @version $Rev: 503274 $ $Date: 2007-02-03 10:19:18 -0800 (Sat, 03 Feb 2007) $
040     */
041    public class TSSCompoundSecMechListConfig implements Serializable {
042    
043        private boolean stateful;
044        private final ArrayList mechs = new ArrayList();
045    
046        public boolean isStateful() {
047            return stateful;
048        }
049    
050        public void setStateful(boolean stateful) {
051            this.stateful = stateful;
052        }
053    
054        public void add(TSSCompoundSecMechConfig mech) {
055            mechs.add(mech);
056        }
057    
058        public TSSCompoundSecMechConfig mechAt(int i) {
059            return (TSSCompoundSecMechConfig) mechs.get(i);
060        }
061    
062        public int size() {
063            return mechs.size();
064        }
065    
066        public TaggedComponent encodeIOR(ORB orb, Codec codec) throws Exception {
067            CompoundSecMechList csml = new CompoundSecMechList();
068    
069            csml.stateful = stateful;
070            csml.mechanism_list = new CompoundSecMech[mechs.size()];
071    
072            for (int i = 0; i < mechs.size(); i++) {
073                csml.mechanism_list[i] = ((TSSCompoundSecMechConfig) mechs.get(i)).encodeIOR(orb, codec);
074            }
075    
076            Any any = orb.create_any();
077            CompoundSecMechListHelper.insert(any, csml);
078    
079            return new TaggedComponent(TAG_CSI_SEC_MECH_LIST.value, codec.encode_value(any));
080        }
081    
082        public static TSSCompoundSecMechListConfig decodeIOR(Codec codec, TaggedComponent taggedComponent) throws Exception {
083            TSSCompoundSecMechListConfig result = new TSSCompoundSecMechListConfig();
084    
085            Any any = codec.decode_value(taggedComponent.component_data, CompoundSecMechListHelper.type());
086            CompoundSecMechList csml = CompoundSecMechListHelper.extract(any);
087    
088            result.setStateful(csml.stateful);
089    
090            for (int i = 0; i < csml.mechanism_list.length; i++) {
091                result.add(TSSCompoundSecMechConfig.decodeIOR(codec, csml.mechanism_list[i]));
092            }
093    
094            return result;
095        }
096    
097        public Subject check(EstablishContext msg) throws SASException {
098            Subject result = null;
099    
100            for (int i = 0; i < mechs.size(); i++) {
101                result = ((TSSCompoundSecMechConfig) mechs.get(i)).check(msg);
102                if (result != null) break;
103            }
104    
105            return result;
106        }
107        public String toString() {
108            StringBuffer buf = new StringBuffer();
109            toString("", buf);
110            return buf.toString();
111        }
112    
113        void toString(String spaces, StringBuffer buf) {
114            buf.append(spaces).append("TSSCompoundSecMechListConfig: [\n");
115            for (Iterator availMechs = mechs.iterator(); availMechs.hasNext();) {
116                TSSCompoundSecMechConfig aConfig = (TSSCompoundSecMechConfig) availMechs.next();
117                aConfig.toString(spaces + "  ", buf);
118                buf.append("\n");
119            }
120            buf.append(spaces).append("]\n");
121        }
122    
123    }