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.clustering.wadi;
018    
019    import java.net.URI;
020    
021    import org.apache.commons.logging.Log;
022    import org.apache.commons.logging.LogFactory;
023    import org.apache.geronimo.clustering.Node;
024    import org.apache.geronimo.gbean.GBeanInfo;
025    import org.apache.geronimo.gbean.GBeanInfoBuilder;
026    import org.apache.geronimo.gbean.GBeanLifecycle;
027    import org.apache.geronimo.j2ee.j2eeobjectnames.NameFactory;
028    import org.codehaus.wadi.group.Dispatcher;
029    import org.codehaus.wadi.group.MessageExchangeException;
030    import org.codehaus.wadi.tribes.TribesDispatcher;
031    import org.codehaus.wadi.web.impl.URIEndPoint;
032    
033    /**
034     *
035     * @version $Rev$ $Date$
036     */
037    public class TribesDispatcherHolder implements GBeanLifecycle, DispatcherHolder {
038        private static final Log log = LogFactory.getLog(TribesDispatcherHolder.class); 
039        
040        private final URI endPointURI;
041        private final String clusterName;
042        private final long inactiveTime;
043        private final Node node;
044    
045        private TribesDispatcher dispatcher;
046    
047    
048        public TribesDispatcherHolder(URI endPointURI, String clusterName, long inactiveTime, Node node) {
049            if (null == endPointURI) {
050                throw new IllegalArgumentException("endPointURI is required");
051            } else if (null == clusterName) {
052                throw new IllegalArgumentException("clusterName is required");
053            } else if (0 > inactiveTime) {
054                throw new IllegalArgumentException("inactiveTime must be > 0");
055            } else if (null == node) {
056                throw new IllegalArgumentException("node is required");
057            }
058            this.endPointURI = endPointURI;
059            this.clusterName = clusterName;
060            this.inactiveTime = inactiveTime;
061            this.node = node;
062        }
063    
064        public void doStart() throws Exception {
065            dispatcher = new TribesDispatcher(clusterName, node.getName(), new URIEndPoint(endPointURI), inactiveTime, null);
066            dispatcher.start();
067        }
068    
069        public void doStop() throws Exception {
070            dispatcher.stop();
071        }
072    
073        public void doFail() {
074            try {
075                dispatcher.stop();
076            } catch (MessageExchangeException e) {
077                log.error(e);
078            }
079        }
080        
081        public Dispatcher getDispatcher() {
082            return dispatcher;
083        }
084    
085        public Node getNode() {
086            return node;
087        }
088        
089        
090        public static final GBeanInfo GBEAN_INFO;
091        
092        public static final String GBEAN_ATTR_END_POINT_URI = "endPointURI";
093        public static final String GBEAN_ATTR_CLUSTER_NAME = "clusterName";
094        public static final String GBEAN_ATTR_CLUSTER_URI = "clusterUri";
095        public static final String GBEAN_ATTR_INACTIVE_TIME = "inactiveTime";
096    
097        public static final String GBEAN_REF_NODE = "Node";
098    
099        static {
100            GBeanInfoBuilder infoBuilder = GBeanInfoBuilder.createStatic(TribesDispatcherHolder.class, 
101                    NameFactory.GERONIMO_SERVICE);
102            
103            infoBuilder.addAttribute(GBEAN_ATTR_END_POINT_URI, URI.class, true);
104            infoBuilder.addAttribute(GBEAN_ATTR_CLUSTER_NAME, String.class, true);
105            infoBuilder.addAttribute(GBEAN_ATTR_INACTIVE_TIME, long.class, true);
106            
107            infoBuilder.addReference(GBEAN_REF_NODE, Node.class, NameFactory.GERONIMO_SERVICE);
108    
109            infoBuilder.addInterface(DispatcherHolder.class);
110            
111            infoBuilder.setConstructor(new String[] {
112                    GBEAN_ATTR_END_POINT_URI,
113                    GBEAN_ATTR_CLUSTER_NAME, 
114                    GBEAN_ATTR_INACTIVE_TIME, 
115                    GBEAN_REF_NODE });
116            
117            GBEAN_INFO = infoBuilder.getBeanInfo();
118        }
119    
120        public static GBeanInfo getGBeanInfo() {
121            return GBEAN_INFO;
122        }
123    }