001/* 002 * Copyright 2024-2025, Warm-Flow (290631660@qq.com). 003 * 004 * Licensed under the Apache License, Version 2.0 (the "License"); 005 * you may not use this file except in compliance with the License. 006 * You may obtain a copy of the License at 007 * 008 * https://www.apache.org/licenses/LICENSE-2.0 009 * 010 * Unless required by applicable law or agreed to in writing, software 011 * distributed under the License is distributed on an "AS IS" BASIS, 012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 013 * See the License for the specific language governing permissions and 014 * limitations under the License. 015 */ 016/* 017 * Copyright 2024-2025, Warm-Flow (290631660@qq.com). 018 * 019 * Licensed under the Apache License, Version 2.0 (the "License"); 020 * you may not use this file except in compliance with the License. 021 * You may obtain a copy of the License at 022 * 023 * https://www.apache.org/licenses/LICENSE-2.0 024 * 025 * Unless required by applicable law or agreed to in writing, software 026 * distributed under the License is distributed on an "AS IS" BASIS, 027 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 028 * See the License for the specific language governing permissions and 029 * limitations under the License. 030 */ 031package org.dromara.warm.flow.solon.jpa; 032 033 034import org.dromara.warm.flow.core.utils.StringUtils; 035import org.hibernate.cfg.AvailableSettings; 036import org.noear.solon.Solon; 037import org.noear.solon.core.BeanInjector; 038import org.noear.solon.core.BeanWrap; 039import org.noear.solon.core.Props; 040import org.noear.solon.core.VarHolder; 041 042import javax.persistence.EntityManagerFactory; 043import javax.persistence.Persistence; 044import javax.persistence.PersistenceContext; 045import javax.sql.DataSource; 046import java.util.Objects; 047import java.util.concurrent.ConcurrentHashMap; 048 049/** 050 * @author vanlin 051 * @className PersistenceContextBeanInjector 052 * @description 053 * @since 2024/6/28 10:07 054 */ 055public class PersistenceContextBeanInjector implements BeanInjector<PersistenceContext> { 056 private ConcurrentHashMap<String, EntityManagerFactory> ENTITY_MANAGER_FACTORIES = new ConcurrentHashMap<>(); 057 058 059 @Override 060 public void doInject(VarHolder varH, PersistenceContext anno) { 061 varH.context().getWrapAsync(DataSource.class, (dsBw) -> { 062 if (dsBw.raw() instanceof DataSource) { 063 inject0(anno, varH, dsBw); 064 } 065 }); 066 067 } 068 069 private void inject0(PersistenceContext anno, VarHolder varH, BeanWrap dsBw) { 070 String unitName = anno.unitName(); 071 if (StringUtils.isEmpty(unitName)) { 072 unitName = "default"; 073 } 074 075 final Props dsProps = Solon.cfg().getProp(unitName); 076 077 final String datasource = dsProps.get("datasource"); 078 if (!Objects.equals(dsBw.name(), datasource)) { 079 // 数据源不匹配不进行注入操作 080 return; 081 } 082 083 final Props properties = dsProps.getProp("properties"); 084 085 properties.put(AvailableSettings.DATASOURCE, dsBw.raw()); 086 087 final EntityManagerFactory entityManagerFactory = ENTITY_MANAGER_FACTORIES.computeIfAbsent(unitName, 088 key -> Persistence.createEntityManagerFactory(key, properties)); 089 090 varH.setValue(new EntityManagerProxy(entityManagerFactory.createEntityManager())); 091 } 092 093}