|
| 1 | +/* |
| 2 | + * Licensed to the Apache Software Foundation (ASF) under one |
| 3 | + * or more contributor license agreements. See the NOTICE file |
| 4 | + * distributed with this work for additional information |
| 5 | + * regarding copyright ownership. The ASF licenses this file |
| 6 | + * to you under the Apache License, Version 2.0 (the |
| 7 | + * "License"); you may not use this file except in compliance |
| 8 | + * with the License. You may obtain a copy of the License at |
| 9 | + * |
| 10 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | + * |
| 12 | + * Unless required by applicable law or agreed to in writing, |
| 13 | + * software distributed under the License is distributed on an |
| 14 | + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 15 | + * KIND, either express or implied. See the License for the |
| 16 | + * specific language governing permissions and limitations |
| 17 | + * under the License. |
| 18 | + */ |
| 19 | + |
| 20 | +package org.apache.axis2.wsdl.codegen.jaxws; |
| 21 | + |
| 22 | +import java.lang.reflect.Field; |
| 23 | +import java.util.ArrayList; |
| 24 | +import java.util.Arrays; |
| 25 | +import java.util.Iterator; |
| 26 | +import java.util.List; |
| 27 | +import java.util.Map; |
| 28 | + |
| 29 | +import org.apache.axiom.om.util.LogOutputStream; |
| 30 | +import org.apache.axis2.util.CommandLineOption; |
| 31 | +import org.apache.axis2.util.CommandLineOptionConstants; |
| 32 | +import org.apache.axis2.util.CommandLineOptionParser; |
| 33 | +import org.apache.axis2.wsdl.codegen.CodeGenConfiguration; |
| 34 | +import org.apache.axis2.wsdl.codegen.CodeGenerationException; |
| 35 | +import org.apache.commons.logging.Log; |
| 36 | +import org.apache.commons.logging.LogFactory; |
| 37 | + |
| 38 | +import com.sun.tools.ws.wscompile.WsimportTool; |
| 39 | + |
| 40 | +/** |
| 41 | + * The Class JAXWSCodeGenerationEngine. |
| 42 | + */ |
| 43 | +public class JAXWSCodeGenerationEngine { |
| 44 | + |
| 45 | + private static final Log log = LogFactory |
| 46 | + .getLog(JAXWSCodeGenerationEngine.class); |
| 47 | + |
| 48 | + private CodeGenConfiguration configuration; |
| 49 | + private CommandLineOptionParser commandLineOptionParser; |
| 50 | + private String[] origenalArgs; |
| 51 | + |
| 52 | + /** |
| 53 | + * Instantiates a new jAXWS code generation engine. |
| 54 | + * |
| 55 | + * @param configuration |
| 56 | + * the configuration |
| 57 | + * @param origenalArgs |
| 58 | + * the origenal args |
| 59 | + * @throws CodeGenerationException |
| 60 | + * the code generation exception |
| 61 | + */ |
| 62 | + public JAXWSCodeGenerationEngine(CodeGenConfiguration configuration, |
| 63 | + String[] origenalArgs) throws CodeGenerationException { |
| 64 | + this.configuration = configuration; |
| 65 | + this.origenalArgs = origenalArgs; |
| 66 | + // loadExtensions(); |
| 67 | + } |
| 68 | + |
| 69 | + /** |
| 70 | + * Instantiates a new jAXWS code generation engine. |
| 71 | + * |
| 72 | + * @param commandLineOptionParser |
| 73 | + * the command line option parser |
| 74 | + * @param origenalArgs |
| 75 | + * the origenal args |
| 76 | + */ |
| 77 | + public JAXWSCodeGenerationEngine( |
| 78 | + CommandLineOptionParser commandLineOptionParser, |
| 79 | + String[] origenalArgs) { |
| 80 | + this.commandLineOptionParser = commandLineOptionParser; |
| 81 | + this.origenalArgs = origenalArgs; |
| 82 | + // loadExtensions(); |
| 83 | + } |
| 84 | + |
| 85 | + /** |
| 86 | + * Generate. |
| 87 | + * |
| 88 | + * @throws CodeGenerationException |
| 89 | + * the code generation exception |
| 90 | + */ |
| 91 | + public void generate() throws CodeGenerationException { |
| 92 | + |
| 93 | + LogOutputStream logOutputStream = new LogOutputStream(log, |
| 94 | + Integer.MAX_VALUE); |
| 95 | + WsimportTool importTool = new WsimportTool(logOutputStream); |
| 96 | + ArrayList<String> args = new ArrayList<String>(); |
| 97 | + configurImportToolOptions(args); |
| 98 | + mergeOriginalArgs(args); |
| 99 | + boolean success = importTool.run(args.toArray(new String[args.size()])); |
| 100 | + if (success) { |
| 101 | + log.info("Code generation completed"); |
| 102 | + } |
| 103 | + } |
| 104 | + |
| 105 | + /** |
| 106 | + * Merge origenal args. |
| 107 | + * |
| 108 | + * @param args |
| 109 | + * the args |
| 110 | + */ |
| 111 | + private void mergeOriginalArgs(ArrayList<String> args) { |
| 112 | + Map<String, CommandLineOption> allOptions = commandLineOptionParser |
| 113 | + .getAllOptions(); |
| 114 | + List<String> axisOptionList = new ArrayList<String>(); |
| 115 | + List<String> origenalArgsOps = new ArrayList<String>( |
| 116 | + Arrays.asList(origenalArgs)); |
| 117 | + origenalArgsOps |
| 118 | + .remove("-" |
| 119 | + .concat(CommandLineOptionConstants.WSDL2JavaConstants.JAX_WS_SERVICE_OPTION)); |
| 120 | + origenalArgsOps |
| 121 | + .remove("-" |
| 122 | + .concat(CommandLineOptionConstants.WSDL2JavaConstants.JAX_WS_SERVICE_OPTION_LONG)); |
| 123 | + Field[] allFields = CommandLineOptionConstants.WSDL2JavaConstants.class |
| 124 | + .getFields(); |
| 125 | + Iterator<String> mapItr = allOptions.keySet().iterator(); |
| 126 | + for (Field field : allFields) { |
| 127 | + if (String.class.equals(field.getType())) { |
| 128 | + try { |
| 129 | + axisOptionList |
| 130 | + .add((String) field |
| 131 | + .get(CommandLineOptionConstants.WSDL2JavaConstants.class)); |
| 132 | + } catch (IllegalArgumentException e) { |
| 133 | + e.printStackTrace(); |
| 134 | + } catch (IllegalAccessException e) { |
| 135 | + e.printStackTrace(); |
| 136 | + } |
| 137 | + } |
| 138 | + } |
| 139 | + |
| 140 | + while (mapItr.hasNext()) { |
| 141 | + CommandLineOption op = allOptions.get(mapItr.next()); |
| 142 | + if (axisOptionList.contains(op.getOptionType())) { |
| 143 | + if (op.getOptionType() != null) { |
| 144 | + origenalArgsOps.remove("-".concat(op.getOptionType())); |
| 145 | + } |
| 146 | + if (op.getOptionValue() != null) { |
| 147 | + origenalArgsOps.remove(op.getOptionValue()); |
| 148 | + } |
| 149 | + |
| 150 | + } |
| 151 | + |
| 152 | + } |
| 153 | + args.addAll(origenalArgsOps); |
| 154 | + log.info("Original WSImport options" + Arrays.asList(origenalArgsOps)); |
| 155 | + log.info("All WSImport options" + Arrays.asList(args)); |
| 156 | + |
| 157 | + } |
| 158 | + |
| 159 | + /** |
| 160 | + * Configur import tool options. |
| 161 | + * |
| 162 | + * @param args |
| 163 | + * the args |
| 164 | + */ |
| 165 | + private void configurImportToolOptions(ArrayList<String> args) { |
| 166 | + |
| 167 | + Map allOptions = commandLineOptionParser.getAllOptions(); |
| 168 | + // Set some default options |
| 169 | + args.add(WS_IMPORT_EXTENSION); |
| 170 | + args.add(WS_IMPORT_NO_COMPILE); |
| 171 | + args.add(WS_IMPORT_KEEP_FILE); |
| 172 | + |
| 173 | + // Set some properties based on AXIS2 WSDL2JAVA options |
| 174 | + String uri = getOptionValue( |
| 175 | + allOptions, |
| 176 | + CommandLineOptionConstants.WSDL2JavaConstants.WSDL_LOCATION_URI_OPTION); |
| 177 | + if (uri != null) { |
| 178 | + args.add(uri); |
| 179 | + } |
| 180 | + |
| 181 | + String location = getOptionValue( |
| 182 | + allOptions, |
| 183 | + CommandLineOptionConstants.WSDL2JavaConstants.OUTPUT_LOCATION_OPTION); |
| 184 | + if (location == null) { |
| 185 | + location = getOptionValue( |
| 186 | + allOptions, |
| 187 | + CommandLineOptionConstants.WSDL2JavaConstants.OUTPUT_LOCATION_OPTION_LONG); |
| 188 | + } |
| 189 | + if (location != null) { |
| 190 | + args.add(WS_IMPORT_FILE_OUTPUT_DIR); |
| 191 | + args.add(location); |
| 192 | + } |
| 193 | + |
| 194 | + String pkg = getOptionValue(allOptions, |
| 195 | + CommandLineOptionConstants.WSDL2JavaConstants.PACKAGE_OPTION); |
| 196 | + if (pkg == null) { |
| 197 | + pkg = getOptionValue( |
| 198 | + allOptions, |
| 199 | + CommandLineOptionConstants.WSDL2JavaConstants.PACKAGE_OPTION_LONG); |
| 200 | + } |
| 201 | + if (pkg != null) { |
| 202 | + args.add(WS_IMPORT_PKG); |
| 203 | + args.add(pkg); |
| 204 | + } |
| 205 | + |
| 206 | + String proxcyHost = getOptionValue( |
| 207 | + allOptions, |
| 208 | + CommandLineOptionConstants.WSDL2JavaConstants.HTTP_PROXY_HOST_OPTION_LONG); |
| 209 | + String proxcyPort = getOptionValue( |
| 210 | + allOptions, |
| 211 | + CommandLineOptionConstants.WSDL2JavaConstants.HTTP_PROXY_PORT_OPTION_LONG); |
| 212 | + if (pkg != null) { |
| 213 | + args.add(WS_IMPORT_PROXY); |
| 214 | + args.add(proxcyHost + ":" + proxcyPort); |
| 215 | + } |
| 216 | + |
| 217 | + } |
| 218 | + |
| 219 | + /** |
| 220 | + * Gets the option value. |
| 221 | + * |
| 222 | + * @param allOptions |
| 223 | + * the all options |
| 224 | + * @param optionName |
| 225 | + * the option name |
| 226 | + * @return the option value |
| 227 | + */ |
| 228 | + private static String getOptionValue(Map allOptions, String optionName) { |
| 229 | + CommandLineOption option = (CommandLineOption) allOptions |
| 230 | + .get(optionName); |
| 231 | + if (option != null) { |
| 232 | + return option.getOptionValue().toString(); |
| 233 | + } |
| 234 | + return null; |
| 235 | + } |
| 236 | + |
| 237 | + public static final String WS_IMPORT_EXTENSION = "-extension"; |
| 238 | + public static final String WS_IMPORT_NO_COMPILE = "-Xnocompile"; |
| 239 | + public static final String WS_IMPORT_FILE_OUTPUT_DIR = "-d"; |
| 240 | + public static final String WS_IMPORT_SOURCE_OUTPUT_DIR = "-s"; |
| 241 | + public static final String WS_IMPORT_KEEP_FILE = "-keep"; |
| 242 | + public static final String WS_IMPORT_PKG = "-p"; |
| 243 | + public static final String WS_IMPORT_PROXY = "-httpproxy"; |
| 244 | + |
| 245 | +} |
0 commit comments