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.jetty;
018
019import org.apache.juneau.commons.inject.*;
020import org.apache.juneau.microservice.*;
021
022/**
023 * <c>@Configuration</c> class that wires Jetty 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 * Jetty-backed microservice:
028 *
029 * <p class='bjava'>
030 *    Microservice
031 *       .<jsm>create</jsm>()
032 *       .args(<jv>args</jv>)
033 *       .configurations(JettyConfiguration.<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 JettyServerFactory} — defaults to {@link BasicJettyServerFactory}, used to build the Jetty
045 *       {@link org.eclipse.jetty.server.Server Server} from <c>jetty.xml</c>.
046 *    <li>{@link JettySettings} — empty defaults; user-supplied <c>@Bean JettySettings</c> can override ports,
047 *       <c>jetty.xml</c> contents, and var-resolution.
048 *    <li>{@link JettyServerComponent} (always contributed, also registered under {@link MicroserviceListener}) —
049 *       drives the Jetty 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 JettyServerComponent} 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/JuneauMicroserviceJetty">juneau-microservice-jetty</a>
059 * </ul>
060 *
061 * @since 10.0.0
062 */
063@Configuration
064public class JettyConfiguration {
065
066   private final JettyServerComponent component = new JettyServerComponent();
067
068   /**
069    * Contributes the default Jetty server factory used to build the {@link org.eclipse.jetty.server.Server Server}
070    * from a <c>jetty.xml</c> file.
071    *
072    * @return The default factory.
073    */
074   @Bean
075   @ConditionalOnMissingBean(JettyServerFactory.class)
076   public JettyServerFactory jettyServerFactory() {
077      return new BasicJettyServerFactory();
078   }
079
080   /**
081    * Contributes an empty default {@link JettySettings} so {@link JettyServerComponent} always finds one in the
082    * store.  Users who need programmatic overrides supply their own <c>@Bean JettySettings</c>.
083    *
084    * @return The default empty settings.
085    */
086   @Bean
087   @ConditionalOnMissingBean(JettySettings.class)
088   public JettySettings jettySettings() {
089      return JettySettings.create().build();
090   }
091
092   /**
093    * Contributes the {@link JettyServerComponent} bean under its own type for direct lookup by external callers.
094    *
095    * @return The component instance.
096    */
097   @Bean
098   public JettyServerComponent jettyServerComponent() {
099      return component;
100   }
101
102   /**
103    * Contributes the same {@link JettyServerComponent} instance under {@link MicroserviceListener} (named
104    * <c>jettyServerListener</c>) so it participates in the {@link Microservice#start()} / {@link Microservice#stop()}
105    * fan-out without clobbering any user-supplied unnamed {@code MicroserviceListener}.
106    *
107    * @return The component instance.
108    */
109   @Bean(name = "jettyServerListener")
110   public MicroserviceListener jettyServerListener() {
111      return component;
112   }
113}