| 1 |
#!/usr/bin/env ruby |
|---|
| 2 |
|
|---|
| 3 |
require 'getoptlong' |
|---|
| 4 |
require 'soap/qname' |
|---|
| 5 |
require 'wsdl/parser' |
|---|
| 6 |
require 'wsdl/soap/classDefCreator' |
|---|
| 7 |
require 'wsdl/soap/servantSkeltonCreator' |
|---|
| 8 |
require 'wsdl/soap/driverCreator' |
|---|
| 9 |
require 'wsdl/soap/clientSkeltonCreator' |
|---|
| 10 |
require 'wsdl/soap/standaloneServerStubCreator' |
|---|
| 11 |
require 'wsdl/soap/cgiStubCreator' |
|---|
| 12 |
require 'wsdl/soap/webrickStubCreator' |
|---|
| 13 |
|
|---|
| 14 |
require 'devel/logger' |
|---|
| 15 |
|
|---|
| 16 |
class WSDL2RubyApp < Devel::Application |
|---|
| 17 |
private |
|---|
| 18 |
|
|---|
| 19 |
OptSet = [ |
|---|
| 20 |
['--wsdl','-w', GetoptLong::REQUIRED_ARGUMENT], |
|---|
| 21 |
['--type','-t', GetoptLong::REQUIRED_ARGUMENT], |
|---|
| 22 |
['--classDef','-e', GetoptLong::NO_ARGUMENT], |
|---|
| 23 |
['--clientSkelton','-c', GetoptLong::OPTIONAL_ARGUMENT], |
|---|
| 24 |
['--servantSkelton','-s', GetoptLong::OPTIONAL_ARGUMENT], |
|---|
| 25 |
['--cgiStub','-g', GetoptLong::OPTIONAL_ARGUMENT], |
|---|
| 26 |
['--webrickStub','-b', GetoptLong::OPTIONAL_ARGUMENT], |
|---|
| 27 |
['--standaloneServerStub','-a', GetoptLong::OPTIONAL_ARGUMENT], |
|---|
| 28 |
['--driver','-d', GetoptLong::OPTIONAL_ARGUMENT], |
|---|
| 29 |
['--force','-f', GetoptLong::NO_ARGUMENT], |
|---|
| 30 |
] |
|---|
| 31 |
|
|---|
| 32 |
def initialize |
|---|
| 33 |
super( 'app' ) |
|---|
| 34 |
STDERR.sync = true |
|---|
| 35 |
@wsdlLocation = nil |
|---|
| 36 |
@opt = {} |
|---|
| 37 |
@wsdl = nil |
|---|
| 38 |
@name = nil |
|---|
| 39 |
end |
|---|
| 40 |
|
|---|
| 41 |
def run |
|---|
| 42 |
@wsdlLocation, @opt = parseOpt( GetoptLong.new( *OptSet )) |
|---|
| 43 |
usageExit unless @wsdlLocation |
|---|
| 44 |
@wsdl = import(@wsdlLocation) |
|---|
| 45 |
@name = @wsdl.name.name || 'default' |
|---|
| 46 |
createFile |
|---|
| 47 |
0 |
|---|
| 48 |
end |
|---|
| 49 |
|
|---|
| 50 |
def createFile |
|---|
| 51 |
createClassDef if @opt.has_key?( 'classDef' ) |
|---|
| 52 |
createServantSkelton( @opt[ 'servantSkelton' ] ) if @opt.has_key?( 'servantSkelton' ) |
|---|
| 53 |
createCgiStub( @opt[ 'cgiStub' ] ) if @opt.has_key?( 'cgiStub' ) |
|---|
| 54 |
createWebrickStub( @opt[ 'webrickStub' ] ) if @opt.has_key?( 'webrickStub' ) |
|---|
| 55 |
createStandaloneServerStub( @opt[ 'standaloneServerStub' ] ) if @opt.has_key?( 'standaloneServerStub' ) |
|---|
| 56 |
createDriver( @opt[ 'driver' ] ) if @opt.has_key?( 'driver' ) |
|---|
| 57 |
createClientSkelton( @opt[ 'clientSkelton' ] ) if @opt.has_key?( 'clientSkelton' ) |
|---|
| 58 |
end |
|---|
| 59 |
|
|---|
| 60 |
def usageExit |
|---|
| 61 |
puts <<__EOU__ |
|---|
| 62 |
Usage: #{ $0 } --wsdl wsdlLocation [options] |
|---|
| 63 |
wsdlLocation: filename or URL |
|---|
| 64 |
|
|---|
| 65 |
Example: |
|---|
| 66 |
For server side: |
|---|
| 67 |
#{ $0 } --wsdl myApp.wsdl --type server |
|---|
| 68 |
For client side: |
|---|
| 69 |
#{ $0 } --wsdl myApp.wsdl --type client |
|---|
| 70 |
|
|---|
| 71 |
Options: |
|---|
| 72 |
--wsdl wsdlLocation |
|---|
| 73 |
--type server|client |
|---|
| 74 |
--type server implies; |
|---|
| 75 |
--classDef |
|---|
| 76 |
--servantSkelton |
|---|
| 77 |
--standaloneServerStub |
|---|
| 78 |
--type client implies; |
|---|
| 79 |
--classDef |
|---|
| 80 |
--clientSkelton |
|---|
| 81 |
--driver |
|---|
| 82 |
--classDef |
|---|
| 83 |
--clientSkelton [serviceName] |
|---|
| 84 |
--servantSkelton [portTypeName] |
|---|
| 85 |
--cgiStub [serviceName] |
|---|
| 86 |
--webrickStub [serviceName] |
|---|
| 87 |
--standaloneServerStub [serviceName] |
|---|
| 88 |
--driver [portTypeName] |
|---|
| 89 |
--force |
|---|
| 90 |
|
|---|
| 91 |
Terminology: |
|---|
| 92 |
Client <-> Driver <-(SOAP)-> Stub <-> Servant |
|---|
| 93 |
|
|---|
| 94 |
Driver and Stub: Automatically generated |
|---|
| 95 |
Client and Servant: Skelton generated (you should change) |
|---|
| 96 |
__EOU__ |
|---|
| 97 |
exit 1 |
|---|
| 98 |
end |
|---|
| 99 |
|
|---|
| 100 |
def parseOpt( getOpt ) |
|---|
| 101 |
opt = {} |
|---|
| 102 |
wsdl = nil |
|---|
| 103 |
begin |
|---|
| 104 |
getOpt.each do | name, arg | |
|---|
| 105 |
case name |
|---|
| 106 |
when "--wsdl" |
|---|
| 107 |
wsdl = arg |
|---|
| 108 |
when "--type" |
|---|
| 109 |
case arg |
|---|
| 110 |
when "server" |
|---|
| 111 |
opt[ 'classDef' ] = nil |
|---|
| 112 |
opt[ 'servantSkelton' ] = nil |
|---|
| 113 |
opt[ 'standaloneServerStub' ] = nil |
|---|
| 114 |
when "client" |
|---|
| 115 |
opt[ 'classDef' ] = nil |
|---|
| 116 |
opt[ 'driver' ] = nil |
|---|
| 117 |
opt[ 'clientSkelton' ] = nil |
|---|
| 118 |
else |
|---|
| 119 |
raise ArgumentError.new( "Unknown type #{ arg }" ) |
|---|
| 120 |
end |
|---|
| 121 |
when "--classDef", "--clientSkelton", "--servantSkelton", "--cgiStub", |
|---|
| 122 |
"--webrickStub", "--standaloneServerStub", "--driver" |
|---|
| 123 |
opt[ name.sub( /^--/, '' ) ] = arg.empty? ? nil : arg |
|---|
| 124 |
when "--force" |
|---|
| 125 |
opt[ 'force' ] = true |
|---|
| 126 |
else |
|---|
| 127 |
raise ArgumentError.new( "Unknown type #{ arg }" ) |
|---|
| 128 |
end |
|---|
| 129 |
end |
|---|
| 130 |
rescue |
|---|
| 131 |
usageExit |
|---|
| 132 |
end |
|---|
| 133 |
return wsdl, opt |
|---|
| 134 |
end |
|---|
| 135 |
|
|---|
| 136 |
def createClassDef |
|---|
| 137 |
log( SEV_INFO ) { "Creating class definition." } |
|---|
| 138 |
@classDefFilename = @name + '.rb' |
|---|
| 139 |
checkFile( @classDefFilename ) or return |
|---|
| 140 |
File.open( @classDefFilename, "w" ) do | f | |
|---|
| 141 |
f << WSDL::SOAP::ClassDefCreator.new( @wsdl ).dump |
|---|
| 142 |
end |
|---|
| 143 |
end |
|---|
| 144 |
|
|---|
| 145 |
def createClientSkelton( serviceName ) |
|---|
| 146 |
log( SEV_INFO ) { "Creating client skelton." } |
|---|
| 147 |
serviceName ||= @wsdl.services[ 0 ].name.name |
|---|
| 148 |
@clientSkeltonFilename = serviceName + 'Client.rb' |
|---|
| 149 |
checkFile( @clientSkeltonFilename ) or return |
|---|
| 150 |
File.open( @clientSkeltonFilename, "w" ) do | f | |
|---|
| 151 |
f << shbang << "\n" |
|---|
| 152 |
f << "require '#{ @driverFilename }'\n\n" if @driverFilename |
|---|
| 153 |
f << WSDL::SOAP::ClientSkeltonCreator.new( @wsdl ).dump( |
|---|
| 154 |
createName( serviceName )) |
|---|
| 155 |
end |
|---|
| 156 |
end |
|---|
| 157 |
|
|---|
| 158 |
def createServantSkelton( portTypeName ) |
|---|
| 159 |
log( SEV_INFO ) { "Creating servant skelton." } |
|---|
| 160 |
@servantSkeltonFilename = ( portTypeName || @name + 'Servant' ) + '.rb' |
|---|
| 161 |
checkFile( @servantSkeltonFilename ) or return |
|---|
| 162 |
File.open( @servantSkeltonFilename, "w" ) do | f | |
|---|
| 163 |
f << "require '#{ @classDefFilename }'\n\n" if @classDefFilename |
|---|
| 164 |
f << WSDL::SOAP::ServantSkeltonCreator.new( @wsdl ).dump( |
|---|
| 165 |
createName( portTypeName )) |
|---|
| 166 |
end |
|---|
| 167 |
end |
|---|
| 168 |
|
|---|
| 169 |
def createCgiStub( serviceName ) |
|---|
| 170 |
log( SEV_INFO ) { "Creating CGI stub." } |
|---|
| 171 |
serviceName ||= @wsdl.services[ 0 ].name.name |
|---|
| 172 |
@cgiStubFilename = serviceName + '.cgi' |
|---|
| 173 |
checkFile( @cgiStubFilename ) or return |
|---|
| 174 |
File.open( @cgiStubFilename, "w" ) do | f | |
|---|
| 175 |
f << shbang << "\n" |
|---|
| 176 |
f << "require '#{ @servantSkeltonFilename }'\n\n" if @servantSkeltonFilename |
|---|
| 177 |
f << WSDL::SOAP::CGIStubCreator.new( @wsdl ).dump( |
|---|
| 178 |
createName( serviceName )) |
|---|
| 179 |
end |
|---|
| 180 |
end |
|---|
| 181 |
|
|---|
| 182 |
def createWebrickStub( serviceName ) |
|---|
| 183 |
log( SEV_INFO ) { "Creating WEBrick SOAPlet stub." } |
|---|
| 184 |
serviceName ||= @wsdl.services[ 0 ].name.name |
|---|
| 185 |
@webrickStubFilename = 'httpd.rb' |
|---|
| 186 |
checkFile( @webrickStubFilename ) or return |
|---|
| 187 |
File.open( @webrickStubFilename, "w" ) do | f | |
|---|
| 188 |
f << shbang << "\n" |
|---|
| 189 |
f << "require '#{ @servantSkeltonFilename }'\n\n" if @servantSkeltonFilename |
|---|
| 190 |
f << WSDL::SOAP::WEBrickStubCreator.new( @wsdl ).dump( |
|---|
| 191 |
createName( serviceName )) |
|---|
| 192 |
end |
|---|
| 193 |
end |
|---|
| 194 |
|
|---|
| 195 |
def createStandaloneServerStub( serviceName ) |
|---|
| 196 |
log( SEV_INFO ) { "Creating standalone stub." } |
|---|
| 197 |
serviceName ||= @wsdl.services[ 0 ].name.name |
|---|
| 198 |
@standaloneServerStubFilename = serviceName + '.rb' |
|---|
| 199 |
checkFile( @standaloneServerStubFilename ) or return |
|---|
| 200 |
File.open( @standaloneServerStubFilename, "w" ) do | f | |
|---|
| 201 |
f << shbang << "\n" |
|---|
| 202 |
f << "require '#{ @servantSkeltonFilename }'\n\n" if @servantSkeltonFilename |
|---|
| 203 |
f << WSDL::SOAP::StandaloneServerStubCreator.new( @wsdl ).dump( |
|---|
| 204 |
createName( serviceName )) |
|---|
| 205 |
end |
|---|
| 206 |
end |
|---|
| 207 |
|
|---|
| 208 |
def createDriver( portTypeName ) |
|---|
| 209 |
log( SEV_INFO ) { "Creating driver." } |
|---|
| 210 |
@driverFilename = ( portTypeName || @name ) + 'Driver.rb' |
|---|
| 211 |
checkFile( @driverFilename ) or return |
|---|
| 212 |
File.open( @driverFilename, "w" ) do | f | |
|---|
| 213 |
f << "require '#{ @classDefFilename }'\n\n" if @classDefFilename |
|---|
| 214 |
f << WSDL::SOAP::DriverCreator.new( @wsdl ).dump( |
|---|
| 215 |
createName( portTypeName )) |
|---|
| 216 |
end |
|---|
| 217 |
end |
|---|
| 218 |
|
|---|
| 219 |
def checkFile( filename ) |
|---|
| 220 |
if FileTest.exist?( filename ) |
|---|
| 221 |
if @opt.has_key?( 'force' ) |
|---|
| 222 |
log( SEV_WARN ) { |
|---|
| 223 |
"File '#{ filename }' exists but overrides it." |
|---|
| 224 |
} |
|---|
| 225 |
true |
|---|
| 226 |
else |
|---|
| 227 |
log( SEV_WARN ) { |
|---|
| 228 |
"File '#{ filename }' exists. #{ $0 } did not override it." |
|---|
| 229 |
} |
|---|
| 230 |
false |
|---|
| 231 |
end |
|---|
| 232 |
else |
|---|
| 233 |
log( SEV_INFO ) { "Creates file '#{ filename }'." } |
|---|
| 234 |
true |
|---|
| 235 |
end |
|---|
| 236 |
end |
|---|
| 237 |
|
|---|
| 238 |
def shbang |
|---|
| 239 |
"#!/usr/bin/env ruby" |
|---|
| 240 |
end |
|---|
| 241 |
|
|---|
| 242 |
def createName( name ) |
|---|
| 243 |
name ? XSD::QName.new( @wsdl.targetNamespace, name ) : nil |
|---|
| 244 |
end |
|---|
| 245 |
|
|---|
| 246 |
def import(location) |
|---|
| 247 |
content = nil |
|---|
| 248 |
if FileTest.exist?(location) |
|---|
| 249 |
content = File.open(location).read |
|---|
| 250 |
else |
|---|
| 251 |
require 'http-access2' |
|---|
| 252 |
c = HTTPAccess2::Client.new(ENV[ 'http_proxy' ] || ENV[ 'HTTP_PROXY' ]) |
|---|
| 253 |
content = c.getContent(location) |
|---|
| 254 |
end |
|---|
| 255 |
WSDL::WSDLParser.createParser.parse(content) |
|---|
| 256 |
end |
|---|
| 257 |
end |
|---|
| 258 |
|
|---|
| 259 |
WSDL2RubyApp.new.start |
|---|