-
Notifications
You must be signed in to change notification settings - Fork 2.5k
/
PersistenceUnitUtil.java
97 lines (85 loc) · 4.71 KB
/
PersistenceUnitUtil.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
package io.quarkus.hibernate.orm.runtime;
import java.util.Comparator;
import java.util.Locale;
import javax.enterprise.inject.Default;
import org.jboss.logging.Logger;
import io.quarkus.arc.Arc;
import io.quarkus.arc.InjectableInstance;
import io.quarkus.hibernate.orm.PersistenceUnit;
import io.quarkus.hibernate.orm.PersistenceUnitExtension;
public class PersistenceUnitUtil {
private static final Logger LOG = Logger.getLogger(PersistenceUnitUtil.class);
public static final String DEFAULT_PERSISTENCE_UNIT_NAME = "<default>";
public static boolean isDefaultPersistenceUnit(String name) {
return DEFAULT_PERSISTENCE_UNIT_NAME.equals(name);
}
public static <T> InjectableInstance<T> singleExtensionInstanceForPersistenceUnit(Class<T> beanType,
String persistenceUnitName) {
InjectableInstance<T> instance = extensionInstanceForPersistenceUnit(beanType, persistenceUnitName);
if (instance.isAmbiguous()) {
throw new IllegalStateException(String.format(Locale.ROOT,
"Multiple instances of %1$s were found for persistence unit %2$s. "
+ "At most one instance can be assigned to each persistence unit.",
beanType.getSimpleName(), persistenceUnitName));
}
return instance;
}
public static <T> InjectableInstance<T> extensionInstanceForPersistenceUnit(Class<T> beanType, String persistenceUnitName) {
return Arc.container().select(beanType,
new PersistenceUnitExtension.Literal(persistenceUnitName));
}
public static class PersistenceUnitNameComparator implements Comparator<String> {
@Override
public int compare(String o1, String o2) {
if (DEFAULT_PERSISTENCE_UNIT_NAME.equals(o1)) {
return -1;
} else if (DEFAULT_PERSISTENCE_UNIT_NAME.equals(o2)) {
return +1;
} else {
return o1.compareTo(o2);
}
}
}
@Deprecated
public static <T> InjectableInstance<T> legacySingleExtensionInstanceForPersistenceUnit(Class<T> beanType,
String persistenceUnitName) {
InjectableInstance<T> instance = singleExtensionInstanceForPersistenceUnit(
beanType, persistenceUnitName);
if (instance.isUnsatisfied()) {
// Legacy behavior
if (PersistenceUnitUtil.isDefaultPersistenceUnit(persistenceUnitName)) {
instance = Arc.container().select(beanType, Default.Literal.INSTANCE);
if (!instance.isUnsatisfied()
// As long as this legacy support exists, the default,
// Quarkus-defined beans need to use this legacy support,
// in order to not override user beans.
// So let's not log a warning when we just default to the Quarkus-defined beans...
&& !isDefaultBean(instance)) {
LOG.warnf("A bean of type %1$s is being retrieved even though it doesn't have a @%2$s qualifier."
+ " This is deprecated usage and will not work in future versions of Quarkus."
+ " Annotate this bean with @%2$s to make it future-proof.",
beanType.getName(), PersistenceUnitExtension.class.getSimpleName());
}
} else {
instance = Arc.container().select(beanType,
new PersistenceUnit.PersistenceUnitLiteral(persistenceUnitName));
if (!instance.isUnsatisfied()
// As long as this legacy support exists, the default,
// Quarkus-defined beans need to use this legacy support,
// in order to not override user beans.
// So let's not log a warning when we just default to the Quarkus-defined beans...
&& !isDefaultBean(instance)) {
LOG.warnf("A bean of type %1$s is being retrieved even though it doesn't have a @%2$s qualifier."
+ " This is deprecated usage and will not work in future versions of Quarkus."
+ " Annotate this bean with @%2$s(\"%4$s\") instead of @%3$s(\"%4$s\") to make it future-proof.",
beanType.getName(), PersistenceUnitExtension.class.getSimpleName(),
PersistenceUnit.class.getSimpleName(), persistenceUnitName);
}
}
}
return instance;
}
private static <T> boolean isDefaultBean(InjectableInstance<T> instance) {
return instance.isResolvable() && instance.getHandle().getBean().isDefaultBean();
}
}