001: /*
002: * JBoss, Home of Professional Open Source.
003: * Copyright 2006, Red Hat Middleware LLC, and individual contributors
004: * as indicated by the @author tags. See the copyright.txt file in the
005: * distribution for a full listing of individual contributors.
006: *
007: * This is free software; you can redistribute it and/or modify it
008: * under the terms of the GNU Lesser General Public License as
009: * published by the Free Software Foundation; either version 2.1 of
010: * the License, or (at your option) any later version.
011: *
012: * This software is distributed in the hope that it will be useful,
013: * but WITHOUT ANY WARRANTY; without even the implied warranty of
014: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
015: * Lesser General Public License for more details.
016: *
017: * You should have received a copy of the GNU Lesser General Public
018: * License along with this software; if not, write to the Free
019: * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
020: * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
021: */
022: package org.jboss.injection;
023:
024: import org.jboss.logging.Logger;
025: import org.jboss.naming.Util;
026:
027: import javax.naming.NameNotFoundException;
028: import javax.naming.LinkRef;
029: import javax.naming.NamingException;
030:
031: /**
032: * Comment
033: *
034: * @author <a href="mailto:bill@jboss.org">Bill Burke</a>
035: * @version $Revision: 60233 $
036: */
037: public class EjbEncInjector implements EncInjector {
038: private static final Logger log = Logger
039: .getLogger(EjbEncInjector.class);
040:
041: private String ejbLink;
042: private Class refClass;
043: private String jndiName;
044: private String error;
045: private String encName;
046:
047: public EjbEncInjector(String name, String mappedName, String error) {
048: this .jndiName = mappedName;
049: this .error = error;
050: this .encName = name;
051: }
052:
053: public EjbEncInjector(String name, Class refClass, String ejbLink,
054: String error) {
055: this .refClass = refClass;
056: String refClassName = refClass == null ? "NULL" : refClass
057: .getName();
058: this .ejbLink = ejbLink;
059: this .error = error;
060: this .encName = name;
061: if (refClass == null && ejbLink == null)
062: throw new RuntimeException(
063: "cannot have null refClass and ejbLink for encName: "
064: + name);
065: }
066:
067: public void inject(InjectionContainer container) {
068: if (jndiName == null || jndiName.equals("")) {
069: if (ejbLink != null && !"".equals(ejbLink)) {
070: jndiName = container.getEjbJndiName(ejbLink, refClass);
071: } else {
072: try {
073: if (refClass != null) {
074: jndiName = container.getEjbJndiName(refClass);
075: } else {
076: throw new RuntimeException(
077: "searching for @EJB"
078: + encName
079: + " has null refClass and null ejbLink.");
080: }
081: } catch (NameNotFoundException e) {
082: throw new RuntimeException(
083: "could not resolve global JNDI name for "
084: + error + " for container "
085: + container.getIdentifier()
086: + ": reference class: "
087: + refClass.getName() + " ejbLink: "
088: + ejbLink + " " + e.getMessage());
089: }
090: }
091: }
092: try {
093: if (jndiName == null)
094: throw new RuntimeException("Failed to populate ENC: "
095: + encName + " global jndi name was null");
096: log.debug(" " + encName + " --> " + jndiName);
097: Util.rebind(container.getEnc(), encName, new LinkRef(
098: jndiName));
099: } catch (NamingException e) {
100: throw new RuntimeException("could not bind enc name '"
101: + encName + "' for " + error + " for container "
102: + container.getIdentifier() + ": reference class: "
103: + refClass.getName() + " ejbLink: " + ejbLink + " "
104: + e.getMessage());
105: }
106: }
107: }
|