Thu, 31 Aug 2017 15:18:52 +0800
merge
aoqi@0 | 1 | /* |
aoqi@0 | 2 | * Copyright (c) 1997, 2013, Oracle and/or its affiliates. All rights reserved. |
aoqi@0 | 3 | * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
aoqi@0 | 4 | * |
aoqi@0 | 5 | * This code is free software; you can redistribute it and/or modify it |
aoqi@0 | 6 | * under the terms of the GNU General Public License version 2 only, as |
aoqi@0 | 7 | * published by the Free Software Foundation. Oracle designates this |
aoqi@0 | 8 | * particular file as subject to the "Classpath" exception as provided |
aoqi@0 | 9 | * by Oracle in the LICENSE file that accompanied this code. |
aoqi@0 | 10 | * |
aoqi@0 | 11 | * This code is distributed in the hope that it will be useful, but WITHOUT |
aoqi@0 | 12 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
aoqi@0 | 13 | * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
aoqi@0 | 14 | * version 2 for more details (a copy is included in the LICENSE file that |
aoqi@0 | 15 | * accompanied this code). |
aoqi@0 | 16 | * |
aoqi@0 | 17 | * You should have received a copy of the GNU General Public License version |
aoqi@0 | 18 | * 2 along with this work; if not, write to the Free Software Foundation, |
aoqi@0 | 19 | * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
aoqi@0 | 20 | * |
aoqi@0 | 21 | * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
aoqi@0 | 22 | * or visit www.oracle.com if you need additional information or have any |
aoqi@0 | 23 | * questions. |
aoqi@0 | 24 | */ |
aoqi@0 | 25 | |
aoqi@0 | 26 | package com.sun.tools.internal.ws.wscompile; |
aoqi@0 | 27 | |
aoqi@0 | 28 | import com.sun.codemodel.internal.JCodeModel; |
aoqi@0 | 29 | import com.sun.tools.internal.ws.processor.generator.GeneratorExtension; |
aoqi@0 | 30 | import com.sun.tools.internal.ws.resources.ConfigurationMessages; |
aoqi@0 | 31 | import com.sun.tools.internal.ws.resources.WscompileMessages; |
aoqi@0 | 32 | import com.sun.tools.internal.ws.util.ForkEntityResolver; |
aoqi@0 | 33 | import com.sun.tools.internal.ws.wsdl.document.jaxws.JAXWSBindingsConstants; |
aoqi@0 | 34 | import com.sun.tools.internal.ws.wsdl.document.schema.SchemaConstants; |
aoqi@0 | 35 | import com.sun.tools.internal.xjc.api.SchemaCompiler; |
aoqi@0 | 36 | import com.sun.tools.internal.xjc.api.SpecVersion; |
aoqi@0 | 37 | import com.sun.tools.internal.xjc.api.XJC; |
aoqi@0 | 38 | import com.sun.tools.internal.xjc.reader.Util; |
aoqi@0 | 39 | import com.sun.xml.internal.ws.api.streaming.XMLStreamReaderFactory; |
aoqi@0 | 40 | import com.sun.xml.internal.ws.streaming.XMLStreamReaderUtil; |
aoqi@0 | 41 | import com.sun.xml.internal.ws.util.ServiceFinder; |
aoqi@0 | 42 | import com.sun.xml.internal.ws.util.JAXWSUtils; |
aoqi@0 | 43 | import com.sun.xml.internal.ws.util.xml.XmlUtil; |
aoqi@0 | 44 | import org.w3c.dom.Element; |
aoqi@0 | 45 | import org.xml.sax.EntityResolver; |
aoqi@0 | 46 | import org.xml.sax.InputSource; |
aoqi@0 | 47 | import org.xml.sax.helpers.LocatorImpl; |
aoqi@0 | 48 | |
aoqi@0 | 49 | import javax.xml.namespace.QName; |
aoqi@0 | 50 | import javax.xml.stream.XMLStreamReader; |
aoqi@0 | 51 | |
aoqi@0 | 52 | import java.io.ByteArrayInputStream; |
aoqi@0 | 53 | import java.io.ByteArrayOutputStream; |
aoqi@0 | 54 | import java.io.File; |
aoqi@0 | 55 | import java.io.IOException; |
aoqi@0 | 56 | import java.io.InputStream; |
aoqi@0 | 57 | import java.io.Reader; |
aoqi@0 | 58 | import java.lang.reflect.Array; |
aoqi@0 | 59 | import java.net.MalformedURLException; |
aoqi@0 | 60 | import java.net.URL; |
aoqi@0 | 61 | import java.util.ArrayList; |
aoqi@0 | 62 | import java.util.Arrays; |
aoqi@0 | 63 | import java.util.List; |
aoqi@0 | 64 | import java.util.HashMap; |
aoqi@0 | 65 | import java.util.logging.Level; |
aoqi@0 | 66 | import java.util.logging.Logger; |
aoqi@0 | 67 | |
aoqi@0 | 68 | /** |
aoqi@0 | 69 | * @author Vivek Pandey |
aoqi@0 | 70 | */ |
aoqi@0 | 71 | public class WsimportOptions extends Options { |
aoqi@0 | 72 | /** |
aoqi@0 | 73 | * -wsdlLocation |
aoqi@0 | 74 | */ |
aoqi@0 | 75 | public String wsdlLocation; |
aoqi@0 | 76 | |
aoqi@0 | 77 | /** |
aoqi@0 | 78 | * Actually stores {@link com.sun.org.apache.xml.internal.resolver.tools.CatalogResolver}, but the field |
aoqi@0 | 79 | * type is made to {@link org.xml.sax.EntityResolver} so that XJC can be |
aoqi@0 | 80 | * used even if resolver.jar is not available in the classpath. |
aoqi@0 | 81 | */ |
aoqi@0 | 82 | public EntityResolver entityResolver = null; |
aoqi@0 | 83 | |
aoqi@0 | 84 | /** |
aoqi@0 | 85 | * The -p option that should control the default Java package that |
aoqi@0 | 86 | * will contain the generated code. Null if unspecified. |
aoqi@0 | 87 | */ |
aoqi@0 | 88 | public String defaultPackage = null; |
aoqi@0 | 89 | |
aoqi@0 | 90 | /** |
aoqi@0 | 91 | * The -clientjar option to package client artifacts as jar |
aoqi@0 | 92 | */ |
aoqi@0 | 93 | public String clientjar = null; |
aoqi@0 | 94 | |
aoqi@0 | 95 | /** |
aoqi@0 | 96 | * -XadditionalHeaders |
aoqi@0 | 97 | */ |
aoqi@0 | 98 | public boolean additionalHeaders; |
aoqi@0 | 99 | |
aoqi@0 | 100 | /** |
aoqi@0 | 101 | * The option indicates the dir where the jwsImpl will be generated. |
aoqi@0 | 102 | */ |
aoqi@0 | 103 | public File implDestDir = null; |
aoqi@0 | 104 | |
aoqi@0 | 105 | /** |
aoqi@0 | 106 | * optional, generated impl file only for the ordered serviceName |
aoqi@0 | 107 | * Note: It is a QName string, formatted as: "{" + Namespace URI + "}" + local part |
aoqi@0 | 108 | */ |
aoqi@0 | 109 | public String implServiceName = null; |
aoqi@0 | 110 | |
aoqi@0 | 111 | /** |
aoqi@0 | 112 | * optional, generated impl file only for the ordered portName |
aoqi@0 | 113 | * Note: It is a QName string, formatted as: "{" + Namespace URI + "}" + local part |
aoqi@0 | 114 | */ |
aoqi@0 | 115 | public String implPortName = null; |
aoqi@0 | 116 | |
aoqi@0 | 117 | /** |
aoqi@0 | 118 | * optional, if true JWS file is generated |
aoqi@0 | 119 | */ |
aoqi@0 | 120 | public boolean isGenerateJWS = false; |
aoqi@0 | 121 | |
aoqi@0 | 122 | /** |
aoqi@0 | 123 | * Setting disableSSLHostVerification to true disables the SSL Hostname verification while fetching the wsdls. |
aoqi@0 | 124 | * -XdisableSSLHostVerification |
aoqi@0 | 125 | */ |
aoqi@0 | 126 | public boolean disableSSLHostnameVerification; |
aoqi@0 | 127 | |
aoqi@0 | 128 | /** |
aoqi@0 | 129 | * Setting useBaseResourceAndURLToLoadWSDL to true causes generated Service classes to load the WSDL file from |
aoqi@0 | 130 | * a URL generated from the base resource. |
aoqi@0 | 131 | * -XuseBaseResourceAndURLToLoadWSDL |
aoqi@0 | 132 | */ |
aoqi@0 | 133 | public boolean useBaseResourceAndURLToLoadWSDL = false; |
aoqi@0 | 134 | |
aoqi@0 | 135 | /** |
aoqi@0 | 136 | * JAXB's {@link SchemaCompiler} to be used for handling the schema portion. |
aoqi@0 | 137 | * This object is also configured through options. |
aoqi@0 | 138 | */ |
aoqi@0 | 139 | private SchemaCompiler schemaCompiler = XJC.createSchemaCompiler(); |
aoqi@0 | 140 | |
aoqi@0 | 141 | /** |
aoqi@0 | 142 | * Authentication file |
aoqi@0 | 143 | */ |
aoqi@0 | 144 | public File authFile = null; |
aoqi@0 | 145 | |
aoqi@0 | 146 | //can user.home value be null? |
aoqi@0 | 147 | public static final String defaultAuthfile |
aoqi@0 | 148 | = System.getProperty("user.home") + System.getProperty("file.separator") |
aoqi@0 | 149 | + ".metro" + System.getProperty("file.separator") + "auth"; |
aoqi@0 | 150 | |
aoqi@0 | 151 | /** |
aoqi@0 | 152 | * Setting disableAuthenticator to true disables the DefaultAuthenticator. |
aoqi@0 | 153 | * -XdisableAuthenticator |
aoqi@0 | 154 | */ |
aoqi@0 | 155 | public boolean disableAuthenticator; |
aoqi@0 | 156 | |
aoqi@0 | 157 | public String proxyAuth = null; |
aoqi@0 | 158 | private String proxyHost = null; |
aoqi@0 | 159 | private String proxyPort = null; |
aoqi@0 | 160 | |
aoqi@0 | 161 | /** |
aoqi@0 | 162 | * Additional arguments |
aoqi@0 | 163 | */ |
aoqi@0 | 164 | public HashMap<String, String> extensionOptions = new HashMap<String, String>(); |
aoqi@0 | 165 | |
aoqi@0 | 166 | /** |
aoqi@0 | 167 | * All discovered {@link Plugin}s. |
aoqi@0 | 168 | * This is lazily parsed, so that we can take '-cp' option into account. |
aoqi@0 | 169 | * |
aoqi@0 | 170 | * @see #getAllPlugins() |
aoqi@0 | 171 | */ |
aoqi@0 | 172 | private List<Plugin> allPlugins; |
aoqi@0 | 173 | |
aoqi@0 | 174 | /** |
aoqi@0 | 175 | * {@link Plugin}s that are enabled in this compilation. |
aoqi@0 | 176 | */ |
aoqi@0 | 177 | public final List<Plugin> activePlugins = new ArrayList<Plugin>(); |
aoqi@0 | 178 | |
aoqi@0 | 179 | public JCodeModel getCodeModel() { |
aoqi@0 | 180 | if(codeModel == null) |
aoqi@0 | 181 | codeModel = new JCodeModel(); |
aoqi@0 | 182 | return codeModel; |
aoqi@0 | 183 | } |
aoqi@0 | 184 | |
aoqi@0 | 185 | public SchemaCompiler getSchemaCompiler() { |
aoqi@0 | 186 | schemaCompiler.setTargetVersion(SpecVersion.parse(target.getVersion())); |
aoqi@0 | 187 | if(entityResolver != null) { |
aoqi@0 | 188 | //set if its not null so as not to override catalog option specified via xjc args |
aoqi@0 | 189 | schemaCompiler.setEntityResolver(entityResolver); |
aoqi@0 | 190 | } |
aoqi@0 | 191 | return schemaCompiler; |
aoqi@0 | 192 | } |
aoqi@0 | 193 | |
aoqi@0 | 194 | public void setCodeModel(JCodeModel codeModel) { |
aoqi@0 | 195 | this.codeModel = codeModel; |
aoqi@0 | 196 | } |
aoqi@0 | 197 | |
aoqi@0 | 198 | private JCodeModel codeModel; |
aoqi@0 | 199 | |
aoqi@0 | 200 | /** |
aoqi@0 | 201 | * This captures jars passed on the commandline and passes them to XJC and puts them in the classpath for compilation |
aoqi@0 | 202 | */ |
aoqi@0 | 203 | public List<String> cmdlineJars = new ArrayList<String>(); |
aoqi@0 | 204 | |
aoqi@0 | 205 | /** |
aoqi@0 | 206 | * Gets all the {@link Plugin}s discovered so far. |
aoqi@0 | 207 | * |
aoqi@0 | 208 | * <p> |
aoqi@0 | 209 | * A plugins are enumerated when this method is called for the first time, |
aoqi@0 | 210 | * by taking {@link #classpath} into account. That means |
aoqi@0 | 211 | * "-cp plugin.jar" has to come before you specify options to enable it. |
aoqi@0 | 212 | */ |
aoqi@0 | 213 | public List<Plugin> getAllPlugins() { |
aoqi@0 | 214 | if(allPlugins==null) { |
aoqi@0 | 215 | allPlugins = new ArrayList<Plugin>(); |
aoqi@0 | 216 | allPlugins.addAll(Arrays.asList(findServices(Plugin.class, getClassLoader()))); |
aoqi@0 | 217 | } |
aoqi@0 | 218 | return allPlugins; |
aoqi@0 | 219 | } |
aoqi@0 | 220 | |
aoqi@0 | 221 | /** |
aoqi@0 | 222 | * Parses arguments and fill fields of this object. |
aoqi@0 | 223 | * |
aoqi@0 | 224 | * @exception BadCommandLineException |
aoqi@0 | 225 | * thrown when there's a problem in the command-line arguments |
aoqi@0 | 226 | */ |
aoqi@0 | 227 | @Override |
aoqi@0 | 228 | public final void parseArguments( String[] args ) throws BadCommandLineException { |
aoqi@0 | 229 | |
aoqi@0 | 230 | for (int i = 0; i < args.length; i++) { |
aoqi@0 | 231 | if(args[i].length()==0) |
aoqi@0 | 232 | throw new BadCommandLineException(); |
aoqi@0 | 233 | if (args[i].charAt(0) == '-') { |
aoqi@0 | 234 | int j = parseArguments(args,i); |
aoqi@0 | 235 | if(j==0) |
aoqi@0 | 236 | throw new BadCommandLineException(WscompileMessages.WSCOMPILE_INVALID_OPTION(args[i])); |
aoqi@0 | 237 | i += (j-1); |
aoqi@0 | 238 | } else { |
aoqi@0 | 239 | if(args[i].endsWith(".jar")) { |
aoqi@0 | 240 | |
aoqi@0 | 241 | try { |
aoqi@0 | 242 | cmdlineJars.add(args[i]); |
aoqi@0 | 243 | schemaCompiler.getOptions().scanEpisodeFile(new File(args[i])); |
aoqi@0 | 244 | |
aoqi@0 | 245 | } catch (com.sun.tools.internal.xjc.BadCommandLineException e) { |
aoqi@0 | 246 | //Driver.usage(jaxbOptions,false); |
aoqi@0 | 247 | throw new BadCommandLineException(e.getMessage(), e); |
aoqi@0 | 248 | } |
aoqi@0 | 249 | } else{ |
aoqi@0 | 250 | addFile(args[i]); |
aoqi@0 | 251 | } |
aoqi@0 | 252 | } |
aoqi@0 | 253 | } |
aoqi@0 | 254 | |
aoqi@0 | 255 | if (encoding != null && schemaCompiler.getOptions().encoding == null) { |
aoqi@0 | 256 | try { |
aoqi@0 | 257 | schemaCompiler.getOptions().parseArgument( |
aoqi@0 | 258 | new String[] {"-encoding", encoding}, 0); |
aoqi@0 | 259 | } catch (com.sun.tools.internal.xjc.BadCommandLineException ex) { |
aoqi@0 | 260 | Logger.getLogger(WsimportOptions.class.getName()).log(Level.SEVERE, null, ex); |
aoqi@0 | 261 | } |
aoqi@0 | 262 | } |
aoqi@0 | 263 | |
aoqi@0 | 264 | if(destDir == null) |
aoqi@0 | 265 | destDir = new File("."); |
aoqi@0 | 266 | if(sourceDir == null) |
aoqi@0 | 267 | sourceDir = destDir; |
aoqi@0 | 268 | } |
aoqi@0 | 269 | |
aoqi@0 | 270 | /** -Xno-addressing-databinding option to disable addressing namespace data binding. This is |
aoqi@0 | 271 | * experimental switch and will be working as a temporary workaround till |
aoqi@0 | 272 | * jaxb can provide a better way to selelctively disable compiling of an |
aoqi@0 | 273 | * schema component. |
aoqi@0 | 274 | * **/ |
aoqi@0 | 275 | public boolean noAddressingBbinding; |
aoqi@0 | 276 | |
aoqi@0 | 277 | @Override |
aoqi@0 | 278 | public int parseArguments(String[] args, int i) throws BadCommandLineException { |
aoqi@0 | 279 | int j = super.parseArguments(args ,i); |
aoqi@0 | 280 | if(j>0) return j; // understood by the super class |
aoqi@0 | 281 | |
aoqi@0 | 282 | if (args[i].equals("-b")) { |
aoqi@0 | 283 | addBindings(requireArgument("-b", args, ++i)); |
aoqi@0 | 284 | return 2; |
aoqi@0 | 285 | } else if (args[i].equals("-wsdllocation")) { |
aoqi@0 | 286 | wsdlLocation = requireArgument("-wsdllocation", args, ++i); |
aoqi@0 | 287 | return 2; |
aoqi@0 | 288 | } else if (args[i].equals("-XadditionalHeaders")) { |
aoqi@0 | 289 | additionalHeaders = true; |
aoqi@0 | 290 | return 1; |
aoqi@0 | 291 | } else if (args[i].equals("-XdisableSSLHostnameVerification")) { |
aoqi@0 | 292 | disableSSLHostnameVerification = true; |
aoqi@0 | 293 | return 1; |
aoqi@0 | 294 | } else if (args[i].equals("-p")) { |
aoqi@0 | 295 | defaultPackage = requireArgument("-p", args, ++i); |
aoqi@0 | 296 | return 2; |
aoqi@0 | 297 | } else if (args[i].equals("-catalog")) { |
aoqi@0 | 298 | String catalog = requireArgument("-catalog", args, ++i); |
aoqi@0 | 299 | try { |
aoqi@0 | 300 | if (entityResolver == null) { |
aoqi@0 | 301 | if (catalog != null && catalog.length() > 0) |
aoqi@0 | 302 | entityResolver = XmlUtil.createEntityResolver(JAXWSUtils.getFileOrURL(JAXWSUtils.absolutize(Util.escapeSpace(catalog)))); |
aoqi@0 | 303 | } else if (catalog != null && catalog.length() > 0) { |
aoqi@0 | 304 | EntityResolver er = XmlUtil.createEntityResolver(JAXWSUtils.getFileOrURL(JAXWSUtils.absolutize(Util.escapeSpace(catalog)))); |
aoqi@0 | 305 | entityResolver = new ForkEntityResolver(er, entityResolver); |
aoqi@0 | 306 | } |
aoqi@0 | 307 | } catch (IOException e) { |
aoqi@0 | 308 | throw new BadCommandLineException(WscompileMessages.WSIMPORT_FAILED_TO_PARSE(catalog, e.getMessage())); |
aoqi@0 | 309 | } |
aoqi@0 | 310 | return 2; |
aoqi@0 | 311 | } else if (args[i].startsWith("-httpproxy:")) { |
aoqi@0 | 312 | String value = args[i].substring(11); |
aoqi@0 | 313 | if (value.length() == 0) { |
aoqi@0 | 314 | throw new BadCommandLineException(WscompileMessages.WSCOMPILE_INVALID_OPTION(args[i])); |
aoqi@0 | 315 | } |
aoqi@0 | 316 | parseProxy(value); |
aoqi@0 | 317 | if (proxyHost != null || proxyPort != null) { |
aoqi@0 | 318 | System.setProperty("proxySet", "true"); |
aoqi@0 | 319 | } |
aoqi@0 | 320 | if (proxyHost != null) { |
aoqi@0 | 321 | System.setProperty("proxyHost", proxyHost); |
aoqi@0 | 322 | } |
aoqi@0 | 323 | if (proxyPort != null) { |
aoqi@0 | 324 | System.setProperty("proxyPort", proxyPort); |
aoqi@0 | 325 | } |
aoqi@0 | 326 | return 1; |
aoqi@0 | 327 | } else if (args[i].equals("-Xno-addressing-databinding")) { |
aoqi@0 | 328 | noAddressingBbinding = true; |
aoqi@0 | 329 | return 1; |
aoqi@0 | 330 | } else if (args[i].startsWith("-B")) { |
aoqi@0 | 331 | // JAXB option pass through. |
aoqi@0 | 332 | String[] subCmd = new String[args.length-i]; |
aoqi@0 | 333 | System.arraycopy(args,i,subCmd,0,subCmd.length); |
aoqi@0 | 334 | subCmd[0] = subCmd[0].substring(2); // trim off the first "-B" |
aoqi@0 | 335 | |
aoqi@0 | 336 | com.sun.tools.internal.xjc.Options jaxbOptions = schemaCompiler.getOptions(); |
aoqi@0 | 337 | try { |
aoqi@0 | 338 | int r = jaxbOptions.parseArgument(subCmd, 0); |
aoqi@0 | 339 | if(r==0) { |
aoqi@0 | 340 | //Driver.usage(jaxbOptions,false); |
aoqi@0 | 341 | throw new BadCommandLineException(WscompileMessages.WSIMPORT_NO_SUCH_JAXB_OPTION(subCmd[0])); |
aoqi@0 | 342 | } |
aoqi@0 | 343 | return r; |
aoqi@0 | 344 | } catch (com.sun.tools.internal.xjc.BadCommandLineException e) { |
aoqi@0 | 345 | //Driver.usage(jaxbOptions,false); |
aoqi@0 | 346 | throw new BadCommandLineException(e.getMessage(),e); |
aoqi@0 | 347 | } |
aoqi@0 | 348 | } else if (args[i].equals("-Xauthfile")) { |
aoqi@0 | 349 | String authfile = requireArgument("-Xauthfile", args, ++i); |
aoqi@0 | 350 | authFile = new File(authfile); |
aoqi@0 | 351 | return 2; |
aoqi@0 | 352 | } else if (args[i].equals("-clientjar")) { |
aoqi@0 | 353 | clientjar = requireArgument("-clientjar", args, ++i); |
aoqi@0 | 354 | return 2; |
aoqi@0 | 355 | } else if (args[i].equals("-implDestDir")) { |
aoqi@0 | 356 | implDestDir = new File(requireArgument("-implDestDir", args, ++i)); |
aoqi@0 | 357 | if (!implDestDir.exists()) |
aoqi@0 | 358 | throw new BadCommandLineException(WscompileMessages.WSCOMPILE_NO_SUCH_DIRECTORY(implDestDir.getPath())); |
aoqi@0 | 359 | return 2; |
aoqi@0 | 360 | } else if (args[i].equals("-implServiceName")) { |
aoqi@0 | 361 | implServiceName = requireArgument("-implServiceName", args, ++i); |
aoqi@0 | 362 | return 2; |
aoqi@0 | 363 | } else if (args[i].equals("-implPortName")) { |
aoqi@0 | 364 | implPortName = requireArgument("-implPortName", args, ++i); |
aoqi@0 | 365 | return 2; |
aoqi@0 | 366 | } else if (args[i].equals("-generateJWS")) { |
aoqi@0 | 367 | isGenerateJWS = true; |
aoqi@0 | 368 | return 1; |
aoqi@0 | 369 | } else if (args[i].equals("-XuseBaseResourceAndURLToLoadWSDL")) { |
aoqi@0 | 370 | useBaseResourceAndURLToLoadWSDL = true; |
aoqi@0 | 371 | return 1; |
aoqi@0 | 372 | } else if (args[i].equals("-XdisableAuthenticator")) { |
aoqi@0 | 373 | disableAuthenticator = true; |
aoqi@0 | 374 | return 1; |
aoqi@0 | 375 | } |
aoqi@0 | 376 | |
aoqi@0 | 377 | // handle additional options |
aoqi@0 | 378 | for (GeneratorExtension f:ServiceFinder.find(GeneratorExtension.class)) { |
aoqi@0 | 379 | if (f.validateOption(args[i])) { |
aoqi@0 | 380 | extensionOptions.put(args[i], requireArgument(args[i], args, ++i)); |
aoqi@0 | 381 | return 2; |
aoqi@0 | 382 | } |
aoqi@0 | 383 | } |
aoqi@0 | 384 | |
aoqi@0 | 385 | // see if this is one of the extensions |
aoqi@0 | 386 | for( Plugin plugin : getAllPlugins() ) { |
aoqi@0 | 387 | try { |
aoqi@0 | 388 | if(('-' + plugin.getOptionName()).equals(args[i])) { |
aoqi@0 | 389 | activePlugins.add(plugin); |
aoqi@0 | 390 | plugin.onActivated(this); |
aoqi@0 | 391 | return 1; |
aoqi@0 | 392 | } |
aoqi@0 | 393 | int r = plugin.parseArgument(this, args, i); |
aoqi@0 | 394 | if (r != 0) { |
aoqi@0 | 395 | return r; |
aoqi@0 | 396 | } |
aoqi@0 | 397 | } catch (IOException e) { |
aoqi@0 | 398 | throw new BadCommandLineException(e.getMessage(),e); |
aoqi@0 | 399 | } |
aoqi@0 | 400 | } |
aoqi@0 | 401 | |
aoqi@0 | 402 | return 0; // what's this option? |
aoqi@0 | 403 | } |
aoqi@0 | 404 | |
aoqi@0 | 405 | public void validate() throws BadCommandLineException { |
aoqi@0 | 406 | if (wsdls.isEmpty()) { |
aoqi@0 | 407 | throw new BadCommandLineException(WscompileMessages.WSIMPORT_MISSING_FILE()); |
aoqi@0 | 408 | } |
aoqi@0 | 409 | |
aoqi@0 | 410 | if(wsdlLocation !=null && clientjar != null) { |
aoqi@0 | 411 | throw new BadCommandLineException(WscompileMessages.WSIMPORT_WSDLLOCATION_CLIENTJAR()); |
aoqi@0 | 412 | } |
aoqi@0 | 413 | if(wsdlLocation == null){ |
aoqi@0 | 414 | wsdlLocation = wsdls.get(0).getSystemId(); |
aoqi@0 | 415 | } |
aoqi@0 | 416 | |
aoqi@0 | 417 | |
aoqi@0 | 418 | } |
aoqi@0 | 419 | |
aoqi@0 | 420 | @Override |
aoqi@0 | 421 | protected void addFile(String arg) throws BadCommandLineException { |
aoqi@0 | 422 | addFile(arg, wsdls, ".wsdl"); |
aoqi@0 | 423 | } |
aoqi@0 | 424 | |
aoqi@0 | 425 | private final List<InputSource> wsdls = new ArrayList<InputSource>(); |
aoqi@0 | 426 | private final List<InputSource> schemas = new ArrayList<InputSource>(); |
aoqi@0 | 427 | private final List<InputSource> bindingFiles = new ArrayList<InputSource>(); |
aoqi@0 | 428 | private final List<InputSource> jaxwsCustomBindings = new ArrayList<InputSource>(); |
aoqi@0 | 429 | private final List<InputSource> jaxbCustomBindings = new ArrayList<InputSource>(); |
aoqi@0 | 430 | private final List<Element> handlerConfigs = new ArrayList<Element>(); |
aoqi@0 | 431 | |
aoqi@0 | 432 | /** |
aoqi@0 | 433 | * There is supposed to be one handler chain per generated SEI. |
aoqi@0 | 434 | * TODO: There is possible bug, how to associate a @HandlerChain |
aoqi@0 | 435 | * with each port on the generated SEI. For now lets preserve the JAXWS 2.0 FCS |
aoqi@0 | 436 | * behaviour and generate only one @HandlerChain on the SEI |
aoqi@0 | 437 | */ |
aoqi@0 | 438 | public Element getHandlerChainConfiguration(){ |
aoqi@0 | 439 | if(handlerConfigs.size() > 0) |
aoqi@0 | 440 | return handlerConfigs.get(0); |
aoqi@0 | 441 | return null; |
aoqi@0 | 442 | } |
aoqi@0 | 443 | |
aoqi@0 | 444 | public void addHandlerChainConfiguration(Element config){ |
aoqi@0 | 445 | handlerConfigs.add(config); |
aoqi@0 | 446 | } |
aoqi@0 | 447 | |
aoqi@0 | 448 | public InputSource[] getWSDLs() { |
aoqi@0 | 449 | return wsdls.toArray(new InputSource[wsdls.size()]); |
aoqi@0 | 450 | } |
aoqi@0 | 451 | |
aoqi@0 | 452 | public InputSource[] getSchemas() { |
aoqi@0 | 453 | return schemas.toArray(new InputSource[schemas.size()]); |
aoqi@0 | 454 | } |
aoqi@0 | 455 | |
aoqi@0 | 456 | public InputSource[] getWSDLBindings() { |
aoqi@0 | 457 | return jaxwsCustomBindings.toArray(new InputSource[jaxwsCustomBindings.size()]); |
aoqi@0 | 458 | } |
aoqi@0 | 459 | |
aoqi@0 | 460 | public InputSource[] getSchemaBindings() { |
aoqi@0 | 461 | return jaxbCustomBindings.toArray(new InputSource[jaxbCustomBindings.size()]); |
aoqi@0 | 462 | } |
aoqi@0 | 463 | |
aoqi@0 | 464 | public void addWSDL(File source) { |
aoqi@0 | 465 | addWSDL(fileToInputSource(source)); |
aoqi@0 | 466 | } |
aoqi@0 | 467 | |
aoqi@0 | 468 | public void addWSDL(InputSource is) { |
aoqi@0 | 469 | wsdls.add(absolutize(is)); |
aoqi@0 | 470 | } |
aoqi@0 | 471 | |
aoqi@0 | 472 | public void addSchema(File source) { |
aoqi@0 | 473 | addSchema(fileToInputSource(source)); |
aoqi@0 | 474 | } |
aoqi@0 | 475 | |
aoqi@0 | 476 | public void addSchema(InputSource is) { |
aoqi@0 | 477 | schemas.add(is); |
aoqi@0 | 478 | } |
aoqi@0 | 479 | |
aoqi@0 | 480 | private InputSource fileToInputSource(File source) { |
aoqi@0 | 481 | try { |
aoqi@0 | 482 | String url = source.toURL().toExternalForm(); |
aoqi@0 | 483 | return new InputSource(Util.escapeSpace(url)); |
aoqi@0 | 484 | } catch (MalformedURLException e) { |
aoqi@0 | 485 | return new InputSource(source.getPath()); |
aoqi@0 | 486 | } |
aoqi@0 | 487 | } |
aoqi@0 | 488 | |
aoqi@0 | 489 | /** |
aoqi@0 | 490 | * Recursively scan directories and add all XSD files in it. |
aoqi@0 | 491 | */ |
aoqi@0 | 492 | public void addGrammarRecursive(File dir) { |
aoqi@0 | 493 | addRecursive(dir, ".wsdl", wsdls); |
aoqi@0 | 494 | addRecursive(dir, ".xsd", schemas); |
aoqi@0 | 495 | } |
aoqi@0 | 496 | |
aoqi@0 | 497 | /** |
aoqi@0 | 498 | * Adds a new input schema. |
aoqi@0 | 499 | */ |
aoqi@0 | 500 | public void addWSDLBindFile(InputSource is) { |
aoqi@0 | 501 | jaxwsCustomBindings.add(new RereadInputSource(absolutize(is))); |
aoqi@0 | 502 | } |
aoqi@0 | 503 | |
aoqi@0 | 504 | public void addSchemmaBindFile(InputSource is) { |
aoqi@0 | 505 | jaxbCustomBindings.add(new RereadInputSource(absolutize(is))); |
aoqi@0 | 506 | } |
aoqi@0 | 507 | |
aoqi@0 | 508 | private void addRecursive(File dir, String suffix, List<InputSource> result) { |
aoqi@0 | 509 | File[] files = dir.listFiles(); |
aoqi@0 | 510 | if (files == null) return; // work defensively |
aoqi@0 | 511 | |
aoqi@0 | 512 | for (File f : files) { |
aoqi@0 | 513 | if (f.isDirectory()) |
aoqi@0 | 514 | addRecursive(f, suffix, result); |
aoqi@0 | 515 | else if (f.getPath().endsWith(suffix)) |
aoqi@0 | 516 | result.add(absolutize(fileToInputSource(f))); |
aoqi@0 | 517 | } |
aoqi@0 | 518 | } |
aoqi@0 | 519 | |
aoqi@0 | 520 | private InputSource absolutize(InputSource is) { |
aoqi@0 | 521 | // absolutize all the system IDs in the input, |
aoqi@0 | 522 | // so that we can map system IDs to DOM trees. |
aoqi@0 | 523 | try { |
aoqi@0 | 524 | URL baseURL = new File(".").getCanonicalFile().toURL(); |
aoqi@0 | 525 | is.setSystemId(new URL(baseURL, is.getSystemId()).toExternalForm()); |
aoqi@0 | 526 | } catch (IOException e) { |
aoqi@0 | 527 | // ignore |
aoqi@0 | 528 | } |
aoqi@0 | 529 | return is; |
aoqi@0 | 530 | } |
aoqi@0 | 531 | |
aoqi@0 | 532 | public void addBindings(String name) throws BadCommandLineException { |
aoqi@0 | 533 | addFile(name, bindingFiles, null); |
aoqi@0 | 534 | } |
aoqi@0 | 535 | |
aoqi@0 | 536 | /** |
aoqi@0 | 537 | * Parses a token to a file (or a set of files) |
aoqi@0 | 538 | * and add them as {@link InputSource} to the specified list. |
aoqi@0 | 539 | * |
aoqi@0 | 540 | * @param suffix If the given token is a directory name, we do a recusive search |
aoqi@0 | 541 | * and find all files that have the given suffix. |
aoqi@0 | 542 | */ |
aoqi@0 | 543 | private void addFile(String name, List<InputSource> target, String suffix) throws BadCommandLineException { |
aoqi@0 | 544 | Object src; |
aoqi@0 | 545 | try { |
aoqi@0 | 546 | src = Util.getFileOrURL(name); |
aoqi@0 | 547 | } catch (IOException e) { |
aoqi@0 | 548 | throw new BadCommandLineException(WscompileMessages.WSIMPORT_NOT_A_FILE_NOR_URL(name)); |
aoqi@0 | 549 | } |
aoqi@0 | 550 | if (src instanceof URL) { |
aoqi@0 | 551 | target.add(absolutize(new InputSource(Util.escapeSpace(((URL) src).toExternalForm())))); |
aoqi@0 | 552 | } else { |
aoqi@0 | 553 | File fsrc = (File) src; |
aoqi@0 | 554 | if (fsrc.isDirectory()) { |
aoqi@0 | 555 | addRecursive(fsrc, suffix, target); |
aoqi@0 | 556 | } else { |
aoqi@0 | 557 | target.add(absolutize(fileToInputSource(fsrc))); |
aoqi@0 | 558 | } |
aoqi@0 | 559 | } |
aoqi@0 | 560 | } |
aoqi@0 | 561 | |
aoqi@0 | 562 | |
aoqi@0 | 563 | /** |
aoqi@0 | 564 | * Exposing it as a public method to allow external tools such as NB to read from wsdl model and work on it. |
aoqi@0 | 565 | * TODO: WSDL model needs to be exposed - basically at tool time we need to use the runtimw wsdl model |
aoqi@0 | 566 | * |
aoqi@0 | 567 | * Binding files could be jaxws or jaxb. This method identifies jaxws and jaxb binding files and keeps them separately. jaxb binding files are given separately |
aoqi@0 | 568 | * to JAXB in {@link com.sun.tools.internal.ws.processor.modeler.wsdl.JAXBModelBuilder} |
aoqi@0 | 569 | * |
aoqi@0 | 570 | * @param receiver {@link ErrorReceiver} |
aoqi@0 | 571 | */ |
aoqi@0 | 572 | public final void parseBindings(ErrorReceiver receiver){ |
aoqi@0 | 573 | for (InputSource is : bindingFiles) { |
aoqi@0 | 574 | XMLStreamReader reader = |
aoqi@0 | 575 | XMLStreamReaderFactory.create(is,true); |
aoqi@0 | 576 | XMLStreamReaderUtil.nextElementContent(reader); |
aoqi@0 | 577 | if (reader.getName().equals(JAXWSBindingsConstants.JAXWS_BINDINGS)) { |
aoqi@0 | 578 | jaxwsCustomBindings.add(new RereadInputSource(is)); |
aoqi@0 | 579 | } else if (reader.getName().equals(JAXWSBindingsConstants.JAXB_BINDINGS) || |
aoqi@0 | 580 | reader.getName().equals(new QName(SchemaConstants.NS_XSD, "schema"))) { |
aoqi@0 | 581 | jaxbCustomBindings.add(new RereadInputSource(is)); |
aoqi@0 | 582 | } else { |
aoqi@0 | 583 | LocatorImpl locator = new LocatorImpl(); |
aoqi@0 | 584 | locator.setSystemId(reader.getLocation().getSystemId()); |
aoqi@0 | 585 | locator.setPublicId(reader.getLocation().getPublicId()); |
aoqi@0 | 586 | locator.setLineNumber(reader.getLocation().getLineNumber()); |
aoqi@0 | 587 | locator.setColumnNumber(reader.getLocation().getColumnNumber()); |
aoqi@0 | 588 | receiver.warning(locator, ConfigurationMessages.CONFIGURATION_NOT_BINDING_FILE(is.getSystemId())); |
aoqi@0 | 589 | } |
aoqi@0 | 590 | } |
aoqi@0 | 591 | } |
aoqi@0 | 592 | |
aoqi@0 | 593 | /** |
aoqi@0 | 594 | * Get extension argument |
aoqi@0 | 595 | */ |
aoqi@0 | 596 | public String getExtensionOption(String argument) { |
aoqi@0 | 597 | return extensionOptions.get(argument); |
aoqi@0 | 598 | } |
aoqi@0 | 599 | |
aoqi@0 | 600 | private void parseProxy(String text) throws BadCommandLineException { |
aoqi@0 | 601 | int i = text.lastIndexOf('@'); |
aoqi@0 | 602 | int j = text.lastIndexOf(':'); |
aoqi@0 | 603 | |
aoqi@0 | 604 | if (i > 0) { |
aoqi@0 | 605 | proxyAuth = text.substring(0, i); |
aoqi@0 | 606 | if (j > i) { |
aoqi@0 | 607 | proxyHost = text.substring(i + 1, j); |
aoqi@0 | 608 | proxyPort = text.substring(j + 1); |
aoqi@0 | 609 | } else { |
aoqi@0 | 610 | proxyHost = text.substring(i + 1); |
aoqi@0 | 611 | proxyPort = "8080"; |
aoqi@0 | 612 | } |
aoqi@0 | 613 | } else { |
aoqi@0 | 614 | //no auth info |
aoqi@0 | 615 | if (j < 0) { |
aoqi@0 | 616 | //no port |
aoqi@0 | 617 | proxyHost = text; |
aoqi@0 | 618 | proxyPort = "8080"; |
aoqi@0 | 619 | } else { |
aoqi@0 | 620 | proxyHost = text.substring(0, j); |
aoqi@0 | 621 | proxyPort = text.substring(j + 1); |
aoqi@0 | 622 | } |
aoqi@0 | 623 | } |
aoqi@0 | 624 | try { |
aoqi@0 | 625 | Integer.valueOf(proxyPort); |
aoqi@0 | 626 | } catch (NumberFormatException e) { |
aoqi@0 | 627 | throw new BadCommandLineException(WscompileMessages.WSIMPORT_ILLEGAL_PROXY(text)); |
aoqi@0 | 628 | } |
aoqi@0 | 629 | } |
aoqi@0 | 630 | |
aoqi@0 | 631 | /** |
aoqi@0 | 632 | * Looks for all "META-INF/services/[className]" files and |
aoqi@0 | 633 | * create one instance for each class name found inside this file. |
aoqi@0 | 634 | */ |
aoqi@0 | 635 | private static <T> T[] findServices(Class<T> clazz, ClassLoader classLoader) { |
aoqi@0 | 636 | ServiceFinder<T> serviceFinder = ServiceFinder.find(clazz, classLoader); |
aoqi@0 | 637 | List<T> r = new ArrayList<T>(); |
aoqi@0 | 638 | for (T t : serviceFinder) { |
aoqi@0 | 639 | r.add(t); |
aoqi@0 | 640 | } |
aoqi@0 | 641 | return r.toArray((T[]) Array.newInstance(clazz, r.size())); |
aoqi@0 | 642 | } |
aoqi@0 | 643 | |
aoqi@0 | 644 | private static final class ByteStream extends ByteArrayOutputStream { |
aoqi@0 | 645 | byte[] getBuffer() { |
aoqi@0 | 646 | return buf; |
aoqi@0 | 647 | } |
aoqi@0 | 648 | } |
aoqi@0 | 649 | |
aoqi@0 | 650 | private static final class RereadInputStream extends InputStream { |
aoqi@0 | 651 | private InputStream is; |
aoqi@0 | 652 | private ByteStream bs; |
aoqi@0 | 653 | |
aoqi@0 | 654 | RereadInputStream(InputStream is) { |
aoqi@0 | 655 | this.is = is; |
aoqi@0 | 656 | this.bs = new ByteStream(); |
aoqi@0 | 657 | } |
aoqi@0 | 658 | |
aoqi@0 | 659 | @Override |
aoqi@0 | 660 | public int available() throws IOException { |
aoqi@0 | 661 | return is.available(); |
aoqi@0 | 662 | } |
aoqi@0 | 663 | |
aoqi@0 | 664 | @Override |
aoqi@0 | 665 | public void close() throws IOException { |
aoqi@0 | 666 | if (bs != null) { |
aoqi@0 | 667 | InputStream i = new ByteArrayInputStream(bs.getBuffer()); |
aoqi@0 | 668 | bs = null; |
aoqi@0 | 669 | is.close(); |
aoqi@0 | 670 | is = i; |
aoqi@0 | 671 | } |
aoqi@0 | 672 | } |
aoqi@0 | 673 | |
aoqi@0 | 674 | @Override |
aoqi@0 | 675 | public synchronized void mark(int readlimit) { |
aoqi@0 | 676 | is.mark(readlimit); |
aoqi@0 | 677 | } |
aoqi@0 | 678 | |
aoqi@0 | 679 | @Override |
aoqi@0 | 680 | public boolean markSupported() { |
aoqi@0 | 681 | return is.markSupported(); |
aoqi@0 | 682 | } |
aoqi@0 | 683 | |
aoqi@0 | 684 | @Override |
aoqi@0 | 685 | public int read() throws IOException { |
aoqi@0 | 686 | int r = is.read(); |
aoqi@0 | 687 | if (bs != null) |
aoqi@0 | 688 | bs.write(r); |
aoqi@0 | 689 | return r; |
aoqi@0 | 690 | } |
aoqi@0 | 691 | |
aoqi@0 | 692 | @Override |
aoqi@0 | 693 | public int read(byte[] b, int off, int len) throws IOException { |
aoqi@0 | 694 | int r = is.read(b, off, len); |
aoqi@0 | 695 | if (r > 0 && bs != null) |
aoqi@0 | 696 | bs.write(b, off, r); |
aoqi@0 | 697 | return r; |
aoqi@0 | 698 | } |
aoqi@0 | 699 | |
aoqi@0 | 700 | @Override |
aoqi@0 | 701 | public int read(byte[] b) throws IOException { |
aoqi@0 | 702 | int r = is.read(b); |
aoqi@0 | 703 | if (r > 0 && bs != null) |
aoqi@0 | 704 | bs.write(b, 0, r); |
aoqi@0 | 705 | return r; |
aoqi@0 | 706 | } |
aoqi@0 | 707 | |
aoqi@0 | 708 | @Override |
aoqi@0 | 709 | public synchronized void reset() throws IOException { |
aoqi@0 | 710 | is.reset(); |
aoqi@0 | 711 | } |
aoqi@0 | 712 | } |
aoqi@0 | 713 | |
aoqi@0 | 714 | private static final class RereadInputSource extends InputSource { |
aoqi@0 | 715 | private InputSource is; |
aoqi@0 | 716 | |
aoqi@0 | 717 | RereadInputSource(InputSource is) { |
aoqi@0 | 718 | this.is = is; |
aoqi@0 | 719 | } |
aoqi@0 | 720 | |
aoqi@0 | 721 | @Override |
aoqi@0 | 722 | public InputStream getByteStream() { |
aoqi@0 | 723 | InputStream i = is.getByteStream(); |
aoqi@0 | 724 | if (i != null && !(i instanceof RereadInputStream)) { |
aoqi@0 | 725 | i = new RereadInputStream(i); |
aoqi@0 | 726 | is.setByteStream(i); |
aoqi@0 | 727 | } |
aoqi@0 | 728 | return i; |
aoqi@0 | 729 | } |
aoqi@0 | 730 | |
aoqi@0 | 731 | @Override |
aoqi@0 | 732 | public Reader getCharacterStream() { |
aoqi@0 | 733 | // TODO Auto-generated method stub |
aoqi@0 | 734 | return is.getCharacterStream(); |
aoqi@0 | 735 | } |
aoqi@0 | 736 | |
aoqi@0 | 737 | @Override |
aoqi@0 | 738 | public String getEncoding() { |
aoqi@0 | 739 | return is.getEncoding(); |
aoqi@0 | 740 | } |
aoqi@0 | 741 | |
aoqi@0 | 742 | @Override |
aoqi@0 | 743 | public String getPublicId() { |
aoqi@0 | 744 | return is.getPublicId(); |
aoqi@0 | 745 | } |
aoqi@0 | 746 | |
aoqi@0 | 747 | @Override |
aoqi@0 | 748 | public String getSystemId() { |
aoqi@0 | 749 | return is.getSystemId(); |
aoqi@0 | 750 | } |
aoqi@0 | 751 | |
aoqi@0 | 752 | @Override |
aoqi@0 | 753 | public void setByteStream(InputStream byteStream) { |
aoqi@0 | 754 | is.setByteStream(byteStream); |
aoqi@0 | 755 | } |
aoqi@0 | 756 | |
aoqi@0 | 757 | @Override |
aoqi@0 | 758 | public void setCharacterStream(Reader characterStream) { |
aoqi@0 | 759 | is.setCharacterStream(characterStream); |
aoqi@0 | 760 | } |
aoqi@0 | 761 | |
aoqi@0 | 762 | @Override |
aoqi@0 | 763 | public void setEncoding(String encoding) { |
aoqi@0 | 764 | is.setEncoding(encoding); |
aoqi@0 | 765 | } |
aoqi@0 | 766 | |
aoqi@0 | 767 | @Override |
aoqi@0 | 768 | public void setPublicId(String publicId) { |
aoqi@0 | 769 | is.setPublicId(publicId); |
aoqi@0 | 770 | } |
aoqi@0 | 771 | |
aoqi@0 | 772 | @Override |
aoqi@0 | 773 | public void setSystemId(String systemId) { |
aoqi@0 | 774 | is.setSystemId(systemId); |
aoqi@0 | 775 | } |
aoqi@0 | 776 | } |
aoqi@0 | 777 | |
aoqi@0 | 778 | @Override |
aoqi@0 | 779 | protected void disableXmlSecurity() { |
aoqi@0 | 780 | super.disableXmlSecurity(); |
aoqi@0 | 781 | schemaCompiler.getOptions().disableXmlSecurity = true; |
aoqi@0 | 782 | } |
aoqi@0 | 783 | } |