blob: 626c5e08a5c11e570337ea8c0ad4d386ed05c936 [file] [log] [blame]
Jiri Simsad7616c92015-03-24 23:44:30 -07001// Copyright 2015 The Vanadium Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style
3// license that can be found in the LICENSE file.
4
Todd Wang232d6492015-02-25 18:04:54 -08005package java
6
7import (
8 "bytes"
9 "log"
10 "path"
11
Jiri Simsaffceefa2015-02-28 11:03:34 -080012 "v.io/x/ref/lib/vdl/compile"
Todd Wang53a4e2e2015-03-18 10:54:54 -070013 "v.io/x/ref/lib/vdl/vdlutil"
Todd Wang232d6492015-02-25 18:04:54 -080014)
15
Jiri Simsa67b8a262015-03-24 21:14:07 -070016const clientFactoryTmpl = header + `
Todd Wang232d6492015-02-25 18:04:54 -080017// Source(s): {{ .Sources }}
18package {{ .PackagePath }};
19
20/* Factory for binding to {{ .ServiceName }}Client interfaces. */
21{{.AccessModifier}} final class {{ .ServiceName }}ClientFactory {
22 public static {{ .ServiceName }}Client bind(final java.lang.String name) {
23 return bind(name, null);
24 }
Suharsh Sivakumarb4123262015-03-26 18:45:11 -070025 public static {{ .ServiceName }}Client bind(final java.lang.String name, final io.v.v23.Options vOpts) {
Matt Rosencrantz94502cf2015-03-18 09:43:44 -070026 io.v.v23.rpc.Client client = null;
Suharsh Sivakumarb4123262015-03-26 18:45:11 -070027 if (vOpts != null && vOpts.get(io.v.v23.OptionDefs.CLIENT) != null) {
28 client = vOpts.get(io.v.v23.OptionDefs.CLIENT, io.v.v23.rpc.Client.class);
Todd Wang232d6492015-02-25 18:04:54 -080029 }
30 return new {{ .StubName }}(client, name);
31 }
32}
33`
34
35// genJavaClientFactoryFile generates the Java file containing client bindings for
36// all interfaces in the provided package.
37func genJavaClientFactoryFile(iface *compile.Interface, env *compile.Env) JavaFileInfo {
Todd Wang53a4e2e2015-03-18 10:54:54 -070038 javaServiceName := vdlutil.FirstRuneToUpper(iface.Name)
Todd Wang232d6492015-02-25 18:04:54 -080039 data := struct {
Todd Wangb90b8de2015-03-31 20:04:13 -070040 FileDoc string
Todd Wang232d6492015-02-25 18:04:54 -080041 AccessModifier string
42 Sources string
43 ServiceName string
44 PackagePath string
45 StubName string
46 }{
Todd Wangb90b8de2015-03-31 20:04:13 -070047 FileDoc: iface.File.Package.FileDoc,
Todd Wang232d6492015-02-25 18:04:54 -080048 AccessModifier: accessModifierForName(iface.Name),
49 Sources: iface.File.BaseName,
50 ServiceName: javaServiceName,
51 PackagePath: javaPath(javaGenPkgPath(iface.File.Package.GenPath)),
52 StubName: javaPath(javaGenPkgPath(path.Join(iface.File.Package.GenPath, iface.Name+"ClientStub"))),
53 }
54 var buf bytes.Buffer
55 err := parseTmpl("client factory", clientFactoryTmpl).Execute(&buf, data)
56 if err != nil {
57 log.Fatalf("vdl: couldn't execute client template: %v", err)
58 }
59 return JavaFileInfo{
60 Name: javaServiceName + "ClientFactory.java",
61 Data: buf.Bytes(),
62 }
63}