KickJava   Java API By Example, From Geeks To Geeks.

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


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 javax.xml.namespace.QName JavaDoc;
41 import javax.xml.ws.Service;
42 import java.lang.reflect.Constructor JavaDoc;
43 import java.net.URL JavaDoc;
44 import java.util.logging.Level JavaDoc;
45 import java.util.logging.Logger JavaDoc;
46
47
48 public class ServiceInjectProgram extends BuilderProgram {
49   private static final Logger JavaDoc log
50     = Logger.getLogger(ServiceInjectProgram.class.getName());
51   private static final L10N L
52     = new L10N(ServiceInjectProgram.class);
53
54   private String JavaDoc _jndiName;
55   private Class JavaDoc _type;
56   private Constructor JavaDoc _constructor;
57   private AccessibleInject _field;
58
59   ServiceInjectProgram(String JavaDoc jndiName,
60                Class JavaDoc type,
61                AccessibleInject field)
62     throws ConfigException
63   {
64     if (! Service.class.isAssignableFrom(type))
65       throw new ConfigException(L.l("'{0}' needs to extend javax.xml.ws.Service.",
66                     type.getName()));
67     
68     try {
69       _jndiName = jndiName;
70       
71       _type = type;
72       
73       _constructor = type.getConstructor(new Class JavaDoc[] { URL JavaDoc.class,
74                                QName JavaDoc.class });
75
76       _field = field;
77     } catch (RuntimeException JavaDoc e) {
78       throw e;
79     } catch (Exception JavaDoc e) {
80       throw new ConfigException(e);
81     }
82   }
83
84   public void configureImpl(NodeBuilder builder, Object JavaDoc bean)
85     throws ConfigException
86   {
87     try {
88       Object JavaDoc value = new InitialContext JavaDoc().lookupLink(_jndiName);
89
90       if (value == null)
91     return;
92
93       if (value instanceof WebServiceClient) {
94     WebServiceClient client = (WebServiceClient) value;
95
96     value = client.createService(_constructor);
97       }
98
99       if (! _field.getType().isAssignableFrom(value.getClass())) {
100     throw new ConfigException(L.l("Resource at '{0}' of type {1} is not assignable to field '{2}' of type {3}.",
101                       _jndiName,
102                       value.getClass().getName(),
103                       _field.getName(),
104                       _field.getType().getName()));
105       }
106
107       _field.inject(bean, value);
108     } catch (RuntimeException JavaDoc e) {
109       throw e;
110     } catch (NamingException JavaDoc e) {
111       log.finer(String.valueOf(e));
112       log.log(Level.FINEST, e.toString(), e);
113     } catch (Exception JavaDoc e) {
114       throw new ConfigException(e);
115     }
116   }
117
118   public Object JavaDoc configure(NodeBuilder builder, Class JavaDoc type)
119     throws ConfigException
120   {
121     throw new UnsupportedOperationException JavaDoc(getClass().getName());
122   }
123 }
124
Popular Tags