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 org.apache.geronimo.gbean.GBeanInfo;
020    import org.apache.geronimo.gbean.GBeanInfoBuilder;
021    import org.apache.geronimo.gbean.GBeanLifecycle;
022    import org.apache.geronimo.j2ee.j2eeobjectnames.NameFactory;
023    import org.codehaus.wadi.replication.strategy.BackingStrategy;
024    import org.codehaus.wadi.replication.strategy.BackingStrategyFactory;
025    import org.codehaus.wadi.replication.strategy.RoundRobinBackingStrategyFactory;
026    
027    /**
028     * 
029     * @version $Rev$ $Date$
030     */
031    public class RoundRobinBackingStrategyFactoryGBean implements BackingStrategyFactory, GBeanLifecycle {
032        private final int nbReplica;
033        
034        private BackingStrategyFactory strategyFactory;
035        
036        public RoundRobinBackingStrategyFactoryGBean(int nbReplica) {
037            this.nbReplica = nbReplica;
038        }
039    
040        public BackingStrategy factory() {
041            return strategyFactory.factory();
042        }
043    
044        public void doFail() {
045            strategyFactory = null;
046        }
047    
048        public void doStart() throws Exception {
049            strategyFactory = new RoundRobinBackingStrategyFactory(nbReplica);
050        }
051    
052        public void doStop() throws Exception {
053            strategyFactory = null;
054        }
055    
056        
057        public static final GBeanInfo GBEAN_INFO;
058        
059        public static final String GBEAN_ATTR_NB_REPLICA = "nbReplica";
060        
061        static {
062            GBeanInfoBuilder infoBuilder = GBeanInfoBuilder.createStatic(RoundRobinBackingStrategyFactoryGBean.class, 
063                    NameFactory.GERONIMO_SERVICE);
064            
065            infoBuilder.addAttribute(GBEAN_ATTR_NB_REPLICA, int.class, true);
066            
067            infoBuilder.addInterface(BackingStrategyFactory.class);
068    
069            infoBuilder.setConstructor(new String[] {GBEAN_ATTR_NB_REPLICA});
070            
071            GBEAN_INFO = infoBuilder.getBeanInfo();
072        }
073        
074        public static GBeanInfo getGBeanInfo() {
075            return GBEAN_INFO;
076        }
077    }