1 22 package org.jboss.injection; 23 24 26 import java.lang.reflect.AccessibleObject ; 27 import java.lang.reflect.Field ; 28 import java.lang.reflect.Method ; 29 import java.util.HashMap ; 30 import java.util.Map ; 31 32 import javax.naming.Context ; 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 45 public class WebServiceRefHandler implements InjectionHandler 46 { 47 private static final Logger log = Logger.getLogger(WebServiceRefHandler.class); 48 private Map <String , ServiceRef> srefMap = new HashMap <String , 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 ("Duplicate <service-ref-name> in " + sref); 59 60 srefMap.put(sref.getEncName(), sref); 61 } 62 } 63 64 public void handleClassAnnotations(Class 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 type, InjectionContainer container, WebServiceRef wsref) 83 { 84 String name = wsref.name(); 85 if (name.equals("")) 86 { 87 name = InjectionUtil.getEncName(type).substring(4); 88 } 89 90 String encName = "env/" + name; 91 Context 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 method, InjectionContainer container, Map <AccessibleObject , 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 ("@WebServiceRef can only be used with a set method: " + method); 106 107 String name = wsref.name(); 108 if (name.equals("")) 109 { 110 name = InjectionUtil.getEncName(method).substring(4); 111 } 112 113 String encName = "env/" + name; 114 Context encCtx = container.getEnc(); 115 if (!container.getEncInjectors().containsKey(name)) 116 { 117 Class 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 field, InjectionContainer container, Map <AccessibleObject , Injector> injectors) 126 { 127 WebServiceRef wsref = field.getAnnotation(WebServiceRef.class); 128 if (wsref == null) return; 129 130 String name = wsref.name(); 131 if (name.equals("")) 132 { 133 name = InjectionUtil.getEncName(field).substring(4); 134 } 135 136 String encName = "env/" + name; 137 Context encCtx = container.getEnc(); 138 if (!container.getEncInjectors().containsKey(name)) 139 { 140 Class 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 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 |