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