Fix sonar issues for APPC
[appc.git] / appc-dispatcher / appc-dispatcher-common / execution-queue-management-lib / src / main / java / org / onap / appc / executionqueue / helper / Util.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP : APPC
4  * ================================================================================
5  * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
6  * ================================================================================
7  * Copyright (C) 2017 Amdocs
8  * =============================================================================
9  * Licensed under the Apache License, Version 2.0 (the "License");
10  * you may not use this file except in compliance with the License.
11  * You may obtain a copy of the License at
12  * 
13  *      http://www.apache.org/licenses/LICENSE-2.0
14  * 
15  * Unless required by applicable law or agreed to in writing, software
16  * distributed under the License is distributed on an "AS IS" BASIS,
17  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18  * See the License for the specific language governing permissions and
19  * limitations under the License.
20  * 
21  * ECOMP is a trademark and service mark of AT&T Intellectual Property.
22  * ============LICENSE_END=========================================================
23  */
24
25 package org.onap.appc.executionqueue.helper;
26
27 import com.att.eelf.configuration.EELFLogger;
28 import com.att.eelf.configuration.EELFManager;
29 import org.onap.appc.configuration.Configuration;
30 import org.onap.appc.configuration.ConfigurationFactory;
31
32 import java.util.concurrent.Executors;
33 import java.util.concurrent.ThreadFactory;
34 import java.util.concurrent.atomic.AtomicInteger;
35
36 public class Util {
37
38     private final EELFLogger logger = EELFManager.getInstance().getLogger(Util.class);
39     private final int default_queue_size = 10;
40     private final int default_threadpool_size = 10;
41     private final String queue_size_key = "appc.dispatcher.executionqueue.backlog.size";
42     private final String threadpool_size_key = "appc.dispatcher.executionqueue.threadpool.size";
43
44     private Configuration configuration;
45
46     /**
47      * Initialization.
48      * <p>Used by blueprint.
49      */
50     public void init() {
51
52         configuration = ConfigurationFactory.getConfiguration();
53     }
54
55     public int getExecutionQueueSize() {
56         String sizeStr = configuration.getProperty(queue_size_key, String.valueOf(default_queue_size));
57
58         int size = default_queue_size;
59         try {
60             size = Integer.parseInt(sizeStr);
61         } catch (NumberFormatException e) {
62             logger.error("Error while parse key:" + queue_size_key + " got from configuration " + e.getMessage(), e);
63         }
64
65         return size;
66     }
67
68     public int getThreadPoolSize() {
69         String sizeStr = configuration.getProperty(threadpool_size_key, String.valueOf(default_threadpool_size));
70
71         int size = default_threadpool_size;
72         try {
73             size = Integer.parseInt(sizeStr);
74         } catch (NumberFormatException e) {
75             logger.error("Error while parse key:" + threadpool_size_key + " got from configuration "
76                 + e.getMessage(), e);
77         }
78
79         return size;
80     }
81
82     public ThreadFactory getThreadFactory(final boolean isDaemon, final String threadNamePrefix) {
83         return new ThreadFactory() {
84             private final String THREAD_NAME_PATTERN = "%s-%d";
85             private final ThreadFactory factory = Executors.defaultThreadFactory();
86             private final AtomicInteger counter = new AtomicInteger();
87
88             public Thread newThread(Runnable r) {
89                 Thread t = factory.newThread(r);
90                 t.setDaemon(isDaemon);
91                 if (threadNamePrefix != null && !threadNamePrefix.isEmpty()) {
92                     final String threadName = String.format(THREAD_NAME_PATTERN, threadNamePrefix, counter
93                         .incrementAndGet());
94                     t.setName(threadName);
95                 }
96                 return t;
97             }
98         };
99     }
100 }