Search in sources :

Example 1 with ValidationException

use of com.hazelcast.config.properties.ValidationException in project hazelcast by hazelcast.

the class DefaultDiscoveryService method buildDiscoveryStrategy.

private DiscoveryStrategy buildDiscoveryStrategy(DiscoveryStrategyConfig config, List<DiscoveryStrategyFactory> candidateFactories) {
    for (DiscoveryStrategyFactory factory : candidateFactories) {
        Class<? extends DiscoveryStrategy> discoveryStrategyType = factory.getDiscoveryStrategyType();
        String className = discoveryStrategyType.getName();
        String factoryClassName = getFactoryClassName(config);
        if (className.equals(factoryClassName)) {
            Map<String, Comparable> properties = DiscoveryServicePropertiesUtil.prepareProperties(config.getProperties(), nullToEmpty(factory.getConfigurationProperties()));
            return factory.newDiscoveryStrategy(discoveryNode, logger, properties);
        }
    }
    throw new ValidationException("There is no discovery strategy factory to create '" + config + "' Is it a typo in a strategy classname? " + "Perhaps you forgot to include implementation on a classpath?");
}
Also used : DiscoveryStrategyFactory(com.hazelcast.spi.discovery.DiscoveryStrategyFactory) ValidationException(com.hazelcast.config.properties.ValidationException)

Example 2 with ValidationException

use of com.hazelcast.config.properties.ValidationException in project hazelcast by hazelcast.

the class SPIAwareMemberGroupFactory method createInternalMemberGroups.

@Override
protected Set<MemberGroup> createInternalMemberGroups(Collection<? extends Member> allMembers) {
    Set<MemberGroup> memberGroups = new HashSet<MemberGroup>();
    for (Member member : allMembers) {
        try {
            if (member.localMember()) {
                DefaultDiscoveryService defaultDiscoveryService = (DefaultDiscoveryService) discoveryService;
                // If no discovery strategy is found fail-fast
                if (!defaultDiscoveryService.getDiscoveryStrategies().iterator().hasNext()) {
                    throw new RuntimeException("Could not load any Discovery Strategy, please " + "check service definitions under META_INF.services folder. ");
                } else {
                    for (DiscoveryStrategy discoveryStrategy : defaultDiscoveryService.getDiscoveryStrategies()) {
                        checkNotNull(discoveryStrategy.getPartitionGroupStrategy());
                        Iterable<MemberGroup> spiGroupsIterator = discoveryStrategy.getPartitionGroupStrategy().getMemberGroups();
                        for (MemberGroup group : spiGroupsIterator) {
                            memberGroups.add(group);
                        }
                        return memberGroups;
                    }
                }
            }
        } catch (Exception e) {
            if (e instanceof ValidationException) {
                throw new InvalidConfigurationException("Invalid configuration", e);
            } else {
                throw new RuntimeException("Failed to configure discovery strategies", e);
            }
        }
    }
    return memberGroups;
}
Also used : ValidationException(com.hazelcast.config.properties.ValidationException) DefaultDiscoveryService(com.hazelcast.spi.discovery.impl.DefaultDiscoveryService) Member(com.hazelcast.core.Member) InvalidConfigurationException(com.hazelcast.config.InvalidConfigurationException) ValidationException(com.hazelcast.config.properties.ValidationException) HashSet(java.util.HashSet) DiscoveryStrategy(com.hazelcast.spi.discovery.DiscoveryStrategy) InvalidConfigurationException(com.hazelcast.config.InvalidConfigurationException)

Example 3 with ValidationException

use of com.hazelcast.config.properties.ValidationException in project hazelcast by hazelcast.

the class DefaultDiscoveryService method loadDiscoveryStrategies.

private Iterable<DiscoveryStrategy> loadDiscoveryStrategies(DiscoveryServiceSettings settings) {
    ClassLoader configClassLoader = settings.getConfigClassLoader();
    try {
        Collection<DiscoveryStrategyConfig> discoveryStrategyConfigs = new ArrayList<DiscoveryStrategyConfig>(settings.getAllDiscoveryConfigs());
        List<DiscoveryStrategyFactory> factories = collectFactories(discoveryStrategyConfigs, configClassLoader);
        List<DiscoveryStrategy> discoveryStrategies = new ArrayList<DiscoveryStrategy>();
        for (DiscoveryStrategyConfig config : discoveryStrategyConfigs) {
            DiscoveryStrategy discoveryStrategy = buildDiscoveryStrategy(config, factories);
            discoveryStrategies.add(discoveryStrategy);
        }
        if (discoveryStrategies.isEmpty() && settings.isAutoDetectionEnabled()) {
            logger.fine("Discovery auto-detection enabled, looking for available discovery strategies");
            DiscoveryStrategyFactory autoDetectedFactory = detectDiscoveryStrategyFactory(factories);
            if (autoDetectedFactory != null) {
                logger.info(String.format("Auto-detection selected discovery strategy: %s", autoDetectedFactory.getClass()));
                discoveryStrategies.add(autoDetectedFactory.newDiscoveryStrategy(discoveryNode, logger, Collections.emptyMap()));
            } else {
                logger.fine("No discovery strategy is applicable for auto-detection");
            }
        }
        return discoveryStrategies;
    } catch (Exception e) {
        if (e instanceof ValidationException) {
            throw new InvalidConfigurationException("Invalid configuration", e);
        } else {
            throw new RuntimeException("Failed to configure discovery strategies", e);
        }
    }
}
Also used : DiscoveryStrategyFactory(com.hazelcast.spi.discovery.DiscoveryStrategyFactory) ValidationException(com.hazelcast.config.properties.ValidationException) DiscoveryStrategyConfig(com.hazelcast.config.DiscoveryStrategyConfig) ArrayList(java.util.ArrayList) InvalidConfigurationException(com.hazelcast.config.InvalidConfigurationException) ValidationException(com.hazelcast.config.properties.ValidationException) DiscoveryStrategy(com.hazelcast.spi.discovery.DiscoveryStrategy) InvalidConfigurationException(com.hazelcast.config.InvalidConfigurationException)

Example 4 with ValidationException

use of com.hazelcast.config.properties.ValidationException in project hazelcast by hazelcast.

the class SPIAwareMemberGroupFactory method createInternalMemberGroups.

@Override
protected Set<MemberGroup> createInternalMemberGroups(Collection<? extends Member> allMembers) {
    Set<MemberGroup> memberGroups = createHashSet(allMembers.size());
    for (Member member : allMembers) {
        try {
            if (member.localMember()) {
                DefaultDiscoveryService defaultDiscoveryService = (DefaultDiscoveryService) discoveryService;
                // If no discovery strategy is found fail-fast
                if (!defaultDiscoveryService.getDiscoveryStrategies().iterator().hasNext()) {
                    throw new RuntimeException("Could not load any Discovery Strategy, please " + "check service definitions under META_INF.services folder. ");
                } else {
                    for (DiscoveryStrategy discoveryStrategy : defaultDiscoveryService.getDiscoveryStrategies()) {
                        PartitionGroupStrategy groupStrategy = discoveryStrategy.getPartitionGroupStrategy(allMembers);
                        if (groupStrategy == null) {
                            groupStrategy = discoveryStrategy.getPartitionGroupStrategy();
                        }
                        checkNotNull(groupStrategy);
                        for (MemberGroup group : groupStrategy.getMemberGroups()) {
                            memberGroups.add(group);
                        }
                        return memberGroups;
                    }
                }
            }
        } catch (Exception e) {
            if (e instanceof ValidationException) {
                throw new InvalidConfigurationException("Invalid configuration", e);
            } else {
                throw new RuntimeException("Failed to configure discovery strategies", e);
            }
        }
    }
    return memberGroups;
}
Also used : MemberGroup(com.hazelcast.spi.partitiongroup.MemberGroup) ValidationException(com.hazelcast.config.properties.ValidationException) PartitionGroupStrategy(com.hazelcast.spi.partitiongroup.PartitionGroupStrategy) DefaultDiscoveryService(com.hazelcast.spi.discovery.impl.DefaultDiscoveryService) Member(com.hazelcast.cluster.Member) InvalidConfigurationException(com.hazelcast.config.InvalidConfigurationException) ValidationException(com.hazelcast.config.properties.ValidationException) DiscoveryStrategy(com.hazelcast.spi.discovery.DiscoveryStrategy) InvalidConfigurationException(com.hazelcast.config.InvalidConfigurationException)

Example 5 with ValidationException

use of com.hazelcast.config.properties.ValidationException in project hazelcast by hazelcast.

the class DiscoveryServicePropertiesUtilTest method invalidProperty.

@Test(expected = ValidationException.class)
public void invalidProperty() {
    // given
    Map<String, Comparable> properties = new HashMap<>(singletonMap(PROPERTY_KEY_1, (Comparable) PROPERTY_VALUE_1));
    ValueValidator<String> valueValidator = mock(ValueValidator.class);
    willThrow(new ValidationException("Invalid property")).given(valueValidator).validate(PROPERTY_VALUE_1);
    Collection<PropertyDefinition> propertyDefinitions = singletonList((PropertyDefinition) new SimplePropertyDefinition(PROPERTY_KEY_1, false, STRING, new DummyValidator()));
    // when
    prepareProperties(properties, propertyDefinitions);
// then
// throw exception
}
Also used : ValidationException(com.hazelcast.config.properties.ValidationException) SimplePropertyDefinition(com.hazelcast.config.properties.SimplePropertyDefinition) HashMap(java.util.HashMap) SimplePropertyDefinition(com.hazelcast.config.properties.SimplePropertyDefinition) PropertyDefinition(com.hazelcast.config.properties.PropertyDefinition) ParallelJVMTest(com.hazelcast.test.annotation.ParallelJVMTest) QuickTest(com.hazelcast.test.annotation.QuickTest) Test(org.junit.Test)

Aggregations

ValidationException (com.hazelcast.config.properties.ValidationException)5 InvalidConfigurationException (com.hazelcast.config.InvalidConfigurationException)3 DiscoveryStrategy (com.hazelcast.spi.discovery.DiscoveryStrategy)3 DiscoveryStrategyFactory (com.hazelcast.spi.discovery.DiscoveryStrategyFactory)2 DefaultDiscoveryService (com.hazelcast.spi.discovery.impl.DefaultDiscoveryService)2 Member (com.hazelcast.cluster.Member)1 DiscoveryStrategyConfig (com.hazelcast.config.DiscoveryStrategyConfig)1 PropertyDefinition (com.hazelcast.config.properties.PropertyDefinition)1 SimplePropertyDefinition (com.hazelcast.config.properties.SimplePropertyDefinition)1 Member (com.hazelcast.core.Member)1 MemberGroup (com.hazelcast.spi.partitiongroup.MemberGroup)1 PartitionGroupStrategy (com.hazelcast.spi.partitiongroup.PartitionGroupStrategy)1 ParallelJVMTest (com.hazelcast.test.annotation.ParallelJVMTest)1 QuickTest (com.hazelcast.test.annotation.QuickTest)1 ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)1 HashSet (java.util.HashSet)1 Test (org.junit.Test)1