001: /*
002: * JFox - The most lightweight Java EE Application Server!
003: * more details please visit http://www.huihoo.org/jfox or http://www.jfox.org.cn.
004: *
005: * JFox is licenced and re-distributable under GNU LGPL.
006: */
007: package org.jfox.ejb3.dependent;
008:
009: import java.lang.reflect.Field;
010: import javax.annotation.Resource;
011: import javax.ejb.EJBContext;
012: import javax.ejb.MessageDrivenContext;
013: import javax.ejb.SessionContext;
014: import javax.ejb.TimerService;
015: import javax.jms.QueueConnectionFactory;
016: import javax.jms.TopicConnectionFactory;
017: import javax.sql.DataSource;
018: import javax.transaction.UserTransaction;
019:
020: import org.jfox.ejb3.EJBBucket;
021: import org.jfox.ejb3.ExtendEJBContext;
022: import org.jfox.entity.EntityManagerFactoryBuilderImpl;
023: import org.jfox.framework.dependent.InjectionException;
024:
025: /**
026: * @author <a href="mailto:jfox.young@gmail.com">Young Yang</a>
027: */
028: public class FieldResourceDependence extends ResourceDependence {
029:
030: private Field field;
031:
032: public FieldResourceDependence(EJBBucket bucket, Field field,
033: Resource resource) {
034: super (bucket, resource);
035: this .field = field;
036: }
037:
038: /**
039: * \@Resource �以 注入以下类型的数�
040: * <p/>
041: * \@Resource(mappedName="DefaultDS") private javax.sql.DataSource ds;
042: * \@Resource javax.ejb.SessionContext ctx;
043: * \@Resource javax.ejb.TimerService timer;
044: * \@Resource javax.ejb.UserTransaction ut;
045: *
046: * @param ejbContext ejb bean ejbContext
047: * @throws InjectionException
048: */
049: public void inject(Object ejbContext) throws InjectionException {
050: Object targetObject = null;
051: if (field.getType().equals(EJBContext.class)
052: || field.getType().equals(SessionContext.class)
053: || field.getType().equals(MessageDrivenContext.class)) {
054: targetObject = (ExtendEJBContext) ejbContext;
055: } else if (field.getType().equals(TimerService.class)) {
056: targetObject = ((ExtendEJBContext) ejbContext)
057: .getTimerService();
058: } else if (field.getType().equals(DataSource.class)) {
059: // æ— éœ€é€šè¿‡ InitialContext
060: // targetObject = getBucket().getEJBContainer().lookup()
061: //注入 DataSource
062: try {
063: if (getResource().name().trim().length() == 0
064: && getResource().mappedName().trim().length() == 0) {
065: targetObject = EntityManagerFactoryBuilderImpl
066: .getDefaultDataSource();
067: } else if (getResource().name().trim().length() != 0) {
068: targetObject = EntityManagerFactoryBuilderImpl
069: .getDataSourceByUnitName(getResource()
070: .name().trim());
071: } else {
072: targetObject = EntityManagerFactoryBuilderImpl
073: .getDataSourceByMappedName(getResource()
074: .name().trim());
075: }
076: } catch (Exception e) {
077: throw new InjectionException(
078: "Inject @Resource with DataSource field failed, "
079: + field, e);
080: }
081: } else if (field.getType().equals(UserTransaction.class)) {
082: //注入 UserTransaction
083: targetObject = getBucket().getEJBContainer()
084: .getTransactionManager();
085: } else if (field.getType().equals(QueueConnectionFactory.class)
086: || field.getType().equals(TopicConnectionFactory.class)) {
087: //注入 JMS Connection Factory
088: targetObject = getBucket().getEJBContainer()
089: .getMessageService();
090: } else {
091: throw new InjectionException("Not support inject type: "
092: + field);
093: }
094: try {
095: field.setAccessible(true);
096: field.set(((ExtendEJBContext) ejbContext).getEJBInstance(),
097: targetObject);
098: } catch (IllegalAccessException e) {
099: throw new InjectionException("Inject EJBContext ");
100: }
101: }
102:
103: public static void main(String[] args) {
104:
105: }
106: }
|