KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > axis > client > ServiceFactory


1 /*
2 * The Apache Software License, Version 1.1
3 *
4 *
5 * Copyright (c) 2001-2003 The Apache Software Foundation. All rights
6 * reserved.
7 *
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
10 * are met:
11 *
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
14 *
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in
17 * the documentation and/or other materials provided with the
18 * distribution.
19 *
20 * 3. The end-user documentation included with the redistribution,
21 * if any, must include the following acknowledgment:
22 * "This product includes software developed by the
23 * Apache Software Foundation (http://www.apache.org/)."
24 * Alternately, this acknowledgment may appear in the software itself,
25 * if and wherever such third-party acknowledgments normally appear.
26 *
27 * 4. The names "Axis" and "Apache Software Foundation" must
28 * not be used to endorse or promote products derived from this
29 * software without prior written permission. For written
30 * permission, please contact apache@apache.org.
31 *
32 * 5. Products derived from this software may not be called "Apache",
33 * nor may "Apache" appear in their name, without prior written
34 * permission of the Apache Software Foundation.
35 *
36 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
37 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
38 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
39 * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
40 * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
41 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
42 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
43 * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
44 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
45 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
46 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
47 * SUCH DAMAGE.
48 * ====================================================================
49 *
50 * This software consists of voluntary contributions made by many
51 * individuals on behalf of the Apache Software Foundation. For more
52 * information on the Apache Software Foundation, please see
53 * <http://www.apache.org/>.
54 */

55
56 package org.jboss.axis.client;
57
58 import org.jboss.axis.EngineConfiguration;
59 import org.jboss.axis.NotImplementedException;
60 import org.jboss.axis.configuration.EngineConfigurationFactoryFinder;
61 import org.jboss.axis.utils.ClassUtils;
62
63 import javax.naming.Context JavaDoc;
64 import javax.naming.InitialContext JavaDoc;
65 import javax.naming.Name JavaDoc;
66 import javax.naming.NamingException JavaDoc;
67 import javax.naming.RefAddr JavaDoc;
68 import javax.naming.Reference JavaDoc;
69 import javax.naming.spi.ObjectFactory JavaDoc;
70 import javax.xml.namespace.QName JavaDoc;
71 import javax.xml.rpc.ServiceException JavaDoc;
72 import java.lang.reflect.Constructor JavaDoc;
73 import java.net.URL JavaDoc;
74 import java.util.Hashtable JavaDoc;
75 import java.util.Map JavaDoc;
76 import java.util.Properties JavaDoc;
77
78 /**
79  * Helper class for obtaining Services from JNDI.
80  * <p/>
81  * !!! WORK IN PROGRESS
82  *
83  * @author Glen Daniels (gdaniels@macromedia.com)
84  */

85
86 public class ServiceFactory extends javax.xml.rpc.ServiceFactory JavaDoc
87         implements ObjectFactory JavaDoc
88 {
89    // Constants for RefAddrs in the Reference.
90
public static final String JavaDoc SERVICE_CLASSNAME = "service classname";
91    public static final String JavaDoc WSDL_LOCATION = "WSDL location";
92    public static final String JavaDoc MAINTAIN_SESSION = "maintain session";
93    public static final String JavaDoc SERVICE_NAMESPACE = "service namespace";
94    public static final String JavaDoc SERVICE_LOCAL_PART = "service local part";
95
96    private static EngineConfiguration defaultEngineConfig = null;
97
98    private static EngineConfiguration getDefaultEngineConfig()
99    {
100       if (defaultEngineConfig == null)
101       {
102          defaultEngineConfig =
103                  EngineConfigurationFactoryFinder.newFactory().getClientEngineConfig();
104       }
105       return defaultEngineConfig;
106    }
107
108    /**
109     * Obtain an AxisClient reference, using JNDI if possible, otherwise
110     * creating one using the standard Axis configuration pattern. If we
111     * end up creating one and do have JNDI access, bind it to the passed
112     * name so we find it next time.
113     *
114     * @param environment
115     * @return a service
116     */

117    public static Service getService(Map JavaDoc environment)
118    {
119       Service service = null;
120       InitialContext JavaDoc context = null;
121
122       EngineConfiguration configProvider =
123               (EngineConfiguration)environment.get(EngineConfiguration.PROPERTY_NAME);
124
125       if (configProvider == null)
126          configProvider = getDefaultEngineConfig();
127
128       // First check to see if JNDI works
129
// !!! Might we need to set up context parameters here?
130
try
131       {
132          context = new InitialContext JavaDoc();
133       }
134       catch (NamingException JavaDoc e)
135       {
136       }
137
138       if (context != null)
139       {
140          String JavaDoc name = (String JavaDoc)environment.get("jndiName");
141          if (name == null)
142          {
143             name = "axisServiceName";
144          }
145
146          // We've got JNDI, so try to find an AxisClient at the
147
// specified name.
148
try
149          {
150             service = (Service)context.lookup(name);
151          }
152          catch (NamingException JavaDoc e)
153          {
154             service = new Service(configProvider);
155             try
156             {
157                context.bind(name, service);
158             }
159             catch (NamingException JavaDoc e1)
160             {
161                // !!! Couldn't do it, what should we do here?
162
}
163          }
164       }
165       else
166       {
167          service = new Service(configProvider);
168       }
169
170       return service;
171    }
172
173    public Object JavaDoc getObjectInstance(Object JavaDoc refObject, Name JavaDoc name,
174                                    Context JavaDoc nameCtx, Hashtable JavaDoc environment) throws Exception JavaDoc
175    {
176       Object JavaDoc instance = null;
177       if (refObject instanceof Reference JavaDoc)
178       {
179          Reference JavaDoc ref = (Reference JavaDoc)refObject;
180
181          RefAddr JavaDoc addr = ref.get(SERVICE_CLASSNAME);
182          Object JavaDoc obj = null;
183          // If an explicit service classname is provided, then this is a
184
// generated Service class. Just use its default constructor.
185
if (addr != null && (obj = addr.getContent()) instanceof String JavaDoc)
186          {
187             instance = ClassUtils.forName((String JavaDoc)obj).newInstance();
188          }
189          // else this is an instance of the Service class, so grab the
190
// reference data...
191
else
192          {
193             // Get the WSDL location...
194
addr = ref.get(WSDL_LOCATION);
195             if (addr != null && (obj = addr.getContent()) instanceof String JavaDoc)
196             {
197                URL JavaDoc wsdlLocation = new URL JavaDoc((String JavaDoc)obj);
198
199                // Build the service qname...
200
addr = ref.get(SERVICE_NAMESPACE);
201                if (addr != null
202                        && (obj = addr.getContent()) instanceof String JavaDoc)
203                {
204                   String JavaDoc namespace = (String JavaDoc)obj;
205                   addr = ref.get(SERVICE_LOCAL_PART);
206                   if (addr != null
207                           && (obj = addr.getContent()) instanceof String JavaDoc)
208                   {
209                      String JavaDoc localPart = (String JavaDoc)obj;
210                      QName JavaDoc serviceName = new QName JavaDoc(namespace, localPart);
211
212                      // Construct an instance of the service
213
Class JavaDoc[] formalArgs = new Class JavaDoc[]
214                      {URL JavaDoc.class, QName JavaDoc.class};
215                      Object JavaDoc[] actualArgs = new Object JavaDoc[]
216                      {wsdlLocation, serviceName};
217                      Constructor JavaDoc ctor =
218                              Service.class.getDeclaredConstructor(formalArgs);
219                      instance = ctor.newInstance(actualArgs);
220                   }
221                }
222             }
223          }
224          // If maintainSession should be set to true, there will be an
225
// addr for it.
226
addr = ref.get(MAINTAIN_SESSION);
227          if (addr != null && instance instanceof Service)
228          {
229             ((Service)instance).setMaintainSession(true);
230          }
231       }
232       return instance;
233    } // getObjectInstance
234

235    /**
236     * Create a Service instance.
237     *
238     * @param wsdlDocumentLocation URL for the WSDL document location
239     * for the service
240     * @param serviceName QName for the service.
241     * @return Service.
242     * @throws ServiceException If any error in creation of the specified service
243     */

244    public javax.xml.rpc.Service JavaDoc createService(URL JavaDoc wsdlDocumentLocation, QName JavaDoc serviceName) throws ServiceException JavaDoc
245    {
246       return new Service(wsdlDocumentLocation, serviceName);
247    }
248
249    public javax.xml.rpc.Service JavaDoc loadService(Class JavaDoc aClass) throws ServiceException JavaDoc
250    {
251       throw new NotImplementedException();
252    }
253
254    public javax.xml.rpc.Service JavaDoc loadService(URL JavaDoc url, Class JavaDoc aClass, Properties JavaDoc properties) throws ServiceException JavaDoc
255    {
256       throw new NotImplementedException();
257    }
258
259    public javax.xml.rpc.Service JavaDoc loadService(URL JavaDoc url, QName JavaDoc qName, Properties JavaDoc properties) throws ServiceException JavaDoc
260    {
261       throw new NotImplementedException();
262    }
263
264    /**
265     * Create a Service instance. Since the WSDL file is not provided
266     * here, the Service object returned is quite simpleminded.
267     * Likewise, the Call object that service.createCall will return
268     * will also be simpleminded. The caller must explicitly fill in
269     * all the info on the Call object (ie., endpoint address, etc.).
270     *
271     * @param serviceName QName for the service
272     * @return Service.
273     * @throws ServiceException If any error in creation of the specified service
274     */

275    public javax.xml.rpc.Service JavaDoc createService(QName JavaDoc serviceName) throws ServiceException JavaDoc
276    {
277       return new Service(serviceName);
278    } // createService
279
}
280
Popular Tags