KickJava   Java API By Example, From Geeks To Geeks.

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


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 // $Id: WebServiceRefHandler.java 57928 2006-10-31 01:40:00Z scott.stark@jboss.org $
25

26 import java.lang.reflect.AccessibleObject JavaDoc;
27 import java.lang.reflect.Field JavaDoc;
28 import java.lang.reflect.Method JavaDoc;
29 import java.util.HashMap JavaDoc;
30 import java.util.Map JavaDoc;
31
32 import javax.naming.Context JavaDoc;
33 import javax.xml.ws.WebServiceRef;
34 import javax.xml.ws.WebServiceRefs;
35
36 import org.jboss.logging.Logger;
37 import org.jboss.metamodel.descriptor.EnvironmentRefGroup;
38 import org.jboss.metamodel.descriptor.ServiceRef;
39
40 /**
41  * Handle @WebServiceRef annotations
42  *
43  * @author Thomas.Diesler@jboss.com
44  */

45 public class WebServiceRefHandler implements InjectionHandler
46 {
47    private static final Logger log = Logger.getLogger(WebServiceRefHandler.class);
48    private Map JavaDoc<String JavaDoc, ServiceRef> srefMap = new HashMap JavaDoc<String JavaDoc, ServiceRef>();
49
50    public void loadXml(EnvironmentRefGroup xml, InjectionContainer container)
51    {
52       if (xml == null) return;
53       if (xml.getServiceRefs() == null) return;
54       for (ServiceRef sref : xml.getServiceRefs())
55       {
56          log.debug("@WebServiceRef override: " + sref);
57          if (srefMap.get(sref.getEncName()) != null)
58                throw new IllegalStateException JavaDoc ("Duplicate <service-ref-name> in " + sref);
59          
60          srefMap.put(sref.getEncName(), sref);
61       }
62    }
63
64    public void handleClassAnnotations(Class JavaDoc type, InjectionContainer container)
65    {
66       WebServiceRef wsref = container.getAnnotation(WebServiceRef.class, type);
67       if (wsref != null)
68       {
69          bindRefOnType(type, container, wsref);
70       }
71
72       WebServiceRefs refs = container.getAnnotation(WebServiceRefs.class, type);
73       if (refs != null)
74       {
75          for (WebServiceRef refItem : refs.value())
76          {
77             bindRefOnType(type, container, refItem);
78          }
79       }
80    }
81
82    private void bindRefOnType(Class JavaDoc type, InjectionContainer container, WebServiceRef wsref)
83    {
84       String JavaDoc name = wsref.name();
85       if (name.equals(""))
86       {
87          name = InjectionUtil.getEncName(type).substring(4);
88       }
89       
90       String JavaDoc encName = "env/" + name;
91       Context JavaDoc encCtx = container.getEnc();
92       if (!container.getEncInjectors().containsKey(name))
93       {
94          ServiceRef sref = getServiceRef(name);
95          container.getEncInjectors().put(name, new WebServiceRefInjector(encCtx, encName, null, wsref, sref));
96       }
97    }
98
99    public void handleMethodAnnotations(Method JavaDoc method, InjectionContainer container, Map JavaDoc<AccessibleObject JavaDoc, Injector> injectors)
100    {
101       WebServiceRef wsref = method.getAnnotation(WebServiceRef.class);
102       if (wsref == null) return;
103
104       if (!method.getName().startsWith("set"))
105          throw new RuntimeException JavaDoc("@WebServiceRef can only be used with a set method: " + method);
106
107       String JavaDoc name = wsref.name();
108       if (name.equals(""))
109       {
110          name = InjectionUtil.getEncName(method).substring(4);
111       }
112       
113       String JavaDoc encName = "env/" + name;
114       Context JavaDoc encCtx = container.getEnc();
115       if (!container.getEncInjectors().containsKey(name))
116       {
117          Class JavaDoc refType = method.getParameterTypes()[0];
118          ServiceRef sref = getServiceRef(name);
119          container.getEncInjectors().put(name, new WebServiceRefInjector(encCtx, encName, refType, wsref, sref));
120       }
121
122       injectors.put(method, new JndiMethodInjector(method, encName, encCtx));
123    }
124
125    public void handleFieldAnnotations(Field JavaDoc field, InjectionContainer container, Map JavaDoc<AccessibleObject JavaDoc, Injector> injectors)
126    {
127       WebServiceRef wsref = field.getAnnotation(WebServiceRef.class);
128       if (wsref == null) return;
129
130       String JavaDoc name = wsref.name();
131       if (name.equals(""))
132       {
133          name = InjectionUtil.getEncName(field).substring(4);
134       }
135
136       String JavaDoc encName = "env/" + name;
137       Context JavaDoc encCtx = container.getEnc();
138       if (!container.getEncInjectors().containsKey(name))
139       {
140          Class JavaDoc refType = field.getType();
141          ServiceRef sref = getServiceRef(name);
142          container.getEncInjectors().put(name, new WebServiceRefInjector(encCtx, encName, refType, wsref, sref));
143       }
144
145       injectors.put(field, new JndiFieldInjector(field, encName, encCtx));
146    }
147
148    private ServiceRef getServiceRef(String JavaDoc name)
149    {
150       ServiceRef sref = srefMap.get(name);
151       if (sref != null)
152       {
153          log.debug("Override @WebServiceRef with: " + sref);
154       }
155       else
156       {
157          log.debug("No override for @WebServiceRef.name: " + name);
158          sref = new ServiceRef(name);
159       }
160       return sref;
161    }
162 }
163
Popular Tags