1 /*
2  * Copyright 2015-2020 the original author or authors.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      https://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16 package org.springframework.data.jpa.repository.support;
17
18 import java.util.List;
19 import java.util.Set;
20
21 import javax.persistence.EntityManager;
22 import javax.persistence.metamodel.ManagedType;
23
24 import org.springframework.data.jpa.repository.JpaContext;
25 import org.springframework.util.Assert;
26 import org.springframework.util.LinkedMultiValueMap;
27 import org.springframework.util.MultiValueMap;
28
29 /**
30  * Default implementation of {@link JpaContext}.
31  *
32  * @author Oliver Gierke
33  * @soundtrack Marcus Miller - Son Of Macbeth (Afrodeezia)
34  * @since 1.9
35  */

36 public class DefaultJpaContext implements JpaContext {
37
38     private final MultiValueMap<Class<?>, EntityManager> entityManagers;
39
40     /**
41      * Creates a new {@link DefaultJpaContext} for the given {@link Set} of {@link EntityManager}s.
42      *
43      * @param entityManagers must not be {@literal null}.
44      */

45     public DefaultJpaContext(Set<EntityManager> entityManagers) {
46
47         Assert.notNull(entityManagers, "EntityManagers must not be null!");
48         Assert.notEmpty(entityManagers, "EntityManagers must not be empty!");
49
50         this.entityManagers = new LinkedMultiValueMap<Class<?>, EntityManager>();
51
52         for (EntityManager em : entityManagers) {
53             for (ManagedType<?> managedType : em.getMetamodel().getManagedTypes()) {
54                 this.entityManagers.add(managedType.getJavaType(), em);
55             }
56         }
57     }
58
59     /*
60      * (non-Javadoc)
61      * @see org.springframework.data.jpa.repository.JpaContext#getByManagedType(java.lang.Class)
62      */

63     @Override
64     public EntityManager getEntityManagerByManagedType(Class<?> type) {
65
66         Assert.notNull(type, "Type must not be null!");
67
68         if (!entityManagers.containsKey(type)) {
69             throw new IllegalArgumentException(String.format("%s is not a managed type!", type));
70         }
71
72         List<EntityManager> candidates = this.entityManagers.get(type);
73
74         if (candidates.size() == 1) {
75             return candidates.get(0);
76         }
77
78         throw new IllegalArgumentException(
79                 String.format("%s managed by more than one EntityManagers: %s!", type.getName(), candidates));
80     }
81 }
82