KickJava   Java API By Example, From Geeks To Geeks.

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


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