1 19 20 package org.netbeans.modules.websvc.wsitconf.wsdlmodelext; 21 22 import java.io.IOException ; 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 50 public class TransportModelHelper { 51 52 private static final String TCP_NONJSR109 = "com.sun.xml.ws.transport.tcp.server.glassfish.WSStartupServlet"; 54 57 public TransportModelHelper() { 58 } 59 60 public static OptimizedMimeSerialization getOptimizedMimeSerialization(Policy p) { 61 return PolicyModelHelper.getTopLevelElement(p, OptimizedMimeSerialization.class); 62 } 63 64 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 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 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 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 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 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 servletClassName = s.getImplementationClass(); Servlet servlet = getServlet(wApp, servletClassName); 129 if (servlet == null) { try { 131 String servletName = s.getName(); 132 servlet = (Servlet)wApp.addBean("Servlet", new String []{"ServletName","ServletClass"}, new Object []{servletName,servletClassName}, "ServletName"); servlet.setLoadOnStartup(new java.math.BigInteger ("1")); String 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 []{"ServletName","UrlPattern"}, new Object []{servletName, "/" + serviceName}, "ServletName"); wApp.write(wm.getDeploymentDescriptor()); 142 } catch (NameAlreadyUsedException ex) { 143 ex.printStackTrace(); 144 } catch (ClassNotFoundException ex) { 145 ex.printStackTrace(); 146 } 147 } else { 148 servlet.setLoadOnStartup(new java.math.BigInteger ("1")); 149 } 150 } else { 151 if (!isTcpListener(wApp)) { 152 try { 153 Listener l = (Listener)wApp.addBean("Listener", new String []{"ListenerClass"}, new Object []{TCP_NONJSR109}, "ListenerClass"); wApp.write(wm.getDeploymentDescriptor()); 156 } catch (NameAlreadyUsedException ex) { 157 ex.printStackTrace(); 158 } catch (ClassNotFoundException ex) { 159 ex.printStackTrace(); 160 } 161 } 162 } 163 } catch (IOException 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 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 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 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 |