Skip to content

Configuration Provider

Loading Cloud Platform Mobile Services Connection Information from a File

Note

This example uses FileConfigurationProvider for simplicity. You would most likely use one of the other providers such as ManagedConfigurationProvider and DiscoveryServiceConfigurationProvider among others.

Save the following content in the raw resources folder (app\src\main\res\raw). The filename for this example is cpmsconfig.json.

1
2
3
4
5
6
{
    "serviceURL" : "https://mobileservices-youraccount.hana.ondemand.com",
    "appID" : "your_application_id",
    "appVersion" : "1.0",
    "connectionID" : "com.sap.edm.sampleservice"
}

Read configuration from the file:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
ConfigurationLoaderCallback configurationLoaderCallback
        = new ConfigurationLoaderCallback() {

    @Override
    public void onCompletion(ProviderIdentifier providerId, boolean success) {
        try {
            JSONObject config = DefaultPersistenceMethod
                    .getPersistedConfiguration(context);

            if (config != null) {
                String serviceURL = config.getString("serviceURL");
                String appID = config.getString("appID");
                String appVersion = config.getString("appVersion");
                String connectionID = config.getString("connectionID");
                logger.debug("URL: {}, appID: {}, version: {}, connID: {}",
                        serviceURL, appID, appVersion, connectionID);

                // Use those values to initialize CpmsParameters

            }
        } catch (ConfigurationPersistenceException e) {
            logger.error("ConfigurationPersistenceException:", e);
        } catch (JSONException e) {
            logger.error("JSONException:", e);
        }
    }

    @Override
    public void onError(ConfigurationLoader configurationLoader,
                        ProviderIdentifier providerId,
                        UserInputs requestedInput,
                        ConfigurationProviderError error) {
        logger.error("Provider failed to load.  {}", error);
    }

    @Override
    public void onInputRequired(ConfigurationLoader configurationLoader,
                                UserInputs requestedInput) {
        logger.debug("Provider requires input. {}", requestedInput);
    }
};

ConfigurationProvider[] fileProvider = {
        new FileConfigurationProvider(context, "cpmsconfig")
};
final ConfigurationLoader configurationLoader =
        new ConfigurationLoader(context,
                configurationLoaderCallback,
                fileProvider);


//Now initiate configuration load from the file

configurationLoader.loadConfiguration();
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
val configurationLoaderCallback = object : ConfigurationLoaderCallback() {

    override fun onCompletion(providerId: ProviderIdentifier, success: Boolean) {
        try {
            val config = DefaultPersistenceMethod
                    .getPersistedConfiguration(context)

            if (config != null) {
                val serviceURL = config.getString("serviceURL")
                val appID = config.getString("appID")
                val appVersion = config.getString("appVersion")
                val connectionID = config.getString("connectionID")
                logger.debug("URL: {}, appID: {}, version: {}, connID: {}",
                        serviceURL, appID, appVersion, connectionID)

                // Use those values to initialize CpmsParameters

            }
        } catch (e: ConfigurationPersistenceException) {
            logger.error("ConfigurationPersistenceException:", e)
        } catch (e: JSONException) {
            logger.error("JSONException:", e)
        }

    }

    override fun onError(configurationLoader: ConfigurationLoader,
                         providerId: ProviderIdentifier,
                         requestedInput: UserInputs,
                         error: ConfigurationProviderError) {
        logger.error("Provider failed to load.  {}", error)
    }

    override fun onInputRequired(configurationLoader: ConfigurationLoader,
                                 requestedInput: UserInputs) {
        logger.debug("Provider requires input. {}", requestedInput)
    }
}

val fileProvider = arrayOf<ConfigurationProvider>(FileConfigurationProvider(context, "cpmsconfig"))
val configurationLoader = ConfigurationLoader(context,
        configurationLoaderCallback,
        fileProvider)


//Now initiate configuration load from the file

configurationLoader.loadConfiguration()