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.catalina.tribes.Channel;
022    import org.apache.catalina.tribes.ChannelInterceptor;
023    import org.apache.catalina.tribes.ChannelReceiver;
024    import org.apache.catalina.tribes.ChannelSender;
025    import org.apache.catalina.tribes.MembershipService;
026    import org.apache.catalina.tribes.group.GroupChannel;
027    import org.apache.commons.logging.Log;
028    import org.apache.commons.logging.LogFactory;
029    import org.apache.geronimo.gbean.GBeanInfo;
030    import org.apache.geronimo.gbean.GBeanInfoBuilder;
031    import org.apache.geronimo.gbean.GBeanLifecycle;
032    import org.apache.geronimo.tomcat.BaseGBean;
033    import org.apache.geronimo.tomcat.ObjectRetriever;
034    
035    /**
036    * @version $Rev: 511136 $ $Date: 2007-02-23 17:12:09 -0500 (Fri, 23 Feb 2007) $
037    */
038    public class ChannelGBean extends BaseGBean implements GBeanLifecycle, ObjectRetriever {
039    
040       private static final Log log = LogFactory.getLog(ChannelGBean.class);
041       
042       public static final String J2EE_TYPE = "Channel";
043       
044       private final Channel channel;
045       
046       public ChannelGBean(){
047           channel=null;
048       }
049    
050       public ChannelGBean(String className, 
051               Map initParams,            
052               MembershipServiceGBean membership,
053               ReceiverGBean receiver,
054               SenderGBean sender,
055               ChannelInterceptorGBean interceptorChain) throws Exception {
056           
057           super(); // TODO: make it an attribute
058           
059           //Validate
060           if (className == null){
061               throw new IllegalArgumentException("Must have a 'className' attribute.");
062           }
063           
064           //Create the Channel object
065           channel = (Channel)Class.forName(className).newInstance();
066           
067           //Set the parameters
068           setParameters(channel, initParams);
069           
070           // if the channel is a GroupChannel then add the sender, receiver, and membership service
071           if (channel instanceof GroupChannel) {
072               GroupChannel groupChannel = (GroupChannel)channel;
073               //Add the MembershipService
074               if (membership != null){
075                   groupChannel.setMembershipService((MembershipService)membership.getInternalObject());
076               }
077               
078               //Add Receiver
079               if (receiver != null){
080                   groupChannel.setChannelReceiver((ChannelReceiver)receiver.getInternalObject());
081               }
082               
083               //Add Sender
084               if (sender != null){
085                   groupChannel.setChannelSender((ChannelSender)sender.getInternalObject());
086               }
087           } else {
088               log.warn(className + " is not an instance of GroupChannel. Did not set Receiver, Sender, or MembershipService");
089           }
090           
091           
092           //Add the interceptros
093           if (interceptorChain != null){
094               ChannelInterceptorGBean channelInterceptorGBean = interceptorChain;
095               while(channelInterceptorGBean != null){
096                   channel.addInterceptor((ChannelInterceptor)channelInterceptorGBean.getInternalObject());
097                   channelInterceptorGBean = channelInterceptorGBean.getNextInterceptor();
098               }
099           }
100    
101       }
102    
103       public Object getInternalObject() {
104           return channel;
105       }
106    
107       public void doFail() {
108           log.warn("Failed");
109       }
110    
111       public void doStart() throws Exception {
112           log.debug("Started channel gbean.");
113       }
114    
115       public void doStop() throws Exception {
116           log.debug("Stopped channel gbean.");
117       }
118    
119       public static final GBeanInfo GBEAN_INFO;
120    
121       static {
122           GBeanInfoBuilder infoFactory = GBeanInfoBuilder.createStatic("Channel", ChannelGBean.class, J2EE_TYPE);
123           infoFactory.addAttribute("className", String.class, true);
124           infoFactory.addAttribute("initParams", Map.class, true);
125           infoFactory.addReference("Membership", MembershipServiceGBean.class, MembershipServiceGBean.J2EE_TYPE);
126           infoFactory.addReference("Receiver", ReceiverGBean.class, ReceiverGBean.J2EE_TYPE);
127           infoFactory.addReference("Sender", SenderGBean.class, SenderGBean.J2EE_TYPE);
128           infoFactory.addReference("ChannelInterceptor", ChannelInterceptorGBean.class, ChannelInterceptorGBean.J2EE_TYPE);
129           infoFactory.addOperation("getInternalObject", "Object");
130           infoFactory.setConstructor(new String[] { 
131                   "className", 
132                   "initParams", 
133                   "Membership", 
134                   "Receiver",
135                   "Sender",
136                   "ChannelInterceptor"});
137           GBEAN_INFO = infoFactory.getBeanInfo();
138       }
139    
140       public static GBeanInfo getGBeanInfo() {
141           return GBEAN_INFO;
142       }
143    }