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    
018    package org.apache.commons.proxy.invoker;
019    
020    import org.apache.commons.proxy.Invoker;
021    import org.apache.commons.proxy.exception.InvokerException;
022    import org.apache.xmlrpc.XmlRpcException;
023    import org.apache.xmlrpc.XmlRpcHandler;
024    
025    import java.lang.reflect.Method;
026    import java.util.Vector;
027    
028    /**
029     * Uses <a href="http://ws.apache.org/xmlrpc/">Apache XML-RPC</a> to invoke methods on an XML-RPC service.
030     * <p/>
031     * <p>
032     * <b>Dependencies</b>:
033     * <ul>
034     * <li>Apache XML-RPC version 2.0 or greater</li>
035     * </ul>
036     * </p>
037     *
038     * @author James Carman
039     * @since 1.0
040     */
041    public class XmlRpcInvoker implements Invoker
042    {
043    //**********************************************************************************************************************
044    // Fields
045    //**********************************************************************************************************************
046    
047        private final XmlRpcHandler handler;
048        private final String handlerName;
049    
050    //**********************************************************************************************************************
051    // Constructors
052    //**********************************************************************************************************************
053    
054        public XmlRpcInvoker( XmlRpcHandler handler, String handlerName )
055        {
056            this.handler = handler;
057            this.handlerName = handlerName;
058        }
059    
060    //**********************************************************************************************************************
061    // Invoker Implementation
062    //**********************************************************************************************************************
063    
064    
065        public Object invoke( Object proxy, Method method, Object[] args ) throws Throwable
066        {
067            final Object returnValue = handler.execute(handlerName + "." + method.getName(), toArgumentVector(args));
068            if( returnValue instanceof XmlRpcException )
069            {
070                throw new InvokerException("Unable to execute XML-RPC call.", ( XmlRpcException ) returnValue);
071            }
072            return returnValue;
073        }
074    
075    //**********************************************************************************************************************
076    // Other Methods
077    //**********************************************************************************************************************
078    
079        private Vector toArgumentVector( Object[] args )
080        {
081            final Vector v = new Vector();
082            for( int i = 0; i < args.length; i++ )
083            {
084                Object arg = args[i];
085                v.addElement(arg);
086            }
087            return v;
088        }
089    }