47
47
48
48
import javax .ws .rs .core .GenericType ;
49
49
50
- import javax .inject .Inject ;
51
50
import javax .inject .Singleton ;
52
51
import javax .persistence .EntityManagerFactory ;
53
52
import javax .persistence .PersistenceUnit ;
54
53
import javax .servlet .ServletConfig ;
55
54
56
55
import org .glassfish .jersey .internal .inject .AbstractBinder ;
57
- import org .glassfish .jersey .internal .inject .InjectionManager ;
56
+ import org .glassfish .jersey .internal .inject .Injectee ;
57
+ import org .glassfish .jersey .internal .inject .InjectionResolver ;
58
58
import org .glassfish .jersey .server .ContainerException ;
59
59
60
- import org .glassfish .hk2 .api .Injectee ;
61
- import org .glassfish .hk2 .api .InjectionResolver ;
62
- import org .glassfish .hk2 .api .ServiceHandle ;
63
-
64
60
/**
65
61
* {@link PersistenceUnit Persistence unit} injection binder.
66
62
*
67
63
* @author Michal Gajdos
68
64
*/
69
65
public class PersistenceUnitBinder extends AbstractBinder {
70
66
67
+ private final ServletConfig servletConfig ;
68
+
71
69
/**
72
70
* Prefix of the persistence unit init param.
73
71
*/
74
72
public static final String PERSISTENCE_UNIT_PREFIX = "unit:" ;
75
73
74
+ /**
75
+ * Creates a new binder for {@link PersistenceUnitInjectionResolver}.
76
+ *
77
+ * @param servletConfig servlet config to find persistence units.
78
+ */
79
+ public PersistenceUnitBinder (ServletConfig servletConfig ) {
80
+ this .servletConfig = servletConfig ;
81
+ }
82
+
76
83
@ Singleton
77
84
private static class PersistenceUnitInjectionResolver implements InjectionResolver <PersistenceUnit > {
78
85
79
86
private final Map <String , String > persistenceUnits = new HashMap <>();
80
87
81
- @ Inject
82
- private PersistenceUnitInjectionResolver (final InjectionManager injectionManager ) {
83
- // Look for persistence units.
84
- final ServletConfig servletConfig = injectionManager .getInstance (ServletConfig .class );
85
-
88
+ private PersistenceUnitInjectionResolver (ServletConfig servletConfig ) {
86
89
for (final Enumeration parameterNames = servletConfig .getInitParameterNames (); parameterNames .hasMoreElements (); ) {
87
90
final String key = (String ) parameterNames .nextElement ();
88
91
@@ -94,7 +97,7 @@ private PersistenceUnitInjectionResolver(final InjectionManager injectionManager
94
97
}
95
98
96
99
@ Override
97
- public Object resolve (final Injectee injectee , final ServiceHandle <?> root ) {
100
+ public Object resolve (Injectee injectee ) {
98
101
if (!injectee .getRequiredType ().equals (EntityManagerFactory .class )) {
99
102
return null ;
100
103
}
@@ -121,12 +124,16 @@ public boolean isConstructorParameterIndicator() {
121
124
public boolean isMethodParameterIndicator () {
122
125
return false ;
123
126
}
127
+
128
+ @ Override
129
+ public Class <PersistenceUnit > getAnnotation () {
130
+ return PersistenceUnit .class ;
131
+ }
124
132
}
125
133
126
134
@ Override
127
135
protected void configure () {
128
- bind (PersistenceUnitInjectionResolver .class )
129
- .to (new GenericType <InjectionResolver <PersistenceUnit >>() {})
130
- .in (Singleton .class );
136
+ bind (new PersistenceUnitInjectionResolver (servletConfig ))
137
+ .to (new GenericType <InjectionResolver <PersistenceUnit >>() {});
131
138
}
132
139
}
0 commit comments