Issue 36: changed to java set types and added domain objects

git-svn-id: http://jclouds.googlecode.com/svn/trunk@874 3d8758e0-26b5-11de-8745-db77d3ebf521
This commit is contained in:
adrian.f.cole 2009-05-29 17:23:25 +00:00
parent b34e7180b9
commit d36eb5e3c8
9 changed files with 1645 additions and 522 deletions

View File

@ -24,9 +24,7 @@
specific language governing permissions and limitations specific language governing permissions and limitations
under the License. under the License.
==================================================================== ====================================================================
--> --><project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<parent> <parent>
<artifactId>jclouds-s3-project</artifactId> <artifactId>jclouds-s3-project</artifactId>
<groupId>org.jclouds</groupId> <groupId>org.jclouds</groupId>
@ -41,7 +39,8 @@
<!-- as this has a dependency on something you can't get from maven, excluding <!-- as this has a dependency on something you can't get from maven, excluding
<module>googleappengine</module> <module>googleappengine</module>
--> -->
</modules> <module>helloworld</module>
</modules>
<dependencies> <dependencies>
<dependency> <dependency>
<groupId>${project.groupId}</groupId> <groupId>${project.groupId}</groupId>
@ -62,4 +61,4 @@
<scope>test</scope> <scope>test</scope>
</dependency> </dependency>
</dependencies> </dependencies>
</project> </project>

View File

@ -23,27 +23,6 @@
# ==================================================================== # ====================================================================
# #
#### ####
#
# Copyright (C) 2009 Adrian Cole <adrian@jclouds.org>
#
# ====================================================================
# Licensed to the Apache Software Foundation (ASF) under one
# or more contributor license agreements. See the NOTICE file
# distributed with this work for additional information
# regarding copyright ownership. The ASF licenses this file
# to you under the Apache License, Version 2.0 (the
# "License"); you may not use this file except in compliance
# with the License. You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing,
# software distributed under the License is distributed on an
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
# KIND, either express or implied. See the License for the
# specific language governing permissions and limitations
# under the License.
####
# Parse EC2 # Parse EC2
# #
# Builds an object tree used to create a REST service for the current EC2 api. # Builds an object tree used to create a REST service for the current EC2 api.
@ -73,8 +52,9 @@ use LWP::UserAgent;
use Data::Dumper; use Data::Dumper;
use JSON; use JSON;
my $refUrl = "http://docs.amazonwebservices.com/AWSEC2/latest/APIReference"; # my $refUrl = "http://docs.amazonwebservices.com/AWSEC2/latest/APIReference";
# my $refUrl = "/tmp/scrape";
my $refUrl = "/tmp/scrape";
my $appUrl = "${refUrl}/OperationList-query.html"; my $appUrl = "${refUrl}/OperationList-query.html";
my $global_package = "org.jclouds.aws.ec2"; my $global_package = "org.jclouds.aws.ec2";
@ -96,9 +76,9 @@ sub parse_file {
sub parse { sub parse {
# return parse_file(shift); return parse_file(shift);
return parse_url(shift); #return parse_url(shift);
} }
sub parse_java_type { sub parse_java_type {
@ -113,10 +93,10 @@ sub parse_java_type {
my $javaType = get_java_name($awsType); my $javaType = get_java_name($awsType);
if ( !/Response/ ) { if ( !/Response/ ) {
$domain->{$awsType} = { $domain->{$awsType} = {
awsType => $awsType, awsType => $awsType,
javaType => $javaType, javaType => $javaType,
packageName => $domain_package, packageName => $domain_package,
className => $domain_package . "." . $javaType, className => $domain_package . "." . $javaType,
see => ["${refUrl}/ApiReference-ItemType-${awsType}.html"], see => ["${refUrl}/ApiReference-ItemType-${awsType}.html"],
fields => fields =>
build_fields("${refUrl}/ApiReference-ItemType-$awsType.html") build_fields("${refUrl}/ApiReference-ItemType-$awsType.html")
@ -183,15 +163,15 @@ sub build_commands {
foreach my $class ( $link->look_down( '_tag', 'a' ) ) { foreach my $class ( $link->look_down( '_tag', 'a' ) ) {
my $awsType = $class->attr('title'); my $awsType = $class->attr('title');
my $command = { my $command = {
awsType => $awsType, awsType => $awsType,
packageName => $commands_package . ".$packageName", packageName => $commands_package . ".$packageName",
className => $commands_package . ".$packageName." . $awsType, className => $commands_package . ".$packageName." . $awsType,
see => ["${refUrl}/ApiReference-query-${awsType}.html"], see => ["${refUrl}/ApiReference-query-${awsType}.html"],
response => { response => {
javaType => $awsType . "Response", javaType => $awsType . "Response",
awsType => $awsType . "Response", awsType => $awsType . "Response",
packageName => $response_package . ".$packageName", packageName => $response_package . ".$packageName",
className => $response_package className => $response_package
. ".$packageName." . ".$packageName."
. $awsType . $awsType
. "Response", . "Response",
@ -200,18 +180,19 @@ sub build_commands {
] ]
}, },
options => { options => {
awsType => $awsType . "Options", javaType => $awsType . "Options",
packageName => $options_package . ".$packageName", awsType => $awsType . "Options",
className => $options_package packageName => $options_package . ".$packageName",
className => $options_package
. ".$packageName." . ".$packageName."
. $awsType . $awsType
. "Options", . "Options",
see => ["${refUrl}/ApiReference-ItemType-${awsType}.html"] see => ["${refUrl}/ApiReference-ItemType-${awsType}.html"]
}, },
handler => { handler => {
awsType => $awsType . "Handler", awsType => $awsType . "Handler",
packageName => $xml_package . ".$packageName", packageName => $xml_package . ".$packageName",
className => $xml_package className => $xml_package
. ".$packageName." . ".$packageName."
. $awsType . $awsType
. "Handler", . "Handler",
@ -224,7 +205,12 @@ sub build_commands {
# do not build options when there are none! # do not build options when there are none!
if ( $#{ $$command{options}->{parameters} } == -1 ) { if ( $#{ $$command{options}->{parameters} } == -1 ) {
delete $$command{options}; $$command{options}->{javaType} =
"BaseEC2RequestOptions<EC2RequestOptions>";
$$command{options}->{packageName} = $options_package;
$$command{options}->{className} = $options_package . "."
. "BaseEC2RequestOptions<EC2RequestOptions>";
$$command{options}->{see} = [];
} }
# clear parameters for commands who don't require them # clear parameters for commands who don't require them
@ -241,16 +227,39 @@ sub build_commands {
} }
$tree->eof; $tree->eof;
$tree->delete; $tree->delete;
foreach my $packageRef (@out) {
my @commands = @{ $packageRef->{commands} };
foreach my $command (@commands) {
my $fieldCount = scalar @{ $command->{response}->{fields} };
# convert to native java type
if ( $fieldCount == 1 ) {
$command->{response}->{javaType} =
${ $command->{response}->{fields} }[0]->{javaType};
}
}
}
return \@out; return \@out;
} }
sub build_app { sub build_app {
my $url = shift; my $url = shift;
my $packages = build_commands($url); my $packages = build_commands($url);
while ( my ( $awsType, $classDef ) = each %$domain ) {
my $fieldCount = scalar @{ $classDef->{fields} };
if ( $fieldCount == 1 ) {
if ( $classDef->{fields}->[0]->{"name"} eq "item" ) {
delete $domain->{$awsType};
}
}
if ( $classDef->{javaType} =~ /^Set/ ) {
delete $domain->{$awsType};
}
}
return { return {
see => [$url], see => [$url],
packages => $packages packages => $packages,
domain => $domain
}; };
} }
@ -440,13 +449,7 @@ sub gen_bean_code_for_response {
my @commands = @{ $packageRef->{commands} }; my @commands = @{ $packageRef->{commands} };
foreach my $command (@commands) { foreach my $command (@commands) {
my $fieldCount = scalar @{ $command->{response}->{fields} }; my $fieldCount = scalar @{ $command->{response}->{fields} };
if ( $fieldCount != 1 ) {
# convert to native java type
if ( $fieldCount == 1 ) {
$command->{response}->{javaType} =
${ $command->{response}->{fields} }[0]->{javaType};
}
else {
gen_bean_code( $command->{response} ); gen_bean_code( $command->{response} );
} }
} }
@ -505,12 +508,8 @@ EOF
} }
# TODO change optons to be default of Base... instead of doing this in render logic.
my $optionsParameter = my $optionsParameter =
"\@Assisted BaseEC2RequestOptions<EC2RequestOptions> options"; "\@Assisted " . $classDef->{options}->{javaType} . " options";
$optionsParameter =
"\@Assisted " . $classDef->{options}->{awsType} . " options"
if defined $classDef->{options}->{awsType};
${code} = ${code} . <<EOF; ${code} = ${code} . <<EOF;
* \@author Adrian Cole * \@author Adrian Cole
@ -627,13 +626,13 @@ EOF
# start app! # start app!
my $app = build_app($appUrl); my $app = build_app($appUrl);
my $app_json = to_json($app, {utf8 => 1, pretty => 1}); my $app_json = to_json( $app, { utf8 => 1, pretty => 1 } );
print $app_json print $app_json
# gen_bean_code_for_domain($domain); # gen_bean_code_for_domain($domain);
# gen_bean_code_for_response($app); # gen_bean_code_for_response($app);
# gen_bean_code_for_command($app); # gen_bean_code_for_command($app);
# #
# print_domain_code($domain); # print_domain_code($domain);
# print_response_code($app); # print_response_code($app);
# print_command_code($app); # print_command_code($app);

View File

@ -24,20 +24,26 @@
package org.jclouds.codegen.model; package org.jclouds.codegen.model;
import java.util.List; import java.util.List;
import java.util.Map;
/** /**
* *
* @author James Murty * @author James Murty
*/ */
public class Model { public class Model {
private List<Package> packages; private List<Package> packages;
private Map<String, Value> domain;
@Override
public String toString() { @Override
return packages.toString(); public String toString() {
} return packages.toString();
}
public List<Package> getPackages() {
return packages; public List<Package> getPackages() {
} return packages;
}
public Map<String, Value> getDomain() {
return domain;
}
} }

View File

@ -0,0 +1,49 @@
/**
*
* Copyright (C) 2009 Global Cloud Specialists, Inc. <info@globalcloudspecialists.com>
*
* ====================================================================
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
* ====================================================================
*/
package org.jclouds.codegen.model;
import java.util.List;
/**
*
* @author James Murty
*/
public class Value extends BaseBean {
private List<ResponseField> fields;
private String javaType;
private List<String> see;
public List<ResponseField> getFields() {
return fields;
}
public String getJavaType() {
return javaType;
}
public List<String> getSee() {
return see;
}
}

View File

@ -0,0 +1,52 @@
/**
*
* Copyright (C) 2009 Global Cloud Specialists, Inc. <info@globalcloudspecialists.com>
*
* ====================================================================
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
* ====================================================================
*/
package org.jclouds.codegen.model;
/**
*
* @author James Murty
*/
public class ValueField {
private String desc;
private String name;
private String type;
private String javaType;
public String getDesc() {
return desc;
}
public String getName() {
return name;
}
public String getType() {
return type;
}
public String getJavaType() {
return javaType;
}
}

View File

@ -36,6 +36,7 @@ import org.jclouds.codegen.model.BaseBean;
import org.jclouds.codegen.model.Command; import org.jclouds.codegen.model.Command;
import org.jclouds.codegen.model.Model; import org.jclouds.codegen.model.Model;
import org.jclouds.codegen.model.Package; import org.jclouds.codegen.model.Package;
import org.jclouds.codegen.model.Value;
import com.google.gson.Gson; import com.google.gson.Gson;
import com.google.gson.JsonParseException; import com.google.gson.JsonParseException;
@ -63,6 +64,7 @@ import freemarker.template.TemplateException;
public class CodeGenerator { public class CodeGenerator {
public final static String COMMAND_TEMPLATE_FILENAME = "Command.ftl"; public final static String COMMAND_TEMPLATE_FILENAME = "Command.ftl";
public final static String BEAN_TEMPLATE_FILENAME = "Bean.ftl"; public final static String BEAN_TEMPLATE_FILENAME = "Bean.ftl";
public final static String VALUE_TEMPLATE_FILENAME = "Value.ftl";
private final File targetDirectory; private final File targetDirectory;
private final String rootPackageName; private final String rootPackageName;
@ -111,7 +113,9 @@ public class CodeGenerator {
if (command.getHandler() != null) { if (command.getHandler() != null) {
generateClassFile(command.getHandler(), BEAN_TEMPLATE_FILENAME); generateClassFile(command.getHandler(), BEAN_TEMPLATE_FILENAME);
} }
if (command.getOptions() != null) { if (command.getOptions() != null
&& command.getOptions().getClassName().indexOf(
"BaseEC2RequestOptions<EC2RequestOptions>") == -1) {
generateClassFile(command.getOptions(), BEAN_TEMPLATE_FILENAME); generateClassFile(command.getOptions(), BEAN_TEMPLATE_FILENAME);
} }
if (command.getResponse() != null) { if (command.getResponse() != null) {
@ -119,6 +123,10 @@ public class CodeGenerator {
} }
} }
} }
for (Value value : model.getDomain().values()) {
generateClassFile(value, VALUE_TEMPLATE_FILENAME);
}
} }
/** /**

File diff suppressed because it is too large Load Diff

View File

@ -0,0 +1,78 @@
[#ftl]
[#--
Copyright (C) 2009 Global Cloud Specialists, Inc. <info@globalcloudspecialists.com>
====================================================================
Licensed to the Apache Software Foundation (ASF) under one
or more contributor license agreements. See the NOTICE file
distributed with this work for additional information
regarding copyright ownership. The ASF licenses this file
to you under the Apache License, Version 2.0 (the
"License"); you may not use this file except in compliance
with the License. You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing,
software distributed under the License is distributed on an
"AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
KIND, either express or implied. See the License for the
specific language governing permissions and limitations
under the License.
====================================================================
--]
package ${bean.packageName};
[#if bean.packageName != rootPackageName]
import ${rootPackageName}.*;
[/#if]
/**
*
[#list bean.see as see]
[#if see?contains(".html")]
* @see <a href='${see}' />
[#else]
* @see ${see}
[/#if]
[/#list]
* @author Generated
*/
public class ${shortClassName} {
[#-- Print fields --]
[#list bean.parameters![] as field]
/**
*
* ${field.desc}
* /
private ${field.javaType} ${field.name?uncap_first};
[/#list]
[#-- Print get/set --]
[#list bean.parameters![] as field]
[#assign lowerName = field.name?uncap_first]
[#assign upperName = field.name?cap_first]
/**
*
* @return ${field.desc}
* /
public ${field.javaType} get${upperName}(){
return this.${lowerName};
}
/**
*
* @param ${lowerName}
* ${field.desc}
* /
public void set${upperName}(${field.javaType} ${lowerName}) {
this.${lowerName} = ${lowerName};
}
[/#list]
}

View File

@ -0,0 +1,67 @@
[#ftl]
[#--
Copyright (C) 2009 Global Cloud Specialists, Inc. <info@globalcloudspecialists.com>
====================================================================
Licensed to the Apache Software Foundation (ASF) under one
or more contributor license agreements. See the NOTICE file
distributed with this work for additional information
regarding copyright ownership. The ASF licenses this file
to you under the Apache License, Version 2.0 (the
"License"); you may not use this file except in compliance
with the License. You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing,
software distributed under the License is distributed on an
"AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
KIND, either express or implied. See the License for the
specific language governing permissions and limitations
under the License.
====================================================================
--]
package ${bean.packageName};
[#if bean.packageName != rootPackageName]
import ${rootPackageName}.*;
[/#if]
/**
*
[#list bean.see as see]
[#if see?contains(".html")]
* @see <a href='${see}' />
[#else]
* @see ${see}
[/#if]
[/#list]
* @author Generated
*/
public class ${shortClassName} {
[#-- Print fields --]
[#list bean.fields![] as field]
private ${field.javaType} ${field.name?uncap_first};
[/#list]
[#-- Print get/set --]
[#list bean.fields![] as field]
[#assign lowerName = field.name?uncap_first]
[#assign upperName = field.name?cap_first]
public ${field.javaType} get${upperName}(){
return this.${lowerName};
}
public void set${upperName}(${field.javaType} ${lowerName}) {
this.${lowerName} = ${lowerName};
}
[/#list]
}