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     *
031     * <p>
032     * <b>Dependencies</b>:
033     * <ul>
034     *   <li>Apache XML-RPC version 2.0 or greater</li>
035     * </ul>
036     * </p>
037     * @author James Carman
038     * @since 1.0
039     */
040    public class XmlRpcInvoker implements Invoker
041    {
042        private final XmlRpcHandler handler;
043        private final String handlerName;
044    
045        public XmlRpcInvoker( XmlRpcHandler handler, String handlerName )
046        {
047            this.handler = handler;
048            this.handlerName = handlerName;
049        }
050    
051        public Object invoke( Object proxy, Method method, Object[] args ) throws Throwable
052        {
053            final Object returnValue = handler.execute( handlerName + "." + method.getName(), toArgumentVector( args ) );
054            if( returnValue instanceof XmlRpcException )
055            {
056                throw new InvokerException( "Unable to execute XML-RPC call.", ( XmlRpcException )returnValue );
057    
058            }
059            return returnValue;
060        }
061    
062        private Vector toArgumentVector( Object[] args )
063        {
064            final Vector v = new Vector();
065            for( int i = 0; i < args.length; i++ )
066            {
067                Object arg = args[i];
068                v.addElement( arg );
069            }
070            return v;
071        }
072    }