Update license header in appc-dispatcher files
[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-2018 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  * ============LICENSE_END=========================================================
22  */
23
24 package org.onap.appc.executionqueue.helper;
25
26 import org.onap.appc.configuration.Configuration;
27 import org.onap.appc.configuration.ConfigurationFactory;
28
29 import java.util.concurrent.Executors;
30 import java.util.concurrent.ThreadFactory;
31 import java.util.concurrent.atomic.AtomicInteger;
32
33 public class Util {
34
35     private int default_queue_size = 10;
36     private int default_threadpool_size = 10;
37     private String queue_size_key = "appc.dispatcher.executionqueue.backlog.size";
38     private String threadpool_size_key = "appc.dispatcher.executionqueue.threadpool.size";
39
40     private Configuration configuration;
41
42     /**
43      * Initialization.
44      * <p>Used by blueprint.
45      */
46     public void init() {
47         configuration = ConfigurationFactory.getConfiguration();
48     }
49
50     public int getExecutionQueueSize() {
51         String sizeStr = configuration.getProperty(queue_size_key, String.valueOf(default_queue_size));
52
53         int size = default_queue_size;
54         try {
55             size = Integer.parseInt(sizeStr);
56         } catch (NumberFormatException e) {
57
58         }
59
60         return size;
61     }
62
63     public int getThreadPoolSize() {
64         String sizeStr = configuration.getProperty(threadpool_size_key, String.valueOf(default_threadpool_size));
65
66         int size = default_threadpool_size;
67         try {
68             size = Integer.parseInt(sizeStr);
69         } catch (NumberFormatException e) {
70
71         }
72
73         return size;
74     }
75
76     public ThreadFactory getThreadFactory(final boolean isDaemon, final String threadNamePrefix) {
77         return new ThreadFactory() {
78             private final String THREAD_NAME_PATTERN = "%s-%d";
79             private final ThreadFactory factory = Executors.defaultThreadFactory();
80             private final AtomicInteger counter = new AtomicInteger();
81
82             public Thread newThread(Runnable r) {
83                 Thread t = factory.newThread(r);
84                 t.setDaemon(isDaemon);
85                 if (threadNamePrefix != null && !threadNamePrefix.isEmpty()) {
86                     final String threadName = String.format(THREAD_NAME_PATTERN, threadNamePrefix, counter.incrementAndGet());
87                     t.setName(threadName);
88                 }
89                 return t;
90             }
91         };
92     }
93 }