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