UI initial implementation.
[validation.git] / ui / src / main / java / org / akraino / validation / ui / config / AppInitializer.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");
5  * you may not use this file except in compliance with the License.
6  * You may obtain 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 implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 package org.akraino.validation.ui.config;
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 import javax.servlet.ServletContext;
26 import javax.servlet.ServletException;
27 import javax.servlet.ServletRegistration;
28
29 import org.springframework.web.WebApplicationInitializer;
30 import org.springframework.web.context.support.AnnotationConfigWebApplicationContext;
31 import org.springframework.web.servlet.DispatcherServlet;
32
33 public class AppInitializer implements WebApplicationInitializer {
34
35     private static final int QUEUE_CAPACITY = 500;
36     private static final int EXECUTOR_SIZE = 20; // the number of threads to keep in the pool, even if
37     // they are idle, unless allowCoreThreadTimeOut is
38     // set
39     private static final int EXECUTOR_MAX_SIZE = 20; // the maximum number of threads to allow in the pool
40     private static final int KEEPALIVE_TIME = 20; // when the number of threads is greater than the
41     // core, this is the maximum time that excess idle
42     // threads will wait for new tasks before
43     // terminating.
44     private static final PriorityBlockingQueue<Runnable> BLOCKING_QUEUE =
45             new PriorityBlockingQueue<Runnable>(QUEUE_CAPACITY, new CFRunnableComparator());
46     public static ExecutorService executorService = new ThreadPoolExecutor(EXECUTOR_SIZE,
47             EXECUTOR_MAX_SIZE, KEEPALIVE_TIME, TimeUnit.SECONDS, BLOCKING_QUEUE);
48
49     @Override
50     public void onStartup(ServletContext container) throws ServletException {
51
52         AnnotationConfigWebApplicationContext ctx = new AnnotationConfigWebApplicationContext();
53         ctx.register(AppConfig.class);
54         ctx.setServletContext(container);
55
56         ServletRegistration.Dynamic servlet = container.addServlet("dispatcher", new DispatcherServlet(ctx));
57
58         servlet.setLoadOnStartup(1);
59         servlet.addMapping("/");
60
61     }
62
63     private static class CFRunnableComparator implements Comparator<Runnable> {
64         @Override
65         @SuppressWarnings("unchecked")
66         public int compare(Runnable runnable1, Runnable runnable2) {
67             // T might be AsyncSupply, UniApply, etc., but we want to
68             // compare our original Runnables.
69             return ((Comparable) unwrap(runnable1)).compareTo(unwrap(runnable2));
70         }
71
72         private Object unwrap(Runnable runnable) {
73             try {
74                 Field field = runnable.getClass().getDeclaredField("fn");
75                 field.setAccessible(true);
76                 // NB: For performance-intensive contexts, you may want to
77                 // cache these in a ConcurrentHashMap<Class<?>, Field>.
78                 return field.get(runnable);
79             } catch (IllegalAccessException | NoSuchFieldException e) {
80                 throw new IllegalArgumentException("Couldn't unwrap " + runnable, e);
81             }
82         }
83     }
84
85 }