First part of onap rename
[appc.git] / appc-dispatcher / appc-dispatcher-common / execution-queue-management-lib / src / main / java / org / openecomp / 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 import org.onap.appc.executionqueue.impl.QueueManager;
32
33 import java.util.concurrent.Executors;
34 import java.util.concurrent.ThreadFactory;
35 import java.util.concurrent.atomic.AtomicInteger;
36
37 public class Util {
38
39     private final EELFLogger logger = EELFManager.getInstance().getLogger(Util.class);
40     private final int default_queue_size = 10;
41     private final int default_threadpool_size = 10;
42     private final String queue_size_key = "appc.dispatcher.executionqueue.backlog.size";
43     private final String threadpool_size_key = "appc.dispatcher.executionqueue.threadpool.size";
44
45     private Configuration configuration;
46
47     /**
48      * Initialization.
49      * <p>Used by blueprint.
50      */
51     public void init() {
52
53         configuration = ConfigurationFactory.getConfiguration();
54     }
55
56     public int getExecutionQueueSize() {
57         String sizeStr = configuration.getProperty(queue_size_key, String.valueOf(default_queue_size));
58
59         int size = default_queue_size;
60         try {
61             size = Integer.parseInt(sizeStr);
62         } catch (NumberFormatException e) {
63             logger.error("Error while parse key:" + queue_size_key + " got from configuration " + e.getMessage(), e);
64         }
65
66         return size;
67     }
68
69     public int getThreadPoolSize() {
70         String sizeStr = configuration.getProperty(threadpool_size_key, String.valueOf(default_threadpool_size));
71
72         int size = default_threadpool_size;
73         try {
74             size = Integer.parseInt(sizeStr);
75         } catch (NumberFormatException e) {
76             logger.error("Error while parse key:" + threadpool_size_key + " got from configuration "
77                 + e.getMessage(), e);
78         }
79
80         return size;
81     }
82
83     public ThreadFactory getThreadFactory(final boolean isDaemon, final String threadNamePrefix) {
84         return new ThreadFactory() {
85             private final String THREAD_NAME_PATTERN = "%s-%d";
86             private final ThreadFactory factory = Executors.defaultThreadFactory();
87             private final AtomicInteger counter = new AtomicInteger();
88
89             public Thread newThread(Runnable r) {
90                 Thread t = factory.newThread(r);
91                 t.setDaemon(isDaemon);
92                 if (threadNamePrefix != null && !threadNamePrefix.isEmpty()) {
93                     final String threadName = String.format(THREAD_NAME_PATTERN, threadNamePrefix, counter
94                         .incrementAndGet());
95                     t.setName(threadName);
96                 }
97                 return t;
98             }
99         };
100     }
101 }