Package org.wso2.carbon.identity.entitlement.internal

Source Code of org.wso2.carbon.identity.entitlement.internal.EntitlementServiceComponent

/*
* Copyright 2004,2005 The Apache Software Foundation.
*
* 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
*
*      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.carbon.identity.entitlement.internal;

import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.IOException;

import org.osgi.service.component.ComponentContext;
import org.wso2.carbon.identity.entitlement.pip.PIPConfigHolder;
import org.wso2.carbon.identity.entitlement.pip.PIPExtensionBuilder;
import org.wso2.carbon.registry.core.service.RegistryService;
import org.wso2.carbon.user.core.service.RealmService;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

/**
* @scr.component name="identity.entitlement.component" immediate="true"
* @scr.reference name="registry.service"
*                interface="org.wso2.carbon.registry.core.service.RegistryService"
*                cardinality="1..1" policy="dynamic" bind="setRegistryService"
*                unbind="unsetRegistryService"
* @scr.reference name="user.realmservice.default"
*                interface="org.wso2.carbon.user.core.service.RealmService" cardinality="1..1"
*                policy="dynamic" bind="setRealmService" unbind="unsetRealmService"
*/
public class EntitlementServiceComponent {
    private static Log log = LogFactory.getLog(EntitlementServiceComponent.class);
    private static final String POLICY_TEMPLATE = "template.xml";
    private static String templatePolicy = null;
    private static RegistryService registryService = null;
    private static PIPConfigHolder pipConfig = null;
    private static RealmService realmservice;
   

    /**
     *
     */
    public EntitlementServiceComponent() {
    }

    /**
     *
     * @param ctxt
     */
    protected void activate(ComponentContext ctxt) {
        if (log.isDebugEnabled()) {
            log.info("Identity Entitlement bundle is activated");
        }
        InputStream inStream = null;
        BufferedReader reader = null;
        try {
            inStream = ctxt.getBundleContext().getBundle().getResource(POLICY_TEMPLATE)
                    .openStream();
            reader = new BufferedReader(new InputStreamReader(inStream));
            StringBuilder sb = new StringBuilder();
            String line = null;
            while ((line = reader.readLine()) != null) {
                sb.append(line.trim() + " " + "\n");
            }
            templatePolicy = sb.toString();
            inStream.close();
            EntitlementServiceInitializer entitlementServiceInitializer = new EntitlementServiceInitializer(
                    registryService);
            entitlementServiceInitializer.putEntitlementPolicyResourcesToRegistry();
            pipConfig = new PIPConfigHolder();
            PIPExtensionBuilder builder = new PIPExtensionBuilder();
            builder.setBundleContext(ctxt.getBundleContext());
            builder.buildPIPConfig(pipConfig);
        } catch (Exception e) {
            log.error("Failed to initialize Identity Provider", e);
        } finally {
            if (reader != null) {
                try {
                    reader.close();
                } catch (IOException e) {
                    log.warn("Unable to close the InputStream " + e.getMessage(), e);
                }
            }
        }
    }

    /**
     *
     * @param ctxt
     */
    protected void deactivate(ComponentContext ctxt) {
        if (log.isDebugEnabled()) {
            log.debug("Identity Entitlement bundle is deactivated");
        }
    }

    /**
     *
     * @param registryService
     */
    protected void setRegistryService(RegistryService registryService) {
        if (log.isDebugEnabled()) {
            log.debug("RegistryService set in Entitlement bundle");
        }
        EntitlementServiceComponent.registryService = registryService;
    }

    /**
     *
     * @param registryService
     */
    protected void unsetRegistryService(RegistryService registryService) {
        if (log.isDebugEnabled()) {
            log.debug("RegistryService unset in Entitlement bundle");
        }
        EntitlementServiceComponent.registryService = null;
    }

    /**
     *
     * @param userRealmDefault
     */
    protected void setRealmService(RealmService realmService) {
        if (log.isDebugEnabled()) {
            log.info("DeafultUserRealm set in Entitlement bundle");
        }
        EntitlementServiceComponent.realmservice = realmService;
    }

    /**
     *
     * @param userRealmDefault
     */
    protected void unsetRealmService(RealmService realmService) {
        if (log.isDebugEnabled()) {
            log.info("DefaultUserRealm unset in Entitlement bundle");
        }
        EntitlementServiceComponent.realmservice = null;
    }

    /**
     *
     * @return
     */
    public static String getTemplatePolicy() {
        return templatePolicy;
    }

    /**
     *
     * @return
     */
    public static PIPConfigHolder getPipConfig() {
        return pipConfig;
    }

    /**
     *
     * @return
     */
    public static RealmService getRealmservice() {
        return realmservice;
    }

    /**
     *
     * @param realmservice
     */
    public static void setRealmservice(RealmService realmservice) {
        EntitlementServiceComponent.realmservice = realmservice;
    }
    /**
     * Return registry service
     * @return RegistryService
     */
    public static RegistryService getRegistryService() {
        return registryService;
    }
}
TOP

Related Classes of org.wso2.carbon.identity.entitlement.internal.EntitlementServiceComponent

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.