001: /*
002: * JBoss, Home of Professional Open Source.
003: * Copyright 2006, Red Hat Middleware LLC, and individual contributors
004: * as indicated by the @author tags. See the copyright.txt file in the
005: * distribution for a full listing of individual contributors.
006: *
007: * This is free software; you can redistribute it and/or modify it
008: * under the terms of the GNU Lesser General Public License as
009: * published by the Free Software Foundation; either version 2.1 of
010: * the License, or (at your option) any later version.
011: *
012: * This software is distributed in the hope that it will be useful,
013: * but WITHOUT ANY WARRANTY; without even the implied warranty of
014: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
015: * Lesser General Public License for more details.
016: *
017: * You should have received a copy of the GNU Lesser General Public
018: * License along with this software; if not, write to the Free
019: * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
020: * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
021: */
022: package org.jboss.injection;
023:
024: // $Id: WebServiceRefHandler.java 61248 2007-03-10 04:12:47Z thomas.diesler@jboss.com $
025:
026: import java.lang.reflect.AccessibleObject;
027: import java.lang.reflect.Field;
028: import java.lang.reflect.Method;
029: import java.util.HashMap;
030: import java.util.Map;
031:
032: import javax.naming.Context;
033: import javax.xml.ws.WebServiceRef;
034: import javax.xml.ws.WebServiceRefs;
035:
036: import org.jboss.logging.Logger;
037: import org.jboss.metadata.serviceref.ServiceRefDelegate;
038: import org.jboss.metamodel.descriptor.EnvironmentRefGroup;
039: import org.jboss.ws.integration.ServiceRefMetaData;
040:
041: /**
042: * Handle @WebServiceRef annotations
043: *
044: * @author Thomas.Diesler@jboss.com
045: */
046: public class WebServiceRefHandler implements InjectionHandler {
047: private static final Logger log = Logger
048: .getLogger(WebServiceRefHandler.class);
049: private Map<String, ServiceRefMetaData> srefMap = new HashMap<String, ServiceRefMetaData>();
050:
051: public void loadXml(EnvironmentRefGroup xml,
052: InjectionContainer container) {
053: if (xml == null)
054: return;
055: if (xml.getServiceRefs() == null)
056: return;
057: for (ServiceRefMetaData sref : xml.getServiceRefs()) {
058: log.debug("@WebServiceRef override: " + sref);
059: if (srefMap.get(sref.getServiceRefName()) != null)
060: throw new IllegalStateException(
061: "Duplicate <service-ref-name> in " + sref);
062:
063: srefMap.put(sref.getServiceRefName(), sref);
064: }
065: }
066:
067: public void handleClassAnnotations(Class type,
068: InjectionContainer container) {
069: WebServiceRef wsref = container.getAnnotation(
070: WebServiceRef.class, type);
071: if (wsref != null) {
072: bindRefOnType(type, container, wsref);
073: }
074:
075: WebServiceRefs refs = container.getAnnotation(
076: WebServiceRefs.class, type);
077: if (refs != null) {
078: for (WebServiceRef refItem : refs.value()) {
079: bindRefOnType(type, container, refItem);
080: }
081: }
082: }
083:
084: private void bindRefOnType(Class type,
085: InjectionContainer container, WebServiceRef wsref) {
086: String name = wsref.name();
087: if (name.equals(""))
088: name = InjectionUtil.getEncName(type).substring(4);
089:
090: if (!container.getEncInjectors().containsKey(name)) {
091: String encName = "env/" + name;
092: ServiceRefMetaData sref = getServiceRefForName(name);
093: container.getEncInjectors().put(name,
094: new WebServiceRefInjector(encName, type, sref));
095: }
096: }
097:
098: public void handleMethodAnnotations(Method method,
099: InjectionContainer container,
100: Map<AccessibleObject, Injector> injectors) {
101: WebServiceRef wsref = method.getAnnotation(WebServiceRef.class);
102: if (wsref == null)
103: return;
104:
105: if (!method.getName().startsWith("set"))
106: throw new RuntimeException(
107: "@WebServiceRef can only be used with a set method: "
108: + method);
109:
110: String name = wsref.name();
111: if (name.equals(""))
112: name = InjectionUtil.getEncName(method).substring(4);
113:
114: String encName = "env/" + name;
115: Context encCtx = container.getEnc();
116: if (!container.getEncInjectors().containsKey(name)) {
117: ServiceRefMetaData sref = getServiceRefForName(name);
118: container.getEncInjectors().put(name,
119: new WebServiceRefInjector(encName, method, sref));
120: }
121:
122: injectors.put(method, new JndiMethodInjector(method, encName,
123: encCtx));
124: }
125:
126: public void handleFieldAnnotations(Field field,
127: InjectionContainer container,
128: Map<AccessibleObject, Injector> injectors) {
129: WebServiceRef wsref = field.getAnnotation(WebServiceRef.class);
130: if (wsref == null)
131: return;
132:
133: String name = wsref.name();
134: if (name.equals(""))
135: name = InjectionUtil.getEncName(field).substring(4);
136:
137: String encName = "env/" + name;
138: Context encCtx = container.getEnc();
139: if (!container.getEncInjectors().containsKey(name)) {
140: ServiceRefMetaData sref = getServiceRefForName(name);
141: container.getEncInjectors().put(name,
142: new WebServiceRefInjector(encName, field, sref));
143: }
144:
145: injectors.put(field, new JndiFieldInjector(field, encName,
146: encCtx));
147: }
148:
149: private ServiceRefMetaData getServiceRefForName(String name) {
150: ServiceRefMetaData sref = srefMap.get(name);
151: if (sref == null) {
152: log.debug("No override for @WebServiceRef.name: " + name);
153: sref = new ServiceRefDelegate().newServiceRefMetaData();
154: sref.setServiceRefName(name);
155: }
156: return sref;
157: }
158: }
|