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 java.util.Collection;
011: import javax.ejb.EJB;
012: import javax.ejb.EJBObject;
013: import javax.naming.NamingException;
014:
015: import org.jfox.ejb3.EJBBucket;
016: import org.jfox.ejb3.ExtendEJBContext;
017: import org.jfox.framework.dependent.InjectionException;
018:
019: /**
020: * 注入 Field Level @EJB
021: *
022: * @author <a href="mailto:jfox.young@gmail.com">Young Yang</a>
023: */
024: public class FieldEJBDependence extends EJBDependence {
025:
026: private Field field;
027:
028: public FieldEJBDependence(EJBBucket bucket, Field field, EJB ejb) {
029: super (bucket, ejb);
030: this .field = field;
031: }
032:
033: /**
034: * 通过 Field å¾€ EJB ä¸æ³¨å…¥ EJB
035: *
036: * @param ejbContext ejb context
037: * @throws InjectionException injection exception
038: */
039: public void inject(Object ejbContext) throws InjectionException {
040: String name = ejb.name().trim();
041: String beanName = ejb.beanName().trim();
042: String mappedName = ejb.mappedName().trim();
043: Class beanInterface = ejb.beanInterface();
044:
045: EJBObject targetEJBObject = null; // resolve dependence
046:
047: if (name.startsWith("/")) {
048: name = name.substring(1);
049: }
050: if (name.length() != 0) {
051: try {
052: Object obj = getBucket().getENContext(null)
053: .lookup(name);
054: if (!(obj instanceof EJBObject)) {
055: throw new InjectionException(
056: "Failed to inject field " + field.getName()
057: + ", name " + name
058: + " is not a ejb, but "
059: + obj.toString() + "!");
060: } else {
061: targetEJBObject = (EJBObject) obj;
062: }
063: } catch (NamingException e) {
064: // ignore
065: }
066: }
067: // 没有在 java:comp/env ä¸æ‰¾åˆ°
068: if (targetEJBObject == null) {
069: if (!beanName.equals("")) { // 分� beanName
070: EJBBucket bucket = this .bucket.getEJBContainer()
071: .getEJBBucket(beanName);
072: if (bucket == null) {
073: throw new InjectionException(
074: "Could not find ejb with bean name: "
075: + beanName);
076: } else {
077: targetEJBObject = bucket.createProxyStub();
078: }
079: } else if (mappedName.length() != 0) {
080: try {
081: Object obj = this .bucket.getEJBContainer()
082: .getNamingContext().lookup(mappedName);
083: if (!(obj instanceof EJBObject)) {
084: throw new InjectionException("MappedName "
085: + mappedName + " is not a ejb, but "
086: + obj.toString() + "!");
087: } else {
088: targetEJBObject = (EJBObject) obj;
089: }
090: } catch (NamingException e) {
091: throw new InjectionException("Failed to lookup "
092: + mappedName);
093: }
094: } else { // 解� beanInterface
095: if (beanInterface.equals(Object.class)) {
096: beanInterface = field.getType();
097: }
098: Collection<EJBBucket> buckets = this .bucket
099: .getEJBContainer().getEJBBucketByBeanInterface(
100: beanInterface);
101: if (buckets.isEmpty()) {
102: throw new InjectionException(
103: "Not found EJB by interface "
104: + beanInterface.getName());
105: } else if (buckets.size() != 1) {
106: throw new InjectionException(
107: "Found more than on EJB bye interface "
108: + beanInterface.getName());
109: } else {
110: targetEJBObject = buckets.iterator().next()
111: .createProxyStub();
112: }
113: }
114:
115: }
116:
117: // 没有找到 @EJB 对象
118: if (targetEJBObject == null) {
119: throw new InjectionException(
120: "Failed to find the dependent EJBObject "
121: + getEJBAnntation());
122: }
123:
124: // 使用 field å??射注入
125: try {
126: field.setAccessible(true);
127: field.set(((ExtendEJBContext) ejbContext).getEJBInstance(),
128: targetEJBObject);
129: } catch (Exception e) {
130: throw new InjectionException("Failed to inject field "
131: + field.getName() + " of EJB "
132: + bucket.getEJBName(), e);
133: }
134: }
135:
136: public static void main(String[] args) {
137:
138: }
139: }
|