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    import java.util.Collections;
021    
022    import org.apache.commons.logging.Log;
023    import org.apache.commons.logging.LogFactory;
024    import org.apache.geronimo.clustering.LocalNode;
025    import org.apache.geronimo.clustering.Node;
026    import org.apache.geronimo.gbean.GBeanInfo;
027    import org.apache.geronimo.gbean.GBeanInfoBuilder;
028    import org.apache.geronimo.gbean.GBeanLifecycle;
029    import org.apache.geronimo.j2ee.j2eeobjectnames.NameFactory;
030    import org.codehaus.wadi.group.Dispatcher;
031    import org.codehaus.wadi.group.DispatcherRegistry;
032    import org.codehaus.wadi.group.MessageExchangeException;
033    import org.codehaus.wadi.group.StaticDispatcherRegistry;
034    import org.codehaus.wadi.servicespace.admin.AdminServiceSpace;
035    import org.codehaus.wadi.tribes.TribesDispatcher;
036    import org.codehaus.wadi.web.impl.URIEndPoint;
037    
038    /**
039     *
040     * @version $Rev$ $Date$
041     */
042    public class TribesDispatcherHolder implements GBeanLifecycle, DispatcherHolder {
043        private static final Log log = LogFactory.getLog(TribesDispatcherHolder.class); 
044        
045        private final URI endPointURI;
046        private final String clusterName;
047        private final LocalNode node;
048        private final DispatcherRegistry dispatcherRegistry;
049    
050        private TribesDispatcher dispatcher;
051        private AdminServiceSpace adminServiceSpace;
052    
053        public TribesDispatcherHolder(URI endPointURI, String clusterName, LocalNode node) {
054            if (null == endPointURI) {
055                throw new IllegalArgumentException("endPointURI is required");
056            } else if (null == clusterName) {
057                throw new IllegalArgumentException("clusterName is required");
058            } else if (null == node) {
059                throw new IllegalArgumentException("node is required");
060            }
061            this.endPointURI = endPointURI;
062            this.clusterName = clusterName;
063            this.node = node;
064            
065            dispatcherRegistry = new StaticDispatcherRegistry();
066        }
067    
068        public void doStart() throws Exception {
069            dispatcher = new TribesDispatcher(clusterName,
070                node.getName(),
071                new URIEndPoint(endPointURI),
072                Collections.EMPTY_SET);
073            dispatcher.start();
074            
075            adminServiceSpace = new AdminServiceSpace(dispatcher);
076            
077            registerCustomAdminServices();
078            
079            adminServiceSpace.start();
080            
081            dispatcherRegistry.register(dispatcher);
082        }
083    
084        public void doStop() throws Exception {
085            adminServiceSpace.stop();
086            dispatcherRegistry.unregister(dispatcher);
087            dispatcher.stop();
088        }
089    
090        public void doFail() {
091            if (null != adminServiceSpace) {
092                try {
093                    adminServiceSpace.stop();
094                } catch (Exception e) {
095                    log.error("see nested", e);
096                }
097            }
098            
099            if (null != dispatcher) {
100                dispatcherRegistry.unregister(dispatcher);
101                try {
102                    dispatcher.stop();
103                } catch (MessageExchangeException e) {
104                    log.error("see nested", e);
105                }
106            }
107        }
108        
109        public Dispatcher getDispatcher() {
110            return dispatcher;
111        }
112    
113        public Node getNode() {
114            return node;
115        }
116        
117        protected void registerCustomAdminServices() {
118            NodeServiceHelper nodeServiceHelper = new NodeServiceHelper(adminServiceSpace);
119            nodeServiceHelper.registerNodeService(new BasicNodeService(node));
120        }
121        
122        public static final GBeanInfo GBEAN_INFO;
123        
124        public static final String GBEAN_ATTR_END_POINT_URI = "endPointURI";
125        public static final String GBEAN_ATTR_CLUSTER_NAME = "clusterName";
126        public static final String GBEAN_ATTR_CLUSTER_URI = "clusterUri";
127    
128        public static final String GBEAN_REF_NODE = "Node";
129    
130        static {
131            GBeanInfoBuilder infoBuilder = GBeanInfoBuilder.createStatic(TribesDispatcherHolder.class, 
132                    NameFactory.GERONIMO_SERVICE);
133            
134            infoBuilder.addAttribute(GBEAN_ATTR_END_POINT_URI, URI.class, true);
135            infoBuilder.addAttribute(GBEAN_ATTR_CLUSTER_NAME, String.class, true);
136            
137            infoBuilder.addReference(GBEAN_REF_NODE, LocalNode.class, NameFactory.GERONIMO_SERVICE);
138    
139            infoBuilder.addInterface(DispatcherHolder.class);
140            
141            infoBuilder.setConstructor(new String[] {
142                    GBEAN_ATTR_END_POINT_URI,
143                    GBEAN_ATTR_CLUSTER_NAME,
144                    GBEAN_REF_NODE });
145            
146            GBEAN_INFO = infoBuilder.getBeanInfo();
147        }
148    
149        public static GBeanInfo getGBeanInfo() {
150            return GBEAN_INFO;
151        }
152    }