Skip to content

Commit

Permalink
adding wsdl2rest support to camel-project generator camel-tooling#11
Browse files Browse the repository at this point in the history
-- initial cut with fat jar and running the wsdl2rest tool with inputs
-- updating to support full paths and a logging.props file
-- Updating version for when we include wsdl2rest functionality
-- also updating the travis file to build the wsdl2rest jar
-- additional work on test and wsdl2rest call
-- additional fixes
-- Setting jar to specific version vs asterisk and returning to broken
test
-- updates to make wsdl2rest run in a promise camel-tooling#11
-- Increasing the mocha timeout in the travis configuration camel-tooling#11
-- addressing some review feedback
-- updating tests to run with npm test call
-- reset readme.md
-- updating console messages to better reflect what's happening to user
-- adding --debug flag for wsdl2rest
-- fixing log4j issue
-- adding appropriate dependencies to pom for wsdl2rest projects
-- fixing issue when using URL to non-local WSDL
-- adding test for non-local WSDL
-- adding validation to avoid java and wsdl2rest combo
-- adding function to avoid hardcoding wsdl2rest fat jar version
-- addressing more feedback
-- fixing test for jar finding function
-- increased mocha test timeout to 15000
-- added new test that starts a sample JAX-WS web service and uses the
WSDL it serves up to create a project
-- updated helloworld wsdl and sample WS
-- added jaxws and jaxrs URL property prompts
-- fixed helloworld wsdl to generate properly
-- added new command line arguments and first test

Signed-off-by: Brian Fitzpatrick <bfitzpat@redhat.com>
  • Loading branch information
bfitzpat committed Nov 28, 2018
1 parent a636b6b commit 7c25bc2
Show file tree
Hide file tree
Showing 18 changed files with 2,180 additions and 122 deletions.
9 changes: 9 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,13 @@
/node_modules/
/coverage/
/.nyc_output/
/bin/
/app/wsdl2rest/target/
log-camel-lsp.out
.classpath
.project
/.settings/
app/wsdl2rest/.classpath
app/wsdl2rest/.project
/app/wsdl2rest/.settings/
.DS_Store
2 changes: 1 addition & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,14 @@ before_install:
- 'export PATH=./node_modules/.bin:$PATH'
- 'npm install -g typescript'
- 'npm install -g mocha'
- 'mvn install -f ./app/wsdl2rest/pom.xml'
install:
- 'npm install --ignore-scripts'
- 'npm install'
# https://github.com/travis-ci/travis-ci/issues/8813
- 'rm -f ./node_modules/.bin/which'
script:
- 'npm test --silent'
- 'mocha'
after_success:
- if [[ $TRAVIS_PULL_REQUEST == "false" && $TRAVIS_BRANCH == "master" ]]; then
sonar-scanner;
Expand Down
29 changes: 17 additions & 12 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -77,30 +77,31 @@ The generator is located in the npm repository (https://www.npmjs.com/package/ge
\____| \__,_| |_| |_| |_| \___| |_|
-----------------------------------------------
Camel Project Generator
Version: 0.1.2
-----------------------------------------------
? Your Camel project name (myproject)
? Your Camel version 2.18.1
? DSL type (blueprint or spring) blueprint
? Your Camel version 2.18.2
? Camel DSL type (blueprint, spring, or java) blueprint
? Package name: com.myproject
camel project name myproject
camel version 2.18.1
camel version 2.18.2
camel DSL blueprint
package name com.myproject
Creating folders
Copying files
Copying dot files
create pom.xml
create README.md
create src\main\resources\OSGI-INF\blueprint\blueprint.xml
create src\main\resources\OSGI-INF\log4j2.properties
>
```

### Notes on input fields

* 'Camel project name' defaults to the name of the directory in which you start the generator.
* 'Camel version' defaults to 2.18.1 but if you provide a different version, that version then becomes the default for the next time the generator is run.
* 'DSL type' defaults to 'spring' but if you change it to a valid DSL type such as 'blueprint', 'spring', or 'java', that becomes the default for the next time the generator is run. If you enter an invalid value, the generator will present an error ">> Camel DSL must be either 'spring', 'blueprint', or 'java'.".
* 'Camel version' defaults to 2.22.1 but if you provide a different version, that version then becomes the default for the next time the generator is run.
* 'Camel DSL type' defaults to 'spring' but if you change it to a valid DSL type such as 'blueprint', 'spring', or 'java', that becomes the default for the next time the generator is run. If you enter an invalid value, the generator will present an error ">> Camel DSL must be either 'spring', 'blueprint', or 'java'.".
* 'Package name' defaults to 'com.' + the name of the directory (i.e. 'com.myproject'). This default does not change if you provide a different value.

## Development Notes
Expand All @@ -122,6 +123,14 @@ Then create a directory you wish to create a Camel project in and run the genera
> yo camel-project
```

## Running the Generator from the Command Line (*NEW*)
With version 0.1.2 we have added command-line capabilities for providing argument values for the prompted information. Without prompting, this allows us to use the generator as part of a larger script to help prep a new project in a more automated fashion.

This allows us to do things like the following and avoid having to go through the prompts:
```
> yo camel-project appname=MyApp camelVersion=2.19.1 camelDSL=spring package=com.myapp
```

### Running the Mocha tests
First you must install mocha with npm.
```
Expand All @@ -132,14 +141,10 @@ Then, in the main generator-camel-project directory:
> mocha
```

## Known issues
## Running the Generated Templates

Generated templates for spring and Java DSLs can be run with:
Generated templates for spring, blueprint, and Java DSLs can be run with:
```
> mvn install
> mvn camel:run
```

Unfortunately, the blueprint version does not run successfully. Working on finding a solution.
Created https://github.com/camel-tooling/generator-camel-project/issues/1

235 changes: 225 additions & 10 deletions app/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,19 @@ var yeoman = require('yeoman-generator');
var glob = require('glob');
var path = require('path');
var mkdirp = require('mkdirp');
var fileUrl = require('file-url');
var exec = require('child_process').exec;
var fs = require('fs');

const chalk = require('chalk');
const utils = require('./util');

const defaultCamelVersion = "2.22.1";
const defaultCamelVersion = "2.22.2";
const defaultCamelDSL = "spring";
const defaultPackagePrefix = "com.";
const defaultOutputDirectory = 'src/main/java';
const defaultJaxRsUrl = "http://localhost:8081/jaxrs";
const defaultJaxWsUrl = "http://localhost:8080/somepath";

function consoleHeader() {
var pjson = require('../package.json');
Expand Down Expand Up @@ -51,10 +59,21 @@ module.exports = class extends yeoman {
constructor(args, opts) {
super(args, opts);

// base arguments
this.argument('appname', { type: String, required: false });
this.argument('camelVersion', { type: String, required: false });
this.argument('camelDSL', { type: String, required: false });
this.argument('package', { type: String, required: false });

// wsdl2rest options
this.option('wsdl2rest');
this.option('debug');

// wsdl2rest arguments
this.argument('wsdl', { type: String, required: false });
this.argument('outdirectory', { type: String, required: false });
this.argument('jaxrs', { type: String, required: false });
this.argument('jaxws', { type: String, required: false });
}

prompting() {
Expand All @@ -69,7 +88,14 @@ module.exports = class extends yeoman {
showPrompts = false;
}

if (showPrompts) {
let showWsdl2Rest = this.options.wsdl2rest;

var showW2EPrompts = true;
if (!showPrompts && showWsdl2Rest) {
showW2EPrompts = !(utils.isNotNull(this.options.wsdl));
}

if (showPrompts || showW2EPrompts) {
consoleHeader();
}

Expand Down Expand Up @@ -98,7 +124,9 @@ module.exports = class extends yeoman {
message: 'Camel DSL type (blueprint, spring, or java)',
choices: ['blueprint', 'spring', 'java'],
default: defaultDSL,
validate: utils.validateCamelDSL,
validate: (value, showWsdl2Rest) => {
return utils.validateCamelDSL(value, showWsdl2Rest);
},
store: true
}, prompts);
utils.addPrompt({
Expand All @@ -109,23 +137,77 @@ module.exports = class extends yeoman {
validate : utils.validatePackage
}, prompts);

if (showPrompts) {
var defaultOutputDir = utils.setDefault(defaultOutputDirectory, this.options.outdirectory);
var defaultJaxRS = utils.setDefault(defaultJaxRsUrl, this.options.jaxrs);
var defaultJaxWS = utils.setDefault(defaultJaxWsUrl, this.options.jaxws);
var defaultWsdl = utils.setDefault('no wsdl specified', this.options.wsdl);

if (showWsdl2Rest) {
utils.addPrompt({
type: 'input',
name: 'wsdl',
message: 'URL to the input WSDL',
store: true
}, prompts);
utils.addPrompt({
type: 'input',
name: 'outdirectory',
message: 'Name of the output directory for generated artifacts',
default: defaultOutputDir,
store: true
}, prompts);
utils.addPrompt({
type: 'input',
name: 'jaxrsURL',
message: 'Address of the generated jaxrs endpoint',
default: defaultJaxRS,
store: true
}, prompts);
utils.addPrompt({
type: 'input',
name: 'jaxwsURL',
message: 'Address of the target jaxws endpoint',
default: defaultJaxWS,
store: true
}, prompts);
}

console.log('showPrompts = ' + showPrompts);
console.log('showw2eprompts = ' + showW2EPrompts);

if (showPrompts || showW2EPrompts) {
return this.prompt(prompts).then(function (props) {
this.appname = props.name;
this.camelVersion = props.camelVersion;
this.camelDSL = props.camelDSL;
this.package = props.package;
if (showWsdl2Rest) {
this.outdirectory = props.outdirectory;
this.wsdl = props.wsdl;
this.jaxwsURL = props.jaxwsURL;
this.jaxrsURL = props.jaxrsURL;
}
}.bind(this));
} else {
this.appname = defaultProject;
this.camelVersion = defaultVersion;
this.camelDSL = defaultDSL;
this.package = defaultPackage;
}
if (showWsdl2Rest) {
this.outdirectory = defaultOutputDir;
this.wsdl = defaultWsdl;
this.jaxwsURL = defaultJaxWS;
this.jaxrsURL = defaultJaxRS;
console.log('wsdl = ' + this.wsdl);
}
}
};

//writing logic here
writing() {
let showWsdl2Rest = this.options.wsdl2rest;
let showDebug = this.options.debug;

var packageFolder = this.package.replace(/\./g, '/');
var src = 'src/main/java';
var myTemplatePath = path.join(this.templatePath(), this.camelDSL);
Expand All @@ -147,11 +229,144 @@ module.exports = class extends yeoman {
userProps.package = this.package;

for (var i = 0; i < this.files.length; i++) {
this.fs.copyTpl(
this.templatePath(this.files[i]),
this.destinationPath(this.files[i].replace(/src\/main\/java/g, path.join(src, packageFolder))),
{ userProps: userProps }
);
var skipFile = false;
if (showWsdl2Rest) {
skipFile = testFileForWsdl2RestSkip(this.files[i]); //(testFileForWsdl2RestSkip(this.files[i]) == true);
} else {
skipFile = testFileForTemplateSkip(this.files[i]);
}

if (!skipFile) {
if(this.files[i].localeCompare('pom.xml.wsdl2rest') == 0) {
var tempOutFile = "pom.xml";
this.fs.copyTpl(
this.templatePath(this.files[i]),
this.destinationPath(tempOutFile.replace(/src\/main\/java/g, path.join(src, packageFolder))),
{ userProps: userProps }
);
} else {
this.fs.copyTpl(
this.templatePath(this.files[i]),
this.destinationPath(this.files[i].replace(/src\/main\/java/g, path.join(src, packageFolder))),
{ userProps: userProps }
);
}
}
}

if (showWsdl2Rest) {
return wsdl2restGenerate(this.wsdl, this.outdirectory, this.jaxrsURL, this.jaxwsURL, this.camelDSL, showDebug);
}
}
};

var skipListForWsdl2RestFiles = [
'src/main/resources/META-INF/spring/camel-context.xml',
'src/main/resources/OSGI-INF/blueprint/blueprint.xml',
'pom.xml'
];

function testFileForWsdl2RestSkip(file) {
for(var i=0; i<skipListForWsdl2RestFiles.length; i++) {
if (skipListForWsdl2RestFiles[i] == file) return true;
}
}

var skipListForTemplateFiles = [
'pom.xml.wsdl2rest'
];

function testFileForTemplateSkip(file) {
for(var i=0; i<skipListForTemplateFiles.length; i++) {
if (skipListForTemplateFiles[i] == file) return true;
}
}

function wsdl2restGenerate(wsdlUrl, outputDirectory, jaxrs, jaxws, dsl, isDebug) {
if (dsl.includes('java')) {
console.log(`Generating Rest DSL from SOAP for a Camel Java DSL is currently unsupported.`);
return;
}

var wsdl2restdir = path.join(__dirname, 'wsdl2rest');
var targetDir = path.join(wsdl2restdir, 'target');
var jar = utils.findWsdl2RestJar(targetDir);

var logPath = path.join(wsdl2restdir, 'config', 'logging.properties');
var logUrl = fileUrl(logPath);

var actualJavaOutDirectory = outputDirectory;
if (actualJavaOutDirectory.endsWith('/java')) {
actualJavaOutDirectory = actualJavaOutDirectory.substring(0, actualJavaOutDirectory.indexOf("/java"));
}
var outPath = path.join(process.cwd(), actualJavaOutDirectory);
var wsdlFileUrl;
if (wsdlUrl.startsWith('http')) {
wsdlFileUrl = wsdlUrl;
} else if (!wsdlUrl.startsWith('file:')) {
wsdlFileUrl = fileUrl(wsdlUrl);
} else {
wsdlFileUrl = wsdlUrl;
}

if (!fs.existsSync(outPath)){
console.log(`Creating wsdl2rest java output directory`);
fs.mkdirSync(outPath);
}

var restContextPath;
var rawContextPath;
var isBlueprint = dsl.includes('blueprint') > 0;
if (isBlueprint) {
rawContextPath = "src/main/resources/OSGI-INF/blueprint/blueprint.xml";
} else {
rawContextPath = "src/main/resources/META-INF/spring/camel-context.xml";
}
restContextPath = path.join(process.cwd(), rawContextPath);

// build the java command with classpath, class name, and the passed parameters
var cmdString = 'java '
+ ' -Dlog4j.configuration=' + logUrl
+ ' -jar ' + jar
+ ' --wsdl ' + wsdlFileUrl
+ ' --out ' + outPath;

if (isBlueprint) {
cmdString = cmdString + ' --blueprint-context ' + restContextPath;
} else {
cmdString = cmdString + ' --camel-context ' + restContextPath;
}

if (jaxrs) {
cmdString = cmdString + ' --jaxrs ' + jaxrs;
}
if (jaxws) {
cmdString = cmdString + ' --jaxws ' + jaxws;
}
console.log('Calling wsdl2rest');
if (isDebug) {
console.log(' command used: ' + cmdString);
}
return new Promise((resolve, reject) => {
const wsdl2rest = exec(cmdString);
if (isDebug) {
wsdl2rest.stdout.on('data', function (data) {
console.log(`stdout: ${data}`);
});
wsdl2rest.stderr.on('data', function (data) {
console.log(`stderr: ${data}`);
});
}
wsdl2rest.on('close', function (code) {
if (code === 0) {
console.log(' ' + chalk.green('create') + ' CXF artifacts for specified WSDL at ' + outputDirectory);
console.log(' ' + chalk.green('create') + ' ' + rawContextPath);
resolve()
} else {
reject()
console.log(` stderr: ${code}`);
console.log(` wsdl2rest did not generate artifacts successfully - please check the log file for details or re-run with --debug flag`);
}
});
})
}
Loading

0 comments on commit 7c25bc2

Please sign in to comment.