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 org.apache.juneau.internal.CollectionUtils.*;
016
017import java.util.*;
018import java.util.stream.*;
019
020import org.apache.juneau.cp.*;
021import org.springframework.context.*;
022
023/**
024 * A bean store that uses Spring bean resolution to find beans if they're not already in this store.
025 *
026 * <h5 class='section'>See Also:</h5><ul>
027 *    <li class='link'><a class="doclink" href="../../../../../index.html#juneau-rest-server-springboot">juneau-rest-server-springboot</a>
028 * </ul>
029 */
030public class SpringBeanStore extends BeanStore {
031
032   private final Optional<ApplicationContext> appContext;
033
034   /**
035    * Constructor.
036    *
037    * @param appContext The Spring application context used to resolve beans.
038    * @param parent The parent REST object bean store.  Can be <jk>null</jk>.
039    * @param resource The REST object.  Can be <jk>null</jk>.
040    */
041   public SpringBeanStore(Optional<ApplicationContext> appContext, Optional<BeanStore> parent, Object resource) {
042      super(create().parent(parent.orElse(null)).outer(resource));
043      this.appContext = appContext;
044   }
045
046   @Override
047   public <T> Optional<T> getBean(Class<T> c) {
048      try {
049         Optional<T> o = super.getBean(c);
050         if (o.isPresent())
051            return o;
052         if (appContext.isPresent()) {
053            return optional(appContext.get().getBeanProvider(c).getIfAvailable());
054         }
055      } catch (Exception e) {
056         e.printStackTrace();
057      }
058      return empty();
059   }
060
061   @Override
062   public <T> Optional<T> getBean(Class<T> c, String name) {
063      try {
064         Optional<T> o = super.getBean(c, name);
065         if (o.isPresent())
066            return o;
067         if (appContext.isPresent()) {
068            ApplicationContext ctx = appContext.get();
069            if (name != null)
070               return optional(ctx.containsBean(name) ? appContext.get().getBean(name, c) : null);
071            return optional(appContext.get().getBean(c));
072         }
073      } catch (Exception e) {
074         e.printStackTrace();
075      }
076      return empty();
077   }
078
079   @SuppressWarnings("unchecked")
080   @Override
081   public <T> Stream<BeanStoreEntry<T>> stream(Class<T> c)  {
082      try {
083         Stream<BeanStoreEntry<T>> o = super.stream(c);
084         if (appContext.isPresent())
085            o = Stream.concat(o, appContext.get().getBeansOfType(c).entrySet().stream().map(x -> BeanStoreEntry.create(c, ()->x.getValue(), x.getKey())));
086         return o;
087      } catch (Exception e) {
088         e.printStackTrace();
089      }
090      return Collections.emptyList().stream().map(x -> (BeanStoreEntry<T>)x);
091   }
092}