View Javadoc

1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    *
9    *      http://www.apache.org/licenses/LICENSE-2.0
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
16   */
17  
18  package org.apache.commons.proxy.provider.remoting;
19  
20  import org.apache.commons.proxy.ObjectProvider;
21  import org.apache.commons.proxy.exception.ObjectProviderException;
22  
23  import javax.xml.namespace.QName;
24  import javax.xml.rpc.Service;
25  import javax.xml.rpc.ServiceException;
26  import javax.xml.rpc.ServiceFactory;
27  import java.net.MalformedURLException;
28  import java.net.URL;
29  
30  /**
31   * Returns a proxy for a JAX-RPC-based service.
32   *
33   * <p>
34   * <b>Dependencies</b>:
35   * <ul>
36   *   <li>A JAX-RPC implementation</li>
37   * </ul>
38   * </p>
39   * @author James Carman
40   * @since 1.0
41   */
42  public class JaxRpcProvider implements ObjectProvider
43  {
44  //----------------------------------------------------------------------------------------------------------------------
45  // Fields
46  //----------------------------------------------------------------------------------------------------------------------
47  
48      private Class serviceInterface;
49      private String wsdlUrl;
50      private String serviceNamespaceUri;
51      private String serviceLocalPart;
52      private String servicePrefix;
53      private String portNamespaceUri;
54      private String portLocalPart;
55      private String portPrefix;
56  
57  //----------------------------------------------------------------------------------------------------------------------
58  // Constructors
59  //----------------------------------------------------------------------------------------------------------------------
60  
61      public JaxRpcProvider()
62      {
63      }
64  
65      public JaxRpcProvider( Class serviceInterface )
66      {
67          this.serviceInterface = serviceInterface;
68      }
69  
70  //----------------------------------------------------------------------------------------------------------------------
71  // ObjectProvider Implementation
72  //----------------------------------------------------------------------------------------------------------------------
73  
74      public Object getObject()
75      {
76          try
77          {
78              final Service service = ( wsdlUrl == null ?
79                                        ServiceFactory.newInstance().createService( getServiceQName() ) : ServiceFactory
80                      .newInstance().createService( new URL( wsdlUrl ), getServiceQName() ) );
81              final QName portQName = getPortQName();
82              return portQName == null ? service.getPort( serviceInterface ) :
83                     service.getPort( portQName, serviceInterface );
84          }
85          catch( ServiceException e )
86          {
87              throw new ObjectProviderException( "Unable to create JAX-RPC service proxy.", e );
88          }
89          catch( MalformedURLException e )
90          {
91              throw new ObjectProviderException( "Invalid URL given.", e );
92          }
93      }
94  
95  //----------------------------------------------------------------------------------------------------------------------
96  // Getter/Setter Methods
97  //----------------------------------------------------------------------------------------------------------------------
98  
99      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