src/share/jaxws_classes/com/sun/tools/internal/ws/wscompile/WsimportOptions.java

Tue, 06 Mar 2012 16:09:35 -0800

author
ohair
date
Tue, 06 Mar 2012 16:09:35 -0800
changeset 286
f50545b5e2f1
child 368
0989ad8c0860
permissions
-rw-r--r--

7150322: Stop using drop source bundles in jaxws
Reviewed-by: darcy, ohrstrom

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

mercurial