KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > websvc > wsitconf > wsdlmodelext > TransportModelHelper


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 2006 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19
20 package org.netbeans.modules.websvc.wsitconf.wsdlmodelext;
21
22 import java.io.IOException JavaDoc;
23 import org.netbeans.api.project.Project;
24 import org.netbeans.modules.j2ee.dd.api.common.NameAlreadyUsedException;
25 import org.netbeans.modules.j2ee.dd.api.web.DDProvider;
26 import org.netbeans.modules.j2ee.dd.api.web.Listener;
27 import org.netbeans.modules.j2ee.dd.api.web.Servlet;
28 import org.netbeans.modules.j2ee.dd.api.web.ServletMapping;
29 import org.netbeans.modules.j2ee.dd.api.web.WebApp;
30 import org.netbeans.modules.web.api.webmodule.WebModule;
31 import org.netbeans.modules.websvc.api.jaxws.project.config.Service;
32 import org.netbeans.modules.websvc.jaxwsruntimemodel.JavaWsdlMapper;
33 import org.netbeans.modules.websvc.wsitmodelext.policy.All;
34 import org.netbeans.modules.websvc.wsitmodelext.policy.Policy;
35 import org.netbeans.modules.websvc.wsitmodelext.mtom.MtomQName;
36 import org.netbeans.modules.websvc.wsitmodelext.mtom.OptimizedMimeSerialization;
37 import org.netbeans.modules.websvc.wsitmodelext.transport.AutomaticallySelectFastInfoset;
38 import org.netbeans.modules.websvc.wsitmodelext.transport.AutomaticallySelectOptimalTransport;
39 import org.netbeans.modules.websvc.wsitmodelext.transport.FIQName;
40 import org.netbeans.modules.websvc.wsitmodelext.transport.OptimizedFastInfosetSerialization;
41 import org.netbeans.modules.websvc.wsitmodelext.transport.OptimizedTCPTransport;
42 import org.netbeans.modules.websvc.wsitmodelext.transport.TCPQName;
43 import org.netbeans.modules.xml.wsdl.model.Binding;
44 import org.netbeans.modules.xml.xam.Model;
45
46 /**
47  *
48  * @author Martin Grebac
49  */

50 public class TransportModelHelper {
51
52     private static final String JavaDoc TCP_NONJSR109 = "com.sun.xml.ws.transport.tcp.server.glassfish.WSStartupServlet"; //NOI18N
53

54     /**
55      * Creates a new instance of TransportModelHelper
56      */

57     public TransportModelHelper() {
58     }
59     
60     public static OptimizedMimeSerialization getOptimizedMimeSerialization(Policy p) {
61         return PolicyModelHelper.getTopLevelElement(p, OptimizedMimeSerialization.class);
62     }
63     
64     // checks if Mtom is enabled in the config wsdl on specified binding
65
public static boolean isMtomEnabled(Binding b) {
66         Policy p = PolicyModelHelper.getPolicyForElement(b);
67         if (p != null) {
68             OptimizedMimeSerialization mtomAssertion = getOptimizedMimeSerialization(p);
69             return (mtomAssertion != null);
70         }
71         return false;
72     }
73     
74     // enables Mtom in the config wsdl on specified binding
75
public static void enableMtom(Binding b) {
76         All a = PolicyModelHelper.createPolicy(b);
77         PolicyModelHelper.createElement(a, MtomQName.OPTIMIZEDMIMESERIALIZATION.getQName(), OptimizedMimeSerialization.class, false);
78     }
79
80     // disables Mtom in the config wsdl on specified binding
81
public static void disableMtom(Binding b) {
82         Policy p = PolicyModelHelper.getPolicyForElement(b);
83         OptimizedMimeSerialization mtom = getOptimizedMimeSerialization(p);
84         if (mtom != null) {
85             PolicyModelHelper.removeElement(mtom);
86         }
87         PolicyModelHelper.cleanPolicies(b);
88     }
89
90     public static OptimizedTCPTransport getOptimizedTCPTransport(Policy p) {
91         return PolicyModelHelper.getTopLevelElement(p, OptimizedTCPTransport.class);
92     }
93     
94     // checks if TCP is enabled in the config wsdl on specified binding
95
public static boolean isTCPEnabled(Binding b) {
96         Policy p = PolicyModelHelper.getPolicyForElement(b);
97         if (p != null) {
98             OptimizedTCPTransport tcpAssertion = getOptimizedTCPTransport(p);
99             if (tcpAssertion != null) {
100                 return tcpAssertion.isEnabled();
101             }
102         }
103         return false;
104     }
105     
106     // enables/disables TCP in the config wsdl on specified binding
107
public static void enableTCP(Service s, boolean isFromJava, Binding b, Project p, boolean enable, boolean jsr109) {
108         All a = PolicyModelHelper.createPolicy(b);
109         OptimizedTCPTransport tcp =
110                 PolicyModelHelper.createElement(a, TCPQName.OPTIMIZEDTCPTRANSPORT.getQName(),
111                 OptimizedTCPTransport.class, false);
112         
113         // make sure the listener is there (in Web project and jsr109 deployment
114
if (enable) {
115
116             Model model = b.getModel();
117             boolean isTransaction = model.isIntransaction();
118             if (!isTransaction) {
119                 model.startTransaction();
120             }
121
122             WebModule wm = WebModule.getWebModule(p.getProjectDirectory());
123             if (wm != null) {
124                 try {
125                     WebApp wApp = DDProvider.getDefault ().getMergedDDRoot(wm.getDeploymentDescriptor());
126                     if (jsr109) {
127                         String JavaDoc servletClassName = s.getImplementationClass(); //NOI18N
128
Servlet servlet = getServlet(wApp, servletClassName);
129                         if (servlet == null) { //NOI18N
130
try {
131                                 String JavaDoc servletName = s.getName();
132                                 servlet = (Servlet)wApp.addBean("Servlet", new String JavaDoc[]{"ServletName","ServletClass"}, //NOI18N
133
new Object JavaDoc[]{servletName,servletClassName}, "ServletName"); //NOI18N
134
servlet.setLoadOnStartup(new java.math.BigInteger JavaDoc("1")); //NOI18N
135
String JavaDoc serviceName = s.getServiceName();
136                                 if (serviceName == null) {
137                                     serviceName = servletClassName.substring(servletClassName.lastIndexOf(".")+1) + JavaWsdlMapper.SERVICE;
138                                 }
139                                 ServletMapping servletMapping = (ServletMapping)wApp.addBean("ServletMapping", new String JavaDoc[]{"ServletName","UrlPattern"}, //NOI18N
140
new Object JavaDoc[]{servletName, "/" + serviceName}, "ServletName"); //NOI18N
141
wApp.write(wm.getDeploymentDescriptor());
142                             } catch (NameAlreadyUsedException ex) {
143                                 ex.printStackTrace();
144                             } catch (ClassNotFoundException JavaDoc ex) {
145                                 ex.printStackTrace();
146                             }
147                         } else {
148                             servlet.setLoadOnStartup(new java.math.BigInteger JavaDoc("1"));
149                         }
150                     } else {
151                         if (!isTcpListener(wApp)) {
152                             try {
153                                 Listener l = (Listener)wApp.addBean("Listener", new String JavaDoc[]{"ListenerClass"}, //NOI18N
154
new Object JavaDoc[]{TCP_NONJSR109}, "ListenerClass"); //NOI18N
155
wApp.write(wm.getDeploymentDescriptor());
156                             } catch (NameAlreadyUsedException ex) {
157                                 ex.printStackTrace();
158                             } catch (ClassNotFoundException JavaDoc ex) {
159                                 ex.printStackTrace();
160                             }
161                         }
162                     }
163                 } catch (IOException JavaDoc ex) {
164                     ex.printStackTrace();
165                 }
166             }
167             try {
168                 tcp.enable(enable);
169             } finally {
170                 if (!isTransaction) {
171                     model.endTransaction();
172                 }
173             }
174         } else {
175             removeTCP(b);
176             PolicyModelHelper.cleanPolicies(b);
177         }
178     }
179
180     private static boolean isTcpListener(WebApp wa) {
181         Listener[] listeners = wa.getListener();
182         for (Listener l : listeners) {
183             if (TCP_NONJSR109.equals(l.getListenerClass())) {
184                 return true;
185             }
186         }
187         return false;
188     }
189
190     private static Servlet getServlet(WebApp wa, String JavaDoc className) {
191         Servlet[] servlets = wa.getServlet();
192         for (Servlet s : servlets) {
193             if (className.equals(s.getServletClass())) {
194                 return s;
195             }
196         }
197         return null;
198     }
199     
200    public static void removeTCP(Binding b) {
201         Policy p = PolicyModelHelper.getPolicyForElement(b);
202         OptimizedTCPTransport tcp = getOptimizedTCPTransport(p);
203         if (tcp != null) {
204             PolicyModelHelper.removeElement(tcp);
205         }
206     }
207        
208     public static OptimizedFastInfosetSerialization getOptimizedFastInfosetSerialization(Policy p) {
209         return PolicyModelHelper.getTopLevelElement(p, OptimizedFastInfosetSerialization.class);
210     }
211     
212     // checks if FI is enabled in the config wsdl on specified binding
213
public static boolean isFIEnabled(Binding b) {
214         Policy p = PolicyModelHelper.getPolicyForElement(b);
215         if (p != null) {
216             OptimizedFastInfosetSerialization fiAssertion = getOptimizedFastInfosetSerialization(p);
217             if (fiAssertion != null) {
218                 return fiAssertion.isEnabled();
219             }
220         }
221         return true;
222     }
223     
224     // enables/disables FI in the config wsdl on specified binding
225
public static void enableFI(Binding b, boolean enable) {
226         All a = PolicyModelHelper.createPolicy(b);
227         OptimizedFastInfosetSerialization fi =
228                 PolicyModelHelper.createElement(a, FIQName.OPTIMIZEDFASTINFOSETSERIALIZATION.getQName(),
229                 OptimizedFastInfosetSerialization.class, false);
230         Model model = b.getModel();
231         boolean isTransaction = model.isIntransaction();
232         if (enable) {
233             removeFI(b);
234             PolicyModelHelper.cleanPolicies(b);
235         } else {
236             if (!isTransaction) {
237                 model.startTransaction();
238             }
239             try {
240                 fi.enable(enable);
241             } finally {
242                 if (!isTransaction) {
243                     model.endTransaction();
244                 }
245             }
246         }
247         if (enable) {
248             PolicyModelHelper.cleanPolicies(b);
249         }
250     }
251     
252     public static void removeFI(Binding b) {
253         Policy p = PolicyModelHelper.getPolicyForElement(b);
254         OptimizedFastInfosetSerialization fi = getOptimizedFastInfosetSerialization(p);
255         if (fi != null) {
256             PolicyModelHelper.removeElement(fi);
257         }
258     }
259     
260     public static AutomaticallySelectFastInfoset getAutoEncoding(Policy p) {
261         return (AutomaticallySelectFastInfoset) PolicyModelHelper.getTopLevelElement(p, AutomaticallySelectFastInfoset.class);
262     }
263     
264     public static boolean isAutoEncodingEnabled(Binding b) {
265         Policy p = PolicyModelHelper.getPolicyForElement(b);
266         if (p != null) {
267             AutomaticallySelectFastInfoset ae = getAutoEncoding(p);
268             return (ae != null);
269         }
270         return false;
271     }
272     
273     public static void setAutoEncoding(Binding b, boolean enable) {
274         if (enable) {
275             All a = PolicyModelHelper.createPolicy(b);
276             PolicyModelHelper.createElement(a, FIQName.AUTOMATICALLYSELECTFASTINFOSET.getQName(), AutomaticallySelectFastInfoset.class, false);
277         } else {
278             Policy p = PolicyModelHelper.getPolicyForElement(b);
279             AutomaticallySelectFastInfoset ae = getAutoEncoding(p);
280             if (ae != null) {
281                 PolicyModelHelper.removeElement(ae);
282             }
283         }
284     }
285     
286     public static AutomaticallySelectOptimalTransport getAutoTransport(Policy p) {
287         return (AutomaticallySelectOptimalTransport) PolicyModelHelper.getTopLevelElement(p, AutomaticallySelectOptimalTransport.class);
288     }
289     
290     public static boolean isAutoTransportEnabled(Binding b) {
291         Policy p = PolicyModelHelper.getPolicyForElement(b);
292         if (p != null) {
293             AutomaticallySelectOptimalTransport at = getAutoTransport(p);
294             return (at != null);
295         }
296         return false;
297     }
298     
299     public static void setAutoTransport(Binding b, boolean enable) {
300         if (enable) {
301             All a = PolicyModelHelper.createPolicy(b);
302             PolicyModelHelper.createElement(a,
303                     TCPQName.AUTOMATICALLYSELECTOPTIMALTRANSPORT.getQName(),
304                     AutomaticallySelectOptimalTransport.class, false);
305         } else {
306             Policy p = PolicyModelHelper.getPolicyForElement(b);
307             AutomaticallySelectOptimalTransport at = getAutoTransport(p);
308             if (at != null) {
309                 PolicyModelHelper.removeElement(at);
310             }
311         }
312     }
313
314 }
315
Popular Tags