Jiri Simsa | d7616c9 | 2015-03-24 23:44:30 -0700 | [diff] [blame] | 1 | // 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 Wang | 232d649 | 2015-02-25 18:04:54 -0800 | [diff] [blame] | 5 | package java |
| 6 | |
| 7 | import ( |
| 8 | "bytes" |
| 9 | "log" |
| 10 | "path" |
| 11 | |
Jiri Simsa | ffceefa | 2015-02-28 11:03:34 -0800 | [diff] [blame] | 12 | "v.io/x/ref/lib/vdl/compile" |
Todd Wang | 53a4e2e | 2015-03-18 10:54:54 -0700 | [diff] [blame] | 13 | "v.io/x/ref/lib/vdl/vdlutil" |
Todd Wang | 232d649 | 2015-02-25 18:04:54 -0800 | [diff] [blame] | 14 | ) |
| 15 | |
Jiri Simsa | 67b8a26 | 2015-03-24 21:14:07 -0700 | [diff] [blame] | 16 | const clientFactoryTmpl = header + ` |
Todd Wang | 232d649 | 2015-02-25 18:04:54 -0800 | [diff] [blame] | 17 | // Source(s): {{ .Sources }} |
| 18 | package {{ .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 Sivakumar | b412326 | 2015-03-26 18:45:11 -0700 | [diff] [blame] | 25 | public static {{ .ServiceName }}Client bind(final java.lang.String name, final io.v.v23.Options vOpts) { |
Matt Rosencrantz | 94502cf | 2015-03-18 09:43:44 -0700 | [diff] [blame] | 26 | io.v.v23.rpc.Client client = null; |
Suharsh Sivakumar | b412326 | 2015-03-26 18:45:11 -0700 | [diff] [blame] | 27 | 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 Wang | 232d649 | 2015-02-25 18:04:54 -0800 | [diff] [blame] | 29 | } |
| 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. |
| 37 | func genJavaClientFactoryFile(iface *compile.Interface, env *compile.Env) JavaFileInfo { |
Todd Wang | 53a4e2e | 2015-03-18 10:54:54 -0700 | [diff] [blame] | 38 | javaServiceName := vdlutil.FirstRuneToUpper(iface.Name) |
Todd Wang | 232d649 | 2015-02-25 18:04:54 -0800 | [diff] [blame] | 39 | data := struct { |
Todd Wang | b90b8de | 2015-03-31 20:04:13 -0700 | [diff] [blame] | 40 | FileDoc string |
Todd Wang | 232d649 | 2015-02-25 18:04:54 -0800 | [diff] [blame] | 41 | AccessModifier string |
| 42 | Sources string |
| 43 | ServiceName string |
| 44 | PackagePath string |
| 45 | StubName string |
| 46 | }{ |
Todd Wang | b90b8de | 2015-03-31 20:04:13 -0700 | [diff] [blame] | 47 | FileDoc: iface.File.Package.FileDoc, |
Todd Wang | 232d649 | 2015-02-25 18:04:54 -0800 | [diff] [blame] | 48 | 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 | } |