Merge "[docker] Add sonobuoy multiarch image build"
[validation.git] / ui / src / main / java / org / akraino / validation / ui / conf / UiUtils.java
1 /*
2  * Copyright (c) 2019 AT&T Intellectual Property. All rights reserved.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License"); you may
5  * not use this file except in compliance with the License. You may obtain
6  * a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
13  * implied. See the License for the specific language governing
14  * permissions and limitations under the License.
15  */
16 package org.akraino.validation.ui.conf;
17
18 import java.lang.reflect.Field;
19 import java.util.Comparator;
20 import java.util.concurrent.ExecutorService;
21 import java.util.concurrent.PriorityBlockingQueue;
22 import java.util.concurrent.ThreadPoolExecutor;
23 import java.util.concurrent.TimeUnit;
24
25 public class UiUtils {
26
27     private static final int QUEUE_CAPACITY = 500;
28     private static final int EXECUTOR_SIZE = 20; // the number of threads to keep in the pool, even if
29     // they are idle, unless allowCoreThreadTimeOut is
30     // set
31     private static final int EXECUTOR_MAX_SIZE = 20; // the maximum number of threads to allow in the pool
32     private static final int KEEPALIVE_TIME = 20; // when the number of threads is greater than the
33     // core, this is the maximum time that excess idle
34     // threads will wait for new tasks before
35     // terminating.
36     private static final PriorityBlockingQueue<Runnable> BLOCKING_QUEUE =
37             new PriorityBlockingQueue<Runnable>(QUEUE_CAPACITY, new CFRunnableComparator());
38     public static ExecutorService executorService =
39             new ThreadPoolExecutor(EXECUTOR_SIZE, EXECUTOR_MAX_SIZE, KEEPALIVE_TIME, TimeUnit.SECONDS, BLOCKING_QUEUE);
40
41     public static final String NEXUS_URL = "https://nexus.akraino.org/content/sites/logs";
42
43     private static class CFRunnableComparator implements Comparator<Runnable> {
44         @Override
45         @SuppressWarnings("unchecked")
46         public int compare(Runnable runnable1, Runnable runnable2) {
47             return ((Comparable) unwrap(runnable1)).compareTo(unwrap(runnable2));
48         }
49
50         private Object unwrap(Runnable runnable) {
51             try {
52                 Field field = runnable.getClass().getDeclaredField("fn");
53                 field.setAccessible(true);
54                 return field.get(runnable);
55             } catch (IllegalAccessException | NoSuchFieldException e) {
56                 throw new IllegalArgumentException("Couldn't unwrap " + runnable, e);
57             }
58         }
59     }
60
61 }