View Javadoc

1   /**
2    *
3    *  Licensed to the Apache Software Foundation (ASF) under one or more
4    *  contributor license agreements.  See the NOTICE file distributed with
5    *  this work for additional information regarding copyright ownership.
6    *  The ASF licenses this file to You under the Apache License, Version 2.0
7    *  (the "License"); you may not use this file except in compliance with
8    *  the License.  You may obtain a copy of the License at
9    *
10   *     http://www.apache.org/licenses/LICENSE-2.0
11   *
12   *  Unless required by applicable law or agreed to in writing, software
13   *  distributed under the License is distributed on an "AS IS" BASIS,
14   *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15   *  See the License for the specific language governing permissions and
16   *  limitations under the License.
17   */
18  package org.apache.geronimo.clustering.wadi;
19  
20  import org.apache.geronimo.gbean.GBeanInfo;
21  import org.apache.geronimo.gbean.GBeanInfoBuilder;
22  import org.apache.geronimo.gbean.GBeanLifecycle;
23  import org.apache.geronimo.j2ee.j2eeobjectnames.NameFactory;
24  import org.codehaus.wadi.replication.strategy.BackingStrategy;
25  import org.codehaus.wadi.replication.strategy.BackingStrategyFactory;
26  import org.codehaus.wadi.replication.strategy.RoundRobinBackingStrategyFactory;
27  
28  /**
29   * 
30   * @version $Rev$ $Date$
31   */
32  public class RoundRobinBackingStrategyFactoryGBean implements BackingStrategyFactory, GBeanLifecycle {
33      private final int nbReplica;
34      
35      private BackingStrategyFactory strategyFactory;
36      
37      public RoundRobinBackingStrategyFactoryGBean(int nbReplica) {
38          this.nbReplica = nbReplica;
39      }
40  
41      public BackingStrategy factory() {
42          return strategyFactory.factory();
43      }
44  
45      public void doFail() {
46          strategyFactory = null;
47      }
48  
49      public void doStart() throws Exception {
50          strategyFactory = new RoundRobinBackingStrategyFactory(nbReplica);
51      }
52  
53      public void doStop() throws Exception {
54          strategyFactory = null;
55      }
56  
57      
58      public static final GBeanInfo GBEAN_INFO;
59      
60      public static final String GBEAN_ATTR_NB_REPLICA = "nbReplica";
61      
62      static {
63          GBeanInfoBuilder infoBuilder = GBeanInfoBuilder.createStatic(RoundRobinBackingStrategyFactoryGBean.class, 
64                  NameFactory.GERONIMO_SERVICE);
65          
66          infoBuilder.addAttribute(GBEAN_ATTR_NB_REPLICA, int.class, true);
67          
68          infoBuilder.addInterface(BackingStrategyFactory.class);
69  
70          infoBuilder.setConstructor(new String[] {GBEAN_ATTR_NB_REPLICA});
71          
72          GBEAN_INFO = infoBuilder.getBeanInfo();
73      }
74      
75      public static GBeanInfo getGBeanInfo() {
76          return GBEAN_INFO;
77      }
78  }