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.annotation.JndiInject;
025: import org.jboss.logging.Logger;
026: import org.jboss.metamodel.descriptor.EnvironmentRefGroup;
027: import org.jboss.metamodel.descriptor.JndiRef;
028:
029: import java.lang.reflect.AccessibleObject;
030: import java.lang.reflect.Field;
031: import java.lang.reflect.Method;
032: import java.util.Map;
033:
034: /**
035: * Searches bean class for all @Inject and create Injectors
036: *
037: * @author <a href="mailto:bill@jboss.org">Bill Burke</a>
038: * @version $Revision: 60233 $
039: */
040: public class JndiInjectHandler implements InjectionHandler {
041: private static final Logger log = Logger
042: .getLogger(JndiInjectHandler.class);
043:
044: public void loadXml(EnvironmentRefGroup xml,
045: InjectionContainer container) {
046: if (xml == null)
047: return;
048: if (xml.getJndiRefs() == null)
049: return;
050: for (JndiRef ref : xml.getJndiRefs()) {
051: if (ref.getMappedName() == null
052: || ref.getMappedName().equals(""))
053: throw new RuntimeException(
054: "mapped-name is required for "
055: + ref.getJndiRefName()
056: + " of container "
057: + container.getIdentifier());
058:
059: String encName = "env/" + ref.getJndiRefName();
060: if (!container.getEncInjectors().containsKey(encName)) {
061: container.getEncInjectors().put(
062: encName,
063: new LinkRefEncInjector(encName, ref
064: .getMappedName(), "jndi ref"));
065: }
066: InjectionUtil.injectionTarget(encName, ref, container,
067: container.getEncInjections());
068: }
069: }
070:
071: public void handleClassAnnotations(Class clazz,
072: InjectionContainer container) {
073: // complete
074: }
075:
076: public void handleMethodAnnotations(Method method,
077: InjectionContainer container,
078: Map<AccessibleObject, Injector> injectors) {
079: JndiInject ref = method.getAnnotation(JndiInject.class);
080: if (ref != null) {
081: if (!method.getName().startsWith("set"))
082: throw new RuntimeException(
083: "@EJB can only be used with a set method: "
084: + method);
085: String encName = InjectionUtil.getEncName(method);
086: if (!container.getEncInjectors().containsKey(encName)) {
087: container.getEncInjectors().put(
088: encName,
089: new LinkRefEncInjector(encName, ref.jndiName(),
090: "@JndiInject"));
091: }
092: injectors.put(method, new JndiMethodInjector(method,
093: encName, container.getEnc()));
094: }
095: }
096:
097: public void handleFieldAnnotations(Field field,
098: InjectionContainer container,
099: Map<AccessibleObject, Injector> injectors) {
100: JndiInject ref = field.getAnnotation(JndiInject.class);
101: if (ref != null) {
102: String encName = InjectionUtil.getEncName(field);
103: if (!container.getEncInjectors().containsKey(encName)) {
104: container.getEncInjectors().put(
105: encName,
106: new LinkRefEncInjector(encName, ref.jndiName(),
107: "@JndiInject"));
108: }
109: injectors.put(field, new JndiFieldInjector(field, encName,
110: container.getEnc()));
111: }
112: }
113: }
|