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 java.lang.reflect.Field;
025: import javax.naming.Context;
026: import javax.naming.NamingException;
027:
028: import org.jboss.logging.Logger;
029:
030: import org.jboss.ejb3.BeanContext;
031:
032: /**
033: * Comment
034: *
035: * @author <a href="mailto:bill@jboss.org">Bill Burke</a>
036: * @version $Revision: 60233 $
037: * @deprecated use JndiPropertyInjector
038: */
039: public class JndiFieldInjector implements Injector, PojoInjector {
040: private static final Logger log = Logger
041: .getLogger(JndiFieldInjector.class);
042:
043: private Field field;
044: private String jndiName;
045: private Context ctx;
046:
047: public JndiFieldInjector(Field field, String jndiName, Context ctx) {
048: this .field = field;
049: this .field.setAccessible(true);
050: this .jndiName = jndiName;
051: this .ctx = ctx;
052: }
053:
054: public JndiFieldInjector(Field field, Context ctx) {
055: this (field, field.getName(), ctx);
056: }
057:
058: public void inject(BeanContext bctx) {
059: inject(bctx, bctx.getInstance());
060: }
061:
062: public Class getInjectionClass() {
063: return field.getType();
064: }
065:
066: public Field getField() {
067: return field;
068: }
069:
070: protected Object lookup(String jndiName, Class field) {
071: Object dependency = null;
072:
073: try {
074: dependency = ctx.lookup(jndiName);
075:
076: if (dependency instanceof javax.xml.rpc.Service
077: && !field
078: .isAssignableFrom(javax.xml.rpc.Service.class)) {
079: javax.xml.rpc.Service service = (javax.xml.rpc.Service) dependency;
080: dependency = service.getPort(field);
081: }
082: } catch (NamingException e) {
083: e.printStackTrace();
084: throw new RuntimeException(
085: "Unable to inject jndi dependency: " + jndiName
086: + " into field " + field, e);
087: } catch (javax.xml.rpc.ServiceException e) {
088: e.printStackTrace();
089: throw new RuntimeException(
090: "Unable to inject jndi webservice dependency: "
091: + jndiName + " into field " + field, e);
092: }
093:
094: return dependency;
095: }
096:
097: public void inject(BeanContext bctx, Object instance) {
098: inject(instance);
099: }
100:
101: public void inject(Object instance) {
102:
103: Object dependency = lookup(jndiName, field.getType());
104:
105: try {
106: field.set(instance, dependency);
107: } catch (IllegalArgumentException e) {
108: String type = "UNKNOWN";
109: String interfaces = "";
110: if (dependency != null) {
111: type = dependency.getClass().getName();
112: Class[] intfs = dependency.getClass().getInterfaces();
113: for (Class intf : intfs)
114: interfaces += ", " + intf.getName();
115: }
116: throw new RuntimeException(
117: "Non matching type for inject of field: " + field
118: + " for type: " + type + " of jndiName "
119: + jndiName + "\nintfs: " + interfaces, e);
120: } catch (IllegalAccessException e) {
121: throw new RuntimeException(e);
122: }
123: }
124:
125: public String toString() {
126: return super .toString() + "{field=" + field + ",jndiName="
127: + jndiName + "}";
128: }
129: }
|