/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with this
* work for additional information regarding copyright ownership. The ASF
* licenses this file to You 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.apache.webbeans.resource.spi.se;
import java.lang.reflect.Field;
import javax.annotation.Resource;
import javax.naming.InitialContext;
import javax.persistence.PersistenceContext;
import javax.persistence.PersistenceUnit;
import org.apache.webbeans.exception.WebBeansException;
import org.apache.webbeans.logger.WebBeansLogger;
import org.apache.webbeans.resource.spi.ResourceService;
public class ResourceServiceImpl implements ResourceService
{
private static InitialContext context = null;
private static WebBeansLogger logger = WebBeansLogger.getLogger(ResourceServiceImpl.class);
static
{
try
{
context = new InitialContext();
}catch(Exception e)
{
throw new ExceptionInInitializerError(e);
}
}
@Override
public Object getResource(Field field) throws WebBeansException
{
Object obj = null;
JPAServicePersistenceImpl impl = new JPAServicePersistenceImpl();
if (field.isAnnotationPresent(PersistenceContext.class))
{
PersistenceContext context = field.getAnnotation(PersistenceContext.class);
obj = impl.getPersistenceContext(context.unitName());
}
else if (field.isAnnotationPresent(PersistenceUnit.class))
{
PersistenceUnit annotation = field.getAnnotation(PersistenceUnit.class);
obj = impl.getPersistenceUnit(annotation.unitName());
}
else if(field.isAnnotationPresent(Resource.class))
{
Resource resource = field.getAnnotation(Resource.class);
try
{
obj = context.lookup("java:/comp/env/"+ resource.name());
}
catch(Exception e)
{
logger.error("Unable to inject resource for : " + resource );
}
}
return obj;
}
}