All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.hazelcast.spi.discovery.AbstractDiscoveryStrategy Maven / Gradle / Ivy

There is a newer version: 62
Show newest version
/*
 * Copyright (c) 2008-2019, Hazelcast, Inc. All Rights Reserved.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.hazelcast.spi.discovery;

import com.hazelcast.config.properties.PropertyDefinition;
import com.hazelcast.logging.ILogger;
import com.hazelcast.spi.partitiongroup.PartitionGroupStrategy;
import com.hazelcast.util.StringUtil;

import java.util.Collections;
import java.util.Map;

import static java.util.Collections.unmodifiableMap;

/**
 * An common abstract superclass for {@link DiscoveryStrategy} implementations,
 * offering convenient access to configuration properties (which may be overridden
 * on the system's environment or JVM properties), as well as a {@link ILogger} instance.
 *
 * @since 3.6
 */
public abstract class AbstractDiscoveryStrategy implements DiscoveryStrategy {

    private final ILogger logger;
    private final Map properties;

    public AbstractDiscoveryStrategy(ILogger logger, Map properties) {
        this.logger = logger;
        this.properties = unmodifiableMap(properties);
    }

    @Override
    public void destroy() {
    }

    @Override
    public void start() {
    }

    @Override
    public PartitionGroupStrategy getPartitionGroupStrategy() {
        return null;
    }

    @Override
    public Map discoverLocalMetadata() {
        return Collections.emptyMap();
    }

    /**
     * Returns an immutable copy of the configuration properties.
     *
     * @return the configuration properties
     */
    protected Map getProperties() {
        return properties;
    }

    /**
     * Returns a {@link ILogger} instance bound to the current class.
     *
     * @return a ILogger instance
     */
    protected ILogger getLogger() {
        return logger;
    }

    /**
     * Returns the value of the requested {@link PropertyDefinition} if available in the
     * declarative or programmatic configuration (XML or Config API), otherwise it will
     * return null.
     * 

* This method overload won't do environment or JVM property lookup. A call to * this overload is equivalent to {@link #getOrNull(String, PropertyDefinition)} * with null passed as the first parameter. * * @param property the PropertyDefinition to lookup * @param the type of the property, must be compatible with the conversion * result of {@link PropertyDefinition#typeConverter()} * @return the value of the given property if available in the configuration, otherwise null */ protected T getOrNull(PropertyDefinition property) { return getOrDefault(property, null); } /** * Returns the value of the requested {@link PropertyDefinition} if available in the * declarative or programmatic configuration (XML or Config API), can be found in the * system's environment, or passed as a JVM property. Otherwise it will return null. *

* This overload will resolve the requested property in the following order, whereas the * higher priority is from top to bottom: *

    *
  • {@link System#getProperty(String)}: JVM properties
  • *
  • {@link System#getenv(String)}: System environment
  • *
  • Configuration properties of this {@link DiscoveryStrategy}
  • *
* To resolve JVM properties or the system environment the property's key is prefixed with * given prefix, therefore a prefix of com.hazelcast.discovery and a property * key of hostname will result in a property lookup of com.hazelcast.discovery.hostname * in the system environment and JVM properties. * * @param prefix the property key prefix for environment and JVM properties lookup * @param property the PropertyDefinition to lookup * @param the type of the property, must be compatible with the conversion * result of {@link PropertyDefinition#typeConverter()} * @return the value of the given property if available in the configuration, system environment * or JVM properties, otherwise null */ protected T getOrNull(String prefix, PropertyDefinition property) { return getOrDefault(prefix, property, null); } /** * Returns the value of the requested {@link PropertyDefinition} if available in the * declarative or programmatic configuration (XML or Config API), otherwise it will * return the given defaultValue. *

* This method overload won't do environment or JVM property lookup. A call to * this overload is equivalent to {@link #getOrDefault(String, PropertyDefinition, Comparable)} * with null passed as the first parameter. * * @param property the PropertyDefinition to lookup * @param the type of the property, must be compatible with the conversion * result of {@link PropertyDefinition#typeConverter()} * @return the value of the given property if available in the configuration, otherwise the * given default value */ protected T getOrDefault(PropertyDefinition property, T defaultValue) { return getOrDefault(null, property, defaultValue); } /** * Returns the value of the requested {@link PropertyDefinition} if available in the * declarative or programmatic configuration (XML or Config API), can be found in the * system's environment, or passed as a JVM property. otherwise it will return the given * defaultValue. *

* This overload will resolve the requested property in the following order, whereas the * higher priority is from top to bottom: *

    *
  • {@link System#getProperty(String)}: JVM properties
  • *
  • {@link System#getenv(String)}: System environment
  • *
  • Configuration properties of this {@link DiscoveryStrategy}
  • *
* To resolve JVM properties or the system environment the property's key is prefixed with * given prefix, therefore a prefix of com.hazelcast.discovery and a property * key of hostname will result in a property lookup of com.hazelcast.discovery.hostname * in the system environment and JVM properties. * * @param prefix the property key prefix for environment and JVM properties lookup * @param property the PropertyDefinition to lookup * @param the type of the property, must be compatible with the conversion * result of {@link PropertyDefinition#typeConverter()} * @return the value of the given property if available in the configuration, system environment * or JVM properties, otherwise the given default value */ protected T getOrDefault(String prefix, PropertyDefinition property, T defaultValue) { if (property == null) { return defaultValue; } Comparable value = readProperty(prefix, property); if (value == null) { value = properties.get(property.key()); } if (value == null) { return defaultValue; } return (T) value; } private Comparable readProperty(String prefix, PropertyDefinition property) { if (prefix != null) { String p = getProperty(prefix, property); String v = System.getProperty(p); if (StringUtil.isNullOrEmpty(v)) { v = System.getenv(p); } if (!StringUtil.isNullOrEmpty(v)) { return property.typeConverter().convert(v); } } return null; } private String getProperty(String prefix, PropertyDefinition property) { StringBuilder sb = new StringBuilder(prefix); if (prefix.charAt(prefix.length() - 1) != '.') { sb.append('.'); } return sb.append(property.key()).toString(); } }




© 2015 - 2024 Weber Informatics LLC | Privacy Policy