2 * ============LICENSE_START=======================================================
3 * feature-distributed-locking
4 * ================================================================================
5 * Copyright (C) 2018-2019 AT&T Intellectual Property. All rights reserved.
6 * ================================================================================
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
11 * http://www.apache.org/licenses/LICENSE-2.0
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
18 * ============LICENSE_END=========================================================
21 package org.onap.policy.distributed.locking;
23 import java.util.Collections;
24 import java.util.HashSet;
25 import java.util.Properties;
29 import org.onap.policy.common.utils.properties.BeanConfigurator;
30 import org.onap.policy.common.utils.properties.Property;
31 import org.onap.policy.common.utils.properties.exception.PropertyException;
36 public class DistributedLockProperties {
37 public static final String PREFIX = "distributed.locking.";
39 public static final String DB_DRIVER = "javax.persistence.jdbc.driver";
40 public static final String DB_URL = "javax.persistence.jdbc.url";
41 public static final String DB_USER = "javax.persistence.jdbc.user";
42 public static final String DB_PASS = "javax.persistence.jdbc.password";
43 public static final String TRANSIENT_ERROR_CODES = PREFIX + "transient.error.codes";
44 public static final String EXPIRE_CHECK_SEC = PREFIX + "expire.check.seconds";
45 public static final String RETRY_SEC = PREFIX + "retry.seconds";
46 public static final String MAX_RETRIES = PREFIX + "max.retries";
51 @Property(name = DB_DRIVER)
52 private String dbDriver;
57 @Property(name = DB_URL)
63 @Property(name = DB_USER)
64 private String dbUser;
69 @Property(name = DB_PASS)
73 * Vendor-specific error codes that are "transient", meaning they may go away if the
74 * command is repeated (e.g., connection issue), as opposed to something like a syntax
75 * error or a duplicate key.
77 @Property(name = TRANSIENT_ERROR_CODES)
78 private String errorCodeStrings;
80 private final Set<Integer> transientErrorCodes;
83 * Time, in seconds, to wait between checks for expired locks.
85 @Property(name = EXPIRE_CHECK_SEC, defaultValue = "900")
86 private int expireCheckSec;
89 * Number of seconds to wait before retrying, after a DB error.
91 @Property(name = RETRY_SEC, defaultValue = "60")
95 * Maximum number of times to retry a DB operation.
97 @Property(name = MAX_RETRIES, defaultValue = "2")
98 private int maxRetries;
101 * Constructs the object, populating fields from the properties.
103 * @param props properties from which to configure this
104 * @throws PropertyException if an error occurs
106 public DistributedLockProperties(Properties props) throws PropertyException {
107 new BeanConfigurator().configureFromProperties(this, props);
109 Set<Integer> set = new HashSet<>();
110 for (String text : errorCodeStrings.split(",")) {
112 if (text.isEmpty()) {
117 set.add(Integer.valueOf(text));
119 } catch (NumberFormatException e) {
120 throw new PropertyException(TRANSIENT_ERROR_CODES, "errorCodeStrings", e);
124 transientErrorCodes = Collections.unmodifiableSet(set);
128 * Determines if an error is transient.
130 * @param errorCode error code to check
131 * @return {@code true} if the error is transient, {@code false} otherwise
133 public boolean isTransient(int errorCode) {
134 return transientErrorCodes.contains(errorCode);