KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > enterprise > webservice > ServiceEngineUtil


1 /*
2  * The contents of this file are subject to the terms
3  * of the Common Development and Distribution License
4  * (the License). You may not use this file except in
5  * compliance with the License.
6  *
7  * You can obtain a copy of the license at
8  * https://glassfish.dev.java.net/public/CDDLv1.0.html or
9  * glassfish/bootstrap/legal/CDDLv1.0.txt.
10  * See the License for the specific language governing
11  * permissions and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL
14  * Header Notice in each file and include the License file
15  * at glassfish/bootstrap/legal/CDDLv1.0.txt.
16  * If applicable, add the following below the CDDL Header,
17  * with the fields enclosed by brackets [] replaced by
18  * you own identifying information:
19  * "Portions Copyrighted [year] [name of copyright owner]"
20  *
21  * Copyright 2006 Sun Microsystems, Inc. All rights reserved.
22  */

23 package com.sun.enterprise.webservice;
24
25 import com.sun.enterprise.deployment.ServiceRefPortInfo;
26 import com.sun.enterprise.deployment.ServiceReferenceDescriptor;
27 import com.sun.xml.ws.spi.runtime.StubBase;
28 import com.sun.xml.ws.client.BindingProviderProperties;
29 import com.sun.xml.ws.spi.runtime.ClientTransportFactory;
30 import com.sun.logging.LogDomains;
31 import java.io.File JavaDoc;
32 import java.io.FileInputStream JavaDoc;
33 import java.io.IOException JavaDoc;
34 import java.util.Map JavaDoc;
35 import java.util.Properties JavaDoc;
36 import javax.xml.namespace.QName JavaDoc;
37 import javax.xml.ws.BindingProvider;
38 import java.util.logging.Logger JavaDoc;
39 import java.util.logging.Level JavaDoc;
40 /**
41  * If web service client is JBI enabled, transport factory is set to NMR, otherwise
42  * ususal JAXWS stack is used.
43  * @author Manisha Umbarje
44  */

45 public class ServiceEngineUtil {
46     
47     public static String JavaDoc JBI_ENABLED = "jbi-enabled";
48     private static final String JavaDoc SE_DISABLE_PROPERTY = "com.sun.enterprise.jbi.se.disable";
49     private static String JavaDoc seDisablePropertyValue = null;
50     private static boolean seEnabledFlag = true;
51
52     private static Logger JavaDoc logger =
53             LogDomains.getLogger(LogDomains.SERVER_LOGGER);
54     /** Creates a new instance of ServiceEngineUtil */
55     public ServiceEngineUtil() {
56     }
57     
58     public static boolean isServiceJBIEnabled(ServiceReferenceDescriptor desc) {
59         if(isServiceEngineEnabled()) {
60             java.util.Set JavaDoc portsInfo = desc.getPortsInfo();
61             java.util.Iterator JavaDoc ports = portsInfo.iterator();
62             while(ports.hasNext()) {
63                 ServiceRefPortInfo portDesc = (ServiceRefPortInfo)ports.next();
64                 if(isPortJbiEnabled(portDesc))
65                     return true;
66             }
67         }
68         return false;
69     }
70     public static void setJBITransportFactory(ServiceReferenceDescriptor serviceRef,
71             Object JavaDoc stubObj, Class JavaDoc sei) {
72         if(isServiceEngineEnabled()) {
73             ServiceRefPortInfo portInfo = getPortInfo(sei, serviceRef);
74             boolean jbiEnabledFlag = isPortJbiEnabled(portInfo);
75             setJBITransportFactory(portInfo, stubObj, jbiEnabledFlag);
76         } else {
77             logger.log(Level.FINE, "Java EE Service Engine's functionality is disabled");
78         }
79     }
80     
81     
82     public static void setJBITransportFactory(ServiceRefPortInfo portInfo,
83             Object JavaDoc stubObj, boolean jbiEnabled) {
84         if(isServiceEngineEnabled()) {
85             ServiceReferenceDescriptor serviceRef = portInfo.getServiceReference();
86             if(serviceRef != null && stubObj != null) {
87                 if (isServiceEngineEnabled() && jbiEnabled) {
88                     if(serviceRef.getMappingFileUri() == null) {
89                         setTransportFactory((com.sun.xml.ws.spi.runtime.StubBase)stubObj, portInfo);
90                     } else {
91                         
92                         setTransportFactory((com.sun.xml.rpc.spi.runtime.StubBase)stubObj, portInfo);
93                     }
94                     
95                 }
96                 
97             }
98         } else {
99             logger.log(Level.FINE, "Java EE Service Engine's functionality is disabled");
100         }
101         
102     }
103     
104      public static boolean isServiceEngineEnabled() {
105         //safe to assume that JBI is installed
106
if(seDisablePropertyValue == null) {
107             seDisablePropertyValue = System.getProperty(SE_DISABLE_PROPERTY);
108             logger.log(Level.FINE,SE_DISABLE_PROPERTY ," value is " + seDisablePropertyValue);
109             if(seDisablePropertyValue != null)
110                 seEnabledFlag = "false".equals(seDisablePropertyValue);
111         }
112         return seEnabledFlag;
113     }
114
115        
116     private static ServiceRefPortInfo getPortInfo(Class JavaDoc sei,
117             ServiceReferenceDescriptor serviceRef) {
118         if(serviceRef != null) {
119             if(sei == null) {
120                 try {
121                     sei = Class.forName(serviceRef.getServiceInterface());
122                 }catch(Throwable JavaDoc cnfe) {
123                     //Log the message
124
if (logger.isLoggable(Level.WARNING)) {
125                         logger.log(Level.WARNING,
126                                 "Error during setting of transport factory"+cnfe.getMessage());
127                     }
128                     return null;
129                 }
130             }
131             return serviceRef.getPortInfo(sei.getName());
132         }
133         return null;
134     }
135     
136     private static boolean isPortJbiEnabled(Class JavaDoc sei,
137             ServiceReferenceDescriptor serviceRef) {
138         if(serviceRef != null) {
139             if(sei == null) {
140                 try {
141                     sei = Class.forName(serviceRef.getServiceInterface());
142                 }catch(Throwable JavaDoc cnfe) {
143                     //Log the message
144
if (logger.isLoggable(Level.WARNING)) {
145                         logger.log(Level.WARNING,
146                                 "Error during setting of transport factory"+cnfe.getMessage());
147                     }
148                     return false;
149                 }
150             }
151             return isPortJbiEnabled(serviceRef.getPortInfo(sei.getName()));
152         }
153         return false;
154     }
155     
156     private static boolean isPortJbiEnabled(ServiceRefPortInfo portInfo) {
157         if(portInfo != null) {
158             String JavaDoc value = portInfo.getStubPropertyValue(JBI_ENABLED);
159             logger.log(Level.FINEST, "JBI_ENABLED flag value is : " + value);
160             return "true".equals(value);
161         } else {
162             // This means the deployer did not resolve the port to
163
// which this SEI is mapped, return false;
164
}
165         return false;
166     }
167     
168     private static void setTransportFactory(
169             com.sun.xml.ws.spi.runtime.StubBase stubObj, ServiceRefPortInfo portInfo) {
170         BindingProvider bindingProvider = (BindingProvider)stubObj;
171         
172         Map JavaDoc<String JavaDoc,Object JavaDoc> requestContext = bindingProvider.getRequestContext();
173         String JavaDoc obj = (String JavaDoc)requestContext.get(com.sun.xml.ws.client.BindingProviderProperties.ONE_WAY_OPERATION);
174         boolean oneWay = Boolean.valueOf(obj).booleanValue();
175         try {
176             // This is done to avoide classloader issues.
177
// Check out ServiceEngineRtObjectFactory for more details.
178
com.sun.xml.ws.spi.runtime.ClientTransportFactory factory =
179                     (com.sun.xml.ws.spi.runtime.ClientTransportFactory)
180                     ServiceEngineRtObjectFactory.getInstance().
181                     getFacade().getTransportFactory(portInfo,
182                     true, oneWay);
183             if (factory == null)
184                 return ;
185             
186             logger.log(Level.FINE, "Before setting setTransportFactory to NMR");
187             // Set JBI transport factory
188
stubObj._setTransportFactory(factory);
189             
190         } catch(Exception JavaDoc e) {
191             // Do nothing.
192
if (logger.isLoggable(Level.WARNING)) {
193                 logger.log(Level.WARNING,
194                         "Error during setting of transport factory"+e.getMessage());
195             }
196         }
197     }
198     
199     private static void setTransportFactory(
200             com.sun.xml.rpc.spi.runtime.StubBase stubObj, ServiceRefPortInfo portInfo) {
201         try {
202             // This is done to avoide classloader issues.
203
// Check out ServiceEngineRtObjectFactory for more details.
204
com.sun.xml.rpc.spi.runtime.ClientTransportFactory factory =
205                     (com.sun.xml.rpc.spi.runtime.ClientTransportFactory)
206                     ServiceEngineRtObjectFactory.getInstance().
207                     getFacade().getTransportFactory(portInfo,
208                     false, true);
209             if (factory == null)
210                 return ;
211             
212             logger.log(Level.FINE, "Before setting setTransportFactory to NMR");
213             // Set JBI transport factory
214
stubObj._setTransportFactory(factory);
215             
216         } catch(Exception JavaDoc e) {
217             // Do nothing.
218
if (logger.isLoggable(Level.WARNING)) {
219                 logger.log(Level.WARNING,
220                         "Error during setting of transport factory"+e.getMessage());
221             }
222         }
223     }
224 }
225
Popular Tags