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.tomcat.cluster;
018    
019    import java.util.Map;
020    
021    import org.apache.geronimo.tomcat.BaseGBean;
022    import org.apache.geronimo.gbean.GBeanInfo;
023    import org.apache.geronimo.gbean.GBeanInfoBuilder;
024    import org.apache.commons.logging.Log;
025    import org.apache.commons.logging.LogFactory;
026    import org.apache.catalina.tribes.ChannelInterceptor;
027    import org.apache.catalina.tribes.group.interceptors.StaticMembershipInterceptor;
028    import org.apache.catalina.tribes.membership.StaticMember;
029    
030    public class ChannelInterceptorGBean extends BaseGBean {
031    
032        private static final Log log = LogFactory.getLog(SenderGBean.class);
033    
034        public static final String J2EE_TYPE = "ChannelInterceptor";
035    
036        private final ChannelInterceptor interceptor;
037        private final ChannelInterceptorGBean nextInterceptor;
038    
039        public ChannelInterceptorGBean() {
040            interceptor = null;
041            nextInterceptor = null;
042        }
043    
044        public ChannelInterceptorGBean(String className, Map initParams,
045           StaticMemberGBean staticMember, ChannelInterceptorGBean nextInterceptor) throws Exception {
046    
047            super(); // TODO: make it an attribute
048    
049            // Validate
050            if (className == null) {
051                throw new IllegalArgumentException("Must have a 'className' attribute.");
052            }
053    
054            if (nextInterceptor != null){
055                if (!(nextInterceptor.getInternalObject() instanceof ChannelInterceptor)){
056                    throw new IllegalArgumentException("nextInterceptor is not of type ChannelInterceptor.");                
057                }
058                
059                this.nextInterceptor = nextInterceptor;
060            } else {
061                this.nextInterceptor = null;
062            }
063    
064            // Create the ChannelInterceptor object
065            interceptor = (ChannelInterceptor) Class.forName(className).newInstance();
066    
067            // Set the parameters
068            setParameters(interceptor, initParams);
069    
070            //Add the static member
071            boolean addNextStaticMember = true;
072            
073            while (addNextStaticMember) {
074                if (staticMember != null && interceptor instanceof StaticMembershipInterceptor){
075                    StaticMembershipInterceptor staticMembershipInterceptor= (StaticMembershipInterceptor) interceptor;
076                    staticMembershipInterceptor.addStaticMember((StaticMember)staticMember.getInternalObject());
077                    if ( addNextStaticMember = (staticMember.getNextStaticMember() != null) ? true : false ) {
078                        staticMember = (StaticMemberGBean) staticMember.getNextStaticMember();
079                    }
080                } else {
081                    addNextStaticMember = false;
082                }
083            }
084    
085        }
086    
087        public Object getInternalObject() {
088            return interceptor;
089        }
090    
091        public void doFail() {
092            log.warn("Failed");
093        }
094    
095        public void doStart() throws Exception {
096            log.debug("Started channel interceptor gbean.");
097        }
098    
099        public void doStop() throws Exception {
100            log.debug("Stopped channel interceptor gbean.");
101        }
102        
103        public ChannelInterceptorGBean getNextInterceptor() {
104            return nextInterceptor;
105        }
106    
107        public static final GBeanInfo GBEAN_INFO;
108    
109        static {
110            GBeanInfoBuilder infoFactory = GBeanInfoBuilder.createStatic("ChannelInterceptor", ChannelInterceptorGBean.class, J2EE_TYPE);
111            infoFactory.addAttribute("className", String.class, true);
112            infoFactory.addAttribute("initParams", Map.class, true);
113            infoFactory.addReference("StaticMember", StaticMemberGBean.class, StaticMemberGBean.J2EE_TYPE);
114            infoFactory.addReference("NextInterceptor", ChannelInterceptorGBean.class, J2EE_TYPE);
115            infoFactory.addOperation("getInternalObject", "Object");
116            infoFactory.addOperation("getNextInterceptor","ChannelInterceptorGBean");
117            infoFactory.setConstructor(new String[] { 
118                    "className", 
119                    "initParams", 
120                    "StaticMember", 
121                    "NextInterceptor" });
122            
123            GBEAN_INFO = infoFactory.getBeanInfo();
124        }
125    
126        public static GBeanInfo getGBeanInfo() {
127            return GBEAN_INFO;
128        }
129    }