001    /**
002     *  Licensed to the Apache Software Foundation (ASF) under one or more
003     *  contributor license agreements.  See the NOTICE file distributed with
004     *  this work for additional information regarding copyright ownership.
005     *  The ASF licenses this file to You under the Apache License, Version 2.0
006     *  (the "License"); you may not use this file except in compliance with
007     *  the License.  You may obtain a copy of the License at
008     *
009     *     http://www.apache.org/licenses/LICENSE-2.0
010     *
011     *  Unless required by applicable law or agreed to in writing, software
012     *  distributed under the License is distributed on an "AS IS" BASIS,
013     *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014     *  See the License for the specific language governing permissions and
015     *  limitations under the License.
016     */
017    package org.apache.geronimo.jaxws;
018    
019    import org.apache.geronimo.jaxws.annotations.AnnotationProcessor;
020    import org.apache.geronimo.jaxws.annotations.EJBAnnotationHandler;
021    import org.apache.geronimo.jaxws.annotations.InjectionException;
022    import org.apache.geronimo.jaxws.annotations.ResourceAnnotationHandler;
023    import org.apache.geronimo.jaxws.annotations.WebServiceRefAnnotationHandler;
024    
025    import javax.naming.NamingException;
026    import javax.xml.ws.WebServiceContext;
027    import java.lang.annotation.Annotation;
028    
029    public class JAXWSAnnotationProcessor extends AnnotationProcessor {
030    
031        private JNDIResolver jndiResolver;
032        private WebServiceContext context;
033        
034        public JAXWSAnnotationProcessor(JNDIResolver jndiResolver, WebServiceContext context) {
035            this.jndiResolver = jndiResolver;
036            this.context = context;
037            
038            // register @Resource annotation handler
039            registerHandler(new JAXWSResourceAnnotationHandler());
040            // register @EJB annotation handler
041            registerHandler(new JAXWSEJBAnnotationHandler());
042            // register @WebServiceRef annotation handler
043            registerHandler(new JAXWSWebServiceRefAnnotationHandler());
044        }
045    
046        private Object lookupJNDI(String name, Class<?> type)
047                throws InjectionException {
048            try {
049                return jndiResolver.resolve(name, type);
050            } catch (NamingException e) {
051                throw new InjectionException("JNDI injection failed for resource '" 
052                        + name + "'", e);
053            }
054        }
055    
056        private class JAXWSResourceAnnotationHandler extends
057                ResourceAnnotationHandler {
058            public Object getAnnotationValue(Annotation annotation,
059                                             String name,
060                                             Class<?> type)
061                    throws InjectionException {
062                if (WebServiceContext.class.isAssignableFrom(type)) {
063                    return type.cast(context);
064                } else {
065                    return lookupJNDI(name, type);
066                }
067            }
068        }
069    
070        private class JAXWSEJBAnnotationHandler extends EJBAnnotationHandler {
071            public Object getAnnotationValue(Annotation annotation,
072                                             String name,
073                                             Class<?> type)
074                    throws InjectionException {
075                return lookupJNDI(name, type);
076            }
077        }
078    
079        private class JAXWSWebServiceRefAnnotationHandler extends
080                WebServiceRefAnnotationHandler {
081            public Object getAnnotationValue(Annotation annotation,
082                                             String name,
083                                             Class<?> type)
084                    throws InjectionException {
085                return lookupJNDI(name, type);
086            }
087        }
088    }