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    
028    public class ChannelInterceptorGBean extends BaseGBean {
029    
030        private static final Log log = LogFactory.getLog(SenderGBean.class);
031    
032        public static final String J2EE_TYPE = "ChannelInterceptor";
033    
034        private final ChannelInterceptor interceptor;
035        private final ChannelInterceptorGBean nextInterceptor;
036    
037        public ChannelInterceptorGBean() {
038            interceptor = null;
039            nextInterceptor = null;
040        }
041    
042        public ChannelInterceptorGBean(String className, Map initParams, ChannelInterceptorGBean nextInterceptor) throws Exception {
043    
044            super(); // TODO: make it an attribute
045    
046            // Validate
047            if (className == null) {
048                throw new IllegalArgumentException("Must have a 'className' attribute.");
049            }
050    
051            if (nextInterceptor != null){
052                if (!(nextInterceptor.getInternalObject() instanceof ChannelInterceptor)){
053                    throw new IllegalArgumentException("nextInterceptor is not of type ChannelInterceptor.");                
054                }
055                
056                this.nextInterceptor = nextInterceptor;
057            } else {
058                this.nextInterceptor = null;
059            }
060    
061            // Create the ChannelInterceptor object
062            interceptor = (ChannelInterceptor) Class.forName(className).newInstance();
063    
064            // Set the parameters
065            setParameters(interceptor, initParams);
066    
067        }
068    
069        public Object getInternalObject() {
070            return interceptor;
071        }
072    
073        public void doFail() {
074            log.warn("Failed");
075        }
076    
077        public void doStart() throws Exception {
078            log.debug("Started channel interceptor gbean.");
079        }
080    
081        public void doStop() throws Exception {
082            log.debug("Stopped channel interceptor gbean.");
083        }
084        
085        public ChannelInterceptorGBean getNextInterceptor() {
086            return nextInterceptor;
087        }
088    
089        public static final GBeanInfo GBEAN_INFO;
090    
091        static {
092            GBeanInfoBuilder infoFactory = GBeanInfoBuilder.createStatic("ChannelInterceptor", ChannelInterceptorGBean.class, J2EE_TYPE);
093            infoFactory.addAttribute("className", String.class, true);
094            infoFactory.addAttribute("initParams", Map.class, true);
095            infoFactory.addReference("NextInterceptor", ChannelInterceptorGBean.class, J2EE_TYPE);
096            infoFactory.addOperation("getInternalObject", "Object");
097            infoFactory.addOperation("getNextInterceptor","ChannelInterceptorGBean");
098            infoFactory.setConstructor(new String[] { 
099                    "className", 
100                    "initParams", 
101                    "NextInterceptor" });
102            
103            GBEAN_INFO = infoFactory.getBeanInfo();
104        }
105    
106        public static GBeanInfo getGBeanInfo() {
107            return GBEAN_INFO;
108        }
109    }