9ff3cb2067a41023b2af8a72d51f2e5d6b257d71
[aai/schema-service.git] / aai-schema-service / src / main / java / org / onap / aai / schemaservice / logging / LocalHostAccessLog.java
1 /**
2  * ============LICENSE_START=======================================================
3  * org.onap.aai
4  * ================================================================================
5  * Copyright © 2017-2018 AT&T Intellectual Property. All rights reserved.
6  * ================================================================================
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  *
11  *    http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  * ============LICENSE_END=========================================================
19  */
20 package org.onap.aai.schemaservice.logging;
21
22 import ch.qos.logback.access.jetty.RequestLogImpl;
23 import org.eclipse.jetty.server.handler.HandlerCollection;
24 import org.eclipse.jetty.server.handler.RequestLogHandler;
25 import org.eclipse.jetty.util.thread.QueuedThreadPool;
26 import org.springframework.beans.factory.annotation.Value;
27 import org.springframework.boot.web.embedded.jetty.JettyServerCustomizer;
28 import org.springframework.boot.web.embedded.jetty.JettyServletWebServerFactory;
29 import org.springframework.boot.web.servlet.server.AbstractServletWebServerFactory;
30 import org.springframework.context.annotation.Bean;
31 import org.springframework.context.annotation.Configuration;
32
33 import java.util.Arrays;
34
35 @Configuration
36 public class LocalHostAccessLog {
37
38     @Bean
39     public AbstractServletWebServerFactory jettyConfigBean(
40         @Value("${jetty.threadPool.maxThreads:200}") final String maxThreads,
41         @Value("${jetty.threadPool.minThreads:8}") final String minThreads
42     ) {
43
44         JettyServletWebServerFactory jef = new JettyServletWebServerFactory();
45         jef.addServerCustomizers((JettyServerCustomizer) server -> {
46
47             HandlerCollection handlers = new HandlerCollection();
48
49             Arrays.stream(server.getHandlers()).forEach(handlers::addHandler);
50
51             RequestLogHandler requestLogHandler = new RequestLogHandler();
52             requestLogHandler.setServer(server);
53
54             RequestLogImpl requestLogImpl = new RequestLogImpl();
55             requestLogImpl.setResource("/localhost-access-logback.xml");
56             requestLogImpl.start();
57
58             requestLogHandler.setRequestLog(requestLogImpl);
59             handlers.addHandler(requestLogHandler);
60             server.setHandler(handlers);
61
62             final QueuedThreadPool threadPool = server.getBean(QueuedThreadPool.class);
63             threadPool.setMaxThreads(Integer.valueOf(maxThreads));
64             threadPool.setMinThreads(Integer.valueOf(minThreads));
65         });
66         return jef;
67     }
68 }