// Path: components/hostobjects/org.jaggeryjs.hostobjects.registry/src/main/java/org/jaggeryjs/hostobjects/registry/RegistryHostObjectContext.java
// public class RegistryHostObjectContext {
// 
//     private static Log log = LogFactory.getLog(RegistryHostObjectContext.class);
// 
//     private static RegistryService registryService;
//     private static RealmService realmService;
// 
//     public static void setRegistryService(RegistryService registryService) {
//         RegistryHostObjectContext.registryService = registryService;
//     }
// 
//     public static Registry getUserRegistry(String mashupAuthor, int tenantId) throws CarbonException {
//         if (registryService == null) {
//             throw new CarbonException("Registry is null");
//         }
//         try {
//             if(registryService.getUserRealm(tenantId).getUserStoreManager().isExistingUser(mashupAuthor)) {
//                 return registryService.getGovernanceUserRegistry(mashupAuthor, tenantId);
//             } else {
//                 throw new CarbonException("Unable to access Registry, mashup author is not an active user");
//             }
//         } catch (UserStoreException e) {
//             log.error(e.getMessage(), e);
//             throw new CarbonException(e);
//         }catch (RegistryException e) {
//             log.error(e.getMessage(), e);
//             throw new CarbonException(e);
//         }
//     }
// 
//     public static RegistryService getRegistryService() {
//         return registryService;
//     }
// 
//     public static void setRealmService(RealmService rs) throws CarbonException {
//         RegistryHostObjectContext.realmService = rs;
//     }
// 
//     public static RealmService getRealmService() throws CarbonException {
//         if (realmService == null) {
//             String msg = "System has not been started properly. Realm Service is null.";
//             log.error(msg);
//             throw new CarbonException(msg);
//         }
//         return realmService;
//     }
// }


import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.jaggeryjs.hostobjects.registry.RegistryHostObjectContext;
import org.osgi.service.component.annotations.Component;
import org.osgi.service.component.annotations.Reference;
import org.osgi.service.component.annotations.ReferenceCardinality;
import org.osgi.service.component.annotations.ReferencePolicy;
import org.wso2.carbon.CarbonException;
import org.wso2.carbon.registry.core.service.RegistryService;
import org.wso2.carbon.user.core.service.RealmService;
package org.jaggeryjs.hostobjects.registry.internal;

/**
 * Copyright (c) WSO2 Inc. (http://www.wso2.org) All Rights Reserved.
 * <p>
 * Licensed 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
 * <p>
 * http://www.apache.org/licenses/LICENSE-2.0
 * <p>
 * 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.
 */


@Component(
        name = "mashup.javascript.hostobjects.registry.dscomponent",
        immediate = true)
public class RegistryHostObjectServiceComponent {

    private static final Log log = LogFactory.getLog(RegistryHostObjectServiceComponent.class);

    @Reference(
            name = "registry.service",
            service = org.wso2.carbon.registry.core.service.RegistryService.class,
            cardinality = ReferenceCardinality.MANDATORY,
            policy = ReferencePolicy.DYNAMIC,
            unbind = "unsetRegistryService")
    protected void setRegistryService(RegistryService registryService) {

        if (log.isDebugEnabled()) {
            log.info("Setting the Registry Service to RegistryHostObjectContext");
        }
// Path: components/hostobjects/org.jaggeryjs.hostobjects.registry/src/main/java/org/jaggeryjs/hostobjects/registry/RegistryHostObjectContext.java
// public class RegistryHostObjectContext {
// 
//     private static Log log = LogFactory.getLog(RegistryHostObjectContext.class);
// 
//     private static RegistryService registryService;
//     private static RealmService realmService;
// 
//     public static void setRegistryService(RegistryService registryService) {
//         RegistryHostObjectContext.registryService = registryService;
//     }
// 
//     public static Registry getUserRegistry(String mashupAuthor, int tenantId) throws CarbonException {
//         if (registryService == null) {
//             throw new CarbonException("Registry is null");
//         }
//         try {
//             if(registryService.getUserRealm(tenantId).getUserStoreManager().isExistingUser(mashupAuthor)) {
//                 return registryService.getGovernanceUserRegistry(mashupAuthor, tenantId);
//             } else {
//                 throw new CarbonException("Unable to access Registry, mashup author is not an active user");
//             }
//         } catch (UserStoreException e) {
//             log.error(e.getMessage(), e);
//             throw new CarbonException(e);
//         }catch (RegistryException e) {
//             log.error(e.getMessage(), e);
//             throw new CarbonException(e);
//         }
//     }
// 
//     public static RegistryService getRegistryService() {
//         return registryService;
//     }
// 
//     public static void setRealmService(RealmService rs) throws CarbonException {
//         RegistryHostObjectContext.realmService = rs;
//     }
// 
//     public static RealmService getRealmService() throws CarbonException {
//         if (realmService == null) {
//             String msg = "System has not been started properly. Realm Service is null.";
//             log.error(msg);
//             throw new CarbonException(msg);
//         }
//         return realmService;
//     }
// }


// Path: components/hostobjects/org.jaggeryjs.hostobjects.registry/src/main/java/org/jaggeryjs/hostobjects/registry/internal/RegistryHostObjectServiceComponent.java
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.jaggeryjs.hostobjects.registry.RegistryHostObjectContext;
import org.osgi.service.component.annotations.Component;
import org.osgi.service.component.annotations.Reference;
import org.osgi.service.component.annotations.ReferenceCardinality;
import org.osgi.service.component.annotations.ReferencePolicy;
import org.wso2.carbon.CarbonException;
import org.wso2.carbon.registry.core.service.RegistryService;
import org.wso2.carbon.user.core.service.RealmService;

package org.jaggeryjs.hostobjects.registry.internal;

/**
 * Copyright (c) WSO2 Inc. (http://www.wso2.org) All Rights Reserved.
 * <p>
 * Licensed 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
 * <p>
 * http://www.apache.org/licenses/LICENSE-2.0
 * <p>
 * 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.
 */


@Component(
        name = "mashup.javascript.hostobjects.registry.dscomponent",
        immediate = true)
public class RegistryHostObjectServiceComponent {

    private static final Log log = LogFactory.getLog(RegistryHostObjectServiceComponent.class);

    @Reference(
            name = "registry.service",
            service = org.wso2.carbon.registry.core.service.RegistryService.class,
            cardinality = ReferenceCardinality.MANDATORY,
            policy = ReferencePolicy.DYNAMIC,
            unbind = "unsetRegistryService")
    protected void setRegistryService(RegistryService registryService) {

        if (log.isDebugEnabled()) {
            log.info("Setting the Registry Service to RegistryHostObjectContext");
        }
        RegistryHostObjectContext.setRegistryService(registryService);