msgpack-idl-java 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
data/AUTHORS ADDED
@@ -0,0 +1 @@
1
+ FURUHASHI Sadayuki <frsyuki _at_ users.sourceforge.jp>
data/README ADDED
@@ -0,0 +1,27 @@
1
+
2
+ = MessagePack IDL Language Module for Java
3
+
4
+ == Description
5
+
6
+ == Installation
7
+
8
+ === Archive Installation
9
+
10
+ rake
11
+ gem install pkg/msgpack-idl-java-*
12
+
13
+ === Gem Installation
14
+
15
+ gem install msgpack-idl-java
16
+
17
+ or
18
+
19
+ gem install msgpack-idl
20
+ msgpack-idl-install java
21
+
22
+ == Copyright
23
+
24
+ Author:: frsyuki <frsyuki@users.sourceforge.jp>
25
+ Copyright:: Copyright (c) 2011 FURUHASHI Sadayuki
26
+ License:: Apache License, Version 2.0
27
+
@@ -0,0 +1,35 @@
1
+ #{format_package("client")}
2
+
3
+ import java.io.Closeable;
4
+ import org.msgpack.rpc.Session;
5
+ import org.msgpack.rpc.Client;
6
+
7
+ public class #{@name} implements Closeable {
8
+ private Session session;
9
+
10
+ public #{@name}(Session session) {
11
+ this.session = session;
12
+ }
13
+
14
+ public void close() {
15
+ if(session instanceof Client) {
16
+ ((Client)session).close();
17
+ }
18
+ }
19
+
20
+ <?rb @scopes.each {|c| ?>
21
+ <?rb s = c.service ?>
22
+ public #{s.name}_#{c.version} #{c.name}() {
23
+ return new #{s.name}_#{c.version}(this.session, "#{c.name}");
24
+ }
25
+ <?rb s.versions_upto(c.version-1) {|sv| ?>
26
+ public #{s.name}_#{sv.version} #{c.name}_#{sv.version}() {
27
+ return #{c.name}().version#{sv.version}();
28
+ }
29
+ <?rb } ?>
30
+ public #{s.name}_#{c.version} #{c.name}_#{c.version}() {
31
+ return #{c.name}();
32
+ }
33
+ <?rb } ?>
34
+ }
35
+
@@ -0,0 +1,95 @@
1
+ #{format_package("client")}
2
+
3
+ import java.io.IOException;
4
+ import java.io.Closeable;
5
+ import org.msgpack.MessageTypeException;
6
+ import org.msgpack.MessagePackObject;
7
+ import org.msgpack.rpc.Session;
8
+ import org.msgpack.rpc.Client;
9
+ import org.msgpack.rpc.error.RemoteError;
10
+ import static #{@message_class}.*;
11
+
12
+ public class #{@name} implements Closeable {
13
+ private Session session;
14
+
15
+ <?rb if @version ?>
16
+ private String scope;
17
+ private String suffix;
18
+
19
+ public #{@name}(Session session) {
20
+ this(session, null);
21
+ }
22
+
23
+ public #{@name}(Session session, String scope) {
24
+ this.session = session;
25
+ this.scope = scope;
26
+ if(scope != null) {
27
+ this.suffix = ":"+scope+":#{@version}";
28
+ } else {
29
+ this.suffix = ":#{@version}";
30
+ }
31
+ }
32
+
33
+ <?rb @service.versions_upto(@version-1) {|sv| ?>
34
+ public #{@service.name}_#{sv.version} version#{sv.version}() {
35
+ return new #{@service.name}_#{sv.version}(this.session, this.scope);
36
+ }
37
+ <?rb } ?>
38
+
39
+ private String scopedMethodName(String methodName) {
40
+ return methodName + this.suffix;
41
+ }
42
+
43
+ <?rb else # if @version ?>
44
+ public #{@name}(Session session) {
45
+ this.session = session;
46
+ }
47
+
48
+ private String scopedMethodName(String methodName) {
49
+ return methodName;
50
+ }
51
+ <?rb end # if @version ?>
52
+
53
+ <?rb @functions.each {|f| ?>
54
+ public #{format_type(f.return_type)} #{f.name}(A#{f.name} args) {
55
+ <?rb if f.return_type.void_type? ?>
56
+ session.callApply(scopedMethodName("#{f.name}"), args);
57
+ <?rb else ?>
58
+ MessagePackObject obj = session.callApply(scopedMethodName("#{f.name}"), args);
59
+ #{format_type(f.return_type)} r;
60
+ #{format_convert("r", "obj", f.return_type)}
61
+ return r;
62
+ <?rb end ?>
63
+ }
64
+
65
+ <?rb args = f.args ?>
66
+ public #{format_type(f.return_type)} #{f.name}(#{ args.map {|arg| format_type(arg.type) +" "+ arg.name }.join(', ') }) {
67
+ A#{f.name} args = new A#{f.name}(#{ args.map {|arg| arg.name }.join(', ') });
68
+ <?rb if f.return_type.void_type? ?>
69
+ this.#{f.name}(args);
70
+ <?rb else ?>
71
+ return this.#{f.name}(args);
72
+ <?rb end ?>
73
+ }
74
+
75
+ <?rb if f.max_id != f.max_required_id && f.max_required_id > 0 ?>
76
+ <?rb args = f.args[0, f.max_required_id] ?>
77
+ public #{format_type(f.return_type)} #{f.name}(#{ args.map {|arg| format_type(arg.type) +" "+ arg.name }.join(', ') }) {
78
+ A#{f.name} args = new A#{f.name}(#{ args.map {|arg| arg.name }.join(', ') });
79
+ <?rb if f.return_type.void_type? ?>
80
+ this.#{f.name}(args);
81
+ <?rb else ?>
82
+ return this.#{f.name}(args);
83
+ <?rb end ?>
84
+ }
85
+ <?rb end ?>
86
+
87
+ <?rb } ?>
88
+
89
+ public void close() {
90
+ if(session instanceof Client) {
91
+ ((Client)session).close();
92
+ }
93
+ }
94
+ }
95
+
@@ -0,0 +1,39 @@
1
+ #{format_package}
2
+
3
+ import java.io.IOException;
4
+ import org.msgpack.MessageTypeException;
5
+ import org.msgpack.Packer;
6
+ import org.msgpack.MessagePackable;
7
+
8
+ public enum #{@name} implements MessagePackable {
9
+ <?rb @fields.each {|f| ?>
10
+ #{f.name}(#{f.id}),
11
+ <?rb } ?>
12
+ ;
13
+
14
+ public static #{@name} enumOf(int id) {
15
+ switch(id) {
16
+ <?rb @fields.each {|f| ?>
17
+ case #{f.id}:
18
+ return #{f.name};
19
+ <?rb } ?>
20
+ default:
21
+ throw new MessageTypeException();
22
+ }
23
+ }
24
+
25
+ public void messagePack(Packer pk) throws IOException {
26
+ pk.packInt(this.id);
27
+ }
28
+
29
+ private int id;
30
+
31
+ public int id() {
32
+ return this.id;
33
+ }
34
+
35
+ private #{@name}(int id) {
36
+ this.id = id;
37
+ }
38
+ }
39
+
@@ -0,0 +1,19 @@
1
+ #{format_package}
2
+
3
+ import java.util.List;
4
+ import java.util.Map;
5
+ import java.util.ArrayList;
6
+ import java.util.HashMap;
7
+ import java.math.BigInteger;
8
+ import java.io.IOException;
9
+ import org.msgpack.Packer;
10
+ import org.msgpack.Unpacker;
11
+ import org.msgpack.MessageTypeException;
12
+ import org.msgpack.MessagePackObject;
13
+ import org.msgpack.MessagePackable;
14
+ import org.msgpack.MessageUnpackable;
15
+ import org.msgpack.MessageConvertable;
16
+ import org.msgpack.rpc.error.RemoteError;
17
+
18
+ public #{format_message(@message, @message.name)}
19
+
@@ -0,0 +1,138 @@
1
+ class #{@name}
2
+ <?rb if @super_class ?>
3
+ extends #{@super_class}
4
+ <?rb end ?>
5
+ implements MessagePackable, MessageUnpackable, MessageConvertable {
6
+
7
+ <?rb @message.new_fields.each {|f| ?>
8
+ protected #{format_type(f.type)} #{f.name};
9
+ <?rb } ?>
10
+
11
+ public #{@name}() {
12
+ <?rb if @super_class ?>
13
+ super();
14
+ <?rb end ?>
15
+ <?rb @message.new_fields.each {|f| ?>
16
+ #{format_initial_value("this.#{f.name}", f)}
17
+ <?rb } ?>
18
+ }
19
+
20
+ <?rb fields = @message.all_fields ?>
21
+ <?rb unless fields.empty? ?>
22
+ public #{@name}(#{ fields.map {|f| format_type(f.type)+" "+f.name }.join(', ') }) {
23
+ <?rb fields.each {|f| ?>
24
+ this.#{f.name} = #{f.name};
25
+ <?rb } ?>
26
+ }
27
+ <?rb end ?>
28
+
29
+ <?rb if @message.max_id != @message.max_required_id && @message.max_required_id > 0 ?>
30
+ <?rb fields = @message.all_fields[0, @message.max_required_id] ?>
31
+ public #{@name}(#{ fields.map {|f| format_type(f.type)+" "+f.name }.join(', ') }) {
32
+ <?rb fields.each {|f| ?>
33
+ this.#{f.name} = #{f.name};
34
+ <?rb } ?>
35
+ <?rb @message.all_fields[@message.max_required_id..-1].each {|f| ?>
36
+ #{format_initial_value("this.#{f.name}", f)}
37
+ <?rb } ?>
38
+ }
39
+ <?rb end ?>
40
+
41
+ <?rb @message.new_fields.each {|f| ?>
42
+ public void set#{f.name.capitalize}(#{format_type(f.type)} #{f.name}) {
43
+ this.#{f.name} = #{f.name};
44
+ }
45
+ public #{format_type(f.type)} get#{f.name.capitalize}() {
46
+ return this.#{f.name};
47
+ }
48
+ <?rb } ?>
49
+
50
+ public void messagePack(Packer pk) throws IOException {
51
+ pk.packArray(#{@message.max_id});
52
+ <?rb 1.upto(@message.max_id) {|i| ?>
53
+ <?rb if f = @message[i]; ?>
54
+ pk.pack(this.#{f.name});
55
+ <?rb else ?>
56
+ pk.packNil();
57
+ <?rb end ?>
58
+ <?rb } ?>
59
+ }
60
+
61
+ public void messageUnpack(Unpacker pac) throws IOException, MessageTypeException {
62
+ int len = pac.unpackArray();
63
+
64
+ if(len < #{@message.max_required_id}) {
65
+ throw new MessageTypeException("#{@message.name} requires at least #{@message.max_required_id} elements.");
66
+ }
67
+
68
+ <?rb 1.upto(@message.max_id) {|i| ?>
69
+ <?rb f = @message[i] ?>
70
+ <?rb if !f ?>
71
+ <?rb if i > @message.max_required_id ?>
72
+ if(len < #{i}) { return; }
73
+ <?rb end ?>
74
+ pac.unpackObject();
75
+ <?rb elsif f.required? ?>
76
+ <?rb if f.type.nullable_type? ?>
77
+ if(!pac.tryUnpackNull()) {
78
+ #{format_unpack("this.#{f.name}", "pac", f.type.real_type)}
79
+ }
80
+ <?rb else ?>
81
+ if(!pac.tryUnpackNull()) {
82
+ throw new MessageTypeException("#{@message.name}.#{f.name} is not nullable bug got nil");
83
+ }
84
+ #{format_unpack("this.#{f.name}", "pac", f.type.real_type)}
85
+ <?rb end ?>
86
+ <?rb elsif f.optional? ?>
87
+ <?rb if i > @message.max_required_id ?>
88
+ if(len < #{i}) { return; }
89
+ <?rb end ?>
90
+ if(!pac.tryUnpackNull()) {
91
+ #{format_unpack("this.#{f.name}", "pac", f.type.real_type)}
92
+ }
93
+ <?rb end ?>
94
+ <?rb } ?>
95
+
96
+ for(int i=#{@message.max_id}; i < len; ++i) {
97
+ pac.unpackObject();
98
+ }
99
+ }
100
+
101
+ public void messageConvert(MessagePackObject obj) throws MessageTypeException {
102
+ if(!obj.isArrayType()) {
103
+ throw new MessageTypeException("target is not array");
104
+ }
105
+ MessagePackObject[] arr = obj.asArray();
106
+ int len = arr.length;
107
+
108
+ if(len < #{@message.max_required_id}) {
109
+ throw new MessageTypeException("#{@message.name} requires at least #{@message.max_required_id} elements.");
110
+ }
111
+
112
+ <?rb 1.upto(@message.max_id) {|i| ?>
113
+ <?rb f = @message[i] ?>
114
+ <?rb if !f ?>
115
+ <?rb elsif f.required? ?>
116
+ obj = arr[#{i-1}];
117
+ <?rb if f.type.nullable_type? ?>
118
+ if(!obj.isNil()) {
119
+ #{format_convert("this.#{f.name}", "obj", f.type.real_type)}
120
+ }
121
+ <?rb else ?>
122
+ if(!obj.isNil()) {
123
+ throw new MessageTypeException("#{@message.name}.#{f.name} is not nullable bug got nil");
124
+ }
125
+ #{format_convert("this.#{f.name}", "obj", f.type.real_type)}
126
+ <?rb end ?>
127
+ <?rb elsif f.optional? ?>
128
+ <?rb if i > @message.max_required_id ?>
129
+ if(len < #{i}) { return; }
130
+ <?rb end ?>
131
+ obj = arr[#{i-1}];
132
+ if(!obj.isNil()) {
133
+ #{format_convert("this.#{f.name}", "obj", f.type.real_type)}
134
+ }
135
+ <?rb end ?>
136
+ <?rb } ?>
137
+ }
138
+ }
@@ -0,0 +1,95 @@
1
+ #{format_package("server")}
2
+
3
+ import java.util.Map;
4
+ import java.util.HashMap;
5
+ import java.io.IOException;
6
+ import org.msgpack.MessageTypeException;
7
+ import org.msgpack.rpc.dispatcher.Dispatcher;
8
+ import org.msgpack.rpc.Request;
9
+
10
+ public class #{@name} implements Dispatcher {
11
+ <?rb @scopes.each {|c| ?>
12
+ <?rb s = c.service ?>
13
+ <?rb s.versions_upto(c.version) {|sv| ?>
14
+ private #{s.name}_#{sv.version} #{c.name}_#{sv.version};
15
+ <?rb } ?>
16
+ <?rb } ?>
17
+
18
+ <?rb @scopes.each {|c| ?>
19
+ <?rb s = c.service ?>
20
+ <?rb s.versions_upto(c.version) {|sv| ?>
21
+ public #{s.name}_#{sv.version} #{c.name}_#{sv.version}() {
22
+ return this.#{c.name}_#{sv.version};
23
+ }
24
+ <?rb } ?>
25
+ <?rb } ?>
26
+
27
+ <?rb @scopes.each {|c| ?>
28
+ <?rb s = c.service ?>
29
+ <?rb s.versions_upto(c.version) {|sv| ?>
30
+ <?rb sv.functions.each {|f| ?>
31
+ private class Default_#{s.name}_#{sv.version}_#{f.name} implements #{s.name}_#{sv.version}.I#{f.name} {
32
+ public #{format_type(f.return_type)} #{f.name}(#{format_message_class(s, sv.version)}.A#{f.name} args) {
33
+ <?rb if f.super_version ?>
34
+ <?rb if f.return_type.void_type? ?>
35
+ #{@name}.this.#{c.name}_#{f.super_version}.get#{f.name.capitalize}().#{f.name}(args);
36
+ <?rb else ?>
37
+ return #{@name}.this.#{c.name}_#{f.super_version}.get#{f.name.capitalize}().#{f.name}(args);
38
+ <?rb end ?>
39
+ <?rb else ?>
40
+ throw new RPCError(".NotImplemented"); // TODO
41
+ <?rb end ?>
42
+ }
43
+ }
44
+ <?rb } ?>
45
+ <?rb } ?>
46
+ <?rb } ?>
47
+
48
+ private HashMap<String, Dispatcher> scopeDispatchTable;
49
+
50
+ public #{@name}() {
51
+ this.scopeDispatchTable = new HashMap<String, Dispatcher>();
52
+ <?rb @scopes.each {|c| ?>
53
+ <?rb s = c.service ?>
54
+ <?rb s.versions_upto(c.version) {|sv| ?>
55
+ this.scopeDispatchTable.put("#{c.name}:#{sv.version}", this.#{c.name}_#{sv.version});
56
+ <?rb } ?>
57
+ this.scopeDispatchTable.put("#{c.name}", this.#{c.name}_#{c.version});
58
+ <?rb } ?>
59
+
60
+ this.scopeDispatchTable.put("", this.#{@default_scope.name}_#{@default_scope.version});
61
+ <?rb @default_scope.service.versions_upto(@default_scope.version) {|sv| ?>
62
+ this.scopeDispatchTable.put("#{sv.version}", this.#{@default_scope.name}_#{sv.version});
63
+ <?rb } ?>
64
+
65
+ <?rb @scopes.each {|c| ?>
66
+ <?rb s = c.service ?>
67
+ <?rb s.versions_upto(c.version) {|sv| ?>
68
+
69
+ <?rb sv.functions.each {|f| ?>
70
+ this.#{c.name}_#{sv.version}.set#{f.name.capitalize}(new Default_#{s.name}_#{sv.version}_#{f.name}());
71
+ <?rb } ?>
72
+
73
+ <?rb } ?>
74
+ <?rb } ?>
75
+ }
76
+
77
+ public void dispatch(Request request) throws Exception {
78
+ String methodName = request.getMethodName();
79
+ int ic = methodName.indexOf(':');
80
+ if(ic < 0) {
81
+ this.#{@default_scope.name}_#{@default_scope.version}.dispatch(methodName, request);
82
+ return;
83
+ }
84
+
85
+ String scope = methodName.substring(ic+1);
86
+ methodName = methodName.substring(0, ic);
87
+ Dispatcher dp = this.scopeDispatchTable.get(scope);
88
+ if(dp == null) {
89
+ throw new MessageTypeException("FIXME");
90
+ }
91
+
92
+ dp.dispatch(methodName, request);
93
+ }
94
+ }
95
+
@@ -0,0 +1,73 @@
1
+ #{format_package("server")}
2
+
3
+ import java.util.Map;
4
+ import java.util.HashMap;
5
+ import java.io.IOException;
6
+ import org.msgpack.MessageTypeException;
7
+ import org.msgpack.rpc.dispatcher.Dispatcher;
8
+ import org.msgpack.rpc.Request;
9
+ import static #{@message_class}.*;
10
+
11
+ public class #{@name} implements Dispatcher {
12
+ <?rb @functions.each {|f| ?>
13
+ public interface I#{f.name} {
14
+ public #{format_type(f.return_type)} #{f.name}(A#{f.name} args);
15
+ }
16
+ <?rb } ?>
17
+
18
+ <?rb @functions.each {|f| ?>
19
+ public void set#{f.name.capitalize}(I#{f.name} #{f.name}) {
20
+ this.#{f.name} = #{f.name};
21
+ }
22
+ public I#{f.name} get#{f.name.capitalize}() {
23
+ return this.#{f.name};
24
+ }
25
+ <?rb } ?>
26
+
27
+ public void set(Object o) {
28
+ <?rb @functions.each {|f| ?>
29
+ if(o instanceof I#{f.name}) {
30
+ this.set#{f.name.capitalize}((I#{f.name})o);
31
+ }
32
+ <?rb } ?>
33
+ }
34
+
35
+ <?rb @functions.each {|f| ?>
36
+ private I#{f.name} #{f.name};
37
+ <?rb } ?>
38
+
39
+
40
+ private HashMap<String, Dispatcher> dispatchTable;
41
+
42
+ public #{@name}() {
43
+ this.dispatchTable = new HashMap<String, Dispatcher>();
44
+ <?rb functions.each {|f| ?>
45
+ this.dispatchTable.put("#{f.name}", new Dispatcher() {
46
+ public void dispatch(Request request) throws Exception {
47
+ A#{f.name} args = new A#{f.name}();
48
+ args.messageConvert(request.getArguments());
49
+ <?rb if f.return_type.void_type? ?>
50
+ #{f.name}.#{f.name}(args);
51
+ request.sendResult(null);
52
+ <?rb else ?>
53
+ #{format_type(f.return_type)} r = #{f.name}.#{f.name}(args);
54
+ request.sendResult(r);
55
+ <?rb end ?>
56
+ }
57
+ });
58
+ <?rb } ?>
59
+ }
60
+
61
+ public void dispatch(Request request) throws Exception {
62
+ dispatch(request.getMethodName(), request);
63
+ }
64
+
65
+ public void dispatch(String methodName, Request request) throws Exception {
66
+ Dispatcher dp = this.dispatchTable.get(methodName);
67
+ if(dp == null) {
68
+ throw new MessageTypeException("FIXME");
69
+ }
70
+ dp.dispatch(request);
71
+ }
72
+ }
73
+
@@ -0,0 +1,26 @@
1
+ #{format_package}
2
+
3
+ import java.util.List;
4
+ import java.util.Map;
5
+ import java.util.ArrayList;
6
+ import java.util.HashMap;
7
+ import java.io.IOException;
8
+ import org.msgpack.Packer;
9
+ import org.msgpack.Unpacker;
10
+ import org.msgpack.MessageTypeException;
11
+ import org.msgpack.MessagePackObject;
12
+ import org.msgpack.MessagePackable;
13
+ import org.msgpack.MessageUnpackable;
14
+ import org.msgpack.MessageConvertable;
15
+
16
+ public abstract class #{@name} {
17
+ <?rb @functions.each {|f| ?>
18
+ <?rb if f.super_version # FIXME ?>
19
+ public static class A#{f.name} extends #{@service.name}_#{f.super_version}.A#{f.name} {
20
+ }
21
+ <?rb else ?>
22
+ public static #{format_message(f, "A#{f.name}")}
23
+ <?rb end ?>
24
+ <?rb } ?>
25
+ }
26
+
@@ -0,0 +1,11 @@
1
+ module MessagePack
2
+ module IDL
3
+ module Lang
4
+ module Java
5
+
6
+ VERSION = '0.1.0'
7
+
8
+ end
9
+ end
10
+ end
11
+ end
@@ -0,0 +1,490 @@
1
+ #
2
+ # MessagePack IDL Generator for Java
3
+ #
4
+ # Copyright (C) 2011 FURUHASHI Sadayuki
5
+ #
6
+ # Licensed under the Apache License, Version 2.0 (the "License");
7
+ # you may not use this file except in compliance with the License.
8
+ # 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, software
13
+ # distributed under the License is distributed on an "AS IS" BASIS,
14
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15
+ # See the License for the specific language governing permissions and
16
+ # limitations under the License.
17
+ #
18
+ module MessagePack
19
+ module IDL
20
+ module Lang
21
+
22
+ require 'fileutils'
23
+ require 'tenjin'
24
+
25
+ class JavaGenerator < GeneratorModule
26
+ Generator.register('java', self)
27
+
28
+ def initialize(ir, outdir)
29
+ @ir = ir
30
+ @outdir = outdir
31
+ end
32
+
33
+ def generate!
34
+ gen_init
35
+ gen_enums
36
+ gen_messages
37
+ gen_services
38
+ gen_server_services
39
+ gen_server_applications
40
+ gen_client_services
41
+ gen_client_applications
42
+ end
43
+
44
+ def gen_init
45
+ @datadir = File.join(File.dirname(__FILE__), 'java')
46
+ @pkgoutdir = File.join(@outdir, @ir.namespace)
47
+
48
+ @engine = Tenjin::Engine.new(:cache => false)
49
+ end
50
+
51
+ def gen_enums
52
+ ctx = Context.new(self, :namespace, :enum, :fields, :name)
53
+ ctx.namespace = @ir.namespace
54
+
55
+ @ir.enums.each {|t|
56
+ ctx.enum = t
57
+ ctx.name = t.name
58
+ ctx.fields = t.fields
59
+ render_file('enum.java', ctx, "#{ctx.name}")
60
+ }
61
+ end
62
+
63
+ def gen_messages
64
+ ctx = Context.new(self, :namespace, :message, :super_class, :name)
65
+ ctx.namespace = @ir.namespace
66
+
67
+ @ir.messages.each {|t|
68
+ ctx.message = t
69
+ if t.super_class
70
+ ctx.super_class = t.super_class.name
71
+ end
72
+ ctx.name = t.name
73
+ render_file('message.java', ctx, "#{ctx.name}")
74
+ }
75
+ end
76
+
77
+ def gen_services
78
+ ctx = Context.new(self, :namespace, :service, :version, :functions, :name)
79
+ ctx.namespace = @ir.namespace
80
+
81
+ @ir.services.each {|s|
82
+ ctx.service = s
83
+ s.versions.each {|v|
84
+ ctx.version = v.version
85
+ ctx.functions = v.functions
86
+ ctx.name = "#{s.name}_#{v.version}"
87
+ render_file('service_version.java', ctx, "#{ctx.name}")
88
+ }
89
+ }
90
+ end
91
+
92
+ def gen_server_services
93
+ ctx = Context.new(self, :namespace, :service, :version, :functions, :name, :message_class)
94
+ ctx.namespace = @ir.namespace
95
+
96
+ @ir.services.each {|s|
97
+ ctx.service = s
98
+ s.versions.each {|v|
99
+ ctx.version = v.version
100
+ ctx.functions = v.functions
101
+ ctx.name = "#{s.name}_#{v.version}"
102
+ ctx.message_class = (@ir.namespace + ["#{s.name}_#{v.version}"]).join('.')
103
+ render_file('server/service_version.java', ctx, "server/#{ctx.name}")
104
+ }
105
+ }
106
+ end
107
+
108
+ def gen_server_applications
109
+ ctx = Context.new(self, :namespace, :name, :scopes, :default_scope)
110
+ ctx.namespace = @ir.namespace
111
+
112
+ service_apps = @ir.services.map {|s|
113
+ scopes = [IR::Scope.new("", s, s.versions.last.version, true)]
114
+ IR::Application.new(s.name, scopes)
115
+ }
116
+
117
+ (@ir.applications + service_apps).each {|app|
118
+ ctx.name = app.name
119
+ ctx.scopes = app.scopes
120
+ ctx.default_scope = app.default_scope
121
+ render_file('server/application.java', ctx, "server/#{ctx.name}")
122
+ }
123
+ end
124
+
125
+ def gen_client_services
126
+ ctx = Context.new(self, :namespace, :service, :version, :functions, :name, :message_class)
127
+ ctx.namespace = @ir.namespace
128
+
129
+ @ir.services.each {|s|
130
+ ctx.service = s
131
+ s.versions.each {|v|
132
+ ctx.version = v.version
133
+ ctx.functions = v.functions
134
+ ctx.name = "#{s.name}_#{v.version}"
135
+ ctx.message_class = (@ir.namespace + ["#{s.name}_#{v.version}"]).join('.')
136
+ render_file('client/service_version.java', ctx, "client/#{ctx.name}")
137
+ }
138
+ v = s.versions.last
139
+ ctx.version = nil
140
+ ctx.functions = v.functions
141
+ ctx.name = "#{s.name}"
142
+ ctx.message_class = (@ir.namespace + ["#{s.name}_#{v.version}"]).join('.')
143
+ render_file('client/service_version.java', ctx, "client/#{ctx.name}")
144
+ }
145
+ end
146
+
147
+ def gen_client_applications
148
+ ctx = Context.new(self, :namespace, :name, :scopes, :default_scope)
149
+ ctx.namespace = @ir.namespace
150
+
151
+ @ir.applications.each {|app|
152
+ ctx.name = app.name
153
+ ctx.scopes = app.scopes
154
+ ctx.default_scope = app.default_scope
155
+ render_file('client/application.java', ctx, "client/#{ctx.name}")
156
+ }
157
+ end
158
+
159
+ def render(template_fname, ctx)
160
+ template_path = File.join(@datadir, template_fname)
161
+ @engine.render(template_path, ctx)
162
+ end
163
+
164
+ def render_file(template_fname, ctx, fname)
165
+ code = render(template_fname, ctx)
166
+ path = File.join(@pkgoutdir, fname) + '.java'
167
+ FileUtils.mkdir_p(File.dirname(path))
168
+ File.open(path, "w") {|f|
169
+ f.write(code)
170
+ }
171
+ end
172
+
173
+ def format_message(t, name = t.name)
174
+ ctx = Context.new(self, :message, :super_class, :name)
175
+ ctx.message = t
176
+ if t.super_class
177
+ ctx.super_class = t.super_class.name
178
+ elsif t.is_a?(IR::Exception)
179
+ ctx.super_class = "RemoteError"
180
+ end
181
+ ctx.name = name
182
+ render('message_body.java', ctx)
183
+ end
184
+
185
+
186
+ class Context
187
+ include Tenjin::ContextHelper
188
+
189
+ def initialize(gen, *member)
190
+ @gen = gen
191
+
192
+ (class << self; self; end).module_eval {
193
+ member.each {|m|
194
+ define_method("#{m}") {
195
+ instance_variable_get("@#{m}")
196
+ }
197
+ define_method("#{m}=") {|v|
198
+ instance_variable_set("@#{m}", v)
199
+ }
200
+ }
201
+ }
202
+ end
203
+
204
+ def format_package(*extra)
205
+ name = format_package_name(*extra)
206
+ if name.empty?
207
+ ""
208
+ else
209
+ "package #{name};"
210
+ end
211
+ end
212
+
213
+ def format_package_name(*extra)
214
+ package = namespace + extra
215
+ package.join('.')
216
+ end
217
+
218
+ PRIMITIVE_TYPEMAP = {
219
+ 'void' => 'void',
220
+ 'byte' => 'byte',
221
+ 'short' => 'short',
222
+ 'int' => 'int',
223
+ 'long' => 'long',
224
+ 'ubyte' => 'short',
225
+ 'ushort' => 'int',
226
+ 'uint' => 'long',
227
+ 'ulong' => 'BigInteger',
228
+ 'float' => 'float',
229
+ 'double' => 'double',
230
+ 'bool' => 'boolean',
231
+ 'raw' => 'byte[]',
232
+ 'string' => 'String',
233
+ 'list' => 'List',
234
+ 'map' => 'Map',
235
+ }
236
+
237
+ NULLABLE_REMAP = {
238
+ 'byte' => 'Byte',
239
+ 'short' => 'Short',
240
+ 'int' => 'Integer',
241
+ 'long' => 'Long',
242
+ 'float' => 'Float',
243
+ 'double' => 'Double',
244
+ 'boolean' => 'Boolean',
245
+ }
246
+
247
+ TYPE_PARAMETER_REMAP = NULLABLE_REMAP.merge({
248
+ :void => 'Void',
249
+ })
250
+
251
+ IFACE_CLASS_REMAP = {
252
+ 'Map' => 'HashMap',
253
+ 'List' => 'ArrayList',
254
+ }
255
+
256
+ def format_nullable_type(t)
257
+ if t.nullable_type?
258
+ real_type = t.real_type
259
+ name = PRIMITIVE_TYPEMAP[real_type.name] || real_type.name
260
+ name = NULLABLE_REMAP[name] || name
261
+ return real_type, name
262
+ else
263
+ name = PRIMITIVE_TYPEMAP[t.name] || t.name
264
+ return t, name
265
+ end
266
+ end
267
+
268
+ def format_parameterized_type(t, name)
269
+ if t.parameterized_type?
270
+ name + '<' +
271
+ t.type_params.map {|tp|
272
+ n = format_type(tp)
273
+ TYPE_PARAMETER_REMAP[n] || n
274
+ }.join(', ') + '>'
275
+ else
276
+ name
277
+ end
278
+ end
279
+
280
+ def format_type(t)
281
+ t, name = format_nullable_type(t)
282
+ format_parameterized_type(t, name)
283
+ end
284
+
285
+ def format_message(t, name = t.name)
286
+ @gen.format_message(t, name)
287
+ end
288
+
289
+ def format_type_parameter(t)
290
+ t, name = format_nullable_type(t)
291
+ name = TYPE_PARAMETER_REMAP[name] || name
292
+ format_parameterized_type(t, name)
293
+ end
294
+
295
+ def format_type_impl(t)
296
+ t, name = format_nullable_type(t)
297
+ name = IFACE_CLASS_REMAP[name] || name
298
+ format_parameterized_type(t, name)
299
+ end
300
+
301
+ PRIMITIVE_DEFAULT = {
302
+ 'byte' => '0',
303
+ 'short' => '0',
304
+ 'int' => '0',
305
+ 'long' => '0',
306
+ 'float' => '0.0f',
307
+ 'double' => '0.0',
308
+ 'boolean' => 'false',
309
+ 'byte[]' => 'new byte[0]',
310
+ 'String' => '""',
311
+ }
312
+
313
+ def format_initial_value(to, f)
314
+ v = f.value
315
+ case v
316
+ when IR::NilValue
317
+ "#{to} = null;"
318
+
319
+ when IR::IntValue
320
+ tt, name = format_nullable_type(f.type)
321
+ if name == "BigInteger"
322
+ "#{to} = BigInteger.valueOf(#{v.int}L);"
323
+ elsif name == "long"
324
+ "#{to} = #{v.int}L;"
325
+ else
326
+ "#{to} = #{v.int};"
327
+ end
328
+
329
+ when IR::BoolValue
330
+ if v.bool
331
+ "#{to} = true;"
332
+ else
333
+ "#{to} = false;"
334
+ end
335
+
336
+ when IR::EnumValue
337
+ "#{to} = #{v.enum.name}.#{v.field.name};"
338
+ when IR::EmptyValue
339
+ tt, name = format_nullable_type(f.type)
340
+ v = PRIMITIVE_DEFAULT[name]
341
+ v ||= "new #{format_type_impl(f.type)}()"
342
+ "#{to} = #{v};"
343
+ else
344
+ raise SemanticsError, "unknown initial value type: #{v.class}"
345
+ end
346
+ end
347
+
348
+ PRIMITIVE_UNPACK = {
349
+ 'byte' => 'unpackByte()',
350
+ 'short' => 'unpackShort()',
351
+ 'int' => 'unpackInt()',
352
+ 'long' => 'unpackLong()',
353
+ 'ubyte' => 'unpackShort()',
354
+ 'ushort' => 'unpackInt()',
355
+ 'uint' => 'unpackLong()',
356
+ 'ulong' => 'unpackBigInteger()',
357
+ 'float' => 'unpackFloat()',
358
+ 'double' => 'unpackDouble()',
359
+ 'bool' => 'unpackBoolean()',
360
+ 'raw' => 'unpackByteArray()',
361
+ 'string' => 'unpackString()',
362
+ }
363
+
364
+ def format_unpack(to, pac, t)
365
+ if t.parameterized_type?
366
+ if t.list_type?
367
+ e = t.type_params[0]
368
+ anon_id = next_anon_id
369
+ vn = "n#{anon_id}"
370
+ ve = "e#{anon_id}"
371
+ vi = "i#{anon_id}"
372
+ return %[{
373
+ #{to} = new #{format_type_impl(t)}();
374
+ int #{vn} = #{pac}.unpackArray();
375
+ #{format_type(e)} #{ve};
376
+ for(int #{vi}=0; #{vi} < #{vn}; #{vi}++) {
377
+ #{format_unpack("#{ve}", pac, e)}
378
+ #{to}.add(#{ve});
379
+ }
380
+ }]
381
+ elsif t.map_type?
382
+ anon_id = next_anon_id
383
+ k = t.type_params[0]
384
+ v = t.type_params[1]
385
+ vn = "n#{anon_id}"
386
+ vk = "k#{anon_id}"
387
+ vv = "v#{anon_id}"
388
+ vi = "i#{anon_id}"
389
+ return %[{
390
+ #{to} = new #{format_type_impl(t)}();
391
+ int #{vn} = #{pac}.unpackMap();
392
+ #{format_type(k)} #{vk};
393
+ #{format_type(v)} #{vv};
394
+ for(int #{vi}=0; #{vi} < #{vn}; #{vi}++) {
395
+ #{format_unpack("#{vk}", pac, k)}
396
+ #{format_unpack("#{vv}", pac, v)}
397
+ #{to}.put(#{vk}, #{vv});
398
+ }
399
+ }]
400
+ end
401
+
402
+ elsif t.is_a?(IR::Message)
403
+ return %[#{to} = new #{t.name}();
404
+ #{to}.messageUnpack(#{pac});]
405
+
406
+ elsif t.is_a?(IR::Enum)
407
+ return "#{to} = #{t.name}.enumOf(#{pac}.unpackInt());"
408
+ end
409
+
410
+ method = PRIMITIVE_UNPACK[t.name] || "unpack(#{t.name}.class)"
411
+ "#{to} = #{pac}.#{method};"
412
+ end
413
+
414
+ PRIMITIVE_CONVERT = {
415
+ 'byte' => 'asByte()',
416
+ 'short' => 'asShort()',
417
+ 'int' => 'asInt()',
418
+ 'long' => 'asLong()',
419
+ 'ubyte' => 'asShort()',
420
+ 'ushort' => 'asInt()',
421
+ 'uint' => 'asLong()',
422
+ 'ulong' => 'asBigInteger()',
423
+ 'float' => 'asFloat()',
424
+ 'double' => 'asDouble()',
425
+ 'bool' => 'asBoolean()',
426
+ 'raw' => 'asByteArray()',
427
+ 'string' => 'asString()',
428
+ }
429
+
430
+ def format_convert(to, obj, t)
431
+ if t.parameterized_type?
432
+ if t.list_type?
433
+ e = t.type_params[0]
434
+ anon_id = next_anon_id
435
+ ve = "e#{anon_id}"
436
+ vo = "o#{anon_id}"
437
+ return %[{
438
+ #{to} = new #{format_type_impl(t)}();
439
+ #{format_type(e)} #{ve};
440
+ for(MessagePackObject #{vo} : #{obj}.asArray()) {
441
+ #{format_convert("#{ve}", "#{vo}", e)}
442
+ #{to}.add(#{ve});
443
+ }
444
+ }]
445
+ elsif t.map_type?
446
+ k = t.type_params[0]
447
+ v = t.type_params[1]
448
+ anon_id = next_anon_id
449
+ vk = "k#{anon_id}"
450
+ vv = "v#{anon_id}"
451
+ vkv = "kv#{anon_id}"
452
+ return %[{
453
+ #{to} = new #{format_type_impl(t)}();
454
+ #{format_type(k)} #{vk};
455
+ #{format_type(v)} #{vv};
456
+ for(Map.Entry<MessagePackObject,MessagePackObject> #{vkv} : #{obj}.asMap().entrySet()) {
457
+ #{format_convert("#{vk}", "#{vkv}.getKey()", k)}
458
+ #{format_convert("#{vv}", "#{vkv}.getValue()", v)}
459
+ #{to}.put(#{vk}, #{vv});
460
+ }
461
+ }]
462
+ end
463
+
464
+ elsif t.is_a?(IR::Message)
465
+ return %[#{to} = new #{t.name}();
466
+ #{to}.messageConvert(#{obj});]
467
+
468
+ elsif t.is_a?(IR::Enum)
469
+ return "#{to} = #{t.name}.enumOf(#{obj}.asInt());"
470
+ end
471
+
472
+ method = PRIMITIVE_CONVERT[t.name] || "convert(new #{t.name}())"
473
+ "#{to} = #{obj}.#{method};"
474
+ end
475
+
476
+ def next_anon_id
477
+ @anon_id ||= -1
478
+ @anon_id += 1
479
+ end
480
+
481
+ def format_message_class(s, version)
482
+ (namespace + ["#{s.name}_#{version}"]).join('.')
483
+ end
484
+ end
485
+ end
486
+
487
+
488
+ end
489
+ end
490
+ end
metadata ADDED
@@ -0,0 +1,123 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: msgpack-idl-java
3
+ version: !ruby/object:Gem::Version
4
+ hash: 27
5
+ prerelease: false
6
+ segments:
7
+ - 0
8
+ - 1
9
+ - 0
10
+ version: 0.1.0
11
+ platform: ruby
12
+ authors:
13
+ - FURUHASHI Sadayuki
14
+ autorequire:
15
+ bindir: bin
16
+ cert_chain: []
17
+
18
+ date: 2011-04-23 00:00:00 +09:00
19
+ default_executable:
20
+ dependencies:
21
+ - !ruby/object:Gem::Dependency
22
+ name: msgpack-idl
23
+ prerelease: false
24
+ requirement: &id001 !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ">="
28
+ - !ruby/object:Gem::Version
29
+ hash: 27
30
+ segments:
31
+ - 0
32
+ - 1
33
+ - 0
34
+ version: 0.1.0
35
+ type: :runtime
36
+ version_requirements: *id001
37
+ - !ruby/object:Gem::Dependency
38
+ name: tenjin
39
+ prerelease: false
40
+ requirement: &id002 !ruby/object:Gem::Requirement
41
+ none: false
42
+ requirements:
43
+ - - ">="
44
+ - !ruby/object:Gem::Version
45
+ hash: 5
46
+ segments:
47
+ - 0
48
+ - 6
49
+ - 1
50
+ version: 0.6.1
51
+ type: :runtime
52
+ version_requirements: *id002
53
+ - !ruby/object:Gem::Dependency
54
+ name: rspec
55
+ prerelease: false
56
+ requirement: &id003 !ruby/object:Gem::Requirement
57
+ none: false
58
+ requirements:
59
+ - - ">="
60
+ - !ruby/object:Gem::Version
61
+ hash: 3
62
+ segments:
63
+ - 0
64
+ version: "0"
65
+ type: :development
66
+ version_requirements: *id003
67
+ description:
68
+ email: frsyuki@users.sourceforge.jp
69
+ executables: []
70
+
71
+ extensions: []
72
+
73
+ extra_rdoc_files: []
74
+
75
+ files:
76
+ - AUTHORS
77
+ - README
78
+ - lib/msgpack/idl/lang/java.rb
79
+ - lib/msgpack/idl/lang/java/client/application.java
80
+ - lib/msgpack/idl/lang/java/client/service_version.java
81
+ - lib/msgpack/idl/lang/java/enum.java
82
+ - lib/msgpack/idl/lang/java/message.java
83
+ - lib/msgpack/idl/lang/java/message_body.java
84
+ - lib/msgpack/idl/lang/java/server/application.java
85
+ - lib/msgpack/idl/lang/java/server/service_version.java
86
+ - lib/msgpack/idl/lang/java/service_version.java
87
+ - lib/msgpack/idl/lang/java/version.rb
88
+ has_rdoc: true
89
+ homepage: http://msgpack.org/
90
+ licenses: []
91
+
92
+ post_install_message:
93
+ rdoc_options:
94
+ - --charset=UTF-8
95
+ require_paths:
96
+ - lib
97
+ required_ruby_version: !ruby/object:Gem::Requirement
98
+ none: false
99
+ requirements:
100
+ - - ">="
101
+ - !ruby/object:Gem::Version
102
+ hash: 3
103
+ segments:
104
+ - 0
105
+ version: "0"
106
+ required_rubygems_version: !ruby/object:Gem::Requirement
107
+ none: false
108
+ requirements:
109
+ - - ">="
110
+ - !ruby/object:Gem::Version
111
+ hash: 3
112
+ segments:
113
+ - 0
114
+ version: "0"
115
+ requirements: []
116
+
117
+ rubyforge_project: msgpack
118
+ rubygems_version: 1.3.7
119
+ signing_key:
120
+ specification_version: 3
121
+ summary: MessagePack IDL Processor Language Module for Java
122
+ test_files: []
123
+