Search in sources :

Example 1 with ExpiryPolicyType

use of com.hazelcast.config.CacheSimpleConfig.ExpiryPolicyFactoryConfig.TimedExpiryPolicyFactoryConfig.ExpiryPolicyType in project hazelcast by hazelcast.

the class CacheConfig method initExpiryPolicyFactoryConfig.

private void initExpiryPolicyFactoryConfig(CacheSimpleConfig simpleConfig) throws Exception {
    CacheSimpleConfig.ExpiryPolicyFactoryConfig expiryPolicyFactoryConfig = simpleConfig.getExpiryPolicyFactoryConfig();
    if (expiryPolicyFactoryConfig != null) {
        if (expiryPolicyFactoryConfig.getClassName() != null) {
            setExpiryPolicyFactory(ClassLoaderUtil.newInstance(null, expiryPolicyFactoryConfig.getClassName()));
        } else {
            TimedExpiryPolicyFactoryConfig timedExpiryPolicyConfig = expiryPolicyFactoryConfig.getTimedExpiryPolicyFactoryConfig();
            if (timedExpiryPolicyConfig != null) {
                DurationConfig durationConfig = timedExpiryPolicyConfig.getDurationConfig();
                ExpiryPolicyType expiryPolicyType = timedExpiryPolicyConfig.getExpiryPolicyType();
                switch(expiryPolicyType) {
                    case CREATED:
                        setExpiryPolicyFactory(CreatedExpiryPolicy.factoryOf(new Duration(durationConfig.getTimeUnit(), durationConfig.getDurationAmount())));
                        break;
                    case MODIFIED:
                        setExpiryPolicyFactory(ModifiedExpiryPolicy.factoryOf(new Duration(durationConfig.getTimeUnit(), durationConfig.getDurationAmount())));
                        break;
                    case ACCESSED:
                        setExpiryPolicyFactory(AccessedExpiryPolicy.factoryOf(new Duration(durationConfig.getTimeUnit(), durationConfig.getDurationAmount())));
                        break;
                    case TOUCHED:
                        setExpiryPolicyFactory(TouchedExpiryPolicy.factoryOf(new Duration(durationConfig.getTimeUnit(), durationConfig.getDurationAmount())));
                        break;
                    case ETERNAL:
                        setExpiryPolicyFactory(EternalExpiryPolicy.factoryOf());
                        break;
                    default:
                        throw new IllegalArgumentException("Unsupported expiry policy type: " + expiryPolicyType);
                }
            }
        }
    }
}
Also used : TimedExpiryPolicyFactoryConfig(com.hazelcast.config.CacheSimpleConfig.ExpiryPolicyFactoryConfig.TimedExpiryPolicyFactoryConfig) DurationConfig(com.hazelcast.config.CacheSimpleConfig.ExpiryPolicyFactoryConfig.DurationConfig) Duration(javax.cache.expiry.Duration) ExpiryPolicyType(com.hazelcast.config.CacheSimpleConfig.ExpiryPolicyFactoryConfig.TimedExpiryPolicyFactoryConfig.ExpiryPolicyType)

Example 2 with ExpiryPolicyType

use of com.hazelcast.config.CacheSimpleConfig.ExpiryPolicyFactoryConfig.TimedExpiryPolicyFactoryConfig.ExpiryPolicyType in project hazelcast by hazelcast.

the class XmlConfigBuilder method getTimedExpiryPolicyFactoryConfig.

private TimedExpiryPolicyFactoryConfig getTimedExpiryPolicyFactoryConfig(Node node) {
    String expiryPolicyTypeStr = getAttribute(node, "expiry-policy-type");
    String durationAmountStr = getAttribute(node, "duration-amount");
    String timeUnitStr = getAttribute(node, "time-unit");
    ExpiryPolicyType expiryPolicyType = ExpiryPolicyType.valueOf(upperCaseInternal(expiryPolicyTypeStr));
    if (expiryPolicyType != ExpiryPolicyType.ETERNAL && (isNullOrEmpty(durationAmountStr) || isNullOrEmpty(timeUnitStr))) {
        throw new InvalidConfigurationException("Both of the \"duration-amount\" or \"time-unit\" attributes " + "are required for expiry policy factory configuration " + "(except \"ETERNAL\" expiry policy type)");
    }
    DurationConfig durationConfig = null;
    if (expiryPolicyType != ExpiryPolicyType.ETERNAL) {
        long durationAmount;
        try {
            durationAmount = parseLong(durationAmountStr);
        } catch (NumberFormatException e) {
            throw new InvalidConfigurationException("Invalid value for duration amount: " + durationAmountStr, e);
        }
        if (durationAmount <= 0) {
            throw new InvalidConfigurationException("Duration amount must be positive: " + durationAmount);
        }
        TimeUnit timeUnit;
        try {
            timeUnit = TimeUnit.valueOf(upperCaseInternal(timeUnitStr));
        } catch (IllegalArgumentException e) {
            throw new InvalidConfigurationException("Invalid value for time unit: " + timeUnitStr, e);
        }
        durationConfig = new DurationConfig(durationAmount, timeUnit);
    }
    return new TimedExpiryPolicyFactoryConfig(expiryPolicyType, durationConfig);
}
Also used : TimedExpiryPolicyFactoryConfig(com.hazelcast.config.CacheSimpleConfig.ExpiryPolicyFactoryConfig.TimedExpiryPolicyFactoryConfig) TimeUnit(java.util.concurrent.TimeUnit) DurationConfig(com.hazelcast.config.CacheSimpleConfig.ExpiryPolicyFactoryConfig.DurationConfig) ExpiryPolicyType(com.hazelcast.config.CacheSimpleConfig.ExpiryPolicyFactoryConfig.TimedExpiryPolicyFactoryConfig.ExpiryPolicyType)

Example 3 with ExpiryPolicyType

use of com.hazelcast.config.CacheSimpleConfig.ExpiryPolicyFactoryConfig.TimedExpiryPolicyFactoryConfig.ExpiryPolicyType in project hazelcast by hazelcast.

the class MemberDomConfigProcessor method getTimedExpiryPolicyFactoryConfig.

private CacheSimpleConfig.ExpiryPolicyFactoryConfig.TimedExpiryPolicyFactoryConfig getTimedExpiryPolicyFactoryConfig(Node node) {
    String expiryPolicyTypeStr = getAttribute(node, "expiry-policy-type");
    String durationAmountStr = getAttribute(node, "duration-amount");
    String timeUnitStr = getAttribute(node, "time-unit");
    ExpiryPolicyType expiryPolicyType = ExpiryPolicyType.valueOf(upperCaseInternal(expiryPolicyTypeStr));
    if (expiryPolicyType != ExpiryPolicyType.ETERNAL && (isNullOrEmpty(durationAmountStr) || isNullOrEmpty(timeUnitStr))) {
        throw new InvalidConfigurationException("Both of the \"duration-amount\" or \"time-unit\" attributes " + "are required for expiry policy factory configuration " + "(except \"ETERNAL\" expiry policy type)");
    }
    CacheSimpleConfig.ExpiryPolicyFactoryConfig.DurationConfig durationConfig = null;
    if (expiryPolicyType != ExpiryPolicyType.ETERNAL) {
        long durationAmount;
        try {
            durationAmount = parseLong(durationAmountStr);
        } catch (NumberFormatException e) {
            throw new InvalidConfigurationException("Invalid value for duration amount: " + durationAmountStr, e);
        }
        if (durationAmount <= 0) {
            throw new InvalidConfigurationException("Duration amount must be positive: " + durationAmount);
        }
        TimeUnit timeUnit;
        try {
            timeUnit = TimeUnit.valueOf(upperCaseInternal(timeUnitStr));
        } catch (IllegalArgumentException e) {
            throw new InvalidConfigurationException("Invalid value for time unit: " + timeUnitStr, e);
        }
        durationConfig = new CacheSimpleConfig.ExpiryPolicyFactoryConfig.DurationConfig(durationAmount, timeUnit);
    }
    return new CacheSimpleConfig.ExpiryPolicyFactoryConfig.TimedExpiryPolicyFactoryConfig(expiryPolicyType, durationConfig);
}
Also used : CacheSimpleConfig(com.hazelcast.config.CacheSimpleConfig) TimeUnit(java.util.concurrent.TimeUnit) ExpiryPolicyType(com.hazelcast.config.CacheSimpleConfig.ExpiryPolicyFactoryConfig.TimedExpiryPolicyFactoryConfig.ExpiryPolicyType) InvalidConfigurationException(com.hazelcast.config.InvalidConfigurationException)

Aggregations

ExpiryPolicyType (com.hazelcast.config.CacheSimpleConfig.ExpiryPolicyFactoryConfig.TimedExpiryPolicyFactoryConfig.ExpiryPolicyType)3 DurationConfig (com.hazelcast.config.CacheSimpleConfig.ExpiryPolicyFactoryConfig.DurationConfig)2 TimedExpiryPolicyFactoryConfig (com.hazelcast.config.CacheSimpleConfig.ExpiryPolicyFactoryConfig.TimedExpiryPolicyFactoryConfig)2 TimeUnit (java.util.concurrent.TimeUnit)2 CacheSimpleConfig (com.hazelcast.config.CacheSimpleConfig)1 InvalidConfigurationException (com.hazelcast.config.InvalidConfigurationException)1 Duration (javax.cache.expiry.Duration)1