X-Git-Url: http://wamblee.org/gitweb/?a=blobdiff_plain;f=test%2Fenterprise%2Fsrc%2Fmain%2Fjava%2Forg%2Fwamblee%2Ftest%2Finject%2FJavaEETestInjector.java;fp=test%2Fenterprise%2Fsrc%2Fmain%2Fjava%2Forg%2Fwamblee%2Ftest%2Finject%2FJavaEETestInjector.java;h=9ea0517506fea7357937117ac1bb0fe571ef253e;hb=8e6201ee8a948b120540f6306c6c9b85bed8ce95;hp=0000000000000000000000000000000000000000;hpb=4e57e113a7bca2baf1588ebb82b0f3a590e5d1c9;p=utils diff --git a/test/enterprise/src/main/java/org/wamblee/test/inject/JavaEETestInjector.java b/test/enterprise/src/main/java/org/wamblee/test/inject/JavaEETestInjector.java new file mode 100644 index 00000000..9ea05175 --- /dev/null +++ b/test/enterprise/src/main/java/org/wamblee/test/inject/JavaEETestInjector.java @@ -0,0 +1,60 @@ +/* + * Copyright 2005-2010 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.wamblee.test.inject; + +import java.util.List; + +import javax.persistence.EntityManager; +import javax.persistence.PersistenceContext; + +import org.wamblee.inject.Injector; +import org.wamblee.reflection.Accessor; +import org.wamblee.reflection.AnnotationUtils; + +/** + * Injector that performs additional injection on top of the injections that are standard + * available (e.g. entity manager in Java SE environment). + * + * @author Erik Brakkee + */ +public class JavaEETestInjector implements Injector { + + private EntityManager entityManager; + private Injector delegate; + private List accessors; + + /** + * Constructs the injector. + * @param aClass Class to inject for. + * @param aEntityManager Entity manager. + * @param aDelegate Injecto to delegate to to perform the standard injections. + */ + public JavaEETestInjector(Class aClass, EntityManager aEntityManager, + Injector aDelegate) { + entityManager = aEntityManager; + delegate = aDelegate; + accessors = AnnotationUtils.analyse(aClass, PersistenceContext.class); + } + + @Override + public void inject(Object aComponent) { + delegate.inject(aComponent); + for (Accessor accessor: accessors) { + accessor.set(aComponent, entityManager); + } + } + +}