# DeviceUtils.java

    /*
     * Copyright (c) 2017, WSO2 Inc. (http://www.wso2.org) All Rights Reserved.
     *
     * WSO2 Inc. licenses this file to you under the Apache License,
     * Version 2.0 (the "License"); you may not use this file except
     * in compliance with the License.
     * You may obtain a copy of the License at
     *
     * http://www.apache.org/licenses/LICENSE-2.0
     *
     * Unless required by applicable law or agreed to in writing,
     * software distributed under the License is distributed on an
     * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
     * KIND, either express or implied. See the License for the
     * specific language governing permissions and limitations
     * under the License.
     */
    package org.wso2.extension.siddhi.device.utils;

    import org.apache.commons.logging.Log;
    import org.apache.commons.logging.LogFactory;
    import org.wso2.carbon.context.PrivilegedCarbonContext;
    import org.wso2.carbon.device.mgt.common.notification.mgt.NotificationManagementService;
    import org.wso2.carbon.device.mgt.core.service.DeviceManagementProviderService;
    import org.wso2.carbon.device.mgt.core.service.GroupManagementProviderService;
    import org.wso2.carbon.identity.jwt.client.extension.service.JWTClientManagerService;

    /**
     * This class holds utility methods to retrieve data.
     */
    public class DeviceUtils {

        private static Log log = LogFactory.getLog(DeviceUtils.class);
        private static DeviceManagementProviderService deviceManagementProviderService;
        private static GroupManagementProviderService groupManagementProviderService;
        private static NotificationManagementService notificationManagementService;
        private static JWTClientManagerService jwtClientManagerService;

        private DeviceUtils(){
        }

        public static DeviceManagementProviderService getDeviceManagementProviderService() {
            if (deviceManagementProviderService != null) {
                return deviceManagementProviderService;
            }
            PrivilegedCarbonContext ctx = PrivilegedCarbonContext.getThreadLocalCarbonContext();
            deviceManagementProviderService =
                    (DeviceManagementProviderService) ctx.getOSGiService(DeviceManagementProviderService.class, null);
            if (deviceManagementProviderService == null) {
                String msg = "Device Management service has not initialized.";
                log.error(msg);
                throw new IllegalStateException(msg);
            }
            return deviceManagementProviderService;
        }

        public static GroupManagementProviderService getGroupManagementProviderService() {
            if (groupManagementProviderService != null) {
                return groupManagementProviderService;
            }
            PrivilegedCarbonContext ctx = PrivilegedCarbonContext.getThreadLocalCarbonContext();
            groupManagementProviderService =
                    (GroupManagementProviderService) ctx.getOSGiService(GroupManagementProviderService.class, null);
            if (groupManagementProviderService == null) {
                String msg = "Group Management service has not initialized.";
                log.error(msg);
                throw new IllegalStateException(msg);
            }
            return groupManagementProviderService;
        }

        public static NotificationManagementService getNotificationManagementService() {
            if (notificationManagementService != null) {
                return notificationManagementService;
            }
            PrivilegedCarbonContext ctx = PrivilegedCarbonContext.getThreadLocalCarbonContext();
            notificationManagementService =
                    (NotificationManagementService) ctx.getOSGiService(NotificationManagementService.class, null);
            if (notificationManagementService == null) {
                String msg = "Notification Management service has not initialized.";
                log.error(msg);
                throw new IllegalStateException(msg);
            }
            return notificationManagementService;
        }

        public static JWTClientManagerService getJWTClientManagerService() {
            if (jwtClientManagerService != null) {
                return jwtClientManagerService;
            }
            PrivilegedCarbonContext ctx = PrivilegedCarbonContext.getThreadLocalCarbonContext();
            jwtClientManagerService =
                    (JWTClientManagerService) ctx.getOSGiService(JWTClientManagerService.class, null);
            if (jwtClientManagerService == null) {
                String msg = "JWTClient Manager service has not initialized.";
                log.error(msg);
                throw new IllegalStateException(msg);
            }
            return jwtClientManagerService;
        }
    }