blob: d439faa93a1dececdc3d116acd44732c349a0f1b [file] [log] [blame]
// Copyright 2015 The Vanadium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
// +build java android
package namespace
import (
"unsafe"
"v.io/v23/namespace"
"v.io/v23/naming"
"v.io/v23/options"
jutil "v.io/x/jni/util"
)
// #include "jni.h"
import "C"
// JavaNamespace converts the provided Go Namespace into a Java Namespace
// object.
// NOTE: Because CGO creates package-local types and because this method may be
// invoked from a different package, Java types are passed in an empty interface
// and then cast into their package local types.
func JavaNamespace(jEnv interface{}, namespace namespace.T) (unsafe.Pointer, error) {
jNamespace, err := jutil.NewObject(jEnv, jNamespaceImplClass, []jutil.Sign{jutil.LongSign}, int64(jutil.PtrValue(&namespace)))
if err != nil {
return nil, err
}
jutil.GoRef(&namespace) // Un-refed when the Java NamespaceImpl is finalized.
return jNamespace, nil
}
func javaToGoOptions(jEnv interface{}, key string, jValue interface{}) (interface{}, error) {
switch key {
case "io.v.v23.SKIP_SERVER_ENDPOINT_AUTHORIZATION":
value, err := jutil.CallBooleanMethod(jEnv, jValue, "booleanValue", []jutil.Sign{})
if err != nil {
return nil, err
}
if value {
return options.SkipServerEndpointAuthorization{}, nil
}
}
// Otherwise we don't know what this option is, ignore it.
return nil, jutil.SkipOption
}
func namespaceOptions(jEnv interface{}, jOptions interface{}) ([]naming.NamespaceOpt, error) {
opts, err := jutil.GoOptions(jEnv, jOptions, javaToGoOptions)
if err != nil {
return nil, err
}
var actualOpts []naming.NamespaceOpt
for _, opt := range opts {
switch opt := opt.(type) {
case naming.NamespaceOpt:
actualOpts = append(actualOpts, opt)
}
}
return actualOpts, nil
}