KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > wsif > naming > WSIFServiceObjectFactory


1 /*
2  * The Apache Software License, Version 1.1
3  *
4  *
5  * Copyright (c) 2002 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 "WSIF" 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 and was
52  * originally based on software copyright (c) 2001, 2002, International
53  * Business Machines, Inc., http://www.apache.org. For more
54  * information on the Apache Software Foundation, please see
55  * <http://www.apache.org/>.
56  */

57
58 package org.apache.wsif.naming;
59
60 import java.util.Hashtable JavaDoc;
61
62 import javax.naming.Context JavaDoc;
63 import javax.naming.Name JavaDoc;
64 import javax.naming.RefAddr JavaDoc;
65 import javax.naming.Reference JavaDoc;
66 import javax.naming.spi.ObjectFactory JavaDoc;
67 import org.apache.wsif.WSIFService;
68 import org.apache.wsif.WSIFServiceFactory;
69 import org.apache.wsif.logging.Trc;
70
71 /**
72  * This is an object factory which creates:<br>
73  * <ul>
74  * <li>instances of WSIFService from a javax.naming.Reference object
75  * representing the service.</li>
76  * <li>instances of a service stub object from a javax.naming.Reference object
77  * representing the service .</li>
78  * </ul>
79  *
80  * @author Owen Burroughs <owenb@apache.org>
81  */

82 public class WSIFServiceObjectFactory implements ObjectFactory JavaDoc {
83
84     // Required no argument constructor
85
public WSIFServiceObjectFactory() {
86         Trc.entry(this);
87         Trc.exit();
88     }
89
90     /**
91      * Instantiates and returns a WSIFService based on information
92      * from the Reference object.
93      * @param obj The possibly null object containing location or
94      * reference information that can be used in creating an object.
95      * @param name The name of this object relative to ctx, or null
96      * if no name is specified
97      * @param context The context relative to which the name parameter
98      * is specified, or null if name is relative to the default initial context.
99      * @param env The possibly null environment that is used in creating the object.
100      * @return A WSIFService or null if this factory cannot create the type of object
101      * required given the information available.
102      */

103     public Object JavaDoc getObjectInstance(
104         Object JavaDoc obj,
105         Name JavaDoc name,
106         Context JavaDoc context,
107         Hashtable JavaDoc env)
108         throws Exception JavaDoc {
109         Trc.entry(this, obj, name, context, env);
110
111         // Check that obj is a Reference object, if not we can't use it.
112
if (obj instanceof Reference JavaDoc && obj != null) {
113             Reference JavaDoc ref = (Reference JavaDoc) obj;
114             if (ref.getClassName().equals(WSIFServiceRef.class.getName())) {
115                 String JavaDoc wsdlLoc = resolveString(ref.get("wsdlLoc"));
116                 String JavaDoc serviceNS = resolveString(ref.get("serviceNS"));
117                 String JavaDoc serviceName = resolveString(ref.get("serviceName"));
118                 String JavaDoc portTypeNS = resolveString(ref.get("portTypeNS"));
119                 String JavaDoc portTypeName = resolveString(ref.get("portTypeName"));
120
121                 if (wsdlLoc != null) {
122                     WSIFServiceFactory factory = WSIFServiceFactory.newInstance();
123                     WSIFService service =
124                         factory.getService(wsdlLoc, serviceNS, serviceName, portTypeNS, portTypeName);
125                     Trc.exit(service);
126                     return service;
127                 }
128             } else if (ref.getClassName().equals(WSIFServiceStubRef.class.getName())) {
129                 String JavaDoc wsdlLoc = resolveString(ref.get("wsdlLoc"));
130                 String JavaDoc serviceNS = resolveString(ref.get("serviceNS"));
131                 String JavaDoc serviceName = resolveString(ref.get("serviceName"));
132                 String JavaDoc portTypeNS = resolveString(ref.get("portTypeNS"));
133                 String JavaDoc portTypeName = resolveString(ref.get("portTypeName"));
134                 String JavaDoc preferredPort = resolveString(ref.get("preferredPort"));
135                 String JavaDoc className = resolveString(ref.get("className"));
136
137                 if (wsdlLoc != null) {
138                     WSIFServiceFactory factory = WSIFServiceFactory.newInstance();
139                     WSIFService service =
140                         factory.getService(wsdlLoc, serviceNS, serviceName, portTypeNS, portTypeName);
141                     Class JavaDoc iface =
142                         Class.forName(className, true, Thread.currentThread().getContextClassLoader());
143                     Object JavaDoc stub = service.getStub(preferredPort, iface);
144                     Trc.exit(stub);
145                     return stub;
146                 }
147             }
148         }
149         // Cannot create a WSIFService or stub from the information available
150
// so return null.
151
Trc.exit();
152         return null;
153     }
154
155     private String JavaDoc resolveString(RefAddr JavaDoc a) {
156         String JavaDoc e = "";
157         String JavaDoc s = (String JavaDoc) a.getContent();
158         return (e.equals(s)) ? null : s;
159     }
160 }
Popular Tags