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