001// ***************************************************************************************************************************
002// * Licensed to the Apache Software Foundation (ASF) under one or more contributor license agreements.  See the NOTICE file *
003// * distributed with this work for additional information regarding copyright ownership.  The ASF licenses this file        *
004// * to you under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance            *
005// * with the License.  You may obtain a copy of the License at                                                              *
006// *                                                                                                                         *
007// *  http://www.apache.org/licenses/LICENSE-2.0                                                                             *
008// *                                                                                                                         *
009// * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an  *
010// * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  See the License for the        *
011// * specific language governing permissions and limitations under the License.                                              *
012// ***************************************************************************************************************************
013package org.apache.juneau.rest.springboot;
014
015import static java.lang.System.*;
016import org.apache.juneau.rest.springboot.annotation.*;
017import org.springframework.context.ApplicationContextInitializer;
018import org.springframework.context.ConfigurableApplicationContext;
019
020/**
021 * Spring Boot context initializer for Juneau REST resources.
022 *
023 * <p>
024 * Looks for the {@link JuneauRestRoot} annotation on the Spring application class to automatically register Juneau REST resources.
025 */
026public class JuneauRestInitializer implements ApplicationContextInitializer<ConfigurableApplicationContext> {
027
028   private final Class<?> appClass;
029
030   /**
031    * Constructor.
032    *
033    * @param appClass The Spring application class.
034    */
035   public JuneauRestInitializer(Class<?> appClass) {
036      this.appClass = appClass;
037   }
038
039   @Override /* ApplicationContextInitializer */
040   public void initialize(ConfigurableApplicationContext ctx) {
041      String port = ctx.getEnvironment().getProperty("server.port");
042      if (port != null && getProperty("juneau.serverPort") == null)
043         setProperty("juneau.serverPort", port);
044      ctx.addBeanFactoryPostProcessor(new JuneauRestPostProcessor(ctx, appClass));
045   }
046}