KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > injection > JndiFieldInjector


1 /*
2   * JBoss, Home of Professional Open Source
3   * Copyright 2005, JBoss Inc., and individual contributors as indicated
4   * by the @authors tag. See the copyright.txt in the distribution for a
5   * full listing of individual contributors.
6   *
7   * This is free software; you can redistribute it and/or modify it
8   * under the terms of the GNU Lesser General Public License as
9   * published by the Free Software Foundation; either version 2.1 of
10   * the License, or (at your option) any later version.
11   *
12   * This software is distributed in the hope that it will be useful,
13   * but WITHOUT ANY WARRANTY; without even the implied warranty of
14   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15   * Lesser General Public License for more details.
16   *
17   * You should have received a copy of the GNU Lesser General Public
18   * License along with this software; if not, write to the Free
19   * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
20   * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
21   */

22 package org.jboss.injection;
23
24 import java.lang.reflect.Field JavaDoc;
25 import javax.naming.Context JavaDoc;
26 import javax.naming.NamingException JavaDoc;
27
28 import org.jboss.logging.Logger;
29
30 import org.jboss.ejb3.BeanContext;
31
32 /**
33  * Comment
34  *
35  * @author <a HREF="mailto:bill@jboss.org">Bill Burke</a>
36  * @version $Revision: 58121 $
37  */

38 public class JndiFieldInjector implements Injector, PojoInjector
39 {
40    private static final Logger log = Logger.getLogger(JndiFieldInjector.class);
41    
42    private Field JavaDoc field;
43    private String JavaDoc jndiName;
44    private Context JavaDoc ctx;
45
46    public JndiFieldInjector(Field JavaDoc field, String JavaDoc jndiName, Context JavaDoc ctx)
47    {
48       this.field = field;
49       this.field.setAccessible(true);
50       this.jndiName = jndiName;
51       this.ctx = ctx;
52    }
53
54    public JndiFieldInjector(Field JavaDoc field, Context JavaDoc ctx)
55    {
56       this(field, field.getName(), ctx);
57    }
58
59    public void inject(BeanContext bctx)
60    {
61       inject(bctx, bctx.getInstance());
62    }
63
64    public Class JavaDoc getInjectionClass()
65    {
66       return field.getType();
67    }
68
69    public Field JavaDoc getField()
70    {
71       return field;
72    }
73
74    protected Object JavaDoc lookup(String JavaDoc jndiName, Class JavaDoc field)
75    {
76       Object JavaDoc dependency = null;
77
78       try
79       {
80          dependency = ctx.lookup(jndiName);
81       }
82       catch (NamingException JavaDoc e)
83       {
84          e.printStackTrace();
85          throw new RuntimeException JavaDoc("Unable to inject jndi dependency: " + jndiName + " into field " + field, e);
86       }
87       
88       return dependency;
89    }
90    
91    public void inject(BeanContext bctx, Object JavaDoc instance)
92    {
93       inject(instance);
94    }
95
96    public void inject(Object JavaDoc instance)
97    {
98       
99       Object JavaDoc dependency = lookup(jndiName, field.getType());
100  // log.info("!! inject jndiName " + jndiName + " " + field.getType() + " " + dependency);
101

102       try
103       {
104  // log.info("!!! inject " + instance + " " + field.getType() + " " + dependency.getClass());
105
// Class[] interfacs = dependency.getClass().getInterfaces();
106
// for (Class interfac : interfacs)
107
// log.info("!! interface " + interfac);
108
field.set(instance, dependency);
109       }
110       catch (IllegalArgumentException JavaDoc e)
111       {
112          String JavaDoc type = "UNKNOWN";
113          String JavaDoc interfaces = "";
114          if (dependency != null)
115          {
116  // log.info("!!! inject failed " + instance + " " + field.getType() + " " + dependency.getClass());
117
// Class[] interfacs = dependency.getClass().getInterfaces();
118
// for (Class interfac : interfacs)
119
// log.info("!! interface " + interfac);
120
type = dependency.getClass().getName();
121             Class JavaDoc[] intfs = dependency.getClass().getInterfaces();
122             for (Class JavaDoc intf : intfs) interfaces += ", " + intf.getName();
123          }
124          throw new RuntimeException JavaDoc("Non matching type for inject of field: " + field + " for type: " + type + " of jndiName " + jndiName + "\nintfs: " + interfaces, e);
125       }
126       catch (IllegalAccessException JavaDoc e)
127       {
128          throw new RuntimeException JavaDoc(e);
129       }
130    }
131    
132    public String JavaDoc toString()
133    {
134       return super.toString() + "{field=" + field + ",jndiName=" + jndiName + "}";
135    }
136 }
137
Popular Tags