KickJava   Java API By Example, From Geeks To Geeks.

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


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.soa.client.WebServiceClient;
36 import com.caucho.util.L10N;
37
38 import javax.naming.InitialContext JavaDoc;
39 import javax.naming.NamingException JavaDoc;
40 import java.lang.reflect.Field JavaDoc;
41 import java.util.logging.Level JavaDoc;
42 import java.util.logging.Logger JavaDoc;
43
44
45 public class JndiWebServiceFieldInjectProgram extends BuilderProgram {
46   private static final Logger JavaDoc log
47     = Logger.getLogger(JndiWebServiceFieldInjectProgram.class.getName());
48   private static final L10N L
49     = new L10N(JndiWebServiceFieldInjectProgram.class);
50
51   private String JavaDoc _jndiName;
52   private Class JavaDoc _type;
53   private Field JavaDoc _field;
54
55   JndiWebServiceFieldInjectProgram(String JavaDoc jndiName,
56                    Class JavaDoc type,
57                    Field JavaDoc field)
58   {
59     _jndiName = jndiName;
60     _type = type;
61     _field = field;
62   }
63
64   public void configureImpl(NodeBuilder builder, Object JavaDoc bean)
65     throws ConfigException
66   {
67     try {
68       Object JavaDoc value = new InitialContext JavaDoc().lookupLink(_jndiName);
69
70       if (value == null)
71     return;
72
73       if (value instanceof WebServiceClient) {
74     WebServiceClient client = (WebServiceClient) value;
75
76     value = client.createProxy(_type);
77       }
78
79       if (! _field.getType().isAssignableFrom(value.getClass())) {
80     throw new ConfigException(L.l("Resource at '{0}' of type {1} is not assignable to field '{2}' of type {3}.",
81                       _jndiName,
82                       value.getClass().getName(),
83                       _field.getName(),
84                       _field.getType().getName()));
85       }
86
87       _field.setAccessible(true);
88       _field.set(bean, value);
89     } catch (RuntimeException JavaDoc e) {
90       throw e;
91     } catch (NamingException JavaDoc e) {
92       log.finer(String.valueOf(e));
93       log.log(Level.FINEST, e.toString(), e);
94     } catch (Exception JavaDoc e) {
95       throw new ConfigException(e);
96     }
97   }
98
99   public Object JavaDoc configure(NodeBuilder builder, Class JavaDoc type)
100     throws ConfigException
101   {
102     throw new UnsupportedOperationException JavaDoc(getClass().getName());
103   }
104 }
105
Popular Tags