如何查询混合应用程序中的worklight服务器主机信息

时间:2014-05-01 02:28:03

标签: ibm-mobilefirst worklight-runtime

有没有办法从混合应用程序内部查询worklight服务器URL信息?我需要为远程服务器构建一个应用程序,以便在我可以从该服务器调用适配器过程之前,与安装了WL的服务器建立凭据。目前我是通过在控制台上执行dojo.xhrGet来实现的。此时,URL在应用程序中进行了硬编码。出于多种原因,我希望能够在运行时查询此信息。在iOS中,此信息存储在worklight.plist中,而在android中,它位于assets / wlclient.properties中。

我正在运行WL 6.1,我尝试调用get属性,如下所示

WL.Client.getAppProperty(WL.AppProp.WORKLIGHT_ROOT_URL);
WL.Client.getAppProperty(WL.AppProp.APP_SERVICES_URL);

但他们返回的只是

/MyApp/apps/services/api/simpleApp/common/
/MyApp/apps/services/

我需要获取主机信息,例如android wlclient.propeties中的以下内容

wlServerProtocol = https
wlServerHost = zzzz.aaa.bb.com
wlServerPort = 15024
wlServerContext = /

或在iOS worklight.plist

<key>protocol</key> 
<string>https</string>
<key>host</key>
<string>zzzz.aaa.bb.com</string>
<key>port</key>
<string>15024</string>
<key>wlServerContext</key>
<string>/</string>

欢迎任何帮助。

2 个答案:

答案 0 :(得分:0)

答案 1 :(得分:0)

我做了一些调查,并且能够为android和ios编写一个cordova插件,以读取WL生成的预期属性文件的值。以下是不同的来源。有关编写cordova插件的更多信息,请参阅WorkLight Getting Started Section 6。实际上相当容易。一旦你了解了......不同的Objective-C,只花了几分钟。


Android代码

WorklightPropertiesPlugin.java

import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;

import org.apache.cordova.CallbackContext;
import org.apache.cordova.CordovaPlugin;
import org.json.JSONArray;
import org.json.JSONException;

import android.content.res.AssetManager;
import android.content.res.Resources;

public class WorklightPropertiesPlugin extends CordovaPlugin {
    @Override
    public boolean execute(String action, JSONArray args, CallbackContext callbackContext) throws JSONException {
        boolean handled = false;
        String responseText = "";
        if(action.equalsIgnoreCase("serverURL")) {
            try {
                AssetManager am = this.cordova.getActivity().getApplicationContext().getAssets();
                InputStream is = am.open("wlclient.properties");
                Properties prop = new Properties();
                prop.load(is);
                String wlServerProtocol = prop.getProperty("wlServerProtocol");
                String wlServerHost = prop.getProperty("wlServerHost");
                String wlServerPort = prop.getProperty("wlServerPort");
                String wlServerContext = prop.getProperty("wlServerContext");
                responseText = wlServerProtocol + "://" + wlServerHost + ":" + wlServerPort + wlServerContext;
                handled = true;
            } catch(IOException e) {
                callbackContext.error("Error loading properties " + e.getLocalizedMessage());
            }
        }
        if(handled) {
            callbackContext.success(responseText);
        }
        return handled;
    }
}

iOS代码

WorklightPropertiesPlugin.h

#import <Foundation/Foundation.h>
#import <Cordova/CDV.h>

@interface WorklightPropertiesPlugin : CDVPlugin

- (void) serverURL:(CDVInvokedUrlCommand*) command;

@end

WorklightPropertiesPlugin.m

#import "WorklightPropertiesPlugin.h"

@implementation WorklightPropertiesPlugin

- (void) serverURL:(CDVInvokedUrlCommand*)command{
    NSString * response = nil;
    CDVPluginResult *pluginResult = nil;

    NSString* plistLoc =[[NSBundle mainBundle]pathForResource:@"worklight" ofType:@"plist"];
    if(plistLoc == nil){
        [NSException raise:@"[Remote Load Initialization Error]" format:@"Unable to locate worklight.plist"];
    }

    NSDictionary* wlProps = [NSDictionary dictionaryWithContentsOfFile:plistLoc];
    NSString* proto = [wlProps valueForKey:@"protocol"];
    NSString* host = [wlProps valueForKey:@"host"];
    NSString* port = [wlProps valueForKey:@"port"];
    NSString* wlServerContext = [wlProps valueForKey:@"wlServerContext"];

    if(proto == nil || host == nil || port == nil){
        [NSException raise:@"[Remote Load Initialization Error]" format:@"host, port and protocol are all required keys in worklight.plist"];
    }

    response = [NSString stringWithFormat:@"%@://%@:%@%@", proto, host, port, wlServerContext];

    pluginResult = [CDVPluginResult resultWithStatus: CDVCommandStatus_OK messageAsString:response];
    [self.commandDelegate sendPluginResult:pluginResult callbackId:command.callbackId];
}

@end