001/** 002 * Copyright (C) 2006-2020 Talend Inc. - www.talend.com 003 * 004 * Licensed under the Apache License, Version 2.0 (the "License"); 005 * you may not use this file except in compliance with the License. 006 * You may obtain a copy of the License at 007 * 008 * http://www.apache.org/licenses/LICENSE-2.0 009 * 010 * Unless required by applicable law or agreed to in writing, software 011 * distributed under the License is distributed on an "AS IS" BASIS, 012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 013 * See the License for the specific language governing permissions and 014 * limitations under the License. 015 */ 016package org.talend.sdk.component.server.tomcat; 017 018import java.util.stream.Stream; 019 020import org.apache.catalina.Lifecycle; 021import org.apache.catalina.Server; 022import org.apache.catalina.Service; 023import org.apache.catalina.core.StandardHost; 024import org.apache.catalina.startup.Tomcat; 025import org.apache.catalina.valves.ErrorReportValve; 026import org.apache.meecrowave.Meecrowave; 027 028public class TomcatSetup implements Meecrowave.InstanceCustomizer { 029 030 @Override 031 public void accept(final Tomcat tomcat) { 032 final Server server = tomcat.getServer(); 033 server.addLifecycleListener(event -> { 034 if (Server.class.isInstance(event.getData()) && Lifecycle.AFTER_DESTROY_EVENT.equals(event.getType()) 035 && Boolean.getBoolean("talend.component.exit-on-destroy")) { 036 System.exit(0); 037 } 038 }); 039 // if we want it to be really configurable we should add it in ComponentServerConfiguration 040 // and set this instance in the standard context to be able to configure it from cdi side 041 final boolean dev = Boolean.getBoolean("talend.component.server.tomcat.valve.error.debug"); 042 if (!dev) { 043 Stream 044 .of(server.findServices()) 045 .map(Service::getContainer) 046 .flatMap(e -> Stream.of(e.findChildren())) 047 .filter(StandardHost.class::isInstance) 048 .map(StandardHost.class::cast) 049 .forEach(host -> host.addLifecycleListener(event -> { 050 if (event.getType().equals(Lifecycle.BEFORE_START_EVENT)) { 051 StandardHost.class 052 .cast(host) 053 .setErrorReportValveClass(MinimalErrorReportValve.class.getName()); 054 } 055 })); 056 } 057 } 058 059 public static class MinimalErrorReportValve extends ErrorReportValve { 060 061 public MinimalErrorReportValve() { 062 setShowReport(false); 063 setShowServerInfo(false); 064 } 065 } 066}