@@ -19,11 +19,19 @@ var yeoman = require('yeoman-generator');
19
19
var glob = require ( 'glob' ) ;
20
20
var path = require ( 'path' ) ;
21
21
var mkdirp = require ( 'mkdirp' ) ;
22
+ var fileUrl = require ( 'file-url' ) ;
23
+ var exec = require ( 'child_process' ) . exec ;
24
+ var fs = require ( 'fs' ) ;
25
+
26
+ const chalk = require ( 'chalk' ) ;
22
27
const utils = require ( './util' ) ;
23
28
24
- const defaultCamelVersion = "2.22.1 " ;
29
+ const defaultCamelVersion = "2.22.2 " ;
25
30
const defaultCamelDSL = "spring" ;
26
31
const defaultPackagePrefix = "com." ;
32
+ const defaultOutputDirectory = 'src/main/java' ;
33
+ const defaultJaxRsUrl = "http://localhost:8081/jaxrs" ;
34
+ const defaultJaxWsUrl = "http://localhost:8080/somepath" ;
27
35
28
36
function consoleHeader ( ) {
29
37
var pjson = require ( '../package.json' ) ;
@@ -51,10 +59,21 @@ module.exports = class extends yeoman {
51
59
constructor ( args , opts ) {
52
60
super ( args , opts ) ;
53
61
62
+ // base arguments
54
63
this . argument ( 'appname' , { type : String , required : false } ) ;
55
64
this . argument ( 'camelVersion' , { type : String , required : false } ) ;
56
65
this . argument ( 'camelDSL' , { type : String , required : false } ) ;
57
66
this . argument ( 'package' , { type : String , required : false } ) ;
67
+
68
+ // wsdl2rest options
69
+ this . option ( 'wsdl2rest' ) ;
70
+ this . option ( 'debug' ) ;
71
+
72
+ // wsdl2rest arguments
73
+ this . argument ( 'wsdl' , { type : String , required : false } ) ;
74
+ this . argument ( 'outdirectory' , { type : String , required : false } ) ;
75
+ this . argument ( 'jaxrs' , { type : String , required : false } ) ;
76
+ this . argument ( 'jaxws' , { type : String , required : false } ) ;
58
77
}
59
78
60
79
prompting ( ) {
@@ -69,7 +88,18 @@ module.exports = class extends yeoman {
69
88
showPrompts = false ;
70
89
}
71
90
72
- if ( showPrompts ) {
91
+ let showWsdl2Rest = this . options . wsdl2rest ;
92
+ if ( typeof showWsdl2Rest == 'undefined' ) showWsdl2Rest = false ;
93
+
94
+ var showW2RPrompts = true ;
95
+ if ( ! showPrompts && showWsdl2Rest ) {
96
+ showW2RPrompts = ( utils . isNull ( this . options . wsdl ) ) &&
97
+ ( utils . isNull ( this . options . outdirectory ) ) &&
98
+ ( utils . isNull ( this . options . jaxrs ) ) &&
99
+ ( utils . isNull ( this . options . jaxws ) ) ;
100
+ }
101
+
102
+ if ( showPrompts || showW2RPrompts ) {
73
103
consoleHeader ( ) ;
74
104
}
75
105
@@ -98,7 +128,9 @@ module.exports = class extends yeoman {
98
128
message : 'Camel DSL type (blueprint, spring, or java)' ,
99
129
choices : [ 'blueprint' , 'spring' , 'java' ] ,
100
130
default : defaultDSL ,
101
- validate : utils . validateCamelDSL ,
131
+ validate : ( value ) => {
132
+ return utils . validateCamelDSL ( value , showWsdl2Rest ) ;
133
+ } ,
102
134
store : true
103
135
} , prompts ) ;
104
136
utils . addPrompt ( {
@@ -109,23 +141,73 @@ module.exports = class extends yeoman {
109
141
validate : utils . validatePackage
110
142
} , prompts ) ;
111
143
112
- if ( showPrompts ) {
144
+ var defaultOutputDir = utils . setDefault ( defaultOutputDirectory , this . options . outdirectory ) ;
145
+ var defaultJaxRS = utils . setDefault ( defaultJaxRsUrl , this . options . jaxrs ) ;
146
+ var defaultJaxWS = utils . setDefault ( defaultJaxWsUrl , this . options . jaxws ) ;
147
+ var defaultWsdl = utils . setDefault ( 'no wsdl specified' , this . options . wsdl ) ;
148
+
149
+ if ( showWsdl2Rest ) {
150
+ utils . addPrompt ( {
151
+ type : 'input' ,
152
+ name : 'wsdl' ,
153
+ message : 'URL to the input WSDL' ,
154
+ store : true
155
+ } , prompts ) ;
156
+ utils . addPrompt ( {
157
+ type : 'input' ,
158
+ name : 'outdirectory' ,
159
+ message : 'Name of the output directory for generated artifacts' ,
160
+ default : defaultOutputDir ,
161
+ store : true
162
+ } , prompts ) ;
163
+ utils . addPrompt ( {
164
+ type : 'input' ,
165
+ name : 'jaxrsURL' ,
166
+ message : 'Address of the generated jaxrs endpoint' ,
167
+ default : defaultJaxRS ,
168
+ store : true
169
+ } , prompts ) ;
170
+ utils . addPrompt ( {
171
+ type : 'input' ,
172
+ name : 'jaxwsURL' ,
173
+ message : 'Address of the target jaxws endpoint' ,
174
+ default : defaultJaxWS ,
175
+ store : true
176
+ } , prompts ) ;
177
+ }
178
+
179
+ if ( showPrompts || showW2RPrompts ) {
113
180
return this . prompt ( prompts ) . then ( function ( props ) {
114
181
this . appname = props . name ;
115
182
this . camelVersion = props . camelVersion ;
116
183
this . camelDSL = props . camelDSL ;
117
184
this . package = props . package ;
185
+ if ( showWsdl2Rest ) {
186
+ this . outdirectory = props . outdirectory ;
187
+ this . wsdl = props . wsdl ;
188
+ this . jaxwsURL = props . jaxwsURL ;
189
+ this . jaxrsURL = props . jaxrsURL ;
190
+ }
118
191
} . bind ( this ) ) ;
119
192
} else {
120
193
this . appname = defaultProject ;
121
194
this . camelVersion = defaultVersion ;
122
195
this . camelDSL = defaultDSL ;
123
196
this . package = defaultPackage ;
124
- }
197
+ if ( showWsdl2Rest ) {
198
+ this . outdirectory = defaultOutputDir ;
199
+ this . wsdl = defaultWsdl ;
200
+ this . jaxwsURL = defaultJaxWS ;
201
+ this . jaxrsURL = defaultJaxRS ;
202
+ }
203
+ }
125
204
} ;
126
205
127
206
//writing logic here
128
207
writing ( ) {
208
+ let showWsdl2Rest = this . options . wsdl2rest ;
209
+ let showDebug = this . options . debug ;
210
+
129
211
var packageFolder = this . package . replace ( / \. / g, '/' ) ;
130
212
var src = 'src/main/java' ;
131
213
var myTemplatePath = path . join ( this . templatePath ( ) , this . camelDSL ) ;
@@ -147,11 +229,144 @@ module.exports = class extends yeoman {
147
229
userProps . package = this . package ;
148
230
149
231
for ( var i = 0 ; i < this . files . length ; i ++ ) {
150
- this . fs . copyTpl (
151
- this . templatePath ( this . files [ i ] ) ,
152
- this . destinationPath ( this . files [ i ] . replace ( / s r c \/ m a i n \/ j a v a / g, path . join ( src , packageFolder ) ) ) ,
153
- { userProps : userProps }
154
- ) ;
232
+ var skipFile = false ;
233
+ if ( showWsdl2Rest ) {
234
+ skipFile = testFileForWsdl2RestSkip ( this . files [ i ] ) ;
235
+ } else {
236
+ skipFile = testFileForTemplateSkip ( this . files [ i ] ) ;
237
+ }
238
+
239
+ if ( ! skipFile ) {
240
+ if ( this . files [ i ] . localeCompare ( 'pom.xml.wsdl2rest' ) == 0 ) {
241
+ var tempOutFile = "pom.xml" ;
242
+ this . fs . copyTpl (
243
+ this . templatePath ( this . files [ i ] ) ,
244
+ this . destinationPath ( tempOutFile . replace ( / s r c \/ m a i n \/ j a v a / g, path . join ( src , packageFolder ) ) ) ,
245
+ { userProps : userProps }
246
+ ) ;
247
+ } else {
248
+ this . fs . copyTpl (
249
+ this . templatePath ( this . files [ i ] ) ,
250
+ this . destinationPath ( this . files [ i ] . replace ( / s r c \/ m a i n \/ j a v a / g, path . join ( src , packageFolder ) ) ) ,
251
+ { userProps : userProps }
252
+ ) ;
253
+ }
254
+ }
255
+ }
256
+
257
+ if ( showWsdl2Rest ) {
258
+ return wsdl2restGenerate ( this . wsdl , this . outdirectory , this . jaxrsURL , this . jaxwsURL , this . camelDSL , showDebug ) ;
155
259
}
156
260
}
157
261
} ;
262
+
263
+ var skipListForWsdl2RestFiles = [
264
+ 'src/main/resources/META-INF/spring/camel-context.xml' ,
265
+ 'src/main/resources/OSGI-INF/blueprint/blueprint.xml' ,
266
+ 'pom.xml'
267
+ ] ;
268
+
269
+ function testFileForWsdl2RestSkip ( file ) {
270
+ for ( var i = 0 ; i < skipListForWsdl2RestFiles . length ; i ++ ) {
271
+ if ( skipListForWsdl2RestFiles [ i ] == file ) return true ;
272
+ }
273
+ }
274
+
275
+ var skipListForTemplateFiles = [
276
+ 'pom.xml.wsdl2rest'
277
+ ] ;
278
+
279
+ function testFileForTemplateSkip ( file ) {
280
+ for ( var i = 0 ; i < skipListForTemplateFiles . length ; i ++ ) {
281
+ if ( skipListForTemplateFiles [ i ] == file ) return true ;
282
+ }
283
+ }
284
+
285
+ function wsdl2restGenerate ( wsdlUrl , outputDirectory , jaxrs , jaxws , dsl , isDebug ) {
286
+ if ( dsl . includes ( 'java' ) ) {
287
+ console . log ( `Generating Rest DSL from SOAP for a Camel Java DSL is currently unsupported.` ) ;
288
+ return ;
289
+ }
290
+
291
+ var wsdl2restdir = path . join ( __dirname , 'wsdl2rest' ) ;
292
+ var targetDir = path . join ( wsdl2restdir , 'target' ) ;
293
+ var jar = utils . findWsdl2RestJar ( targetDir ) ;
294
+
295
+ var logPath = path . join ( wsdl2restdir , 'config' , 'logging.properties' ) ;
296
+ var logUrl = fileUrl ( logPath ) ;
297
+
298
+ var actualJavaOutDirectory = outputDirectory ;
299
+ if ( actualJavaOutDirectory . endsWith ( '/java' ) ) {
300
+ actualJavaOutDirectory = actualJavaOutDirectory . substring ( 0 , actualJavaOutDirectory . indexOf ( "/java" ) ) ;
301
+ }
302
+ var outPath = path . join ( process . cwd ( ) , actualJavaOutDirectory ) ;
303
+ var wsdlFileUrl ;
304
+ if ( wsdlUrl . startsWith ( 'http' ) ) {
305
+ wsdlFileUrl = wsdlUrl ;
306
+ } else if ( ! wsdlUrl . startsWith ( 'file:' ) ) {
307
+ wsdlFileUrl = fileUrl ( wsdlUrl ) ;
308
+ } else {
309
+ wsdlFileUrl = wsdlUrl ;
310
+ }
311
+
312
+ if ( ! fs . existsSync ( outPath ) ) {
313
+ console . log ( `Creating wsdl2rest java output directory` ) ;
314
+ fs . mkdirSync ( outPath ) ;
315
+ }
316
+
317
+ var restContextPath ;
318
+ var rawContextPath ;
319
+ var isBlueprint = dsl . includes ( 'blueprint' ) > 0 ;
320
+ if ( isBlueprint ) {
321
+ rawContextPath = "src/main/resources/OSGI-INF/blueprint/blueprint.xml" ;
322
+ } else {
323
+ rawContextPath = "src/main/resources/META-INF/spring/camel-context.xml" ;
324
+ }
325
+ restContextPath = path . join ( process . cwd ( ) , rawContextPath ) ;
326
+
327
+ // build the java command with classpath, class name, and the passed parameters
328
+ var cmdString = 'java '
329
+ + ' -Dlog4j.configuration=' + logUrl
330
+ + ' -jar ' + jar
331
+ + ' --wsdl ' + wsdlFileUrl
332
+ + ' --out ' + outPath ;
333
+
334
+ if ( isBlueprint ) {
335
+ cmdString = cmdString + ' --blueprint-context ' + restContextPath ;
336
+ } else {
337
+ cmdString = cmdString + ' --camel-context ' + restContextPath ;
338
+ }
339
+
340
+ if ( jaxrs ) {
341
+ cmdString = cmdString + ' --jaxrs ' + jaxrs ;
342
+ }
343
+ if ( jaxws ) {
344
+ cmdString = cmdString + ' --jaxws ' + jaxws ;
345
+ }
346
+ console . log ( 'Calling wsdl2rest' ) ;
347
+ if ( isDebug ) {
348
+ console . log ( ' command used: ' + cmdString ) ;
349
+ }
350
+ return new Promise ( ( resolve , reject ) => {
351
+ const wsdl2rest = exec ( cmdString ) ;
352
+ if ( isDebug ) {
353
+ wsdl2rest . stdout . on ( 'data' , function ( data ) {
354
+ console . log ( `stdout: ${ data } ` ) ;
355
+ } ) ;
356
+ wsdl2rest . stderr . on ( 'data' , function ( data ) {
357
+ console . log ( `stderr: ${ data } ` ) ;
358
+ } ) ;
359
+ }
360
+ wsdl2rest . on ( 'close' , function ( code ) {
361
+ if ( code === 0 ) {
362
+ console . log ( ' ' + chalk . green ( 'create' ) + ' CXF artifacts for specified WSDL at ' + outputDirectory ) ;
363
+ console . log ( ' ' + chalk . green ( 'create' ) + ' ' + rawContextPath ) ;
364
+ resolve ( )
365
+ } else {
366
+ reject ( )
367
+ console . log ( ` stderr: ${ code } ` ) ;
368
+ console . log ( ` wsdl2rest did not generate artifacts successfully - please check the log file for details or re-run with --debug flag` ) ;
369
+ }
370
+ } ) ;
371
+ } )
372
+ }
0 commit comments