001/*
002 * Licensed to the Apache Software Foundation (ASF) under one or more
003 * contributor license agreements.  See the NOTICE file distributed with
004 * this work for additional information regarding copyright ownership.
005 * The ASF licenses this file to You under the Apache License, Version 2.0
006 * (the "License"); you may not use this file except in compliance with
007 * the License.  You may obtain a copy of the License at
008 *
009 *      http://www.apache.org/licenses/LICENSE-2.0
010 *
011 * Unless required by applicable law or agreed to in writing, software
012 * distributed under the License is distributed on an "AS IS" BASIS,
013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014 * See the License for the specific language governing permissions and
015 * limitations under the License.
016 */
017package org.apache.juneau.microservice.tomcat;
018
019import org.apache.juneau.commons.inject.*;
020import org.apache.juneau.microservice.*;
021
022/**
023 * <c>@Configuration</c> class that wires embedded Tomcat into a {@link Microservice} bootstrap.
024 *
025 * <p>
026 * Add this class to {@link org.apache.juneau.microservice.Microservice.Builder#configurations(Class...) configurations(...)} to enable a
027 * Tomcat-backed microservice:
028 *
029 * <p class='bjava'>
030 *    Microservice
031 *       .<jsm>create</jsm>()
032 *       .args(<jv>args</jv>)
033 *       .configurations(TomcatConfiguration.<jk>class</jk>, MyAppConfig.<jk>class</jk>)
034 *       .build()
035 *       .start()
036 *       .startConsole()
037 *       .join();
038 * </p>
039 *
040 * <p>
041 * Contributes the following beans (each gated on {@code @ConditionalOnMissingBean} so user-supplied beans of the
042 * same type win):
043 * <ul>
044 *    <li>{@link TomcatServerFactory} — defaults to {@link BasicTomcatServerFactory}, used to build the
045 *       {@link org.apache.catalina.startup.Tomcat Tomcat} instance programmatically.
046 *    <li>{@link TomcatSettings} — empty defaults; user-supplied <c>@Bean TomcatSettings</c> can override ports
047 *       and the Catalina base directory.
048 *    <li>{@link TomcatServerComponent} (always contributed, also registered under {@link MicroserviceListener}) —
049 *       drives the Tomcat server lifecycle via the microservice's {@code start()} / {@code stop()} fan-out.
050 * </ul>
051 *
052 * <p>
053 * <c>@Bean Servlet</c> methods in user-supplied <c>@Configuration</c> classes are auto-mounted at
054 * {@link org.apache.juneau.rest.server.Rest#path()} during start-up.  See {@link TomcatServerComponent} for
055 * full details.
056 *
057 * <h5 class='section'>See Also:</h5><ul>
058 *    <li class='link'><a class="doclink" href="https://juneau.apache.org/docs/topics/JuneauMicroserviceTomcat">juneau-microservice-tomcat</a>
059 * </ul>
060 *
061 * @since 10.0.0
062 */
063@Configuration
064public class TomcatConfiguration {
065
066   private final TomcatServerComponent component = new TomcatServerComponent();
067
068   /**
069    * Constructor.
070    */
071   public TomcatConfiguration() { /* intentionally empty */ }
072
073   /**
074    * Contributes the default Tomcat server factory used to build the
075    * {@link org.apache.catalina.startup.Tomcat Tomcat} instance.
076    *
077    * @return The default factory.
078    */
079   @Bean
080   @ConditionalOnMissingBean(TomcatServerFactory.class)
081   public TomcatServerFactory tomcatServerFactory() {
082      return new BasicTomcatServerFactory();
083   }
084
085   /**
086    * Contributes an empty default {@link TomcatSettings} so {@link TomcatServerComponent} always finds one in the
087    * store.  Users who need programmatic overrides supply their own <c>@Bean TomcatSettings</c>.
088    *
089    * @return The default empty settings.
090    */
091   @Bean
092   @ConditionalOnMissingBean(TomcatSettings.class)
093   public TomcatSettings tomcatSettings() {
094      return TomcatSettings.create().build();
095   }
096
097   /**
098    * Contributes the {@link TomcatServerComponent} bean under its own type for direct lookup by external callers.
099    *
100    * @return The component instance.
101    */
102   @Bean
103   public TomcatServerComponent tomcatServerComponent() {
104      return component;
105   }
106
107   /**
108    * Contributes the same {@link TomcatServerComponent} instance under {@link MicroserviceListener} (named
109    * <c>tomcatServerListener</c>) so it participates in the {@link Microservice#start()} / {@link Microservice#stop()}
110    * fan-out without clobbering any user-supplied unnamed {@code MicroserviceListener}.
111    *
112    * @return The component instance.
113    */
114   @Bean(name = "tomcatServerListener")
115   public MicroserviceListener tomcatServerListener() {
116      return component;
117   }
118}