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.provider.remoting;
019    
020    import org.apache.commons.proxy.ObjectProvider;
021    import org.apache.commons.proxy.exception.ObjectProviderException;
022    
023    import javax.xml.namespace.QName;
024    import javax.xml.rpc.Service;
025    import javax.xml.rpc.ServiceException;
026    import javax.xml.rpc.ServiceFactory;
027    import java.net.MalformedURLException;
028    import java.net.URL;
029    
030    /**
031     * Returns a proxy for a JAX-RPC-based service.
032     *
033     * <p>
034     * <b>Dependencies</b>:
035     * <ul>
036     *   <li>A JAX-RPC implementation</li>
037     * </ul>
038     * </p>
039     * @author James Carman
040     * @since 1.0
041     */
042    public class JaxRpcProvider implements ObjectProvider
043    {
044    //----------------------------------------------------------------------------------------------------------------------
045    // Fields
046    //----------------------------------------------------------------------------------------------------------------------
047    
048        private Class serviceInterface;
049        private String wsdlUrl;
050        private String serviceNamespaceUri;
051        private String serviceLocalPart;
052        private String servicePrefix;
053        private String portNamespaceUri;
054        private String portLocalPart;
055        private String portPrefix;
056    
057    //----------------------------------------------------------------------------------------------------------------------
058    // Constructors
059    //----------------------------------------------------------------------------------------------------------------------
060    
061        public JaxRpcProvider()
062        {
063        }
064    
065        public JaxRpcProvider( Class serviceInterface )
066        {
067            this.serviceInterface = serviceInterface;
068        }
069    
070    //----------------------------------------------------------------------------------------------------------------------
071    // ObjectProvider Implementation
072    //----------------------------------------------------------------------------------------------------------------------
073    
074        public Object getObject()
075        {
076            try
077            {
078                final Service service = ( wsdlUrl == null ?
079                                          ServiceFactory.newInstance().createService( getServiceQName() ) : ServiceFactory
080                        .newInstance().createService( new URL( wsdlUrl ), getServiceQName() ) );
081                final QName portQName = getPortQName();
082                return portQName == null ? service.getPort( serviceInterface ) :
083                       service.getPort( portQName, serviceInterface );
084            }
085            catch( ServiceException e )
086            {
087                throw new ObjectProviderException( "Unable to create JAX-RPC service proxy.", e );
088            }
089            catch( MalformedURLException e )
090            {
091                throw new ObjectProviderException( "Invalid URL given.", e );
092            }
093        }
094    
095    //----------------------------------------------------------------------------------------------------------------------
096    // Getter/Setter Methods
097    //----------------------------------------------------------------------------------------------------------------------
098    
099        public void setPortLocalPart( String portLocalPart )
100        {
101            this.portLocalPart = portLocalPart;
102        }
103    
104        public void setPortNamespaceUri( String portNamespaceUri )
105        {
106            this.portNamespaceUri = portNamespaceUri;
107        }
108    
109        public void setPortPrefix( String portPrefix )
110        {
111            this.portPrefix = portPrefix;
112        }
113    
114        public void setServiceInterface( Class serviceInterface )
115        {
116            this.serviceInterface = serviceInterface;
117        }
118    
119        public void setServiceLocalPart( String serviceLocalPart )
120        {
121            this.serviceLocalPart = serviceLocalPart;
122        }
123    
124        public void setServiceNamespaceUri( String serviceNamespaceUri )
125        {
126            this.serviceNamespaceUri = serviceNamespaceUri;
127        }
128    
129        public void setServicePrefix( String servicePrefix )
130        {
131            this.servicePrefix = servicePrefix;
132        }
133    
134        public void setWsdlUrl( String wsdlUrl )
135        {
136            this.wsdlUrl = wsdlUrl;
137        }
138    
139    //----------------------------------------------------------------------------------------------------------------------
140    // Other Methods
141    //----------------------------------------------------------------------------------------------------------------------
142    
143        private QName getPortQName()
144        {
145            return getQName( portNamespaceUri, portLocalPart, portPrefix );
146        }
147    
148        private QName getQName( String namespaceUri, String localPart, String prefix )
149        {
150            if( namespaceUri != null && localPart != null && prefix != null )
151            {
152                return new QName( namespaceUri, localPart, prefix );
153            }
154            else if( namespaceUri != null && localPart != null )
155            {
156                return new QName( namespaceUri, localPart );
157            }
158            else if( localPart != null )
159            {
160                return new QName( localPart );
161            }
162            return null;
163        }
164    
165        private QName getServiceQName()
166        {
167            return getQName( serviceNamespaceUri, serviceLocalPart, servicePrefix );
168        }
169    }
170