9ea0517506fea7357937117ac1bb0fe571ef253e
[utils] / test / enterprise / src / main / java / org / wamblee / test / inject / JavaEETestInjector.java
1 /*
2  * Copyright 2005-2010 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  *      http://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.wamblee.test.inject;
17
18 import java.util.List;
19
20 import javax.persistence.EntityManager;
21 import javax.persistence.PersistenceContext;
22
23 import org.wamblee.inject.Injector;
24 import org.wamblee.reflection.Accessor;
25 import org.wamblee.reflection.AnnotationUtils;
26
27 /**
28  * Injector that performs additional injection on top of the injections that are standard
29  * available (e.g. entity manager in Java SE environment).
30  * 
31  * @author Erik Brakkee
32  */
33 public class JavaEETestInjector implements Injector {
34
35     private EntityManager entityManager;
36     private Injector delegate;
37     private List<Accessor> accessors;
38
39     /**
40      * Constructs the injector. 
41      * @param aClass Class to inject for. 
42      * @param aEntityManager Entity manager. 
43      * @param aDelegate Injecto to delegate to to perform the standard injections. 
44      */
45     public JavaEETestInjector(Class aClass, EntityManager aEntityManager,
46         Injector aDelegate) {
47         entityManager = aEntityManager;
48         delegate = aDelegate;
49         accessors = AnnotationUtils.analyse(aClass, PersistenceContext.class);
50     }
51
52     @Override
53     public void inject(Object aComponent) {
54         delegate.inject(aComponent);
55         for (Accessor accessor: accessors) {
56             accessor.set(aComponent, entityManager);
57         }
58     }
59
60 }