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.InvocationTargetException;
025: import java.lang.reflect.Method;
026: import javax.naming.Context;
027: import javax.naming.NamingException;
028: import org.jboss.ejb3.BeanContext;
029: import org.jboss.logging.Logger;
030:
031: /**
032: * Comment
033: *
034: * @author <a href="mailto:bill@jboss.org">Bill Burke</a>
035: * @version $Revision: 60233 $
036: * @deprecated use JndiPropertyInjector
037: */
038: public class JndiMethodInjector implements Injector, PojoInjector {
039: @SuppressWarnings("unused")
040: private static final Logger log = Logger
041: .getLogger(JndiMethodInjector.class);
042:
043: private Method setMethod;
044: private String jndiName;
045: private Context ctx;
046:
047: public JndiMethodInjector(Method setMethod, String jndiName,
048: Context ctx) {
049: this .setMethod = setMethod;
050: setMethod.setAccessible(true);
051: this .jndiName = jndiName;
052: this .ctx = ctx;
053: }
054:
055: public void inject(BeanContext bctx) {
056: inject(bctx, bctx.getInstance());
057: }
058:
059: public Class getInjectionClass() {
060: return setMethod.getParameterTypes()[0];
061: }
062:
063: protected Object lookup(String jndiName, Class param) {
064: Object dependency = null;
065:
066: try {
067: dependency = ctx.lookup(jndiName);
068: } catch (NamingException e) {
069: e.printStackTrace();
070: throw new RuntimeException(
071: "Unable to @Inject jndi dependency: " + jndiName
072: + " into method " + setMethod, e);
073: }
074: return dependency;
075: }
076:
077: public void inject(BeanContext bctx, Object instance) {
078: inject(instance);
079: }
080:
081: public void inject(Object instance) {
082: Object dependency = lookup(jndiName, setMethod
083: .getParameterTypes()[0]);
084:
085: Object[] args = { dependency };
086: try {
087: setMethod.invoke(instance, args);
088: } catch (IllegalAccessException e) {
089: throw new RuntimeException(e); //To change body of catch statement use Options | File Templates.
090: } catch (IllegalArgumentException e) {
091: String type = "UNKNOWN";
092: if (dependency != null)
093: type = dependency.getClass().getName();
094: throw new RuntimeException(
095: "Non matching type for @Inject of setter: "
096: + setMethod + " for type: " + type, e); //To change body of catch statement use Options | File Templates.
097: } catch (InvocationTargetException e) {
098: throw new RuntimeException(e.getCause()); //To change body of catch statement use Options | File Templates.
099: }
100: }
101: }
|