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.util.Collection;
010: import javax.ejb.EJB;
011: import javax.ejb.EJBObject;
012: import javax.naming.NamingException;
013:
014: import org.jfox.ejb3.EJBBucket;
015: import org.jfox.framework.dependent.InjectionException;
016: import org.jfox.framework.dependent.Dependence;
017:
018: /**
019: * Class Level EJB Dependence
020: *
021: * @author <a href="mailto:jfox.young@gmail.com">Young Yang</a>
022: */
023: public class EJBDependence implements Dependence {
024:
025: protected EJBBucket bucket;
026: protected EJB ejb;
027:
028: public EJBDependence(EJBBucket bucket, EJB ejb) {
029: this .bucket = bucket;
030: this .ejb = ejb;
031: }
032:
033: public EJBBucket getBucket() {
034: return bucket;
035: }
036:
037: public EJB getEJBAnntation() {
038: return ejb;
039: }
040:
041: /**
042: * class level, instance is null
043: *
044: * @param instance null
045: * @throws InjectionException inject exception
046: */
047: public void inject(Object instance) throws InjectionException {
048:
049: EJBObject targetEJBObject = null; // resolved dependended ejbObject
050:
051: String name = ejb.name().trim();
052: String beanName = ejb.beanName().trim();
053: String mappedName = ejb.mappedName().trim();
054: Class beanInterface = ejb.beanInterface();
055: //inject @EJB class level, bind to java:comp/env
056: if (!beanName.equals("")) { // 分� beanName
057: EJBBucket bucket = this .bucket.getEJBContainer()
058: .getEJBBucket(beanName);
059: if (bucket == null) {
060: throw new InjectionException(
061: "Could not find ejb with bean name: "
062: + beanName);
063: } else {
064: targetEJBObject = bucket.createProxyStub();
065: }
066: } else if (!beanInterface.equals(Object.class)) { // 解� beanInterface
067: Collection<EJBBucket> buckets = this .bucket
068: .getEJBContainer().getEJBBucketByBeanInterface(
069: beanInterface);
070: if (buckets.isEmpty()) {
071: throw new InjectionException("");
072: } else if (buckets.size() != 1) {
073: throw new InjectionException("");
074: } else {
075: targetEJBObject = buckets.iterator().next()
076: .createProxyStub();
077: }
078: } else if (mappedName.length() != 0) {
079: try {
080: Object obj = this .bucket.getEJBContainer()
081: .getNamingContext().lookup(mappedName);
082: if (!(obj instanceof EJBObject)) {
083: throw new InjectionException("MappedName "
084: + mappedName + " is not a ejb, but "
085: + obj.toString() + "!");
086: } else {
087: targetEJBObject = (EJBObject) obj;
088: }
089: } catch (NamingException e) {
090: throw new InjectionException("Failed to lookup "
091: + mappedName);
092: }
093: }
094: if (name.length() != 0) {
095: try {
096: // bind 到 java:comp/env
097: if (name.startsWith("/")) {
098: name = name.substring(1);
099: }
100: bucket.getENContext(null).bind(name, targetEJBObject);
101: } catch (NamingException e) {
102: throw new InjectionException(
103: "Failed to injection bind java:comp/env/"
104: + name, e);
105: }
106: }
107: }
108:
109: public static void main(String[] args) {
110:
111: }
112: }
|