PageRenderTime 21ms CodeModel.GetById 11ms app.highlight 7ms RepoModel.GetById 1ms app.codeStats 0ms

/hazelcast/src/main/java/com/hazelcast/impl/partition/ConfigMemberGroupFactory.java

https://bitbucket.org/gabral6_gmailcom/hazelcast
Java | 59 lines | 37 code | 7 blank | 15 comment | 7 complexity | eddb88ecdf9cd74e744403cef59ddb31 MD5 | raw file
 1/*
 2 * Copyright (c) 2008-2013, Hazelcast, Inc. All Rights Reserved.
 3 *
 4 * Licensed under the Apache License, Version 2.0 (the "License");
 5 * you may not use this file except in compliance with the License.
 6 * You may obtain a copy of the License at
 7 *
 8 * http://www.apache.org/licenses/LICENSE-2.0
 9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package com.hazelcast.impl.partition;
18
19import com.hazelcast.config.MemberGroupConfig;
20import com.hazelcast.impl.MemberImpl;
21import com.hazelcast.util.AddressUtil;
22
23import java.util.*;
24import java.util.Map.Entry;
25
26public class ConfigMemberGroupFactory extends BackupSafeMemberGroupFactory implements MemberGroupFactory {
27
28    private final Map<Integer, MemberGroupConfig> memberGroupConfigMap;
29
30    public ConfigMemberGroupFactory(Collection<MemberGroupConfig> memberGroupConfigs) {
31        super();
32        this.memberGroupConfigMap = new LinkedHashMap<Integer, MemberGroupConfig>();
33        int key = 0;
34        for (MemberGroupConfig groupConfig : memberGroupConfigs) {
35            memberGroupConfigMap.put(key++, groupConfig);
36        }
37    }
38
39    protected Set<MemberGroup> createInternalMemberGroups(Collection<MemberImpl> members) {
40        final Map<Integer, MemberGroup> memberGroups = new HashMap<Integer, MemberGroup>();
41        for (MemberImpl member : members) {
42            if (member.isLiteMember()) {
43                continue;
44            }
45            for (Entry<Integer, MemberGroupConfig> groupConfigEntry : memberGroupConfigMap.entrySet()) {
46                if (AddressUtil.matchAnyInterface(member.getAddress().getHost(), groupConfigEntry.getValue().getInterfaces())) {
47                    MemberGroup group = memberGroups.get(groupConfigEntry.getKey());
48                    if (group == null) {
49                        group = new DefaultMemberGroup();
50                        memberGroups.put(groupConfigEntry.getKey(), group);
51                    }
52                    group.addMember(member);
53                    break;
54                }
55            }
56        }
57        return new HashSet<MemberGroup>(memberGroups.values());
58    }
59}