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