f470c8e2df7b04d86d644c0eae19eddc4201f855
[policy/drools-pdp.git] /
1 /*
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
10  *
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  *
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=========================================================
19  */
20
21 package org.onap.policy.distributed.locking;
22
23 import java.util.Collections;
24 import java.util.HashSet;
25 import java.util.Properties;
26 import java.util.Set;
27 import lombok.Getter;
28 import lombok.Setter;
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;
32
33
34 @Getter
35 @Setter
36 public class DistributedLockProperties {
37     public static final String PREFIX = "distributed.locking.";
38
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";
47
48     /**
49      * Database driver.
50      */
51     @Property(name = DB_DRIVER)
52     private String dbDriver;
53
54     /**
55      * Database url.
56      */
57     @Property(name = DB_URL)
58     private String dbUrl;
59
60     /**
61      * Database user.
62      */
63     @Property(name = DB_USER)
64     private String dbUser;
65
66     /**
67      * Database password.
68      */
69     @Property(name = DB_PASS)
70     private String dbPwd;
71
72     /**
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.
76      */
77     @Property(name = TRANSIENT_ERROR_CODES)
78     private String errorCodeStrings;
79
80     private final Set<Integer> transientErrorCodes;
81
82     /**
83      * Time, in seconds, to wait between checks for expired locks.
84      */
85     @Property(name = EXPIRE_CHECK_SEC, defaultValue = "900")
86     private int expireCheckSec;
87
88     /**
89      * Number of seconds to wait before retrying, after a DB error.
90      */
91     @Property(name = RETRY_SEC, defaultValue = "60")
92     private int retrySec;
93
94     /**
95      * Maximum number of times to retry a DB operation.
96      */
97     @Property(name = MAX_RETRIES, defaultValue = "2")
98     private int maxRetries;
99
100     /**
101      * Constructs the object, populating fields from the properties.
102      *
103      * @param props properties from which to configure this
104      * @throws PropertyException if an error occurs
105      */
106     public DistributedLockProperties(Properties props) throws PropertyException {
107         new BeanConfigurator().configureFromProperties(this, props);
108
109         Set<Integer> set = new HashSet<>();
110         for (String text : errorCodeStrings.split(",")) {
111             text = text.trim();
112             if (text.isEmpty()) {
113                 continue;
114             }
115
116             try {
117                 set.add(Integer.valueOf(text));
118
119             } catch (NumberFormatException e) {
120                 throw new PropertyException(TRANSIENT_ERROR_CODES, "errorCodeStrings", e);
121             }
122         }
123
124         transientErrorCodes = Collections.unmodifiableSet(set);
125     }
126
127     /**
128      * Determines if an error is transient.
129      *
130      * @param errorCode error code to check
131      * @return {@code true} if the error is transient, {@code false} otherwise
132      */
133     public boolean isTransient(int errorCode) {
134         return transientErrorCodes.contains(errorCode);
135     }
136 }