KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > caucho > config > j2ee > JndiFieldInjectProgram


1 /*
2  * Copyright (c) 1998-2006 Caucho Technology -- all rights reserved
3  *
4  * This file is part of Resin(R) Open Source
5  *
6  * Each copy or derived work must preserve the copyright notice and this
7  * notice unmodified.
8  *
9  * Resin Open Source is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation; either version 2 of the License, or
12  * (at your option) any later version.
13  *
14  * Resin Open Source is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE, or any warranty
17  * of NON-INFRINGEMENT. See the GNU General Public License for more
18  * details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with Resin Open Source; if not, write to the
22  *
23  * Free Software Foundation, Inc.
24  * 59 Temple Place, Suite 330
25  * Boston, MA 02111-1307 USA
26  *
27  * @author Scott Ferguson;
28  */

29
30 package com.caucho.config.j2ee;
31
32 import com.caucho.config.BuilderProgram;
33 import com.caucho.config.ConfigException;
34 import com.caucho.config.NodeBuilder;
35 import com.caucho.util.L10N;
36
37 import javax.naming.InitialContext JavaDoc;
38 import javax.naming.NamingException JavaDoc;
39 import java.lang.reflect.Field JavaDoc;
40 import java.util.logging.Level JavaDoc;
41 import java.util.logging.Logger JavaDoc;
42
43
44 public class JndiFieldInjectProgram extends BuilderProgram {
45   private static final Logger JavaDoc log
46     = Logger.getLogger(JndiFieldInjectProgram.class.getName());
47   private static final L10N L = new L10N(JndiFieldInjectProgram.class);
48
49   private String JavaDoc _jndiName;
50   private Field JavaDoc _field;
51
52   JndiFieldInjectProgram(String JavaDoc jndiName, Field JavaDoc field)
53   {
54     _jndiName = jndiName;
55     _field = field;
56   }
57
58   @Override JavaDoc
59   public void configureImpl(NodeBuilder builder, Object JavaDoc bean)
60     throws ConfigException
61   {
62     try {
63       Object JavaDoc value = new InitialContext JavaDoc().lookup(_jndiName);
64
65       if (value == null)
66     return;
67
68       if (! _field.getType().isAssignableFrom(value.getClass())
69       && ! _field.getType().isPrimitive()) {
70     throw new ConfigException(L.l("Resource at '{0}' of type {1} is not assignable to field '{2}' of type {3}.",
71                       _jndiName,
72                       value.getClass().getName(),
73                       _field.getName(),
74                       _field.getType().getName()));
75       }
76
77       _field.setAccessible(true);
78       _field.set(bean, value);
79     } catch (RuntimeException JavaDoc e) {
80       throw e;
81     } catch (NamingException JavaDoc e) {
82       log.finer(String.valueOf(e));
83       log.log(Level.FINEST, e.toString(), e);
84     } catch (Exception JavaDoc e) {
85       throw new ConfigException(e);
86     }
87   }
88
89   @Override JavaDoc
90   public Object JavaDoc configureImpl(NodeBuilder builder, Class JavaDoc type)
91     throws ConfigException
92   {
93     throw new UnsupportedOperationException JavaDoc(getClass().getName());
94   }
95
96   @Override JavaDoc
97   public String JavaDoc toString()
98   {
99     return getClass().getSimpleName() + "[" + _jndiName + "," + _field + "]";
100   }
101 }
102
Popular Tags