001/**
002 * Licensed under the Apache License, Version 2.0 (the "License");
003 * you may not use this file except in compliance with the License.
004 * You may obtain a copy of the License at
005 *
006 *     http://www.apache.org/licenses/LICENSE-2.0
007 *
008 * Unless required by applicable law or agreed to in writing, software
009 * distributed under the License is distributed on an "AS IS" BASIS,
010 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
011 * See the License for the specific language governing permissions and
012 * limitations under the License.
013 */
014package org.apache.tapestry5.internal.jpa;
015
016import java.lang.reflect.Method;
017
018import javax.annotation.PreDestroy;
019import javax.enterprise.context.spi.Contextual;
020import javax.enterprise.context.spi.CreationalContext;
021import javax.enterprise.inject.spi.AnnotatedType;
022import javax.enterprise.inject.spi.InjectionTarget;
023
024import org.apache.tapestry5.commons.ObjectLocator;
025import org.apache.tapestry5.ioc.annotations.Inject;
026import org.slf4j.Logger;
027import org.slf4j.LoggerFactory;
028
029public class TapestryCDIBeanManagerForJPAEntityListeners extends NoopBeanManager
030{
031    private static final Logger logger = LoggerFactory
032            .getLogger(TapestryCDIBeanManagerForJPAEntityListeners.class);
033
034    @Inject
035    private ObjectLocator objectLocator;
036
037    @Override
038    public <T> AnnotatedType<T> createAnnotatedType(final Class<T> type)
039    {
040        return new NoopAnnotatedType<T>()
041        {
042            @Override
043            public Class<T> getJavaClass()
044            {
045                return type;
046            }
047        };
048    }
049
050    @Override
051    public <T> InjectionTarget<T> createInjectionTarget(final AnnotatedType<T> type)
052    {
053        return new NoopInjectionTarget<T>()
054        {
055            @Override
056            public T produce(CreationalContext<T> ctx)
057            {
058                return objectLocator.autobuild(type.getJavaClass());
059            }
060
061            @Override
062            public void preDestroy(T instance)
063            {
064                try
065                {
066                    for (Method method : type.getJavaClass().getMethods())
067                    {
068                        if (method.getAnnotation(PreDestroy.class) != null)
069                        {
070                            method.invoke(instance);
071                        }
072                    }
073                }
074                catch (Exception e)
075                {
076                    logger.error(
077                            "Error invoking @PreDestroy callback on instance of class "
078                                    + type.getJavaClass(), e);
079                }
080            }
081        };
082    }
083
084    @Override
085    public <T> CreationalContext<T> createCreationalContext(Contextual<T> contextual)
086    {
087        return new NoopCreationalContext<T>();
088    }
089}