View Javadoc

1   package org.apache.helix.participant.statemachine;
2   
3   /*
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   *   http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing,
15   * software distributed under the License is distributed on an
16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17   * KIND, either express or implied.  See the License for the
18   * specific language governing permissions and limitations
19   * under the License.
20   */
21  
22  import java.util.Map;
23  import java.util.concurrent.ConcurrentHashMap;
24  import java.util.concurrent.ConcurrentMap;
25  
26  import org.apache.helix.messaging.handling.BatchMessageWrapper;
27  
28  public abstract class StateModelFactory<T extends StateModel>
29  {
30    // partitionName -> StateModel
31    private ConcurrentMap<String, T> _stateModelMap = new ConcurrentHashMap<String, T>();
32    
33    // resourceName -> BatchMessageWrapper
34    private final ConcurrentMap<String, BatchMessageWrapper> _batchMsgWrapperMap
35        = new ConcurrentHashMap<String, BatchMessageWrapper>();
36  
37    /**
38     * This method will be invoked only once per partitionName per session
39     * 
40     * @param partitionName
41     * @return
42     */
43    public abstract T createNewStateModel(String partitionName);
44  
45  //  /**
46  //   * Add a state model for a partition
47  //   * 
48  //   * @param partitionName
49  //   * @return
50  //   */
51  //  public void addStateModel(String partitionName, T stateModel)
52  //  {
53  //    _stateModelMap.put(partitionName, stateModel);
54  //  }
55    
56    /**
57     * Create a state model for a partition
58     * 
59     * @param partitionName
60     */
61    public T createAndAddStateModel(String partitionName)
62    {
63  		T stateModel = createNewStateModel(partitionName);
64  	    _stateModelMap.put(partitionName, stateModel);
65  	    return stateModel;
66    }
67  
68    /**
69     * Get the state model for a partition
70     * 
71     * @param partitionName
72     * @return
73     */
74    public T getStateModel(String partitionName)
75    {
76      return _stateModelMap.get(partitionName);
77    }
78  
79    /**
80     * Get the state model map
81     * 
82     * @return
83     */
84    public Map<String, T> getStateModelMap()
85    {
86      return _stateModelMap;
87    }
88    
89    /**
90     * create a default batch-message-wrapper for a resource
91     * 
92     * @param resourceName
93     * @return
94     */
95    public BatchMessageWrapper createBatchMessageWrapper(String resourceName)
96    {
97      return new BatchMessageWrapper();
98    }
99    
100   /**
101    * create a batch-message-wrapper for a resource and put it into map
102    * 
103    * @param resourceName
104    * @return
105    */
106   public BatchMessageWrapper createAndAddBatchMessageWrapper(String resourceName)
107   {
108     BatchMessageWrapper wrapper = createBatchMessageWrapper(resourceName);
109     _batchMsgWrapperMap.put(resourceName, wrapper);
110     return wrapper;
111   }
112   
113   /**
114    * get batch-message-wrapper for a resource
115    * 
116    * @param resourceName
117    * @return
118    */
119   public BatchMessageWrapper getBatchMessageWrapper(String resourceName)
120   {
121     return _batchMsgWrapperMap.get(resourceName);
122   }
123   
124 }