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