001    /**
002     * 
003     * Copyright 2004 Protique Ltd
004     * 
005     * Licensed under the Apache License, Version 2.0 (the "License"); 
006     * you may not use this file except in compliance with the License. 
007     * 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     **/
018    package org.codehaus.activesoap.handler.xmlbeans;
019    
020    import org.apache.xmlbeans.XmlObject;
021    import org.codehaus.activesoap.MessageExchange;
022    
023    import javax.xml.stream.XMLStreamException;
024    import javax.xml.stream.XMLStreamWriter;
025    import java.lang.reflect.Method;
026    
027    /**
028     * @version $Revision: 1.3 $
029     */
030    public class XMLBeansInvokeMethodHandler extends XMLBeansHandler {
031        private Class serviceClass;
032        private Object instance;
033        private Method method;
034    
035        public XMLBeansInvokeMethodHandler(Class serviceClass, Method method) {
036            this.serviceClass = serviceClass;
037            this.method = method;
038        }
039    
040        public XMLBeansInvokeMethodHandler(Object instance, Method method) {
041            this.instance = instance;
042            this.method = method;
043            if (instance == null) {
044                throw new IllegalArgumentException("instance should not be null");
045            }
046            this.serviceClass = instance.getClass();
047        }
048    
049        public XMLBeansInvokeMethodHandler(Class serviceClass, Object instance, Method method) {
050            this.serviceClass = serviceClass;
051            this.instance = instance;
052            this.method = method;
053        }
054    
055        // Implementation methods
056        //-------------------------------------------------------------------------
057        protected void handleBody(MessageExchange exchange, XmlObject body, XMLStreamWriter out) throws Exception {
058            Object instance = createService(exchange);
059            invokeService(body, exchange, out, instance);
060        }
061    
062        /**
063         * Invokes the service on the given serviceInstance
064         */
065        protected void invokeService(XmlObject body, MessageExchange exchange, XMLStreamWriter out, Object serviceInstance) throws Exception {
066            Object answer = method.invoke(serviceInstance, new Object[]{body});
067            if (answer != null) {
068                if (answer instanceof XmlObject) {
069                    reply(exchange, (XmlObject) answer, out);
070                }
071                else {
072                    outputServiceResult(exchange, out, body, answer);
073                }
074            }
075        }
076    
077        /**
078         * Handles the result of a service invocation which is non null and is not an XMLBeans object.
079         * The default implementation just outputs the String value
080         */
081        protected void outputServiceResult(MessageExchange exchange, XMLStreamWriter out, XmlObject request, Object response) throws XMLStreamException {
082            // TODO think of more ways to handle the return value
083            out.writeCharacters(response.toString());
084        }
085    
086        /**
087         * Factory method to create a new instance temporary instance of the service
088         *
089         * @return
090         * @param exchange
091         */
092        protected Object createService(MessageExchange exchange) throws Exception {
093            if (instance != null) {
094                return instance;
095            }
096            return serviceClass.newInstance();
097        }
098    }