1 /*
2  * Copyright 2010-2020 Amazon.com, Inc. or its affiliates. All Rights Reserved.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License").
5  * You may not use this file except in compliance with the License.
6  * A copy of the License is located at
7  *
8  *  http://aws.amazon.com/apache2.0
9  *
10  * or in the "license" file accompanying this file. This file is distributed
11  * on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
12  * express or implied. See the License for the specific language governing
13  * permissions and limitations under the License.
14  */

15 package com.amazonaws.monitoring;
16
17 import com.amazonaws.SdkClientException;
18 import com.amazonaws.annotation.ThreadSafe;
19
20 import static com.amazonaws.SDKGlobalConfiguration.AWS_CSM_CLIENT_ID_SYSTEM_PROPERTY;
21 import static com.amazonaws.SDKGlobalConfiguration.AWS_CSM_ENABLED_SYSTEM_PROPERTY;
22 import static com.amazonaws.SDKGlobalConfiguration.AWS_CSM_HOST_SYSTEM_PROPERTY;
23 import static com.amazonaws.SDKGlobalConfiguration.AWS_CSM_PORT_SYSTEM_PROPERTY;
24 import static com.amazonaws.SDKGlobalConfiguration.DEFAULT_AWS_CSM_PORT;
25 import static com.amazonaws.SDKGlobalConfiguration.DEFAULT_AWS_CSM_HOST;
26
27 /**
28  * Configuration provider that sources the client side monitoring
29  * configuration parameters from system properties.
30  *
31  * @see com.amazonaws.SDKGlobalConfiguration#AWS_CSM_CLIENT_ID_SYSTEM_PROPERTY
32  * @see com.amazonaws.SDKGlobalConfiguration#AWS_CSM_ENABLED_SYSTEM_PROPERTY
33  * @see com.amazonaws.SDKGlobalConfiguration#AWS_CSM_PORT_SYSTEM_PROPERTY
34  */

35 @ThreadSafe
36 public final class SystemPropertyCsmConfigurationProvider implements CsmConfigurationProvider {
37     @Override
38     public CsmConfiguration getConfiguration() throws SdkClientException {
39         String enabled = System.getProperty(AWS_CSM_ENABLED_SYSTEM_PROPERTY);
40
41         if (enabled == null) {
42             throw new SdkClientException("Unable to load Client Side Monitoring configurations from"
43                     + " system properties variables!");
44         }
45
46         String host = System.getProperty(AWS_CSM_HOST_SYSTEM_PROPERTY, DEFAULT_AWS_CSM_HOST);
47         String port = System.getProperty(AWS_CSM_PORT_SYSTEM_PROPERTY);
48         String clientId = System.getProperty(AWS_CSM_CLIENT_ID_SYSTEM_PROPERTY, "");
49
50         try {
51             int portNumber = port == null ? DEFAULT_AWS_CSM_PORT : Integer.parseInt(port);
52             return CsmConfiguration.builder()
53                     .withEnabled(Boolean.parseBoolean(enabled))
54                     .withHost(host)
55                     .withPort(portNumber)
56                     .withClientId(clientId)
57                     .build();
58         } catch (Exception e) {
59             throw new SdkClientException("Unable to load Client Side Monitoring configurations from"
60                     + " system properties variables!", e);
61         }
62     }
63 }
64